Skip to content

Commit

Permalink
Merge pull request #609 from scireum/feature/mbo/OX-10357-1
Browse files Browse the repository at this point in the history
JUnit: Migrates MongoExistsTest and MongoFilledTest to Kotlin
  • Loading branch information
mkeckmkeck authored Jan 30, 2024
2 parents 675d8a0 + 1ef6d77 commit 90f70bf
Show file tree
Hide file tree
Showing 4 changed files with 204 additions and 152 deletions.
77 changes: 0 additions & 77 deletions src/test/java/sirius/db/mongo/properties/MongoExistsSpec.groovy

This file was deleted.

75 changes: 0 additions & 75 deletions src/test/java/sirius/db/mongo/properties/MongoFilledSpec.groovy

This file was deleted.

109 changes: 109 additions & 0 deletions src/test/kotlin/sirius/db/mongo/properties/MongoExistsTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Made with all the love in the world
* by scireum in Remshalden, Germany
*
* Copyright by scireum GmbH
* http://www.scireum.de - [email protected]
*/

package sirius.db.mongo.properties

import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import sirius.db.KeyGenerator
import sirius.db.mongo.Mango
import sirius.db.mongo.Mongo
import sirius.db.mongo.MongoEntity
import sirius.db.mongo.QueryBuilder
import sirius.kernel.SiriusExtension
import sirius.kernel.di.std.Part
import kotlin.test.assertEquals

@ExtendWith(SiriusExtension::class)
class MongoExistsTest {
@Test
fun `exists query works`() {

val fieldMissing = mongo.insert().set(MongoEntity.ID, keyGen.generateId()).into(MongoExistsEntity::class.java)
val fieldPresent =
mongo.insert().set(MongoEntity.ID, keyGen.generateId()).set(MongoExistsEntity.TEST_FIELD, "test")
.into(MongoExistsEntity::class.java)

assertEquals(
fieldMissing.getString(MongoEntity.ID), mango.select(MongoExistsEntity::class.java)
.eq(MongoExistsEntity.TEST_FIELD, null)
.queryFirst().getIdAsString()
)
assertEquals(
1, mango.select(MongoExistsEntity::class.java)
.eq(MongoExistsEntity.TEST_FIELD, null)
.count()
)

assertEquals(
fieldPresent.getString(MongoEntity.ID), mango.select(MongoExistsEntity::class.java)
.ne(MongoExistsEntity.TEST_FIELD, null)
.queryFirst().getIdAsString()
)
assertEquals(
1, mango.select(MongoExistsEntity::class.java)
.ne(MongoExistsEntity.TEST_FIELD, null)
.count()
)

assertEquals(
fieldPresent.getString(MongoEntity.ID), mango.select(MongoExistsEntity::class.java)
.where(QueryBuilder.FILTERS.exists(MongoExistsEntity.TEST_FIELD))
.queryFirst().getIdAsString()
)
assertEquals(
1, mango.select(MongoExistsEntity::class.java)
.where(QueryBuilder.FILTERS.exists(MongoExistsEntity.TEST_FIELD))
.count()
)

assertEquals(
fieldMissing.getString(MongoEntity.ID), mango.select(MongoExistsEntity::class.java)
.where(QueryBuilder.FILTERS.notExists(MongoExistsEntity.TEST_FIELD))
.queryFirst().getIdAsString()
)
assertEquals(
1, mango.select(MongoExistsEntity::class.java)
.where(QueryBuilder.FILTERS.notExists(MongoExistsEntity.TEST_FIELD))
.count()
)

assertEquals(
fieldPresent.getString(MongoEntity.ID), mango.select(MongoExistsEntity::class.java)
.where(QueryBuilder.FILTERS.filled(MongoExistsEntity.TEST_FIELD))
.queryFirst().getIdAsString()
)
assertEquals(
1, mango.select(MongoExistsEntity::class.java)
.where(QueryBuilder.FILTERS.filled(MongoExistsEntity.TEST_FIELD))
.count()
)

assertEquals(
fieldMissing.getString(MongoEntity.ID), mango.select(MongoExistsEntity::class.java)
.where(QueryBuilder.FILTERS.notFilled(MongoExistsEntity.TEST_FIELD))
.queryFirst().getIdAsString()
)
assertEquals(
1, mango.select(MongoExistsEntity::class.java)
.where(QueryBuilder.FILTERS.notFilled(MongoExistsEntity.TEST_FIELD))
.count()
)
}

companion object {
@Part
private lateinit var mango: Mango

@Part
private lateinit var mongo: Mongo

@Part
private lateinit var keyGen: KeyGenerator
}
}
95 changes: 95 additions & 0 deletions src/test/kotlin/sirius/db/mongo/properties/MongoFilledTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Made with all the love in the world
* by scireum in Remshalden, Germany
*
* Copyright by scireum GmbH
* http://www.scireum.de - [email protected]
*/

