Add patch functions
This commit is contained in:
87
src/integration-test/kotlin/common/Patch.kt
Normal file
87
src/integration-test/kotlin/common/Patch.kt
Normal file
@@ -0,0 +1,87 @@
|
||||
package solutions.bitbadger.documents.common
|
||||
|
||||
import solutions.bitbadger.documents.*
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertNotNull
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
/**
|
||||
* Integration tests for the `Find` object
|
||||
*/
|
||||
object Patch {
|
||||
|
||||
fun byIdMatch(db: ThrowawayDatabase) {
|
||||
JsonDocument.load(db)
|
||||
db.conn.patchById(TEST_TABLE, "one", mapOf("numValue" to 44))
|
||||
val doc = db.conn.findById<String, JsonDocument>(TEST_TABLE, "one")
|
||||
assertNotNull(doc, "There should have been a document returned")
|
||||
assertEquals("one", doc.id, "An incorrect document was returned")
|
||||
assertEquals(44, doc.numValue, "The document was not patched")
|
||||
}
|
||||
|
||||
fun byIdNoMatch(db: ThrowawayDatabase) {
|
||||
JsonDocument.load(db)
|
||||
assertFalse("Document with ID \"forty-seven\" should not exist") {
|
||||
db.conn.existsById(TEST_TABLE, "forty-seven")
|
||||
}
|
||||
db.conn.patchById(TEST_TABLE, "forty-seven", mapOf("foo" to "green")) // no exception = pass
|
||||
}
|
||||
|
||||
fun byFieldsMatch(db: ThrowawayDatabase) {
|
||||
JsonDocument.load(db)
|
||||
db.conn.patchByFields(TEST_TABLE, listOf(Field.equal("value", "purple")), mapOf("numValue" to 77))
|
||||
assertEquals(
|
||||
2,
|
||||
db.conn.countByFields(TEST_TABLE, listOf(Field.equal("numValue", 77))),
|
||||
"There should have been 2 documents with numeric value 77"
|
||||
)
|
||||
}
|
||||
|
||||
fun byFieldsNoMatch(db: ThrowawayDatabase) {
|
||||
JsonDocument.load(db)
|
||||
val fields = listOf(Field.equal("value", "burgundy"))
|
||||
assertFalse("There should be no documents with value of \"burgundy\"") {
|
||||
db.conn.existsByFields(TEST_TABLE, fields)
|
||||
}
|
||||
db.conn.patchByFields(TEST_TABLE, fields, mapOf("foo" to "green")) // no exception = pass
|
||||
}
|
||||
|
||||
fun byContainsMatch(db: ThrowawayDatabase) {
|
||||
JsonDocument.load(db)
|
||||
val contains = mapOf("value" to "another")
|
||||
db.conn.patchByContains(TEST_TABLE, contains, mapOf("numValue" to 12))
|
||||
val doc = db.conn.findFirstByContains<JsonDocument, Map<String, String>>(TEST_TABLE, contains)
|
||||
assertNotNull(doc, "There should have been a document returned")
|
||||
assertEquals("two", doc.id, "The incorrect document was returned")
|
||||
assertEquals(12, doc.numValue, "The document was not updated")
|
||||
}
|
||||
|
||||
fun byContainsNoMatch(db: ThrowawayDatabase) {
|
||||
JsonDocument.load(db)
|
||||
val contains = mapOf("value" to "updated")
|
||||
assertFalse("There should be no matching documents") { db.conn.existsByContains(TEST_TABLE, contains) }
|
||||
db.conn.patchByContains(TEST_TABLE, contains, mapOf("sub.foo" to "green")) // no exception = pass
|
||||
}
|
||||
|
||||
fun byJsonPathMatch(db: ThrowawayDatabase) {
|
||||
JsonDocument.load(db)
|
||||
val path = "$.numValue ? (@ > 10)"
|
||||
db.conn.patchByJsonPath(TEST_TABLE, path, mapOf("value" to "blue"))
|
||||
val docs = db.conn.findByJsonPath<JsonDocument>(TEST_TABLE, path)
|
||||
assertEquals(2, docs.size, "There should have been two documents returned")
|
||||
docs.forEach {
|
||||
assertTrue(listOf("four", "five").contains(it.id), "An incorrect document was returned (${it.id})")
|
||||
assertEquals("blue", it.value, "The value for ID ${it.id} was incorrect")
|
||||
}
|
||||
}
|
||||
|
||||
fun byJsonPathNoMatch(db: ThrowawayDatabase) {
|
||||
JsonDocument.load(db)
|
||||
val path = "$.numValue ? (@ > 100)"
|
||||
assertFalse("There should be no documents with numeric values over 100") {
|
||||
db.conn.existsByJsonPath(TEST_TABLE, path)
|
||||
}
|
||||
db.conn.patchByJsonPath(TEST_TABLE, path, mapOf("value" to "blue")) // no exception = pass
|
||||
}
|
||||
}
|
||||
52
src/integration-test/kotlin/postgresql/PatchIT.kt
Normal file
52
src/integration-test/kotlin/postgresql/PatchIT.kt
Normal file
@@ -0,0 +1,52 @@
|
||||
package solutions.bitbadger.documents.postgresql
|
||||
|
||||
import org.junit.jupiter.api.DisplayName
|
||||
import solutions.bitbadger.documents.common.Patch
|
||||
import kotlin.test.Test
|
||||
|
||||
/**
|
||||
* PostgreSQL integration tests for the `Patch` object / `patchBy*` connection extension functions
|
||||
*/
|
||||
@DisplayName("PostgreSQL - Patch")
|
||||
class PatchIT {
|
||||
|
||||
@Test
|
||||
@DisplayName("byId patches an existing document")
|
||||
fun byIdMatch() =
|
||||
PgDB().use(Patch::byIdMatch)
|
||||
|
||||
@Test
|
||||
@DisplayName("byId succeeds for a non-existent document")
|
||||
fun byIdNoMatch() =
|
||||
PgDB().use(Patch::byIdNoMatch)
|
||||
|
||||
@Test
|
||||
@DisplayName("byFields patches matching document")
|
||||
fun byFieldsMatch() =
|
||||
PgDB().use(Patch::byFieldsMatch)
|
||||
|
||||
@Test
|
||||
@DisplayName("byFields succeeds when no documents match")
|
||||
fun byFieldsNoMatch() =
|
||||
PgDB().use(Patch::byFieldsNoMatch)
|
||||
|
||||
@Test
|
||||
@DisplayName("byContains patches matching document")
|
||||
fun byContainsMatch() =
|
||||
PgDB().use(Patch::byContainsMatch)
|
||||
|
||||
@Test
|
||||
@DisplayName("byContains succeeds when no documents match")
|
||||
fun byContainsNoMatch() =
|
||||
PgDB().use(Patch::byContainsNoMatch)
|
||||
|
||||
@Test
|
||||
@DisplayName("byJsonPath patches matching document")
|
||||
fun byJsonPathMatch() =
|
||||
PgDB().use(Patch::byJsonPathMatch)
|
||||
|
||||
@Test
|
||||
@DisplayName("byJsonPath succeeds when no documents match")
|
||||
fun byJsonPathNoMatch() =
|
||||
PgDB().use(Patch::byJsonPathNoMatch)
|
||||
}
|
||||
46
src/integration-test/kotlin/sqlite/PatchIT.kt
Normal file
46
src/integration-test/kotlin/sqlite/PatchIT.kt
Normal 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.Patch
|
||||
import kotlin.test.Test
|
||||
|
||||
/**
|
||||
* SQLite integration tests for the `Patch` object / `patchBy*` connection extension functions
|
||||
*/
|
||||
@DisplayName("SQLite - Patch")
|
||||
class PatchIT {
|
||||
|
||||
@Test
|
||||
@DisplayName("byId patches an existing document")
|
||||
fun byIdMatch() =
|
||||
SQLiteDB().use(Patch::byIdMatch)
|
||||
|
||||
@Test
|
||||
@DisplayName("byId succeeds for a non-existent document")
|
||||
fun byIdNoMatch() =
|
||||
SQLiteDB().use(Patch::byIdNoMatch)
|
||||
|
||||
@Test
|
||||
@DisplayName("byFields patches matching document")
|
||||
fun byFieldsMatch() =
|
||||
SQLiteDB().use(Patch::byFieldsMatch)
|
||||
|
||||
@Test
|
||||
@DisplayName("byFields succeeds when no documents match")
|
||||
fun byFieldsNoMatch() =
|
||||
SQLiteDB().use(Patch::byFieldsNoMatch)
|
||||
|
||||
@Test
|
||||
@DisplayName("byContains fails")
|
||||
fun byContainsFails() {
|
||||
assertThrows<DocumentException> { SQLiteDB().use(Patch::byContainsMatch) }
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("byJsonPath fails")
|
||||
fun byJsonPathFails() {
|
||||
assertThrows<DocumentException> { SQLiteDB().use(Patch::byJsonPathMatch) }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user