Files
myice-kmp/composeApp/src/commonTest/kotlin/ch/parano/myicek/models/ModelSerializationTest.kt
T
herel 418a8978f2 ref: Rename package ch.parano.myice to ch.parano.myicek
Complete the MyIceK rename by updating the Kotlin package from ch.parano.myice to ch.parano.myicek across all source sets (androidMain, commonMain, desktopMain, iosMain, commonTest), the build.gradle.kts (namespace, applicationId, mainClass, desktop packageName) and the iOS Info.plist bundle URL name. The myice:// OAuth scheme is kept unchanged as it depends on the backend.

Also add pull-to-refresh to ScheduleScreen using Material 3 PullToRefreshBox, replacing the manual loading overlay.
2026-08-06 15:20:47 +02:00

201 lines
6.8 KiB
Kotlin

// SPDX-License-Identifier: GPL-3.0-or-later
// MyIce Kotlin Multiplatform — schedule/convocation viewer
// Copyright (C) 2026 parano.ch
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package ch.parano.myicek.models
import kotlinx.serialization.json.Json
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNull
class ModelSerializationTest {
private val json = Json { ignoreUnknownKeys = true }
@Test
fun testAccountDeserialization() {
val input = """{"name":"default","label":"Default"}"""
val account = json.decodeFromString<Account>(input)
assertEquals("default", account.name)
assertEquals("Default", account.label)
}
@Test
fun testEventDeserialization() {
val input = """
{
"id_event": 761040,
"agegroup": "U13 (Elite)",
"name": "U13 Elite - HC Ajoie",
"title": "U13 (Elite)\nGame\nRaiffeisen Arena",
"opponent": "HC Ajoie",
"place": "Raiffeisen Arena, Porrentruy",
"start": "2024-11-10T14:00:00",
"end": "2024-11-10T16:15:00",
"color": "#e4222e",
"event": "Jeu"
}
""".trimIndent()
val event = json.decodeFromString<Event>(input)
assertEquals("761040", event.idEvent)
assertEquals("U13 (Elite)", event.agegroup)
assertEquals("U13 Elite - HC Ajoie", event.name)
assertEquals("HC Ajoie", event.opponent)
assertEquals("#e4222e", event.color)
assertEquals("Jeu", event.eventType)
}
@Test
fun testEventDeserializationWithNullFields() {
val input = """
{
"id_event": "12345",
"agegroup": "U15",
"name": "",
"title": "Practice",
"opponent": "",
"place": "Les Vernets",
"start": "2024-11-10T14:00:00",
"end": "2024-11-10T15:30:00"
}
""".trimIndent()
val event = json.decodeFromString<Event>(input)
assertEquals("12345", event.idEvent)
assertEquals("", event.name)
assertEquals("", event.opponent)
assertNull(event.color)
assertEquals("", event.eventType)
}
@Test
fun testPlayerWithIntNumber() {
val input = """
{"position":"Gardien","number":1,"fname":"Jean","lname":"Dupont","dob":"2012-03-15"}
""".trimIndent()
val player = json.decodeFromString<Player>(input)
assertEquals("Gardien", player.position)
assertEquals("1", player.number)
assertEquals("Jean", player.fname)
}
@Test
fun testPlayerWithStringNumber() {
val input = """
{"position":"Attaquant","number":"27","fname":"Pierre","lname":"Martin","dob":"2012-05-20"}
""".trimIndent()
val player = json.decodeFromString<Player>(input)
assertEquals("27", player.number)
}
@Test
fun testPlayerWithNullNumber() {
val input = """
{"position":null,"number":null,"fname":"Paul","lname":"Durand","dob":"2012-01-10"}
""".trimIndent()
val player = json.decodeFromString<Player>(input)
assertNull(player.position)
assertNull(player.number)
}
@Test
fun testStaffDeserialization() {
val input = """
{"role":"Coach","fname":"Jean","lname":"Dupont"}
""".trimIndent()
val staff = json.decodeFromString<Staff>(input)
assertEquals("Coach", staff.role)
assertEquals("Jean", staff.fname)
assertEquals("Dupont", staff.lname)
}
@Test
fun testConvocationDeserialization() {
val input = """
{
"available": [
{"position":"Gardien","number":1,"fname":"Jean","lname":"Dupont","dob":"2012-03-15"}
],
"staff": [
{"role":"Coach","fname":"Marc","lname":"Blanc"}
]
}
""".trimIndent()
val convocation = json.decodeFromString<Convocation>(input)
assertEquals(1, convocation.available.size)
assertEquals("1", convocation.available[0].number)
assertEquals(1, convocation.staff.size)
assertEquals("Coach", convocation.staff[0].role)
}
@Test
fun testConvocationWithEmptyArrays() {
val input = """{"available":[],"staff":[]}"""
val convocation = json.decodeFromString<Convocation>(input)
assertEquals(0, convocation.available.size)
assertEquals(0, convocation.staff.size)
}
@Test
fun testEventDetailDeserialization() {
val input = """
{
"title": "U13 (Elite) - Saison HC Ajoie",
"type": "games",
"place": "Raiffeisen Arena, Porrentruy",
"time_start": "2024-11-10T14:00:00",
"time_end": "2024-11-10T16:15:00",
"is_away": "1",
"convocation": {
"available": [],
"staff": []
}
}
""".trimIndent()
val detail = json.decodeFromString<EventDetail>(input)
assertEquals("U13 (Elite) - Saison HC Ajoie", detail.title)
assertEquals("games", detail.type)
assertEquals("2024-11-10T14:00:00", detail.timeStart)
assertEquals(0, detail.convocation.available.size)
}
@Test
fun testUserInfoDeserialization() {
val input = """{"email":"rene@luria.ch","sub":"abc123"}"""
val userInfo = json.decodeFromString<UserInfo>(input)
assertEquals("rene@luria.ch", userInfo.email)
}
@Test
fun testEventRoundTripSerialization() {
val event = Event(
idEvent = "761040",
agegroup = "U13 (Elite)",
name = "U13 Elite - HC Ajoie",
title = "U13 (Elite)\nGame\nArena",
opponent = "HC Ajoie",
place = "Arena",
start = "2024-11-10T14:00:00",
end = "2024-11-10T16:15:00",
color = "#e4222e",
eventType = "Jeu",
)
val jsonString = json.encodeToString(Event.serializer(), event)
val decoded = json.decodeFromString<Event>(jsonString)
assertEquals(event, decoded)
}
}