Skip to content

Commit

Permalink
breaking-maybe(mongo): mongo ser/de ops
Browse files Browse the repository at this point in the history
  • Loading branch information
osoykan committed Nov 27, 2024
1 parent b6ab801 commit 3dacf24
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 46 deletions.
4 changes: 3 additions & 1 deletion lib/stove-testing-e2e-mongodb/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
dependencies {
api(projects.lib.stoveTestingE2e)
api(libs.testcontainers.mongodb)
api(libs.mongojack)
api(libs.mongojack) {
exclude(group = "org.mongodb", module = "mongodb-driver-sync")
}
implementation(libs.mongodb.kotlin.coroutine)
implementation(libs.kotlinx.io.reactor.extensions)
implementation(libs.kotlinx.reactive)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import kotlinx.coroutines.flow.*
import kotlinx.coroutines.runBlocking
import org.bson.*
import org.bson.codecs.EncoderContext
import org.bson.codecs.configuration.CodecRegistry
import org.bson.conversions.Bson
import org.bson.types.ObjectId
import org.slf4j.*
Expand Down Expand Up @@ -49,8 +50,8 @@ class MongodbSystem internal constructor(
query: String,
collection: String = context.options.databaseOptions.default.collection,
assertion: (List<T>) -> Unit
): MongodbSystem = mongoClient.getDatabase(context.options.databaseOptions.default.name)
.let { it.withCodecRegistry(PojoRegistry(it.codecRegistry).register(T::class).build()) }
): MongodbSystem = mongoClient
.getDatabase(context.options.databaseOptions.default.name)
.getCollection<Document>(collection)
.withDocumentClass(T::class.java)
.find(BsonDocument.parse(query))
Expand All @@ -63,21 +64,43 @@ class MongodbSystem internal constructor(
objectId: String,
collection: String = context.options.databaseOptions.default.collection,
assertion: (T) -> Unit
): MongodbSystem = mongoClient.getDatabase(context.options.databaseOptions.default.name)
): MongodbSystem = mongoClient
.getDatabase(context.options.databaseOptions.default.name)
.getCollection<Document>(collection)
.withDocumentClass<T>()
.let { it.withCodecRegistry(PojoRegistry(it.codecRegistry).register(T::class).build()) }
.find(filterById(objectId))
.first()
.also(assertion)
.let { this }

/**
* Saves the [instance] with given [objectId] to the [collection]
*/
@MongoDsl
suspend inline fun <reified T : Any> save(
instance: T,
objectId: String = ObjectId().toHexString(),
collection: String = context.options.databaseOptions.default.collection,
codecRegistry: CodecRegistry = context.options.codecRegistry
): MongodbSystem = mongoClient
.getDatabase(context.options.databaseOptions.default.name)
.getCollection<Document>(collection)
.also { coll ->
val bson = BsonDocument()
codecRegistry.get(T::class.java)
.encode(BsonDocumentWriter(bson), instance, EncoderContext.builder().build())
val doc = Document(bson).append(RESERVED_ID, ObjectId(objectId))
coll.insertOne(doc)
}
.let { this }

@MongoDsl
suspend fun shouldNotExist(
objectId: String,
collection: String = context.options.databaseOptions.default.collection
): MongodbSystem {
val exists = mongoClient.getDatabase(context.options.databaseOptions.default.name)
val exists = mongoClient
.getDatabase(context.options.databaseOptions.default.name)
.getCollection<Document>(collection)
.find(filterById(objectId))
.firstOrNull() != null
Expand All @@ -91,30 +114,11 @@ class MongodbSystem internal constructor(
suspend fun shouldDelete(
objectId: String,
collection: String = context.options.databaseOptions.default.collection
): MongodbSystem = mongoClient.getDatabase(context.options.databaseOptions.default.name)
): MongodbSystem = mongoClient
.getDatabase(context.options.databaseOptions.default.name)
.getCollection<Document>(collection)
.deleteOne(filterById(objectId)).let { this }

/**
* Saves the [instance] with given [objectId] to the [collection]
*/
@MongoDsl
suspend inline fun <reified T : Any> save(
instance: T,
objectId: String = ObjectId().toHexString(),
collection: String = context.options.databaseOptions.default.collection
): MongodbSystem = mongoClient.getDatabase(context.options.databaseOptions.default.name)
.let { it.withCodecRegistry(PojoRegistry(it.codecRegistry).register(T::class).build()) }
.getCollection<Document>(collection)
.also { coll ->
val bson = BsonDocument()
context.options.codecRegistry.get(T::class.java)
.encode(BsonDocumentWriter(bson), instance, EncoderContext.builder().build())
val doc = Document(bson).append(RESERVED_ID, ObjectId(objectId))
coll.insertOne(doc)
}
.let { this }

/**
* Pauses the container. Use with care, as it will pause the container which might affect other tests.
* @return KafkaSystem
Expand Down

This file was deleted.

0 comments on commit 3dacf24

Please sign in to comment.