Skip to content

Commit

Permalink
test: DateToTimestampSerializer
Browse files Browse the repository at this point in the history
  • Loading branch information
LunarX committed Jan 17, 2025
1 parent 8eee360 commit 1b8d102
Showing 1 changed file with 68 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Infomaniak SwissTransfer - Multiplatform
* Copyright (C) 2025 Infomaniak Network SA
*
* 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 <http://www.gnu.org/licenses/>.
*/
package com.infomaniak.multiplatform_swisstransfer.network

import com.infomaniak.multiplatform_swisstransfer.network.serializers.DateToTimestampSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import kotlin.test.Test
import kotlin.test.assertEquals

class DateToTimestampSerializerTest {
@Test
fun canParseIso8601DatesWithTimeZones() {
val data = """
{
"date": "2025-02-15T09:51:45+01:00"
}
""".trimIndent()

val testDate = Json.decodeFromString<TestDate>(data)
assertEquals(1739609505, testDate.date)
}

@Test
fun canParseIso8601DatesWithoutTimeZones() {
val data = """
{
"date": "2025-02-15T09:51:45Z"
}
""".trimIndent()

val testDate = Json.decodeFromString<TestDate>(data)
assertEquals(1739613105, testDate.date)
}

@Test
fun canParseIso8601DatesWithFractionalSeconds() {
val data = """
{
"date": "2025-02-15T09:51:45.123+01:00"
}
""".trimIndent()

val testDate = Json.decodeFromString<TestDate>(data)
assertEquals(1739609505, testDate.date) // Adjust for rounding or truncation behavior.
}

@Serializable
private class TestDate {
@Serializable(DateToTimestampSerializer::class)
val date: Long = 0
}
}

0 comments on commit 1b8d102

Please sign in to comment.