From e35adb496e8d499122d968ec4272717ec4171b17 Mon Sep 17 00:00:00 2001 From: Claude Brisson Date: Thu, 29 Aug 2024 02:25:14 +0200 Subject: [PATCH] Fix tests --- .../kotlin/org/jeudego/pairgoth/model/Game.kt | 11 ++++++----- .../org/jeudego/pairgoth/model/Tournament.kt | 15 ++++++++++++--- api-webapp/src/test/kotlin/BasicTests.kt | 12 ++++++------ 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/api-webapp/src/main/kotlin/org/jeudego/pairgoth/model/Game.kt b/api-webapp/src/main/kotlin/org/jeudego/pairgoth/model/Game.kt index 7d6f618..c0c7a90 100644 --- a/api-webapp/src/main/kotlin/org/jeudego/pairgoth/model/Game.kt +++ b/api-webapp/src/main/kotlin/org/jeudego/pairgoth/model/Game.kt @@ -38,16 +38,17 @@ data class Game( // serialization -fun Game.toJson() = Json.Object( +fun Game.toJson() = Json.MutableObject( "id" to id, "t" to table, "w" to white, "b" to black, "h" to handicap, - "r" to "${result.symbol}", - "dd" to drawnUpDown, - "ft" to forcedTable -) + "r" to "${result.symbol}" +).also { game -> + if (drawnUpDown != 0) game["dd"] = drawnUpDown + if (forcedTable) game["ft"] = true +} fun Game.Companion.fromJson(json: Json.Object) = Game( id = json.getID("id") ?: throw Error("missing game id"), diff --git a/api-webapp/src/main/kotlin/org/jeudego/pairgoth/model/Tournament.kt b/api-webapp/src/main/kotlin/org/jeudego/pairgoth/model/Tournament.kt index ede0652..ab740a1 100644 --- a/api-webapp/src/main/kotlin/org/jeudego/pairgoth/model/Tournament.kt +++ b/api-webapp/src/main/kotlin/org/jeudego/pairgoth/model/Tournament.kt @@ -369,9 +369,15 @@ fun Tournament<*>.toJson() = Json.MutableObject( "gobanSize" to gobanSize, "timeSystem" to timeSystem.toJson(), "rounds" to rounds, - "pairing" to pairing.toJson(), - "tablesExclusion" to tablesExclusion.toJsonArray() -) + "pairing" to pairing.toJson() +).also { tour -> + if (tablesExclusion.isNotEmpty()) { + tour["tablesExclusion"] = tablesExclusion.toJsonArray() + } + if (frozen != null) { + tour["frozen"] = frozen + } +} fun Tournament<*>.toFullJson(): Json.Object { val json = toJson() @@ -380,6 +386,9 @@ fun Tournament<*>.toFullJson(): Json.Object { json["teams"] = Json.Array(teams.values.map { it.toJson() }) } json["games"] = Json.Array((1..lastRound()).mapTo(Json.MutableArray()) { round -> games(round).values.mapTo(Json.MutableArray()) { it.toJson() } }); + if (tablesExclusion.isNotEmpty()) { + json["tablesExclusion"] = tablesExclusion.toJsonArray() + } if (frozen != null) { json["frozen"] = frozen } diff --git a/api-webapp/src/test/kotlin/BasicTests.kt b/api-webapp/src/test/kotlin/BasicTests.kt index 005b4b1..650110e 100644 --- a/api-webapp/src/test/kotlin/BasicTests.kt +++ b/api-webapp/src/test/kotlin/BasicTests.kt @@ -162,7 +162,7 @@ class BasicTests: TestBase() { assertEquals(aTournamentID, resp.getInt("id"), "First tournament should have id #$aTournamentID") // filter out "id", and also "komi", "rules" and "gobanSize" which were provided by default // also filter out "pairing", which is filled by all default values - val cmp = Json.Object(*resp.entries.filter { it.key !in listOf("id", "komi", "rules", "gobanSize", "pairing") }.map { Pair(it.key, it.value) }.toTypedArray()) + val cmp = Json.Object(*resp.entries.filter { it.key !in listOf("id", "komi", "rules", "gobanSize", "pairing", "frozen") }.map { Pair(it.key, it.value) }.toTypedArray()) val expected = aTournament.entries.filter { it.key != "pairing" }.map { Pair(it.key, it.value) }.toMap().toMutableJsonObject().also { map -> map["stats"] = Json.Array( Json.Object("participants" to 0, "paired" to 0, "games" to 0, "ready" to 0), @@ -170,7 +170,7 @@ class BasicTests: TestBase() { ) map["teamSize"] = 1 } - assertEquals(expected.toString(), cmp.toString(), "tournament differs") + assertEquals(expected.entries.sortedBy { it.key }.map { Pair(it.key, it.value) }.toJsonObject().toString(), cmp.entries.sortedBy { it.key }.map { Pair(it.key, it.value) }.toJsonObject().toString(), "tournament differs") } @Test @@ -203,8 +203,8 @@ class BasicTests: TestBase() { var games = TestAPI.post("/api/tour/$aTournamentID/pair/1", Json.Array("all")).asArray() aTournamentGameID = (games[0] as Json.Object).getInt("id") val possibleResults = setOf( - """[{"id":$aTournamentGameID,"t":1,"w":$aPlayerID,"b":$anotherPlayerID,"h":0,"r":"?","dd":0}]""", - """[{"id":$aTournamentGameID,"t":1,"w":$anotherPlayerID,"b":$aPlayerID,"h":0,"r":"?","dd":0}]""" + """[{"id":$aTournamentGameID,"t":1,"w":$aPlayerID,"b":$anotherPlayerID,"h":0,"r":"?"}]""", + """[{"id":$aTournamentGameID,"t":1,"w":$anotherPlayerID,"b":$aPlayerID,"h":0,"r":"?"}]""" ) assertTrue(possibleResults.contains(games.toString()), "pairing differs") games = TestAPI.get("/api/tour/$aTournamentID/res/1").asArray()!! @@ -219,8 +219,8 @@ class BasicTests: TestBase() { assertTrue(resp.getBoolean("success") == true, "expecting success") val games = TestAPI.get("/api/tour/$aTournamentID/res/1") val possibleResults = setOf( - """[{"id":$aTournamentGameID,"t":1,"w":$aPlayerID,"b":$anotherPlayerID,"h":0,"r":"b","dd":0}]""", - """[{"id":$aTournamentGameID,"t":1,"w":$anotherPlayerID,"b":$aPlayerID,"h":0,"r":"b","dd":0}]""" + """[{"id":$aTournamentGameID,"t":1,"w":$aPlayerID,"b":$anotherPlayerID,"h":0,"r":"b"}]""", + """[{"id":$aTournamentGameID,"t":1,"w":$anotherPlayerID,"b":$aPlayerID,"h":0,"r":"b"}]""" ) assertTrue(possibleResults.contains(games.toString()), "results differ") }