Fix tests

This commit is contained in:
Claude Brisson
2024-08-29 02:25:14 +02:00
parent a2afde292d
commit e35adb496e
3 changed files with 24 additions and 14 deletions

View File

@@ -38,16 +38,17 @@ data class Game(
// serialization // serialization
fun Game.toJson() = Json.Object( fun Game.toJson() = Json.MutableObject(
"id" to id, "id" to id,
"t" to table, "t" to table,
"w" to white, "w" to white,
"b" to black, "b" to black,
"h" to handicap, "h" to handicap,
"r" to "${result.symbol}", "r" to "${result.symbol}"
"dd" to drawnUpDown, ).also { game ->
"ft" to forcedTable if (drawnUpDown != 0) game["dd"] = drawnUpDown
) if (forcedTable) game["ft"] = true
}
fun Game.Companion.fromJson(json: Json.Object) = Game( fun Game.Companion.fromJson(json: Json.Object) = Game(
id = json.getID("id") ?: throw Error("missing game id"), id = json.getID("id") ?: throw Error("missing game id"),

View File

@@ -369,9 +369,15 @@ fun Tournament<*>.toJson() = Json.MutableObject(
"gobanSize" to gobanSize, "gobanSize" to gobanSize,
"timeSystem" to timeSystem.toJson(), "timeSystem" to timeSystem.toJson(),
"rounds" to rounds, "rounds" to rounds,
"pairing" to pairing.toJson(), "pairing" to pairing.toJson()
"tablesExclusion" to tablesExclusion.toJsonArray() ).also { tour ->
) if (tablesExclusion.isNotEmpty()) {
tour["tablesExclusion"] = tablesExclusion.toJsonArray()
}
if (frozen != null) {
tour["frozen"] = frozen
}
}
fun Tournament<*>.toFullJson(): Json.Object { fun Tournament<*>.toFullJson(): Json.Object {
val json = toJson() val json = toJson()
@@ -380,6 +386,9 @@ fun Tournament<*>.toFullJson(): Json.Object {
json["teams"] = Json.Array(teams.values.map { it.toJson() }) 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() } }); 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) { if (frozen != null) {
json["frozen"] = frozen json["frozen"] = frozen
} }

View File

@@ -162,7 +162,7 @@ class BasicTests: TestBase() {
assertEquals(aTournamentID, resp.getInt("id"), "First tournament should have id #$aTournamentID") 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 // 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 // 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 -> val expected = aTournament.entries.filter { it.key != "pairing" }.map { Pair(it.key, it.value) }.toMap().toMutableJsonObject().also { map ->
map["stats"] = Json.Array( map["stats"] = Json.Array(
Json.Object("participants" to 0, "paired" to 0, "games" to 0, "ready" to 0), Json.Object("participants" to 0, "paired" to 0, "games" to 0, "ready" to 0),
@@ -170,7 +170,7 @@ class BasicTests: TestBase() {
) )
map["teamSize"] = 1 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 @Test
@@ -203,8 +203,8 @@ class BasicTests: TestBase() {
var games = TestAPI.post("/api/tour/$aTournamentID/pair/1", Json.Array("all")).asArray() var games = TestAPI.post("/api/tour/$aTournamentID/pair/1", Json.Array("all")).asArray()
aTournamentGameID = (games[0] as Json.Object).getInt("id") aTournamentGameID = (games[0] as Json.Object).getInt("id")
val possibleResults = setOf( val possibleResults = setOf(
"""[{"id":$aTournamentGameID,"t":1,"w":$aPlayerID,"b":$anotherPlayerID,"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":"?","dd":0}]""" """[{"id":$aTournamentGameID,"t":1,"w":$anotherPlayerID,"b":$aPlayerID,"h":0,"r":"?"}]"""
) )
assertTrue(possibleResults.contains(games.toString()), "pairing differs") assertTrue(possibleResults.contains(games.toString()), "pairing differs")
games = TestAPI.get("/api/tour/$aTournamentID/res/1").asArray()!! games = TestAPI.get("/api/tour/$aTournamentID/res/1").asArray()!!
@@ -219,8 +219,8 @@ class BasicTests: TestBase() {
assertTrue(resp.getBoolean("success") == true, "expecting success") assertTrue(resp.getBoolean("success") == true, "expecting success")
val games = TestAPI.get("/api/tour/$aTournamentID/res/1") val games = TestAPI.get("/api/tour/$aTournamentID/res/1")
val possibleResults = setOf( val possibleResults = setOf(
"""[{"id":$aTournamentGameID,"t":1,"w":$aPlayerID,"b":$anotherPlayerID,"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","dd":0}]""" """[{"id":$aTournamentGameID,"t":1,"w":$anotherPlayerID,"b":$aPlayerID,"h":0,"r":"b"}]"""
) )
assertTrue(possibleResults.contains(games.toString()), "results differ") assertTrue(possibleResults.contains(games.toString()), "results differ")
} }