Skip to content
This repository was archived by the owner on Jun 8, 2025. It is now read-only.

Commit aa875fb

Browse files
committed
use testcontianers for tests, pass env vars safely
1 parent 5f1e96c commit aa875fb

File tree

6 files changed

+60
-29
lines changed

6 files changed

+60
-29
lines changed

backend/build.gradle.kts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ val exposed_version: String by project
44
val h2_version: String by project
55
val kotlin_version: String by project
66
val logback_version: String by project
7+
val testcontainers_version: String by project
8+
79

810
plugins {
911
kotlin("jvm") version "2.1.20"
@@ -66,4 +68,7 @@ dependencies {
6668
implementation("io.github.serpro69:kotlin-faker:1.14.0")
6769
testImplementation("io.ktor:ktor-server-test-host")
6870
testImplementation("org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version")
71+
testImplementation("org.testcontainers:testcontainers:$testcontainers_version")
72+
testImplementation("org.testcontainers:postgresql:$testcontainers_version")
73+
testImplementation("org.testcontainers:junit-jupiter:$testcontainers_version")
6974
}

backend/gradle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ h2_version=2.3.232
44
kotlin_version=2.1.20
55
ktor_version=3.1.2
66
logback_version=1.4.14
7+
testcontainers_version=1.20.6

backend/src/main/kotlin/edu/agh/roomie/Databases.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ import kotlin.random.Random
1313

1414
fun Application.configureDatabases(): Database {
1515
val database = if (isDeployment) Database.connect(
16-
url = "jdbc:postgresql://dpg-cvtqhis9c44c738puva0-a.oregon-postgres.render.com/roomie_hkrz",
16+
url = System.getenv("DATABASE_URL"),
1717
driver = "org.postgresql.Driver",
18-
user = "roomie",
19-
password = "A9hloke0pADrSXz8TzIGEUTYfiJzQM72"
18+
user = System.getenv("DB_USER"),
19+
password = System.getenv("DB_PASSWORD")
2020
) else Database.connect(
2121
url = "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1",
2222
user = "root",

backend/src/test/kotlin/edu/agh/roomie/TestUtils.kt

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,48 @@ package edu.agh.roomie
22

33
import org.jetbrains.exposed.sql.Database
44
import org.jetbrains.exposed.sql.transactions.transaction
5+
import org.testcontainers.containers.PostgreSQLContainer
6+
import org.testcontainers.utility.DockerImageName
57

68
object TestUtils {
7-
/**
8-
* Creates an in-memory H2 database for testing
9-
*/
10-
fun createTestDatabase(): Database {
11-
return Database.connect(
12-
url = "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1",
13-
user = "root",
14-
driver = "org.h2.Driver",
15-
password = ""
16-
)
9+
private val postgresContainer by lazy {
10+
PostgreSQLContainer<Nothing>(DockerImageName.parse("postgres:15-alpine")).apply {
11+
withDatabaseName("testdb")
12+
withUsername("test")
13+
withPassword("test")
14+
start()
1715
}
18-
19-
/**
20-
* Executes a transaction on the test database
21-
*/
22-
fun <T> withTestDatabase(block: () -> T): T {
23-
val database = createTestDatabase()
24-
return transaction(database) {
25-
block()
26-
}
16+
}
17+
18+
/**
19+
* Creates a PostgreSQL database for testing using TestContainers
20+
*/
21+
22+
val testDatabase: Database by lazy {
23+
Database.connect(
24+
url = postgresContainer.jdbcUrl,
25+
user = postgresContainer.username,
26+
driver = "org.postgresql.Driver",
27+
password = postgresContainer.password
28+
)
29+
}
30+
31+
fun createTestDatabase(): Database {
32+
// Ensure the container is started
33+
if (!postgresContainer.isRunning) {
34+
postgresContainer.start()
35+
}
36+
37+
return testDatabase
38+
}
39+
40+
/**
41+
* Executes a transaction on the test database
42+
*/
43+
fun <T> withTestDatabase(block: () -> T): T {
44+
val database = createTestDatabase()
45+
return transaction(database) {
46+
block()
2747
}
28-
}
48+
}
49+
}

backend/src/test/kotlin/edu/agh/roomie/rest/endpoints/UserRoutingTest.kt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
package edu.agh.roomie.rest.endpoints
22

33
import edu.agh.roomie.rest.model.*
4-
import io.ktor.client.call.*
54
import io.ktor.client.request.*
65
import io.ktor.client.statement.*
76
import io.ktor.http.*
8-
import io.ktor.server.testing.*
9-
import kotlinx.serialization.json.Json
10-
import kotlin.test.*
11-
import kotlinx.serialization.builtins.*
7+
import kotlin.test.Test
8+
import kotlin.test.assertEquals
9+
import kotlin.test.assertTrue
1210

1311
class UserRoutingTest {
1412

@@ -210,7 +208,7 @@ class UserRoutingTest {
210208

211209
val info2 = Info(
212210
fullName = "User Two",
213-
gender = 2, // Female
211+
gender = 1,
214212
age = 23,
215213
description = "Test description",
216214
sleepSchedule = Pair("23:00", "07:00"),

render.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ services:
1111
value: 8080
1212
- key: DEPLOYMENT
1313
value: "true"
14+
- key: DATABASE_URL
15+
sync: false
16+
- key: DB_USER
17+
sync: false
18+
- key: DB_PASSWORD
19+
sync: false
1420

1521
- type: web
1622
name: roomie-frontend

0 commit comments

Comments
 (0)