testApplication

This commit is contained in:
2025-03-27 11:57:58 +01:00
parent 027747a20b
commit 933907b608
7 changed files with 95 additions and 115 deletions

View File

@@ -1,12 +1,9 @@
package eventDemo.adapter.infrastructureLayer
import eventDemo.configuration.configure
import eventDemo.testKoinApplicationWithConfig
import io.kotest.core.NamedTag
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.equals.shouldBeEqual
import io.ktor.server.testing.testApplication
import org.koin.core.context.stopKoin
import org.koin.ktor.ext.inject
import javax.sql.DataSource
class PostgresqlTest :
@@ -14,23 +11,18 @@ class PostgresqlTest :
tags(NamedTag("postgresql"))
test("test connection with postgresql") {
testApplication {
application {
stopKoin()
configure()
val datasource by inject<DataSource>()
datasource.connection.use { connection ->
connection
.prepareStatement(
"""
select 1;
""".trimIndent(),
).execute()
.let {
it shouldBeEqual true
}
}
testKoinApplicationWithConfig {
val datasource by inject<DataSource>()
datasource.connection.use { connection ->
connection
.prepareStatement(
"""
select 1;
""".trimIndent(),
).execute()
.let {
it shouldBeEqual true
}
}
}
}

View File

@@ -7,7 +7,7 @@ import eventDemo.business.event.event.GameStartedEvent
import eventDemo.business.event.event.NewPlayerEvent
import eventDemo.business.event.event.PlayerReadyEvent
import eventDemo.business.event.projection.gameList.GameList
import eventDemo.configuration.configure
import eventDemo.testApplicationWithConfig
import io.kotest.assertions.nondeterministic.eventually
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.collections.shouldContain
@@ -19,10 +19,7 @@ import io.ktor.client.request.get
import io.ktor.client.statement.bodyAsText
import io.ktor.http.ContentType
import io.ktor.http.HttpStatusCode
import io.ktor.server.testing.testApplication
import kotlinx.coroutines.runBlocking
import org.koin.core.context.stopKoin
import org.koin.ktor.ext.inject
import kotlin.test.assertEquals
import kotlin.test.assertTrue
import kotlin.time.Duration.Companion.seconds
@@ -30,12 +27,9 @@ import kotlin.time.Duration.Companion.seconds
class GameListRouteTest :
FunSpec({
test("/games with no game started") {
testApplication {
testApplicationWithConfig {
val player1 = Player(name = "Nikola")
application {
stopKoin()
configure()
}
httpClient()
.get("/games") {
@@ -50,18 +44,13 @@ class GameListRouteTest :
}
test("/games return a game with status OPENING") {
testApplication {
testApplicationWithConfig { koin ->
val gameId = GameId()
val player1 = Player(name = "Nikola")
application {
stopKoin()
configure()
val eventHandler by inject<GameEventHandler>()
runBlocking {
eventHandler.handle(gameId) { NewPlayerEvent(gameId, player1, it) }
}
val eventHandler = koin.get<GameEventHandler>()
runBlocking {
eventHandler.handle(gameId) { NewPlayerEvent(gameId, player1, it) }
}
// Wait until the projection is created
@@ -84,29 +73,23 @@ class GameListRouteTest :
}
test("/games return a game with status IS_STARTED") {
testApplication {
testApplicationWithConfig { koin ->
val gameId = GameId()
val player1 = Player(name = "Nikola")
val player2 = Player(name = "Einstein")
application {
stopKoin()
configure()
val eventHandler by inject<GameEventHandler>()
runBlocking {
eventHandler.handle(gameId) { NewPlayerEvent(gameId, player1, it) }
eventHandler.handle(gameId) { NewPlayerEvent(gameId, player2, it) }
eventHandler.handle(gameId) { PlayerReadyEvent(gameId, player1, it) }
eventHandler.handle(gameId) { PlayerReadyEvent(gameId, player2, it) }
eventHandler.handle(gameId) {
GameStartedEvent.new(
gameId,
setOf(player1, player2),
it,
shuffleIsDisabled = true,
)
}
val eventHandler = koin.get<GameEventHandler>()
runBlocking {
eventHandler.handle(gameId) { NewPlayerEvent(gameId, player1, it) }
eventHandler.handle(gameId) { NewPlayerEvent(gameId, player2, it) }
eventHandler.handle(gameId) { PlayerReadyEvent(gameId, player1, it) }
eventHandler.handle(gameId) { PlayerReadyEvent(gameId, player2, it) }
eventHandler.handle(gameId) {
GameStartedEvent.new(
gameId,
setOf(player1, player2),
it,
shuffleIsDisabled = true,
)
}
}

View File

@@ -23,7 +23,7 @@ import eventDemo.business.notification.PlayerWasReadyNotification
import eventDemo.business.notification.TheGameWasStartedNotification
import eventDemo.business.notification.WelcomeToTheGameNotification
import eventDemo.libs.event.projection.ProjectionSnapshotRepositoryInMemory
import eventDemo.testApplicationWithConfig
import eventDemo.testKoinApplicationWithConfig
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.collections.shouldHaveSize
import io.kotest.matchers.equals.shouldBeEqual
@@ -169,7 +169,7 @@ class GameSimulationTest :
}
}
testApplicationWithConfig {
testKoinApplicationWithConfig {
val commandHandler by inject<GameCommandHandler>()
val eventStore by inject<GameEventStore>()
val playerNotificationListener by inject<PlayerNotificationListener>()

View File

@@ -10,7 +10,7 @@ import eventDemo.business.event.event.NewPlayerEvent
import eventDemo.business.event.event.PlayerReadyEvent
import eventDemo.business.event.projection.gameState.GameState
import eventDemo.business.event.projection.gameState.GameStateRepository
import eventDemo.configuration.configure
import eventDemo.testApplicationWithConfig
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.collections.shouldHaveSize
import io.kotest.matchers.equals.shouldBeEqual
@@ -20,11 +20,8 @@ import io.ktor.client.request.get
import io.ktor.client.statement.bodyAsText
import io.ktor.http.ContentType
import io.ktor.http.HttpStatusCode
import io.ktor.server.testing.testApplication
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import org.koin.core.context.stopKoin
import org.koin.ktor.ext.inject
import kotlin.test.assertEquals
import kotlin.test.assertIs
import kotlin.test.assertNotNull
@@ -32,13 +29,9 @@ import kotlin.test.assertNotNull
class GameStateRouteTest :
FunSpec({
test("/games/{id}/state on empty game") {
testApplication {
testApplicationWithConfig {
val id = GameId()
val player1 = Player(name = "Nikola")
application {
stopKoin()
configure()
}
httpClient()
.get("/games/$id/state") {
@@ -55,50 +48,46 @@ class GameStateRouteTest :
}
test("/games/{id}/card/last") {
testApplication {
testApplicationWithConfig { koin ->
val gameId = GameId()
val player1 = Player(name = "Nikola")
val player2 = Player(name = "Einstein")
var lastPlayedCard: Card? = null
application {
stopKoin()
configure()
val eventHandler = koin.get<GameEventHandler>()
val stateRepo = koin.get<GameStateRepository>()
val eventHandler by inject<GameEventHandler>()
val stateRepo by inject<GameStateRepository>()
runBlocking {
eventHandler.handle(gameId) { NewPlayerEvent(gameId, player1, it) }
eventHandler.handle(gameId) { NewPlayerEvent(gameId, player2, it) }
eventHandler.handle(gameId) { PlayerReadyEvent(gameId, player1, it) }
eventHandler.handle(gameId) { PlayerReadyEvent(gameId, player2, it) }
eventHandler.handle(gameId) {
GameStartedEvent.new(
gameId,
setOf(player1, player2),
it,
shuffleIsDisabled = true,
)
}
delay(100)
lastPlayedCard = stateRepo.getLast(gameId).playableCards(player1).first()
assertNotNull(lastPlayedCard)
.let { assertIs<Card.NumericCard>(lastPlayedCard) }
.let {
it.number shouldBeEqual 0
it.color shouldBeEqual Card.Color.Red
}
delay(100)
eventHandler.handle(gameId) {
CardIsPlayedEvent(
gameId,
assertNotNull(lastPlayedCard),
player1,
it,
)
}
delay(100)
runBlocking {
eventHandler.handle(gameId) { NewPlayerEvent(gameId, player1, it) }
eventHandler.handle(gameId) { NewPlayerEvent(gameId, player2, it) }
eventHandler.handle(gameId) { PlayerReadyEvent(gameId, player1, it) }
eventHandler.handle(gameId) { PlayerReadyEvent(gameId, player2, it) }
eventHandler.handle(gameId) {
GameStartedEvent.new(
gameId,
setOf(player1, player2),
it,
shuffleIsDisabled = true,
)
}
delay(100)
lastPlayedCard = stateRepo.getLast(gameId).playableCards(player1).first()
assertNotNull(lastPlayedCard)
.let { assertIs<Card.NumericCard>(lastPlayedCard) }
.let {
it.number shouldBeEqual 0
it.color shouldBeEqual Card.Color.Red
}
delay(100)
eventHandler.handle(gameId) {
CardIsPlayedEvent(
gameId,
assertNotNull(lastPlayedCard),
player1,
it,
)
}
delay(100)
}
httpClient()