From cdad6963c740b337a9e38db149559fe7a6e3e90e Mon Sep 17 00:00:00 2001 From: ccornu <ccornu@takima.fr> Date: Mon, 10 Feb 2025 19:10:10 +0100 Subject: [PATCH] feat: improve integration tests --- src/test/kotlin/BaseIntegrationTest.kt | 1 + .../kotlin/player/PlayerIntegrationTest.kt | 30 ++++++++++++++----- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/test/kotlin/BaseIntegrationTest.kt b/src/test/kotlin/BaseIntegrationTest.kt index da696fa..c6eb51f 100644 --- a/src/test/kotlin/BaseIntegrationTest.kt +++ b/src/test/kotlin/BaseIntegrationTest.kt @@ -7,6 +7,7 @@ abstract class BaseIntegrationTest : KoinTest { environment { config = ApplicationConfig("application-test.yaml") } + startApplication() test() } } \ No newline at end of file diff --git a/src/test/kotlin/player/PlayerIntegrationTest.kt b/src/test/kotlin/player/PlayerIntegrationTest.kt index 4585d90..896b785 100644 --- a/src/test/kotlin/player/PlayerIntegrationTest.kt +++ b/src/test/kotlin/player/PlayerIntegrationTest.kt @@ -1,8 +1,10 @@ package player import BaseIntegrationTest +import betclic.test.player.Player import betclic.test.player.PlayerRepository import betclic.test.player.dtos.PlayerCreationDTO +import betclic.test.player.dtos.PlayerUpdateDTO import io.ktor.client.request.* import io.ktor.http.* import kotlinx.serialization.encodeToString @@ -13,14 +15,15 @@ import org.koin.test.inject import kotlin.test.assertEquals -private const val PLAYER_NAME = "Clement" +private const val NAME_1 = "Clement" +private const val NAME_2 = "Maxime" class PlayerIntegrationTest : BaseIntegrationTest() { @Test fun `When calling player creation, a player should be saved in DB`() = iTest { val response = client.post("/players") { - val player = PlayerCreationDTO(pseudo = PLAYER_NAME) + val player = PlayerCreationDTO(pseudo = NAME_1) val json = Json.encodeToString(player) header(HttpHeaders.ContentType, ContentType.Application.Json) setBody(json) @@ -28,10 +31,23 @@ class PlayerIntegrationTest : BaseIntegrationTest() { assertEquals(HttpStatusCode.Created, response.status) - val playerRepository: PlayerRepository by inject() - val players = playerRepository.findAll() - assertThat(players).hasSize(1) - assertThat(players.first()).extracting("pseudo", "pointsNumber") - .containsExactly(PLAYER_NAME, 0) + val playerRepository by inject<PlayerRepository>() + val player = playerRepository.findPlayerByPseudo(NAME_1) + assertThat(player).extracting("pseudo", "pointsNumber") + .containsExactly(NAME_1, 0) + } + + @Test + fun `When calling player update, a player should be updated in DB`() = iTest { + val playerRepository by inject<PlayerRepository>() + playerRepository.createNewPlayer(Player(pseudo = NAME_2, pointsNumber = 0)) + val response = client.put("/players") { + header(HttpHeaders.ContentType, ContentType.Application.Json) + setBody(Json.encodeToString(PlayerUpdateDTO(pseudo = NAME_2, pointsNumber = 30))) + } + assertEquals(HttpStatusCode.OK, response.status) + val player = playerRepository.findPlayerByPseudo(NAME_2) + assertThat(player).extracting("pseudo", "pointsNumber") + .containsExactly(NAME_2, 30) } } -- GitLab