Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Vertx test context from database tests #392

Merged
merged 1 commit into from
May 27, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import fi.vauhtijuoksu.vauhtijuoksuapi.models.Donation
import fi.vauhtijuoksu.vauhtijuoksuapi.testdata.TestDonation.Companion.donation1
import fi.vauhtijuoksu.vauhtijuoksuapi.testdata.TestDonation.Companion.donation2
import fi.vauhtijuoksu.vauhtijuoksuapi.testdata.TestDonation.Companion.donation3
import io.vertx.junit5.VertxTestContext
import io.vertx.kotlin.coroutines.coAwait
import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import java.util.UUID
Expand Down Expand Up @@ -37,31 +38,29 @@ class DonationDatabaseTest : VauhtijuoksuDatabaseTest<Donation>() {
}

@Test
fun testUpdate(testContext: VertxTestContext) {
fun testUpdate() = runTest {
val newDonation = donation1.copy(read = true, message = null)
db.update(donation1.copy(read = true, message = null))
.compose { db.getAll() }
.onFailure(testContext::failNow)
.onSuccess { res ->
testContext.verify {
assertEquals(listOf(newDonation, donation2), res)
}
testContext.completeNow()
.coAwait()

db.getAll()
.coAwait()
.let { res ->
assertEquals(listOf(newDonation, donation2), res)
}
}

@Test
fun testUpdatingNonExistingRecord(testContext: VertxTestContext) {
fun testUpdatingNonExistingRecord() = runTest {
db.update(donation3)
.failOnSuccess(testContext)
.recoverIfMissingEntity(testContext)
.compose { db.getAll() }
.onSuccess { res ->
testContext.verify {
assertEquals(listOf(donation1, donation2), res)
}
testContext.completeNow()
.failOnSuccess()
.recoverIfMissingEntity()
.coAwait()

db.getAll()
.coAwait()
.let { res ->
assertEquals(listOf(donation1, donation2), res)
}
.onFailure(testContext::failNow)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import com.google.inject.Injector
import fi.vauhtijuoksu.vauhtijuoksuapi.database.api.VauhtijuoksuDatabase
import fi.vauhtijuoksu.vauhtijuoksuapi.models.Incentive
import fi.vauhtijuoksu.vauhtijuoksuapi.testdata.TestIncentive
import io.vertx.junit5.VertxTestContext
import io.vertx.kotlin.coroutines.coAwait
import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import java.util.UUID
Expand Down Expand Up @@ -35,32 +36,29 @@ class IncentiveDatabaseTest : VauhtijuoksuDatabaseTest<Incentive>() {
}

@Test
fun testUpdate(testContext: VertxTestContext) {
fun testUpdate() = runTest {
val expectedIncentive = existingRecord1().copy(title = "Changed the title")
db.update(existingRecord1().copy(title = "Changed the title"))
.onFailure(testContext::failNow)
.compose { db.getAll() }
.onFailure(testContext::failNow)
.onSuccess { res ->
testContext.verify {
assertEquals(listOf(expectedIncentive, existingRecord2()), res)
}
testContext.completeNow()
.coAwait()

db.getAll()
.coAwait()
.let { res ->
assertEquals(listOf(expectedIncentive, existingRecord2()), res)
}
}

@Test
fun testUpdatingNonExistingRecord(testContext: VertxTestContext) {
fun testUpdatingNonExistingRecord() = runTest {
db.update(newRecord())
.failOnSuccess(testContext)
.recoverIfMissingEntity(testContext)
.compose { db.getAll() }
.onFailure(testContext::failNow)
.onSuccess { res ->
testContext.verify {
assertEquals(listOf(existingRecord1(), existingRecord2()), res)
}
testContext.completeNow()
.failOnSuccess()
.recoverIfMissingEntity()
.coAwait()

db.getAll()
.coAwait()
.let { res ->
assertEquals(listOf(existingRecord1(), existingRecord2()), res)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,16 @@ import fi.vauhtijuoksu.vauhtijuoksuapi.database.configuration.DatabaseConfigurat
import fi.vauhtijuoksu.vauhtijuoksuapi.models.StreamMetadata
import fi.vauhtijuoksu.vauhtijuoksuapi.testdata.TestGameData
import fi.vauhtijuoksu.vauhtijuoksuapi.testdata.TestPlayer
import io.vertx.junit5.VertxExtension
import io.vertx.junit5.VertxTestContext
import io.vertx.kotlin.coroutines.coAwait
import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.testcontainers.containers.PostgreSQLContainer
import org.testcontainers.junit.jupiter.Container
import org.testcontainers.junit.jupiter.Testcontainers

@Testcontainers
@ExtendWith(VertxExtension::class)
class MetadataTimerDatabaseTest {
private lateinit var db: StreamMetadataDatabase
private lateinit var gameDataDb: GameDataDatabase
Expand Down Expand Up @@ -71,19 +69,16 @@ class MetadataTimerDatabaseTest {
}

@Test
fun `database returns empty data initially`(testContext: VertxTestContext) {
fun `database returns empty data initially`() = runTest {
db.get()
.onFailure(testContext::failNow)
.onSuccess {
testContext.verify {
assertEquals(emptyData, it)
}
testContext.completeNow()
.coAwait()
.let {
assertEquals(emptyData, it)
}
}

@Test
fun `database saves given data`(testContext: VertxTestContext) {
fun `database saves given data`() = runTest {
val gameData = TestGameData.gameData1
assertEquals(1, gameData.players.size)
assertEquals(TestPlayer.player1.id, gameData.players.first())
Expand All @@ -100,23 +95,20 @@ class MetadataTimerDatabaseTest {
)
}
.compose { db.get() }
.onFailure(testContext::failNow)
.onSuccess {
testContext.verify {
assertEquals(
someData.copy(
currentGameId = gameData.id,
counters = listOf(1, 3, 100),
),
it,
)
}
testContext.completeNow()
.coAwait()
.let {
assertEquals(
someData.copy(
currentGameId = gameData.id,
counters = listOf(1, 3, 100),
),
it,
)
}
}

@Test
fun `current game is set to null when the game is deleted`(testContext: VertxTestContext) {
fun `current game is set to null when the game is deleted`() = runTest {
val gameData = TestGameData.gameData1
assertEquals(1, gameData.players.size)
assertEquals(TestPlayer.player1.id, gameData.players.first())
Expand All @@ -129,12 +121,9 @@ class MetadataTimerDatabaseTest {
}
.compose { gameDataDb.delete(gameData.id) }
.compose { db.get() }
.onFailure(testContext::failNow)
.onSuccess {
testContext.verify {
assertEquals(someData.copy(currentGameId = null), it)
}
testContext.completeNow()
.coAwait()
.let {
assertEquals(someData.copy(currentGameId = null), it)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import com.google.inject.Injector
import fi.vauhtijuoksu.vauhtijuoksuapi.database.api.VauhtijuoksuDatabase
import fi.vauhtijuoksu.vauhtijuoksuapi.models.Player
import fi.vauhtijuoksu.vauhtijuoksuapi.testdata.TestPlayer
import io.vertx.junit5.VertxTestContext
import io.vertx.kotlin.coroutines.coAwait
import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import java.util.*
import java.util.UUID

class PlayerDatabaseTest : VauhtijuoksuDatabaseTest<Player>() {
override fun existingRecord1(): Player {
Expand Down Expand Up @@ -35,37 +36,31 @@ class PlayerDatabaseTest : VauhtijuoksuDatabaseTest<Player>() {
}

@Test
fun testUpdate(testContext: VertxTestContext) {
fun testUpdate() = runTest {
val oldId = existingRecord1().id
val newPlayer = existingRecord2().copy(id = oldId)
db.update(newPlayer)
.compose {
.flatMap {
db.getAll()
}
.map {
testContext.verify {
assertEquals(listOf(existingRecord2(), newPlayer), it)
}
testContext.completeNow()
.coAwait()
.let {
assertEquals(listOf(existingRecord2(), newPlayer), it)
}
.onFailure(testContext::failNow)
}

@Test
fun testUpdateNonExisting(testContext: VertxTestContext) {
fun testUpdateNonExisting() = runTest {
val newUuid = UUID.randomUUID()
db.update(existingRecord2().copy(id = newUuid))
.failOnSuccess(testContext)
.recoverIfMissingEntity(testContext)
.compose {
db.getAll()
}
.map {
testContext.verify {
assertEquals(listOf(existingRecord1(), existingRecord2()), it)
}
testContext.completeNow()
.failOnSuccess()
.recoverIfMissingEntity()
.coAwait()

db.getAll()
.coAwait()
.let {
assertEquals(listOf(existingRecord1(), existingRecord2()), it)
}
.onFailure(testContext::failNow)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,18 @@ package fi.vauhtijuoksu.vauhtijuoksuapi.database.impl

import fi.vauhtijuoksu.vauhtijuoksuapi.exceptions.MissingEntityException
import io.vertx.core.Future
import io.vertx.junit5.VertxTestContext
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.fail

internal fun <V> Future<V>.failOnSuccess(testContext: VertxTestContext): Future<V> {
return this.onSuccess { testContext.failNow("Expected to fail") }
internal fun <V> Future<V>.failOnSuccess(): Future<V> {
return this.onSuccess {
fail("Expected to fail")
}
}

internal fun <V> Future<V>.recoverIfMissingEntity(testContext: VertxTestContext): Future<V> {
internal fun <V> Future<V>.recoverIfMissingEntity(): Future<V> {
return this.recover {
testContext.verify {
assertTrue(it is MissingEntityException)
}
assertTrue(it is MissingEntityException)
return@recover Future.succeededFuture()
}
}

internal fun <V> Future<V>.completeOnSuccessOrFail(testContext: VertxTestContext): Future<V> {
return this.onSuccess {
testContext.completeNow()
}.onFailure(testContext::failNow)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package fi.vauhtijuoksu.vauhtijuoksuapi.database.impl
import com.google.inject.Injector
import fi.vauhtijuoksu.vauhtijuoksuapi.database.api.VauhtijuoksuDatabase
import fi.vauhtijuoksu.vauhtijuoksuapi.models.Timer
import io.vertx.junit5.VertxTestContext
import io.vertx.kotlin.coroutines.coAwait
import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import java.time.Instant
Expand Down Expand Up @@ -77,7 +78,7 @@ class TimerDatabaseTest : VauhtijuoksuDatabaseTest<Timer>() {
}

@Test
fun testUpdate(testContext: VertxTestContext) {
fun testUpdate() = runTest {
val newTimer = timer1.copy(
startTime = OffsetDateTime.ofInstant(
Instant.from(DateTimeFormatter.ISO_INSTANT.parse("2022-06-05T16:00:00Z")),
Expand All @@ -90,32 +91,30 @@ class TimerDatabaseTest : VauhtijuoksuDatabaseTest<Timer>() {
name = "new timer name",
)
db.update(newTimer)
.compose { db.getAll() }
.onFailure(testContext::failNow)
.onSuccess { res ->
testContext.verify {
val list1 = mutableListOf(newTimer, timer2)
list1.sortBy { it.id }
val results = res.toMutableList()
results.sortBy { it.id }
assertEquals(list1, results)
}
testContext.completeNow()
.coAwait()

db.getAll()
.coAwait()
.let { res ->
val list1 = mutableListOf(newTimer, timer2)
list1.sortBy { it.id }
val results = res.toMutableList()
results.sortBy { it.id }
assertEquals(list1, results)
}
}

@Test
fun testUpdatingNonExistingRecord(testContext: VertxTestContext) {
fun testUpdatingNonExistingRecord() = runTest {
db.update(timer3)
.failOnSuccess(testContext)
.recoverIfMissingEntity(testContext)
.compose { db.getAll() }
.onFailure(testContext::failNow)
.onSuccess { res ->
testContext.verify {
assertEquals(listOf(timer1, timer2), res)
}
testContext.completeNow()
.failOnSuccess()
.recoverIfMissingEntity()
.coAwait()

db.getAll()
.coAwait()
.let { res ->
assertEquals(listOf(timer1, timer2), res)
}
}
}
Loading
Loading