Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ repositories {

dependencies {
implementation("org.springframework.boot:spring-boot-starter-data-jdbc")
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.springframework.boot:spring-boot-starter-security")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
Expand All @@ -30,6 +31,7 @@ dependencies {
testImplementation("org.jetbrains.kotlin:kotlin-test-junit5")
testImplementation("org.springframework.security:spring-security-test")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
testRuntimeOnly("com.h2database:h2")
}

kotlin {
Expand Down
31 changes: 30 additions & 1 deletion src/main/kotlin/com/example/workoutloggerdemo/model/Workout.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,33 @@
package com.example.workoutloggerdemo.model

class Workout {
import jakarta.persistence.*
import java.time.LocalDateTime

@Entity
@Table(name = "workouts")
data class Workout(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long? = null,

@Column(nullable = false)
val name: String = "",

@Column
val description: String? = null,

@Column(name = "start_time", nullable = false)
val startTime: LocalDateTime = LocalDateTime.now(),

@Column(name = "end_time")
val endTime: LocalDateTime? = null,

@Column(name = "calories_burned")
val caloriesBurned: Int? = null
) {
// No-args constructor required by JPA
constructor() : this(
name = "",
startTime = LocalDateTime.now()
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.example.workoutloggerdemo.repository

import com.example.workoutloggerdemo.model.Workout
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.stereotype.Repository

@Repository
interface WorkoutRepository : JpaRepository<Workout, Long>
Original file line number Diff line number Diff line change
@@ -1,13 +1,90 @@
package com.example.workoutloggerdemo

import com.example.workoutloggerdemo.model.Workout
import com.example.workoutloggerdemo.repository.WorkoutRepository
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.ActiveProfiles
import java.time.LocalDateTime

@SpringBootTest
@ActiveProfiles("test")
class WorkoutLoggerDemoApplicationTests {

@Autowired
private lateinit var workoutRepository: WorkoutRepository

@Test
fun contextLoads() {
}

@Test
fun testWorkoutDatabaseOperations() {
// Create a new workout
val workout = Workout(
name = "Morning Run",
description = "5km run in the park",
startTime = LocalDateTime.now().minusHours(1),
endTime = LocalDateTime.now(),
caloriesBurned = 350
)

// Save the workout to the database
val savedWorkout = workoutRepository.save(workout)

// Verify the workout was saved with an ID
assertNotNull(savedWorkout.id)

// Retrieve the workout from the database
val retrievedWorkout = workoutRepository.findById(savedWorkout.id!!).orElse(null)

// Verify the retrieved workout matches the saved one
assertNotNull(retrievedWorkout)
assertEquals(workout.name, retrievedWorkout.name)
assertEquals(workout.description, retrievedWorkout.description)
assertEquals(workout.caloriesBurned, retrievedWorkout.caloriesBurned)
}

@Test
fun testFindAllWorkouts() {
// Clear any existing data
workoutRepository.deleteAll()

// Create multiple workouts
val workout1 = Workout(
name = "Morning Yoga",
description = "30 minutes of yoga",
startTime = LocalDateTime.now().minusDays(1),
endTime = LocalDateTime.now().minusDays(1).plusMinutes(30),
caloriesBurned = 150
)

val workout2 = Workout(
name = "Evening Run",
description = "3km run around the neighborhood",
startTime = LocalDateTime.now().minusHours(5),
endTime = LocalDateTime.now().minusHours(4),
caloriesBurned = 250
)

// Save workouts to the database
workoutRepository.saveAll(listOf(workout1, workout2))

// Retrieve all workouts
val allWorkouts = workoutRepository.findAll()

// Verify we have exactly 2 workouts
assertEquals(2, allWorkouts.size)

// Verify the workouts contain the expected data
val workoutNames = allWorkouts.map { it.name }.toSet()
assertTrue(workoutNames.contains("Morning Yoga"))
assertTrue(workoutNames.contains("Evening Run"))

// Verify calorie data is correct
val totalCalories = allWorkouts.mapNotNull { it.caloriesBurned }.sum()
assertEquals(400, totalCalories)
}
}
7 changes: 7 additions & 0 deletions src/test/resources/application-test.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
spring.datasource.url=jdbc:postgresql://localhost:5432/mydatabase
spring.datasource.username=myuser
spring.datasource.password=secret
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=true