110 lines
4.9 KiB
Kotlin
110 lines
4.9 KiB
Kotlin
package solutions.bitbadger.documents.integration.common
|
|
|
|
import solutions.bitbadger.documents.*
|
|
import solutions.bitbadger.documents.integration.JsonDocument
|
|
import solutions.bitbadger.documents.integration.TEST_TABLE
|
|
import solutions.bitbadger.documents.integration.ThrowawayDatabase
|
|
import kotlin.test.*
|
|
|
|
|
|
/**
|
|
* Integration tests for the `RemoveFields` object
|
|
*/
|
|
object RemoveFields {
|
|
|
|
fun byIdMatchFields(db: ThrowawayDatabase) {
|
|
JsonDocument.load(db)
|
|
db.conn.removeFieldsById(TEST_TABLE, "two", listOf("sub", "value"))
|
|
val doc = db.conn.findById<String, JsonDocument>(TEST_TABLE, "two")
|
|
assertNotNull(doc, "There should have been a document returned")
|
|
assertEquals("", doc.value, "The value should have been empty")
|
|
assertNull(doc.sub, "The sub-document should have been removed")
|
|
}
|
|
|
|
fun byIdMatchNoFields(db: ThrowawayDatabase) {
|
|
JsonDocument.load(db)
|
|
assertFalse { db.conn.existsByFields(TEST_TABLE, listOf(Field.exists("a_field_that_does_not_exist"))) }
|
|
db.conn.removeFieldsById(TEST_TABLE, "one", listOf("a_field_that_does_not_exist")) // no exception = pass
|
|
}
|
|
|
|
fun byIdNoMatch(db: ThrowawayDatabase) {
|
|
JsonDocument.load(db)
|
|
assertFalse { db.conn.existsById(TEST_TABLE, "fifty") }
|
|
db.conn.removeFieldsById(TEST_TABLE, "fifty", listOf("sub")) // no exception = pass
|
|
}
|
|
|
|
fun byFieldsMatchFields(db: ThrowawayDatabase) {
|
|
JsonDocument.load(db)
|
|
val fields = listOf(Field.equal("numValue", 17))
|
|
db.conn.removeFieldsByFields(TEST_TABLE, fields, listOf("sub"))
|
|
val doc = db.conn.findFirstByFields<JsonDocument>(TEST_TABLE, fields)
|
|
assertNotNull(doc, "The document should have been returned")
|
|
assertEquals("four", doc.id, "An incorrect document was returned")
|
|
assertNull(doc.sub, "The sub-document should have been removed")
|
|
}
|
|
|
|
fun byFieldsMatchNoFields(db: ThrowawayDatabase) {
|
|
JsonDocument.load(db)
|
|
assertFalse { db.conn.existsByFields(TEST_TABLE, listOf(Field.exists("nada"))) }
|
|
db.conn.removeFieldsByFields(TEST_TABLE, listOf(Field.equal("numValue", 17)), listOf("nada")) // no exn = pass
|
|
}
|
|
|
|
fun byFieldsNoMatch(db: ThrowawayDatabase) {
|
|
JsonDocument.load(db)
|
|
val fields = listOf(Field.notEqual("missing", "nope"))
|
|
assertFalse { db.conn.existsByFields(TEST_TABLE, fields) }
|
|
db.conn.removeFieldsByFields(TEST_TABLE, fields, listOf("value")) // no exception = pass
|
|
}
|
|
|
|
fun byContainsMatchFields(db: ThrowawayDatabase) {
|
|
JsonDocument.load(db)
|
|
val criteria = mapOf("sub" to mapOf("foo" to "green"))
|
|
db.conn.removeFieldsByContains(TEST_TABLE, criteria, listOf("value"))
|
|
val docs = db.conn.findByContains<JsonDocument, Map<String, Map<String, String>>>(TEST_TABLE, criteria)
|
|
assertEquals(2, docs.size, "There should have been 2 documents returned")
|
|
docs.forEach {
|
|
assertTrue(listOf("two", "four").contains(it.id), "An incorrect document was returned (${it.id})")
|
|
assertEquals("", it.value, "The value should have been empty")
|
|
}
|
|
}
|
|
|
|
fun byContainsMatchNoFields(db: ThrowawayDatabase) {
|
|
JsonDocument.load(db)
|
|
assertFalse { db.conn.existsByFields(TEST_TABLE, listOf(Field.exists("invalid_field"))) }
|
|
db.conn.removeFieldsByContains(TEST_TABLE, mapOf("sub" to mapOf("foo" to "green")), listOf("invalid_field"))
|
|
// no exception = pass
|
|
}
|
|
|
|
fun byContainsNoMatch(db: ThrowawayDatabase) {
|
|
JsonDocument.load(db)
|
|
val contains = mapOf("value" to "substantial")
|
|
assertFalse { db.conn.existsByContains(TEST_TABLE, contains) }
|
|
db.conn.removeFieldsByContains(TEST_TABLE, contains, listOf("numValue"))
|
|
}
|
|
|
|
fun byJsonPathMatchFields(db: ThrowawayDatabase) {
|
|
JsonDocument.load(db)
|
|
val path = "$.value ? (@ == \"purple\")"
|
|
db.conn.removeFieldsByJsonPath(TEST_TABLE, path, listOf("sub"))
|
|
val docs = db.conn.findByJsonPath<JsonDocument>(TEST_TABLE, path)
|
|
assertEquals(2, docs.size, "There should have been 2 documents returned")
|
|
docs.forEach {
|
|
assertTrue(listOf("four", "five").contains(it.id), "An incorrect document was returned (${it.id})")
|
|
assertNull(it.sub, "The sub-document should have been removed")
|
|
}
|
|
}
|
|
|
|
fun byJsonPathMatchNoFields(db: ThrowawayDatabase) {
|
|
JsonDocument.load(db)
|
|
assertFalse { db.conn.existsByFields(TEST_TABLE, listOf(Field.exists("submarine"))) }
|
|
db.conn.removeFieldsByJsonPath(TEST_TABLE, "$.value ? (@ == \"purple\")", listOf("submarine")) // no exn = pass
|
|
}
|
|
|
|
fun byJsonPathNoMatch(db: ThrowawayDatabase) {
|
|
JsonDocument.load(db)
|
|
val path = "$.value ? (@ == \"mauve\")"
|
|
assertFalse { db.conn.existsByJsonPath(TEST_TABLE, path) }
|
|
db.conn.removeFieldsByJsonPath(TEST_TABLE, path, listOf("value")) // no exception = pass
|
|
}
|
|
}
|