Fix import/export test

This commit is contained in:
Claude Brisson
2023-10-02 10:10:09 +02:00
parent c2d1e53f3f
commit dcd752040c
3 changed files with 18 additions and 10 deletions

View File

@@ -203,3 +203,13 @@ fun Tournament<*>.toJson() = Json.Object(
"rounds" to rounds, "rounds" to rounds,
"pairing" to pairing.toJson() "pairing" to pairing.toJson()
) )
fun Tournament<*>.toFullJson(): Json.Object {
val json = Json.MutableObject(toJson())
json["players"] = Json.Array(players.values.map { it.toJson() })
if (this is TeamTournament) {
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() } });
return json
}

View File

@@ -8,6 +8,7 @@ import org.jeudego.pairgoth.model.TeamTournament
import org.jeudego.pairgoth.model.Tournament import org.jeudego.pairgoth.model.Tournament
import org.jeudego.pairgoth.model.fromJson import org.jeudego.pairgoth.model.fromJson
import org.jeudego.pairgoth.model.getID import org.jeudego.pairgoth.model.getID
import org.jeudego.pairgoth.model.toFullJson
import org.jeudego.pairgoth.model.toID import org.jeudego.pairgoth.model.toID
import org.jeudego.pairgoth.model.toJson import org.jeudego.pairgoth.model.toJson
import java.nio.file.Path import java.nio.file.Path
@@ -49,12 +50,7 @@ class FileStore(pathStr: String): StoreImplementation {
val filename = tournament.filename() val filename = tournament.filename()
val file = path.resolve(filename).toFile() val file = path.resolve(filename).toFile()
if (file.exists()) throw Error("File $filename already exists") if (file.exists()) throw Error("File $filename already exists")
val json = Json.MutableObject(tournament.toJson()) val json = tournament.toFullJson()
json["players"] = Json.Array(tournament.players.values.map { it.toJson() })
if (tournament is TeamTournament) {
json["teams"] = Json.Array(tournament.teams.values.map { it.toJson() })
}
json["games"] = Json.Array((1..tournament.lastRound()).map { round -> tournament.games(round).values.map { it.toJson() } });
file.printWriter().use { out -> file.printWriter().use { out ->
out.println(json.toPrettyString()) out.println(json.toPrettyString())
} }

View File

@@ -1,6 +1,7 @@
package org.jeudego.pairgoth.test package org.jeudego.pairgoth.test
import org.jeudego.pairgoth.ext.OpenGotha import org.jeudego.pairgoth.ext.OpenGotha
import org.jeudego.pairgoth.model.toFullJson
import org.jeudego.pairgoth.model.toJson import org.jeudego.pairgoth.model.toJson
import org.jeudego.pairgoth.util.XmlUtils import org.jeudego.pairgoth.util.XmlUtils
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
@@ -10,10 +11,9 @@ import kotlin.test.assertEquals
class ImportExportTests: TestBase() { class ImportExportTests: TestBase() {
companion object { companion object {
val maskIdRegex = Regex("(?<=\"id\" ?: )\\d+") val maskIdRegex = Regex("(?<=\"id\" ?: ?)\\d+")
} }
/*
@Test @Test
fun `001 test imports`() { fun `001 test imports`() {
getTestResources("opengotha/tournamentfiles/").forEach { file -> getTestResources("opengotha/tournamentfiles/").forEach { file ->
@@ -34,8 +34,6 @@ class ImportExportTests: TestBase() {
} }
} }
*/
@Test @Test
fun `002 test opengotha import export`() { fun `002 test opengotha import export`() {
// We import a tournament // We import a tournament
@@ -44,10 +42,14 @@ class ImportExportTests: TestBase() {
val resource = file.readText(StandardCharsets.UTF_8) val resource = file.readText(StandardCharsets.UTF_8)
val root_xml = XmlUtils.parse(resource) val root_xml = XmlUtils.parse(resource)
val tournament = OpenGotha.import(root_xml) val tournament = OpenGotha.import(root_xml)
// version which also compares players and games (not ready, need to reset ids in store)
// val jsonTournament = tournament.toFullJson().toPrettyString()!!.replace(maskIdRegex, "0")
val jsonTournament = tournament.toJson().toPrettyString()!!.replace(maskIdRegex, "0") val jsonTournament = tournament.toJson().toPrettyString()!!.replace(maskIdRegex, "0")
val exported = OpenGotha.export(tournament) val exported = OpenGotha.export(tournament)
val tournament2 = OpenGotha.import(XmlUtils.parse(exported)) val tournament2 = OpenGotha.import(XmlUtils.parse(exported))
// version which also compares players and games (not ready, need to reset ids in store)
// val jsonTournament2 = tournament2.toFullJson().toPrettyString()!!.replace(maskIdRegex, "0")
val jsonTournament2 = tournament2.toJson().toPrettyString()!!.replace(maskIdRegex, "0") val jsonTournament2 = tournament2.toJson().toPrettyString()!!.replace(maskIdRegex, "0")
assertEquals(jsonTournament, jsonTournament2) assertEquals(jsonTournament, jsonTournament2)