-
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.
add default serializer for documentid + test
- Loading branch information
1 parent
6b7b3ee
commit f1fa404
Showing
2 changed files
with
38 additions
and
0 deletions.
There are no files selected for viewing
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
36 changes: 36 additions & 0 deletions
36
model/src/commonTest/kotlin/DocumentIdSerializationTest.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,36 @@ | ||
import dev.stashy.mongoservices.model.DocumentId | ||
import dev.stashy.mongoservices.model.DocumentIdSerializer | ||
import kotlinx.serialization.Contextual | ||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.encodeToString | ||
import kotlinx.serialization.json.Json | ||
import kotlinx.serialization.modules.SerializersModule | ||
import kotlinx.serialization.modules.contextual | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class DocumentIdSerializationTest { | ||
private val customJson = Json { serializersModule = SerializersModule { contextual(DocumentIdSerializer) } } | ||
|
||
@Serializable | ||
data class TestObj(@SerialName("_id") @Contextual val id: @Contextual DocumentId, val foo: String) | ||
|
||
@Test | ||
fun `documentId serialization`() { | ||
val documentId = DocumentId("test") | ||
val serializedDefault = Json.encodeToString(documentId) | ||
val serializedCustom = customJson.encodeToString(documentId) | ||
|
||
assertEquals("\"test\"", serializedDefault) | ||
assertEquals("\"test\"", serializedCustom) | ||
} | ||
|
||
@Test | ||
fun `contextual documentId inside an object`() { | ||
val documentId = DocumentId("test") | ||
val testObj = TestObj(documentId, "test") | ||
|
||
assertEquals(customJson.encodeToString(testObj), Json.encodeToString(testObj)) | ||
} | ||
} |