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
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,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
18 changes: 16 additions & 2 deletions src/main/kotlin/com/example/workoutloggerdemo/model/Workout.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
package com.example.workoutloggerdemo.model

class Workout {
}
import org.springframework.data.annotation.Id
import org.springframework.data.relational.core.mapping.Column
import org.springframework.data.relational.core.mapping.Table
import java.time.LocalDateTime

@Table("workouts")
data class Workout(
@Id
val id: Long? = null,
val name: String,
val description: String? = null,
val duration: Int, // in minutes
@Column("calories_burned")
val caloriesBurned: Int? = null,
val date: LocalDateTime = LocalDateTime.now()
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example.workoutloggerdemo.repository

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

@Repository
interface WorkoutRepository : CrudRepository<Workout, Long> {
fun findByName(name: String): List<Workout>
}
41 changes: 41 additions & 0 deletions src/test/kotlin/com/example/workoutloggerdemo/DatabaseTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.example.workoutloggerdemo

import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.jdbc.core.JdbcTemplate
import org.springframework.test.context.ActiveProfiles
import kotlin.test.assertEquals
import kotlin.test.assertTrue

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

@Autowired
private lateinit var jdbcTemplate: JdbcTemplate

@Test
fun `should connect to database and execute query`() {
// Create a test table
jdbcTemplate.execute("""
CREATE TABLE IF NOT EXISTS test_table (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL
)
""")

// Insert test data
jdbcTemplate.update("INSERT INTO test_table (name) VALUES (?)", "Test Name")

// Query the data
val count = jdbcTemplate.queryForObject("SELECT COUNT(*) FROM test_table", Int::class.java)
val name = jdbcTemplate.queryForObject("SELECT name FROM test_table WHERE id = 1", String::class.java)

// Assert
assertEquals(1, count)
assertEquals("Test Name", name)

println("[DEBUG_LOG] Database test successful. Count: $count, Name: $name")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.example.workoutloggerdemo

import com.example.workoutloggerdemo.model.Workout
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.jdbc.core.JdbcTemplate
import org.springframework.jdbc.core.simple.SimpleJdbcInsert
import org.springframework.test.context.ActiveProfiles
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import kotlin.test.assertEquals
import kotlin.test.assertNotNull

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

@Autowired
private lateinit var jdbcTemplate: JdbcTemplate

@BeforeEach
fun setup() {
// Create workouts table
jdbcTemplate.execute("""
CREATE TABLE IF NOT EXISTS workouts (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
description VARCHAR(1000),
duration INT NOT NULL,
calories_burned INT,
date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
""")

// Clear any existing data
jdbcTemplate.execute("DELETE FROM workouts")
}

@Test
fun `should insert and retrieve workout from database`() {
// Arrange
val workout = Workout(
name = "Morning Run",
description = "5km run in the park",
duration = 30,
caloriesBurned = 300,
date = LocalDateTime.now()
)

// Act - Insert workout using SimpleJdbcInsert
val simpleJdbcInsert = SimpleJdbcInsert(jdbcTemplate)
.withTableName("workouts")
.usingGeneratedKeyColumns("id")

val parameters = mapOf(
"name" to workout.name,
"description" to workout.description,
"duration" to workout.duration,
"calories_burned" to workout.caloriesBurned,
"date" to workout.date
)

val workoutId = simpleJdbcInsert.executeAndReturnKey(parameters).toLong()

// Query the workout
val retrievedWorkout = jdbcTemplate.queryForMap(
"SELECT * FROM workouts WHERE id = ?",
workoutId
)

// Assert
assertNotNull(retrievedWorkout)
assertEquals(workout.name, retrievedWorkout["name"])
assertEquals(workout.description, retrievedWorkout["description"])
assertEquals(workout.duration, retrievedWorkout["duration"])
assertEquals(workout.caloriesBurned, retrievedWorkout["calories_burned"])

println("[DEBUG_LOG] Inserted workout with ID: $workoutId")
println("[DEBUG_LOG] Retrieved workout: $retrievedWorkout")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.example.workoutloggerdemo.repository

import com.example.workoutloggerdemo.model.Workout
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.data.jdbc.DataJdbcTest
import org.springframework.context.annotation.Import
import org.springframework.data.jdbc.repository.config.EnableJdbcRepositories
import org.springframework.test.context.ActiveProfiles
import org.springframework.test.context.TestPropertySource
import java.time.LocalDateTime
import kotlin.test.assertEquals
import kotlin.test.assertNotNull

@DataJdbcTest
@ActiveProfiles("test")
@EnableJdbcRepositories(basePackages = ["com.example.workoutloggerdemo.repository"])
@TestPropertySource(properties = [
"spring.datasource.url=jdbc:h2:mem:testdb",
"spring.datasource.driverClassName=org.h2.Driver",
"spring.datasource.username=sa",
"spring.datasource.password=password",
"spring.sql.init.mode=embedded"
])
class WorkoutRepositoryTest {

@Autowired
private lateinit var workoutRepository: WorkoutRepository

@Test
fun `should save and retrieve workout from database`() {
// Arrange
val workout = Workout(
name = "Morning Run",
description = "5km run in the park",
duration = 30,
caloriesBurned = 300,
date = LocalDateTime.now()
)

// Act
val savedWorkout = workoutRepository.save(workout)
val retrievedWorkout = workoutRepository.findById(savedWorkout.id!!).orElse(null)

// Assert
assertNotNull(retrievedWorkout)
assertEquals(workout.name, retrievedWorkout.name)
assertEquals(workout.description, retrievedWorkout.description)
assertEquals(workout.duration, retrievedWorkout.duration)
assertEquals(workout.caloriesBurned, retrievedWorkout.caloriesBurned)

println("[DEBUG_LOG] Saved workout: $savedWorkout")
println("[DEBUG_LOG] Retrieved workout: $retrievedWorkout")
}
}
11 changes: 11 additions & 0 deletions src/test/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.sql.init.mode=embedded

# Enable H2 console for debugging (optional)
spring.h2.console.enabled=true

# Configure schema generation
spring.sql.init.schema-locations=classpath:schema.sql
8 changes: 8 additions & 0 deletions src/test/resources/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CREATE TABLE IF NOT EXISTS workouts (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
description VARCHAR(1000),
duration INT NOT NULL,
calories_burned INT,
date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);