-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🐛 fix firestore timestamp decoding by normalizing the data
The firestore module was merged with the common module, we try to find a better way
- Loading branch information
Showing
11 changed files
with
114 additions
and
152 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
78 changes: 78 additions & 0 deletions
78
...rc/test/kotlin/de/sipgate/federmappe/common/FirestoreTimestampToDecodableTimestampTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package de.sipgate.federmappe.common | ||
|
||
import com.google.firebase.Timestamp | ||
import de.sipgate.federmappe.common.decoder.StringMapToObjectDecoder | ||
import de.sipgate.federmappe.firestore.types.toDecodableTimestamp | ||
import kotlinx.datetime.Instant | ||
import kotlinx.datetime.serializers.InstantComponentSerializer | ||
import kotlinx.datetime.toJavaInstant | ||
import kotlinx.serialization.Contextual | ||
import kotlinx.serialization.ExperimentalSerializationApi | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.modules.SerializersModule | ||
import kotlinx.serialization.modules.contextual | ||
import kotlinx.serialization.serializer | ||
import java.util.Date | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertIs | ||
import kotlin.test.assertTrue | ||
|
||
class FirestoreTimestampToDecodableTimestampTest { | ||
|
||
@Test | ||
fun timestampWithNanosecondPrecisionIsConvertedSuccessfully() { | ||
val expectedInstant = Instant.fromEpochSeconds(1716823455, 854) | ||
|
||
val timestamp = Timestamp(expectedInstant.toJavaInstant()) | ||
|
||
val result = timestamp.toDecodableTimestamp() | ||
assertEquals(expectedInstant.epochSeconds, result["epochSeconds"]) | ||
assertEquals(expectedInstant.nanosecondsOfSecond.toLong(), result["nanosecondsOfSecond"]) | ||
} | ||
|
||
@Test | ||
fun timestampWithSecondPrecisionIsConvertedSuccessfully() { | ||
val expectedDate = Date.from(Instant.fromEpochSeconds(1716823455).toJavaInstant()) | ||
val expectedEpochSeconds = expectedDate.time / 1000 | ||
|
||
val timestamp = Timestamp(expectedDate) | ||
|
||
val result = timestamp.toDecodableTimestamp() | ||
|
||
assertEquals(expectedEpochSeconds, result["epochSeconds"]) | ||
assertEquals(0L, result["nanosecondsOfSecond"]) | ||
} | ||
|
||
@OptIn(ExperimentalSerializationApi::class) | ||
@Test | ||
fun firestoreTimestampIsDecodedCorrectly() { | ||
// 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, | ||
serializersModule = SerializersModule { contextual(InstantComponentSerializer) } | ||
), | ||
) | ||
|
||
// Assert | ||
assertEquals(expectedInstant, result.createdAt) | ||
assertIs<MockLocalDataClass>(result) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
35 changes: 0 additions & 35 deletions
35
firestore/src/testDebug/kotlin/FirestoreTimestampToDecodableTimestampTest.kt
This file was deleted.
Oops, something went wrong.