Reorganize tests

This commit is contained in:
Claude Brisson
2023-12-23 07:18:53 +01:00
parent abd9fdf837
commit e666efdcc3
3 changed files with 168 additions and 146 deletions

View File

@@ -16,6 +16,8 @@ import kotlin.test.fail
@TestInstance(TestInstance.Lifecycle.PER_CLASS) @TestInstance(TestInstance.Lifecycle.PER_CLASS)
class BasicTests: TestBase() { class BasicTests: TestBase() {
companion object {
val aTournament = Json.Object( val aTournament = Json.Object(
"type" to "INDIVIDUAL", "type" to "INDIVIDUAL",
"name" to "Mon Tournoi", "name" to "Mon Tournoi",
@@ -77,7 +79,6 @@ class BasicTests: TestBase() {
"type" to "MAC_MAHON" "type" to "MAC_MAHON"
) )
) )
val aPlayer = Json.Object( val aPlayer = Json.Object(
"name" to "Burma", "name" to "Burma",
"firstname" to "Nestor", "firstname" to "Nestor",
@@ -118,10 +119,10 @@ class BasicTests: TestBase() {
) )
) )
) )
}
var aTournamentID: ID? = null var aTournamentID: ID? = null
var aTeamTournamentID: ID? = null
var aPlayerID: ID? = null var aPlayerID: ID? = null
var anotherPlayerID: ID? = null var anotherPlayerID: ID? = null
var aTournamentGameID: ID? = null var aTournamentGameID: ID? = null
@@ -196,50 +197,4 @@ class BasicTests: TestBase() {
) )
assertTrue(possibleResults.contains(games.toString()), "results differ") assertTrue(possibleResults.contains(games.toString()), "results differ")
} }
@Test
fun `007 Mac Mahon handicap`() {
var resp = TestAPI.post("/api/tour", aMMTournament).asObject()
val tourId = resp.getInt("id") ?: throw Error("tournament creation failed")
resp = TestAPI.post("/api/tour/$tourId/part", aPlayer).asObject().also { assertTrue(it.getBoolean("success")!!) }
val p1 = resp.getInt("id")!!
resp = TestAPI.post("/api/tour/$tourId/part", anotherPlayer).asObject().also { assertTrue(it.getBoolean("success")!!) }
val p2 = resp.getInt("id")!!
val game = TestAPI.post("/api/tour/$tourId/pair/1", Json.Array("all")).asArray().getObject(0) ?: throw Error("pairing failed")
assertEquals(p2, game.getInt("w"))
assertEquals(p1, game.getInt("b"))
assertEquals(3, game.getInt("h"))
}
@Test
fun `008 team tournament, MacMahon`() {
var resp = TestAPI.post("/api/tour", aTeamTournament).asObject()
assertTrue(resp.getBoolean("success") == true, "expecting success")
aTeamTournamentID = resp.getInt("id")
resp = TestAPI.post("/api/tour/$aTeamTournamentID/part", aPlayer).asObject()
assertTrue(resp.getBoolean("success") == true, "expecting success")
val aTeamPlayerID = resp.getInt("id") ?: fail("id cannot be null")
resp = TestAPI.post("/api/tour/$aTeamTournamentID/part", anotherPlayer).asObject()
assertTrue(resp.getBoolean("success") == true, "expecting success")
val anotherTeamPlayerID = resp.getInt("id") ?: fail("id cannot be null")
var arr = TestAPI.get("/api/tour/$aTeamTournamentID/pair/1").asArray()
assertEquals("[]", arr.toString(), "expecting an empty array")
resp = TestAPI.post("/api/tour/$aTeamTournamentID/team", Json.parse("""{ "name":"The Buffallos", "players":[$aTeamPlayerID, $anotherTeamPlayerID] }""")?.asObject() ?: fail("no null allowed here")).asObject()
assertTrue(resp.getBoolean("success") == true, "expecting success")
val aTeamID = resp.getInt("id") ?: error("no null allowed here")
resp = TestAPI.get("/api/tour/$aTeamTournamentID/team/$aTeamID").asObject()
assertEquals("""{"id":$aTeamID,"name":"The Buffallos","players":[$aTeamPlayerID,$anotherTeamPlayerID]}""", resp.toString(), "expecting team description")
arr = TestAPI.get("/api/tour/$aTeamTournamentID/pair/1").asArray()
assertEquals("[$aTeamID]", arr.toString(), "expecting a singleton array")
// nothing stops us in reusing players in different teams, at least for now...
resp = TestAPI.post("/api/tour/$aTeamTournamentID/team", Json.parse("""{ "name":"The Billies", "players":[$aTeamPlayerID, $anotherTeamPlayerID] }""")?.asObject() ?: fail("no null here")).asObject()
assertTrue(resp.getBoolean("success") == true, "expecting success")
val anotherTeamID = resp.getInt("id") ?: fail("no null here")
arr = TestAPI.get("/api/tour/$aTeamTournamentID/pair/1").asArray()
assertEquals("[$aTeamID,$anotherTeamID]", arr.toString(), "expecting two pairables")
arr = TestAPI.post("/api/tour/$aTeamTournamentID/pair/1", Json.parse("""["all"]""")).asArray()
assertTrue(resp.getBoolean("success") == true, "expecting success")
// TODO check pairing
// val expected = """"["id":1,"w":5,"b":6,"h":3,"r":"?"]"""
}
} }