package sirius.db.mongo.properties

import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import sirius.db.mongo.Mango
import sirius.db.mongo.Mongo
import sirius.db.mongo.QueryBuilder
import sirius.kernel.SiriusExtension
import sirius.kernel.commons.Strings
import sirius.kernel.di.std.Part
import kotlin.test.assertEquals
import kotlin.test.assertTrue

@ExtendWith(SiriusExtension::class)
class MongoFilledTest {
@Test
fun `filled notFilled query works`() {
val fieldFilled = MongoFilledEntity()
fieldFilled.testField = "test"
val fieldNotFilled = MongoFilledEntity()

mango.update(fieldFilled)
mango.update(fieldNotFilled)

assertEquals(
fieldNotFilled.getIdAsString(),
mango.select(MongoFilledEntity::class.java).eq(MongoFilledEntity.TEST_FIELD, null).queryFirst()
.getIdAsString()
)
assertEquals(
1, mango.select(MongoFilledEntity::class.java).eq(MongoFilledEntity.TEST_FIELD, null).count()
)

assertEquals(
fieldFilled.getIdAsString(),
mango.select(MongoFilledEntity::class.java).ne(MongoFilledEntity.TEST_FIELD, null).queryFirst()
.getIdAsString()
)
assertEquals(1, mango.select(MongoFilledEntity::class.java).ne(MongoFilledEntity.TEST_FIELD, null).count())

assertEquals(
fieldFilled.getIdAsString(),
mango.select(MongoFilledEntity::class.java)
.where(QueryBuilder.FILTERS.filled(MongoFilledEntity.TEST_FIELD)).queryFirst().getIdAsString()
)
assertEquals(
1,
mango.select(MongoFilledEntity::class.java)
.where(QueryBuilder.FILTERS.filled(MongoFilledEntity.TEST_FIELD)).count()
)

assertEquals(
fieldNotFilled.getIdAsString(),
mango.select(MongoFilledEntity::class.java)
.where(QueryBuilder.FILTERS.notFilled(MongoFilledEntity.TEST_FIELD)).queryFirst()
.getIdAsString()
)
assertEquals(
1,
mango.select(MongoFilledEntity::class.java)
.where(QueryBuilder.FILTERS.notFilled(MongoFilledEntity.TEST_FIELD)).count()
)

assertTrue {
mango.select(MongoFilledEntity::class.java).where(QueryBuilder.FILTERS.exists(MongoFilledEntity.TEST_FIELD))
.queryList().any { entity ->
Strings.areEqual(
entity.getIdAsString(),
fieldNotFilled.getIdAsString()
) || Strings.areEqual(entity.getIdAsString(), fieldFilled.getIdAsString())
}
}
assertEquals(
0,
mango.select(MongoFilledEntity::class.java)
.where(QueryBuilder.FILTERS.notExists(MongoFilledEntity.TEST_FIELD)).count()
)
}

companion object {
@Part
private lateinit var mango: Mango

@Part
private lateinit var mongo: Mongo
}
}

0 comments on commit 90f70bf

Please sign in to comment.