Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes #365 CoroutineCollection#save doesn't use specified Serializer …
…(kotlinx.serialization)
- Loading branch information
Showing
6 changed files
with
211 additions
and
31 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
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
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
92 changes: 92 additions & 0 deletions
92
kmongo-serialization/src/test/kotlin/Issue365UUIDSerializer.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,92 @@ | ||
/* | ||
* Copyright (C) 2016/2021 Litote | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.litote.kmongo.issues | ||
|
||
import kotlinx.serialization.Contextual | ||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.descriptors.PrimitiveKind | ||
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor | ||
import kotlinx.serialization.descriptors.SerialDescriptor | ||
import kotlinx.serialization.encoding.Decoder | ||
import kotlinx.serialization.encoding.Encoder | ||
import org.junit.Test | ||
import org.litote.kmongo.AllCategoriesKMongoBaseTest | ||
import org.litote.kmongo.findOne | ||
import org.litote.kmongo.save | ||
import org.litote.kmongo.serialization.registerSerializer | ||
import java.util.UUID | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertTrue | ||
|
||
@Serializable | ||
data class DBPlayer( | ||
@SerialName("_id") @Contextual val uuid: UUID, | ||
) | ||
|
||
object UUIDSerializer : KSerializer<UUID> { | ||
override val descriptor: SerialDescriptor = | ||
PrimitiveSerialDescriptor("UUID", PrimitiveKind.STRING) | ||
|
||
override fun deserialize(decoder: Decoder): UUID { | ||
deserialized = true | ||
return UUID.fromString(decoder.decodeString()) | ||
} | ||
|
||
override fun serialize(encoder: Encoder, value: UUID) { | ||
serialized = true | ||
encoder.encodeString(value.toString()) | ||
} | ||
} | ||
|
||
private var deserialized = false | ||
private var serialized = false | ||
|
||
/** | ||
* | ||
*/ | ||
class Issue365UUIDSerializer : AllCategoriesKMongoBaseTest<DBPlayer>() { | ||
|
||
init { | ||
registerSerializer(UUIDSerializer) | ||
} | ||
|
||
@Test | ||
fun testInsertSerialization() { | ||
val test = DBPlayer(UUID.randomUUID()) | ||
|
||
col.insertOne(test) | ||
val test2 = col.findOne() | ||
|
||
assertEquals(test, test2) | ||
assertTrue(deserialized) | ||
assertTrue(serialized) | ||
} | ||
|
||
@Test | ||
fun testSaveSerialization() { | ||
val test = DBPlayer(UUID.randomUUID()) | ||
|
||
col.save(test) | ||
val test2 = col.findOne() | ||
|
||
assertEquals(test, test2) | ||
assertTrue(deserialized) | ||
assertTrue(serialized) | ||
} | ||
} |
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
69 changes: 69 additions & 0 deletions
69
kmongo/src/test/kotlin/org/litote/kmongo/issues/TypedTest.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,69 @@ | ||
/* | ||
* Copyright (C) 2016/2021 Litote | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.litote.kmongo.issues | ||
|
||
import org.bson.codecs.pojo.annotations.BsonId | ||
import org.bson.types.ObjectId | ||
import org.junit.Test | ||
import org.litote.kmongo.AllCategoriesKMongoBaseTest | ||
import org.litote.kmongo.MongoOperator | ||
import org.litote.kmongo.aggregate | ||
import org.litote.kmongo.div | ||
import org.litote.kmongo.eq | ||
import org.litote.kmongo.from | ||
import org.litote.kmongo.match | ||
import org.litote.kmongo.project | ||
import org.litote.kmongo.projection | ||
import org.litote.kmongo.save | ||
import java.time.Instant | ||
import java.time.ZoneOffset | ||
import kotlin.test.assertEquals | ||
|
||
data class MyClass( | ||
val items: List<Item>, | ||
val timestamp: Long = Instant.now().atOffset(ZoneOffset.UTC).toEpochSecond(), | ||
@BsonId | ||
val id: String = ObjectId().toString() | ||
) | ||
|
||
data class Item( | ||
val name: String, | ||
val price: Double, | ||
val qty: Double | ||
) | ||
|
||
|
||
data class Result(val sumPrice: Double) | ||
|
||
/** | ||
* | ||
*/ | ||
class TypedTest : | ||
AllCategoriesKMongoBaseTest<MyClass>() { | ||
|
||
@Test | ||
fun `serialization and deserialization is ok`() { | ||
val d = MyClass(listOf(Item("a", 12.9, 1.0)), id = "id") | ||
col.save(d) | ||
|
||
val r: Double = col.aggregate<Result>( | ||
match(MyClass::id eq "id"), | ||
project(Result::sumPrice to MongoOperator.sum.from((MyClass::items / Item::price).projection)) | ||
).first()?.sumPrice ?: 0.0 | ||
assertEquals(12.9, r) | ||
} | ||
} |