View File

@@ -0,0 +1,23 @@
package org.jeudego.pairgoth.test
import com.republicate.kson.Json
import org.junit.jupiter.api.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
class MacMahonTest {
@Test
fun `Mac Mahon with handicap`() {
var resp = TestAPI.post("/api/tour", BasicTests.aMMTournament).asObject()
val tourId = resp.getInt("id") ?: throw Error("tournament creation failed")
resp = TestAPI.post("/api/tour/$tourId/part", BasicTests.aPlayer).asObject().also { assertTrue(it.getBoolean("success")!!) }
val p1 = resp.getInt("id")!!
resp = TestAPI.post("/api/tour/$tourId/part", BasicTests.anotherPlayer).asObject().also { assertTrue(it.getBoolean("success")!!) }
val p2 = resp.getInt("id")!!
val game = TestAPI.post("/api/tour/$tourId/pair/1", Json.Array("all")).asArray().getObject(0) ?: throw Error("pairing failed")
assertEquals(p2, game.getInt("w"))
assertEquals(p1, game.getInt("b"))
assertEquals(3, game.getInt("h"))
}
}

View File

@@ -0,0 +1,44 @@
package org.jeudego.pairgoth.test
import com.republicate.kson.Json
import org.jeudego.pairgoth.test.BasicTests.Companion.aPlayer
import org.jeudego.pairgoth.test.BasicTests.Companion.aTeamTournament
import org.jeudego.pairgoth.test.BasicTests.Companion.anotherPlayer
import org.junit.jupiter.api.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
import kotlin.test.fail
class TeamTest {
@Test
fun `team tournament, MacMahon`() {
var resp = TestAPI.post("/api/tour", aTeamTournament).asObject()
assertTrue(resp.getBoolean("success") == true, "expecting success")
val aTeamTournamentID = resp.getInt("id")
resp = TestAPI.post("/api/tour/$aTeamTournamentID/part", aPlayer).asObject()
assertTrue(resp.getBoolean("success") == true, "expecting success")
val aTeamPlayerID = resp.getInt("id") ?: fail("id cannot be null")
resp = TestAPI.post("/api/tour/$aTeamTournamentID/part", anotherPlayer).asObject()
assertTrue(resp.getBoolean("success") == true, "expecting success")
val anotherTeamPlayerID = resp.getInt("id") ?: fail("id cannot be null")
var arr = TestAPI.get("/api/tour/$aTeamTournamentID/pair/1").asArray()
assertEquals("[]", arr.toString(), "expecting an empty array")
resp = TestAPI.post("/api/tour/$aTeamTournamentID/team", Json.parse("""{ "name":"The Buffallos", "players":[$aTeamPlayerID, $anotherTeamPlayerID] }""")?.asObject() ?: fail("no null allowed here")).asObject()
assertTrue(resp.getBoolean("success") == true, "expecting success")
val aTeamID = resp.getInt("id") ?: error("no null allowed here")
resp = TestAPI.get("/api/tour/$aTeamTournamentID/team/$aTeamID").asObject()
assertEquals("""{"id":$aTeamID,"name":"The Buffallos","players":[$aTeamPlayerID,$anotherTeamPlayerID]}""", resp.toString(), "expecting team description")
arr = TestAPI.get("/api/tour/$aTeamTournamentID/pair/1").asArray()
assertEquals("[$aTeamID]", arr.toString(), "expecting a singleton array")
// nothing stops us in reusing players in different teams, at least for now...
resp = TestAPI.post("/api/tour/$aTeamTournamentID/team", Json.parse("""{ "name":"The Billies", "players":[$aTeamPlayerID, $anotherTeamPlayerID] }""")?.asObject() ?: fail("no null here")).asObject()
assertTrue(resp.getBoolean("success") == true, "expecting success")
val anotherTeamID = resp.getInt("id") ?: fail("no null here")
arr = TestAPI.get("/api/tour/$aTeamTournamentID/pair/1").asArray()
assertEquals("[$aTeamID,$anotherTeamID]", arr.toString(), "expecting two pairables")
arr = TestAPI.post("/api/tour/$aTeamTournamentID/pair/1", Json.parse("""["all"]""")).asArray()
assertTrue(resp.getBoolean("success") == true, "expecting success")
// TODO check pairing
// val expected = """"["id":1,"w":5,"b":6,"h":3,"r":"?"]"""
}
}