Skip to content

Commit

Permalink
✅ Add additional tests for firestore timestamp parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
geigi committed Aug 12, 2024
1 parent f526d07 commit fbd93bf
Showing 1 changed file with 63 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class FirestoreTimestampToDecodableTimestampTest {

@OptIn(ExperimentalSerializationApi::class)
@Test
fun firestoreTimestampIsDecodedCorrectly() {
fun firestoreTimestampIsDecodedToKotlinInstantCorrectly() {
// Arrange
val expectedInstant = Instant.fromEpochSeconds(1716823455)
val expectedDate = Date.from(expectedInstant.toJavaInstant())
Expand Down Expand Up @@ -76,4 +76,66 @@ class FirestoreTimestampToDecodableTimestampTest {
assertEquals(expectedInstant, result.createdAt)
assertIs<MockLocalDataClass>(result)
}

@OptIn(ExperimentalSerializationApi::class)
@Test
fun firestoreTimestampIsDecodedToNullableKotlinInstantCorrectly() {
// Arrange
val expectedInstant = Instant.fromEpochSeconds(1716823455)
val expectedDate = Date.from(expectedInstant.toJavaInstant())
val timestamp = Timestamp(expectedDate)

@Serializable
data class MockLocalDataClass(
@Contextual
val createdAt: Instant?
)

val serializer = serializer<MockLocalDataClass>()

val data = mapOf<String, Any?>("createdAt" to timestamp)

// Act
val result =
serializer.deserialize(
StringMapToObjectDecoder(
data = data,
serializersModule = SerializersModule { contextual(InstantComponentSerializer) },
dataNormalizer = FirestoreTimestampDataNormalizer()
),
)

// Assert
assertEquals(expectedInstant, result.createdAt)
assertIs<MockLocalDataClass>(result)
}

@OptIn(ExperimentalSerializationApi::class)
@Test
fun nullIsDecodedToNullableKotlinInstantCorrectly() {
// Arrange
@Serializable
data class MockLocalDataClass(
@Contextual
val createdAt: Instant?
)

val serializer = serializer<MockLocalDataClass>()

val data = mapOf<String, Any?>("createdAt" to null)

// Act
val result =
serializer.deserialize(
StringMapToObjectDecoder(
data = data,
serializersModule = SerializersModule { contextual(InstantComponentSerializer) },
dataNormalizer = FirestoreTimestampDataNormalizer()
),
)

// Assert
assertEquals(null, result.createdAt)
assertIs<MockLocalDataClass>(result)
}
}

0 comments on commit fbd93bf

Please sign in to comment.