Add exists functions

This commit is contained in:
2025-03-01 11:35:08 -05:00
parent bc3b4bb012
commit 4d4e1d0897
9 changed files with 343 additions and 8 deletions

View File

@@ -0,0 +1,63 @@
package solutions.bitbadger.documents.common
import solutions.bitbadger.documents.*
import kotlin.test.assertFalse
import kotlin.test.assertTrue
/**
* Integration tests for the `Exists` object
*/
object Exists {
fun byIdMatch(db: ThrowawayDatabase) {
JsonDocument.load(db)
assertTrue("The document with ID \"three\" should exist") { db.conn.existsById(TEST_TABLE, "three") }
}
fun byIdNoMatch(db: ThrowawayDatabase) {
JsonDocument.load(db)
assertFalse("The document with ID \"seven\" should not exist") { db.conn.existsById(TEST_TABLE, "seven") }
}
fun byFieldsMatch(db: ThrowawayDatabase) {
JsonDocument.load(db)
assertTrue("Matching documents should have been found") {
db.conn.existsByFields(TEST_TABLE, listOf(Field.equal("numValue", 10)))
}
}
fun byFieldsNoMatch(db: ThrowawayDatabase) {
JsonDocument.load(db)
assertFalse("No matching documents should have been found") {
db.conn.existsByFields(TEST_TABLE, listOf(Field.equal("nothing", "none")))
}
}
fun byContainsMatch(db: ThrowawayDatabase) {
JsonDocument.load(db)
assertTrue("Matching documents should have been found") {
db.conn.existsByContains(TEST_TABLE, mapOf("value" to "purple"))
}
}
fun byContainsNoMatch(db: ThrowawayDatabase) {
JsonDocument.load(db)
assertFalse("Matching documents should not have been found") {
db.conn.existsByContains(TEST_TABLE, mapOf("value" to "violet"))
}
}
fun byJsonPathMatch(db: ThrowawayDatabase) {
JsonDocument.load(db)
assertTrue("Matching documents should have been found") {
db.conn.existsByJsonPath(TEST_TABLE, "$.numValue ? (@ == 10)")
}
}
fun byJsonPathNoMatch(db: ThrowawayDatabase) {
JsonDocument.load(db)
assertFalse("Matching documents should not have been found") {
db.conn.existsByJsonPath(TEST_TABLE, "$.numValue ? (@ == 10.1)")
}
}
}

View File

@@ -4,6 +4,9 @@ import org.junit.jupiter.api.DisplayName
import solutions.bitbadger.documents.common.Delete
import kotlin.test.Test
/**
* PostgreSQL integration tests for the `Delete` object / `deleteBy*` connection extension functions
*/
@DisplayName("PostgreSQL - Delete")
class DeleteIT {

View File

@@ -0,0 +1,52 @@
package solutions.bitbadger.documents.postgresql
import org.junit.jupiter.api.DisplayName
import solutions.bitbadger.documents.common.Exists
import kotlin.test.Test
/**
* PostgreSQL integration tests for the `Exists` object / `existsBy*` connection extension functions
*/
@DisplayName("PostgreSQL - Exists")
class ExistsIT {
@Test
@DisplayName("byId returns true when a document matches the ID")
fun byIdMatch() =
PgDB().use(Exists::byIdMatch)
@Test
@DisplayName("byId returns false when no document matches the ID")
fun byIdNoMatch() =
PgDB().use(Exists::byIdNoMatch)
@Test
@DisplayName("byFields returns true when documents match")
fun byFieldsMatch() =
PgDB().use(Exists::byFieldsMatch)
@Test
@DisplayName("byFields returns false when no documents match")
fun byFieldsNoMatch() =
PgDB().use(Exists::byFieldsNoMatch)
@Test
@DisplayName("byContains returns true when documents match")
fun byContainsMatch() =
PgDB().use(Exists::byContainsMatch)
@Test
@DisplayName("byContains returns false when no documents match")
fun byContainsNoMatch() =
PgDB().use(Exists::byContainsNoMatch)
@Test
@DisplayName("byJsonPath returns true when documents match")
fun byJsonPathMatch() =
PgDB().use(Exists::byJsonPathMatch)
@Test
@DisplayName("byJsonPath returns false when no documents match")
fun byJsonPathNoMatch() =
PgDB().use(Exists::byJsonPathNoMatch)
}

View File

@@ -6,6 +6,9 @@ import solutions.bitbadger.documents.DocumentException
import solutions.bitbadger.documents.common.Delete
import kotlin.test.Test
/**
* SQLite integration tests for the `Delete` object / `deleteBy*` connection extension functions
*/
@DisplayName("SQLite - Delete")
class DeleteIT {
@@ -31,13 +34,13 @@ class DeleteIT {
@Test
@DisplayName("byContains fails")
fun byContainsMatch() {
fun byContainsFails() {
assertThrows<DocumentException> { SQLiteDB().use(Delete::byContainsMatch) }
}
@Test
@DisplayName("byJsonPath fails")
fun byJsonPathMatch() {
fun byJsonPathFails() {
assertThrows<DocumentException> { SQLiteDB().use(Delete::byJsonPathMatch) }
}
}

View File

@@ -0,0 +1,46 @@
package solutions.bitbadger.documents.sqlite
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.assertThrows
import solutions.bitbadger.documents.DocumentException
import solutions.bitbadger.documents.common.Exists
import kotlin.test.Test
/**
* SQLite integration tests for the `Exists` object / `existsBy*` connection extension functions
*/
@DisplayName("SQLite - Exists")
class ExistsIT {
@Test
@DisplayName("byId returns true when a document matches the ID")
fun byIdMatch() =
SQLiteDB().use(Exists::byIdMatch)
@Test
@DisplayName("byId returns false when no document matches the ID")
fun byIdNoMatch() =
SQLiteDB().use(Exists::byIdNoMatch)
@Test
@DisplayName("byFields returns true when documents match")
fun byFieldsMatch() =
SQLiteDB().use(Exists::byFieldsMatch)
@Test
@DisplayName("byFields returns false when no documents match")
fun byFieldsNoMatch() =
SQLiteDB().use(Exists::byFieldsNoMatch)
@Test
@DisplayName("byContains fails")
fun byContainsFails() {
assertThrows<DocumentException> { SQLiteDB().use(Exists::byContainsMatch) }
}
@Test
@DisplayName("byJsonPath fails")
fun byJsonPathFails() {
assertThrows<DocumentException> { SQLiteDB().use(Exists::byJsonPathMatch) }
}
}