-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle correctly MongoDb ObjectId type
Signed-off-by: Uberto Barbini <[email protected]>
- Loading branch information
Showing
5 changed files
with
116 additions
and
81 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
kondor-mongo/src/main/kotlin/com/ubertob/kondor/mongo/json/JObjectId.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,19 @@ | ||
package com.ubertob.kondor.mongo.json | ||
|
||
import com.ubertob.kondor.json.JAny | ||
import com.ubertob.kondor.json.JField | ||
import com.ubertob.kondor.json.jsonnode.JsonNodeObject | ||
import com.ubertob.kondor.json.str | ||
import org.bson.types.ObjectId | ||
|
||
object JObjectId : JAny<ObjectId>() { | ||
|
||
val `$oid` by str(ObjectId::toHexString) | ||
override fun JsonNodeObject.deserializeOrThrow() = | ||
ObjectId(+`$oid`) | ||
|
||
} | ||
|
||
|
||
@JvmName("bindObjectId") | ||
fun <PT : Any> str(binder: PT.() -> ObjectId) = JField(binder, JObjectId) |
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
80 changes: 80 additions & 0 deletions
80
kondor-mongo/src/test/kotlin/com/ubertob/kondor/mongo/core/MongoObjectIdTest.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,80 @@ | ||
import com.ubertob.kondor.mongo.core.* | ||
import com.ubertob.kondortools.expectFailure | ||
import com.ubertob.kondortools.expectSuccess | ||
import org.bson.types.ObjectId | ||
import org.junit.jupiter.api.Test | ||
import org.testcontainers.junit.jupiter.Testcontainers | ||
import strikt.api.expectThat | ||
import strikt.assertions.contains | ||
import strikt.assertions.isEqualTo | ||
|
||
@Testcontainers | ||
class MongoObjectIdTest { | ||
|
||
|
||
private val localMongo = MongoExecutorDbClient.fromConnectionString( | ||
connection = MongoTableTest.mongoConnection, | ||
databaseName = "mongoCollTest" | ||
) | ||
|
||
object keyValueStoreTable : TypedTable<KeyValueStore>(JKeyValueStore) { | ||
override val collectionName: String = "simpleDocs" | ||
} | ||
|
||
val id1 = ObjectId() | ||
val id2 = ObjectId() | ||
val id3 = ObjectId() | ||
|
||
private val storeThreeKV = mongoOperation { | ||
keyValueStoreTable.insertOne(KeyValueStore(id1, "first", 0.0)) | ||
keyValueStoreTable.insertOne(KeyValueStore(id2, "second", 1.0)) | ||
keyValueStoreTable.insertOne(KeyValueStore(id3, "third", 2.0)) | ||
}.ignoreValue() | ||
|
||
fun queryByKey(id: ObjectId) = mongoOperation { | ||
keyValueStoreTable.findById(id) | ||
} | ||
|
||
@Test | ||
fun `query by mongo id`() { | ||
|
||
val res = localMongo(storeThreeKV + queryByKey(id2)) | ||
|
||
expectThat(res.expectSuccess()?.description).isEqualTo("second") | ||
} | ||
|
||
@Test | ||
fun `mongo id must be unique`() { | ||
val uniqueId = ObjectId() | ||
|
||
val firstTime = mongoOperation { | ||
keyValueStoreTable.insertOne(KeyValueStore(uniqueId, "first", 0.0)) | ||
} | ||
val objId = localMongo(firstTime).expectSuccess() | ||
|
||
val updateAgain = mongoOperation { | ||
keyValueStoreTable.insertOne(KeyValueStore(uniqueId, "second", 1.0)) | ||
keyValueStoreTable.insertOne(KeyValueStore(uniqueId, "third", 2.0)) | ||
} | ||
|
||
val fail = localMongo(updateAgain).expectFailure() | ||
expectThat(fail.msg).contains("duplicate key error") | ||
|
||
val res = localMongo(queryByKey(uniqueId)) | ||
|
||
expectThat(res.expectSuccess()?.description).isEqualTo("first") | ||
} | ||
|
||
@Test | ||
fun `store objectId`() { | ||
val uniqueId = ObjectId() | ||
|
||
val storeOne = mongoOperation { | ||
keyValueStoreTable.insertOne(KeyValueStore(uniqueId, "first", 0.0)) | ||
} | ||
val objId = localMongo(storeOne).expectSuccess() | ||
|
||
expectThat(objId?.asObjectId()?.value).isEqualTo(uniqueId) | ||
|
||
} | ||
} |
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