Fix import/export test
This commit is contained in:
@@ -203,3 +203,13 @@ fun Tournament<*>.toJson() = Json.Object(
|
||||
"rounds" to rounds,
|
||||
"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
|
||||
}
|
||||
|
@@ -8,6 +8,7 @@ import org.jeudego.pairgoth.model.TeamTournament
|
||||
import org.jeudego.pairgoth.model.Tournament
|
||||
import org.jeudego.pairgoth.model.fromJson
|
||||
import org.jeudego.pairgoth.model.getID
|
||||
import org.jeudego.pairgoth.model.toFullJson
|
||||
import org.jeudego.pairgoth.model.toID
|
||||
import org.jeudego.pairgoth.model.toJson
|
||||
import java.nio.file.Path
|
||||
@@ -49,12 +50,7 @@ class FileStore(pathStr: String): StoreImplementation {
|
||||
val filename = tournament.filename()
|
||||
val file = path.resolve(filename).toFile()
|
||||
if (file.exists()) throw Error("File $filename already exists")
|
||||
val json = Json.MutableObject(tournament.toJson())
|
||||
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() } });
|
||||
val json = tournament.toFullJson()
|
||||
file.printWriter().use { out ->
|
||||
out.println(json.toPrettyString())
|
||||
}
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package org.jeudego.pairgoth.test
|
||||
|
||||
import org.jeudego.pairgoth.ext.OpenGotha
|
||||
import org.jeudego.pairgoth.model.toFullJson
|
||||
import org.jeudego.pairgoth.model.toJson
|
||||
import org.jeudego.pairgoth.util.XmlUtils
|
||||
import org.junit.jupiter.api.Test
|
||||
@@ -10,10 +11,9 @@ import kotlin.test.assertEquals
|
||||
class ImportExportTests: TestBase() {
|
||||
|
||||
companion object {
|
||||
val maskIdRegex = Regex("(?<=\"id\" ?: )\\d+")
|
||||
val maskIdRegex = Regex("(?<=\"id\" ?: ?)\\d+")
|
||||
}
|
||||
|
||||
/*
|
||||
@Test
|
||||
fun `001 test imports`() {
|
||||
getTestResources("opengotha/tournamentfiles/").forEach { file ->
|
||||
@@ -34,8 +34,6 @@ class ImportExportTests: TestBase() {
|
||||
}
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
@Test
|
||||
fun `002 test opengotha import export`() {
|
||||
// We import a tournament
|
||||
@@ -44,10 +42,14 @@ class ImportExportTests: TestBase() {
|
||||
val resource = file.readText(StandardCharsets.UTF_8)
|
||||
val root_xml = XmlUtils.parse(resource)
|
||||
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 exported = OpenGotha.export(tournament)
|
||||
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")
|
||||
|
||||
assertEquals(jsonTournament, jsonTournament2)
|
||||
|
Reference in New Issue
Block a user