328 lines
13 KiB
Kotlin
328 lines
13 KiB
Kotlin
package solutions.bitbadger.documents.jvm.integration.common
|
|
|
|
import solutions.bitbadger.documents.Configuration
|
|
import solutions.bitbadger.documents.Field
|
|
import solutions.bitbadger.documents.FieldMatch
|
|
import solutions.bitbadger.documents.extensions.*
|
|
import solutions.bitbadger.documents.support.*
|
|
import kotlin.test.assertEquals
|
|
import kotlin.test.assertNotNull
|
|
import kotlin.test.assertNull
|
|
import kotlin.test.assertTrue
|
|
|
|
/**
|
|
* Integration tests for the `Find` object
|
|
*/
|
|
object Find {
|
|
|
|
fun allDefault(db: ThrowawayDatabase) {
|
|
JsonDocument.load(db)
|
|
assertEquals(
|
|
5,
|
|
db.conn.findAll(TEST_TABLE, JsonDocument::class.java).size,
|
|
"There should have been 5 documents returned"
|
|
)
|
|
}
|
|
|
|
fun allAscending(db: ThrowawayDatabase) {
|
|
JsonDocument.load(db)
|
|
val docs = db.conn.findAll(TEST_TABLE, JsonDocument::class.java, listOf(Field.named("id")))
|
|
assertEquals(5, docs.size, "There should have been 5 documents returned")
|
|
assertEquals(
|
|
"five|four|one|three|two",
|
|
docs.joinToString("|") { it.id },
|
|
"The documents were not ordered correctly"
|
|
)
|
|
}
|
|
|
|
fun allDescending(db: ThrowawayDatabase) {
|
|
JsonDocument.load(db)
|
|
val docs = db.conn.findAll(TEST_TABLE, JsonDocument::class.java, listOf(Field.named("id DESC")))
|
|
assertEquals(5, docs.size, "There should have been 5 documents returned")
|
|
assertEquals(
|
|
"two|three|one|four|five",
|
|
docs.joinToString("|") { it.id },
|
|
"The documents were not ordered correctly"
|
|
)
|
|
}
|
|
|
|
fun allNumOrder(db: ThrowawayDatabase) {
|
|
JsonDocument.load(db)
|
|
val docs = db.conn.findAll(
|
|
TEST_TABLE,
|
|
JsonDocument::class.java,
|
|
listOf(Field.named("sub.foo NULLS LAST"), Field.named("n:numValue"))
|
|
)
|
|
assertEquals(5, docs.size, "There should have been 5 documents returned")
|
|
assertEquals(
|
|
"two|four|one|three|five",
|
|
docs.joinToString("|") { it.id },
|
|
"The documents were not ordered correctly"
|
|
)
|
|
}
|
|
|
|
fun allEmpty(db: ThrowawayDatabase) =
|
|
assertEquals(
|
|
0,
|
|
db.conn.findAll(TEST_TABLE, JsonDocument::class.java).size,
|
|
"There should have been no documents returned"
|
|
)
|
|
|
|
fun byIdString(db: ThrowawayDatabase) {
|
|
JsonDocument.load(db)
|
|
val doc = db.conn.findById(TEST_TABLE, "two", JsonDocument::class.java)
|
|
assertNotNull(doc, "The document should have been returned")
|
|
assertEquals("two", doc.id, "An incorrect document was returned")
|
|
}
|
|
|
|
fun byIdNumber(db: ThrowawayDatabase) {
|
|
Configuration.idField = "key"
|
|
try {
|
|
db.conn.insert(TEST_TABLE, NumIdDocument(18, "howdy"))
|
|
val doc = db.conn.findById(TEST_TABLE, 18, NumIdDocument::class.java)
|
|
assertNotNull(doc, "The document should have been returned")
|
|
} finally {
|
|
Configuration.idField = "id"
|
|
}
|
|
}
|
|
|
|
fun byIdNotFound(db: ThrowawayDatabase) {
|
|
JsonDocument.load(db)
|
|
assertNull(
|
|
db.conn.findById(TEST_TABLE, "x", JsonDocument::class.java),
|
|
"There should have been no document returned"
|
|
)
|
|
}
|
|
|
|
fun byFieldsMatch(db: ThrowawayDatabase) {
|
|
JsonDocument.load(db)
|
|
val docs = db.conn.findByFields(
|
|
TEST_TABLE,
|
|
listOf(Field.any("value", listOf("blue", "purple")), Field.exists("sub")),
|
|
JsonDocument::class.java,
|
|
FieldMatch.ALL
|
|
)
|
|
assertEquals(1, docs.size, "There should have been a document returned")
|
|
assertEquals("four", docs[0].id, "The incorrect document was returned")
|
|
}
|
|
|
|
fun byFieldsMatchOrdered(db: ThrowawayDatabase) {
|
|
JsonDocument.load(db)
|
|
val docs = db.conn.findByFields(
|
|
TEST_TABLE,
|
|
listOf(Field.equal("value", "purple")),
|
|
JsonDocument::class.java,
|
|
orderBy = listOf(Field.named("id"))
|
|
)
|
|
assertEquals(2, docs.size, "There should have been 2 documents returned")
|
|
assertEquals("five|four", docs.joinToString("|") { it.id }, "The documents were not ordered correctly")
|
|
}
|
|
|
|
fun byFieldsMatchNumIn(db: ThrowawayDatabase) {
|
|
JsonDocument.load(db)
|
|
val docs = db.conn.findByFields(
|
|
TEST_TABLE,
|
|
listOf(Field.any("numValue", listOf(2, 4, 6, 8))),
|
|
JsonDocument::class.java
|
|
)
|
|
assertEquals(1, docs.size, "There should have been a document returned")
|
|
assertEquals("three", docs[0].id, "The incorrect document was returned")
|
|
}
|
|
|
|
fun byFieldsNoMatch(db: ThrowawayDatabase) {
|
|
JsonDocument.load(db)
|
|
assertEquals(
|
|
0,
|
|
db.conn.findByFields(TEST_TABLE, listOf(Field.greater("numValue", 100)), JsonDocument::class.java).size,
|
|
"There should have been no documents returned"
|
|
)
|
|
}
|
|
|
|
fun byFieldsMatchInArray(db: ThrowawayDatabase) {
|
|
ArrayDocument.testDocuments.forEach { db.conn.insert(TEST_TABLE, it) }
|
|
val docs =
|
|
db.conn.findByFields(
|
|
TEST_TABLE,
|
|
listOf(Field.inArray("values", TEST_TABLE, listOf("c"))),
|
|
ArrayDocument::class.java
|
|
)
|
|
assertEquals(2, docs.size, "There should have been two documents returned")
|
|
assertTrue(listOf("first", "second").contains(docs[0].id), "An incorrect document was returned (${docs[0].id}")
|
|
assertTrue(listOf("first", "second").contains(docs[1].id), "An incorrect document was returned (${docs[1].id}")
|
|
}
|
|
|
|
fun byFieldsNoMatchInArray(db: ThrowawayDatabase) {
|
|
ArrayDocument.testDocuments.forEach { db.conn.insert(TEST_TABLE, it) }
|
|
assertEquals(
|
|
0,
|
|
db.conn.findByFields(
|
|
TEST_TABLE,
|
|
listOf(Field.inArray("values", TEST_TABLE, listOf("j"))),
|
|
ArrayDocument::class.java
|
|
).size,
|
|
"There should have been no documents returned"
|
|
)
|
|
}
|
|
|
|
fun byContainsMatch(db: ThrowawayDatabase) {
|
|
JsonDocument.load(db)
|
|
val docs = db.conn.findByContains(TEST_TABLE, mapOf("value" to "purple"), JsonDocument::class.java)
|
|
assertEquals(2, docs.size, "There should have been 2 documents returned")
|
|
assertTrue(listOf("four", "five").contains(docs[0].id), "An incorrect document was returned (${docs[0].id}")
|
|
assertTrue(listOf("four", "five").contains(docs[1].id), "An incorrect document was returned (${docs[1].id}")
|
|
}
|
|
|
|
fun byContainsMatchOrdered(db: ThrowawayDatabase) {
|
|
JsonDocument.load(db)
|
|
val docs = db.conn.findByContains(
|
|
TEST_TABLE,
|
|
mapOf("sub" to mapOf("foo" to "green")),
|
|
JsonDocument::class.java,
|
|
listOf(Field.named("value"))
|
|
)
|
|
assertEquals(2, docs.size, "There should have been 2 documents returned")
|
|
assertEquals("two|four", docs.joinToString("|") { it.id }, "The documents were not ordered correctly")
|
|
}
|
|
|
|
fun byContainsNoMatch(db: ThrowawayDatabase) {
|
|
JsonDocument.load(db)
|
|
assertEquals(
|
|
0,
|
|
db.conn.findByContains(TEST_TABLE, mapOf("value" to "indigo"), JsonDocument::class.java).size,
|
|
"There should have been no documents returned"
|
|
)
|
|
}
|
|
|
|
fun byJsonPathMatch(db: ThrowawayDatabase) {
|
|
JsonDocument.load(db)
|
|
val docs = db.conn.findByJsonPath(TEST_TABLE, "$.numValue ? (@ > 10)", JsonDocument::class.java)
|
|
assertEquals(2, docs.size, "There should have been 2 documents returned")
|
|
assertTrue(listOf("four", "five").contains(docs[0].id), "An incorrect document was returned (${docs[0].id}")
|
|
assertTrue(listOf("four", "five").contains(docs[1].id), "An incorrect document was returned (${docs[1].id}")
|
|
}
|
|
|
|
fun byJsonPathMatchOrdered(db: ThrowawayDatabase) {
|
|
JsonDocument.load(db)
|
|
val docs = db.conn.findByJsonPath(
|
|
TEST_TABLE,
|
|
"$.numValue ? (@ > 10)",
|
|
JsonDocument::class.java,
|
|
listOf(Field.named("id"))
|
|
)
|
|
assertEquals(2, docs.size, "There should have been 2 documents returned")
|
|
assertEquals("five|four", docs.joinToString("|") { it.id }, "The documents were not ordered correctly")
|
|
}
|
|
|
|
fun byJsonPathNoMatch(db: ThrowawayDatabase) {
|
|
JsonDocument.load(db)
|
|
assertEquals(
|
|
0,
|
|
db.conn.findByJsonPath(TEST_TABLE, "$.numValue ? (@ > 100)", JsonDocument::class.java).size,
|
|
"There should have been no documents returned"
|
|
)
|
|
}
|
|
|
|
fun firstByFieldsMatchOne(db: ThrowawayDatabase) {
|
|
JsonDocument.load(db)
|
|
val doc =
|
|
db.conn.findFirstByFields(TEST_TABLE, listOf(Field.equal("value", "another")), JsonDocument::class.java)
|
|
assertNotNull(doc, "There should have been a document returned")
|
|
assertEquals("two", doc.id, "The incorrect document was returned")
|
|
}
|
|
|
|
fun firstByFieldsMatchMany(db: ThrowawayDatabase) {
|
|
JsonDocument.load(db)
|
|
val doc =
|
|
db.conn.findFirstByFields(TEST_TABLE, listOf(Field.equal("sub.foo", "green")), JsonDocument::class.java)
|
|
assertNotNull(doc, "There should have been a document returned")
|
|
assertTrue(listOf("two", "four").contains(doc.id), "An incorrect document was returned (${doc.id}")
|
|
}
|
|
|
|
fun firstByFieldsMatchOrdered(db: ThrowawayDatabase) {
|
|
JsonDocument.load(db)
|
|
val doc = db.conn.findFirstByFields(
|
|
TEST_TABLE, listOf(Field.equal("sub.foo", "green")), JsonDocument::class.java, orderBy = listOf(
|
|
Field.named("n:numValue DESC")
|
|
)
|
|
)
|
|
assertNotNull(doc, "There should have been a document returned")
|
|
assertEquals("four", doc.id, "An incorrect document was returned")
|
|
}
|
|
|
|
fun firstByFieldsNoMatch(db: ThrowawayDatabase) {
|
|
JsonDocument.load(db)
|
|
assertNull(
|
|
db.conn.findFirstByFields(TEST_TABLE, listOf(Field.equal("value", "absent")), JsonDocument::class.java),
|
|
"There should have been no document returned"
|
|
)
|
|
}
|
|
|
|
fun firstByContainsMatchOne(db: ThrowawayDatabase) {
|
|
JsonDocument.load(db)
|
|
val doc = db.conn.findFirstByContains(TEST_TABLE, mapOf("value" to "FIRST!"), JsonDocument::class.java)
|
|
assertNotNull(doc, "There should have been a document returned")
|
|
assertEquals("one", doc.id, "An incorrect document was returned")
|
|
}
|
|
|
|
fun firstByContainsMatchMany(db: ThrowawayDatabase) {
|
|
JsonDocument.load(db)
|
|
val doc = db.conn.findFirstByContains(TEST_TABLE, mapOf("value" to "purple"), JsonDocument::class.java)
|
|
assertNotNull(doc, "There should have been a document returned")
|
|
assertTrue(listOf("four", "five").contains(doc.id), "An incorrect document was returned (${doc.id}")
|
|
}
|
|
|
|
fun firstByContainsMatchOrdered(db: ThrowawayDatabase) {
|
|
JsonDocument.load(db)
|
|
val doc = db.conn.findFirstByContains(
|
|
TEST_TABLE,
|
|
mapOf("value" to "purple"),
|
|
JsonDocument::class.java,
|
|
listOf(Field.named("sub.bar NULLS FIRST"))
|
|
)
|
|
assertNotNull(doc, "There should have been a document returned")
|
|
assertEquals("five", doc.id, "An incorrect document was returned")
|
|
}
|
|
|
|
fun firstByContainsNoMatch(db: ThrowawayDatabase) {
|
|
JsonDocument.load(db)
|
|
assertNull(
|
|
db.conn.findFirstByContains(TEST_TABLE, mapOf("value" to "indigo"), JsonDocument::class.java),
|
|
"There should have been no document returned"
|
|
)
|
|
}
|
|
|
|
fun firstByJsonPathMatchOne(db: ThrowawayDatabase) {
|
|
JsonDocument.load(db)
|
|
val doc = db.conn.findFirstByJsonPath(TEST_TABLE, "$.numValue ? (@ == 10)", JsonDocument::class.java)
|
|
assertNotNull(doc, "There should have been a document returned")
|
|
assertEquals("two", doc.id, "An incorrect document was returned")
|
|
}
|
|
|
|
fun firstByJsonPathMatchMany(db: ThrowawayDatabase) {
|
|
JsonDocument.load(db)
|
|
val doc = db.conn.findFirstByJsonPath(TEST_TABLE, "$.numValue ? (@ > 10)", JsonDocument::class.java)
|
|
assertNotNull(doc, "There should have been a document returned")
|
|
assertTrue(listOf("four", "five").contains(doc.id), "An incorrect document was returned (${doc.id}")
|
|
}
|
|
|
|
fun firstByJsonPathMatchOrdered(db: ThrowawayDatabase) {
|
|
JsonDocument.load(db)
|
|
val doc = db.conn.findFirstByJsonPath(
|
|
TEST_TABLE,
|
|
"$.numValue ? (@ > 10)",
|
|
JsonDocument::class.java,
|
|
listOf(Field.named("id DESC"))
|
|
)
|
|
assertNotNull(doc, "There should have been a document returned")
|
|
assertEquals("four", doc.id, "An incorrect document was returned")
|
|
}
|
|
|
|
fun firstByJsonPathNoMatch(db: ThrowawayDatabase) {
|
|
JsonDocument.load(db)
|
|
assertNull(
|
|
db.conn.findFirstByJsonPath(TEST_TABLE, "$.numValue ? (@ > 100)", JsonDocument::class.java),
|
|
"There should have been no document returned"
|
|
)
|
|
}
|
|
}
|