Initial Development #1

Merged
danieljsummers merged 88 commits from v1-rc into main 2025-04-16 01:29:20 +00:00
4 changed files with 529 additions and 200 deletions
Showing only changes of commit 4e6cfa35a7 - Show all commits

View File

@ -2,6 +2,8 @@ package solutions.bitbadger.documents.groovy.tests.integration
import solutions.bitbadger.documents.Configuration import solutions.bitbadger.documents.Configuration
import solutions.bitbadger.documents.Dialect import solutions.bitbadger.documents.Dialect
import solutions.bitbadger.documents.Field
import solutions.bitbadger.documents.FieldMatch
import static org.junit.jupiter.api.Assertions.* import static org.junit.jupiter.api.Assertions.*
import static solutions.bitbadger.documents.groovy.tests.Types.TEST_TABLE import static solutions.bitbadger.documents.groovy.tests.Types.TEST_TABLE
@ -43,8 +45,8 @@ final class JsonFunctions {
static void allDefault(ThrowawayDatabase db) { static void allDefault(ThrowawayDatabase db) {
JsonDocument.load(db) JsonDocument.load(db)
def json = db.conn.jsonAll(TEST_TABLE) String json = db.conn.jsonAll(TEST_TABLE)
assertTrue(json.startsWith("["), "JSON should start with '[' ($json)") assertTrue(json.startsWith('['), "JSON should start with '[' ($json)")
switch (Configuration.dialect()) { switch (Configuration.dialect()) {
case Dialect.SQLITE: case Dialect.SQLITE:
assertTrue(json.contains(JsonDocument.one), "Document 'one' not found in JSON ($json)") assertTrue(json.contains(JsonDocument.one), "Document 'one' not found in JSON ($json)")
@ -54,343 +56,349 @@ final class JsonFunctions {
assertTrue(json.contains(JsonDocument.five), "Document 'five' not found in JSON ($json)") assertTrue(json.contains(JsonDocument.five), "Document 'five' not found in JSON ($json)")
break break
case Dialect.POSTGRESQL: case Dialect.POSTGRESQL:
assertTrue(json.contains(docId("one")), "Document 'one' not found in JSON ($json)") assertTrue(json.contains(docId('one')), "Document 'one' not found in JSON ($json)")
assertTrue(json.contains(docId("two")), "Document 'two' not found in JSON ($json)") assertTrue(json.contains(docId('two')), "Document 'two' not found in JSON ($json)")
assertTrue(json.contains(docId("three")), "Document 'three' not found in JSON ($json)") assertTrue(json.contains(docId('three')), "Document 'three' not found in JSON ($json)")
assertTrue(json.contains(docId("four")), "Document 'four' not found in JSON ($json)") assertTrue(json.contains(docId('four')), "Document 'four' not found in JSON ($json)")
assertTrue(json.contains(docId("five")), "Document 'five' not found in JSON ($json)") assertTrue(json.contains(docId('five')), "Document 'five' not found in JSON ($json)")
break break
} }
assertTrue(json.endsWith("]"), "JSON should end with ']' ($json)") assertTrue(json.endsWith(']'), "JSON should end with ']' ($json)")
} }
static void allEmpty(ThrowawayDatabase db) = static void allEmpty(ThrowawayDatabase db) {
assertEquals("[]", db.conn.jsonAll(TEST_TABLE), "There should have been no documents returned") assertEquals('[]', db.conn.jsonAll(TEST_TABLE), 'There should have been no documents returned')
}
static void byIdString(ThrowawayDatabase db) { static void byIdString(ThrowawayDatabase db) {
JsonDocument.load(db) JsonDocument.load(db)
val json = db.conn.jsonById(TEST_TABLE, "two") String json = db.conn.jsonById(TEST_TABLE, 'two')
when (Configuration.dialect()) { switch (Configuration.dialect()) {
Dialect.SQLITE -> assertEquals(JsonDocument.two, json, "An incorrect document was returned") case Dialect.SQLITE:
Dialect.POSTGRESQL -> assertTrue(json.contains(docId("two")), "An incorrect document was returned ($json)") assertEquals(JsonDocument.two, json, 'An incorrect document was returned')
break
case Dialect.POSTGRESQL:
assertTrue(json.contains(docId('two')), "An incorrect document was returned ($json)")
break
} }
} }
static void byIdNumber(ThrowawayDatabase db) { static void byIdNumber(ThrowawayDatabase db) {
Configuration.idField = "key" Configuration.idField = 'key'
try { try {
db.conn.insert(TEST_TABLE, NumIdDocument(18, "howdy")) db.conn.insert(TEST_TABLE, new NumIdDocument(18, 'howdy'))
assertEquals( assertEquals(maybeJsonB('{"key":18,"text":"howdy"}'), db.conn.jsonById(TEST_TABLE, 18),
maybeJsonB("{\"key\":18,\"text\":\"howdy\"}"), db.conn.jsonById(TEST_TABLE, 18), 'The document should have been found by numeric ID')
"The document should have been found by numeric ID"
)
} finally { } finally {
Configuration.idField = "id" Configuration.idField = 'id'
} }
} }
static void byIdNotFound(ThrowawayDatabase db) { static void byIdNotFound(ThrowawayDatabase db) {
JsonDocument.load(db) JsonDocument.load(db)
assertEquals("{}", db.conn.jsonById(TEST_TABLE, "x"), "There should have been no document returned") assertEquals('{}', db.conn.jsonById(TEST_TABLE, 'x'), 'There should have been no document returned')
} }
static void byFieldsMatch(ThrowawayDatabase db) { static void byFieldsMatch(ThrowawayDatabase db) {
JsonDocument.load(db) JsonDocument.load(db)
val json = db.conn.jsonByFields( String json = db.conn.jsonByFields(TEST_TABLE, List.of(Field.any('value', List.of('blue', 'purple')),
TEST_TABLE, listOf(Field.any("value", listOf("blue", "purple")), Field.exists("sub")), FieldMatch.ALL Field.exists('sub')), FieldMatch.ALL)
) switch (Configuration.dialect()) {
when (Configuration.dialect()) { case Dialect.SQLITE:
Dialect.SQLITE -> assertEquals("[${JsonDocument.four}]", json, "The incorrect document was returned") assertEquals("[${JsonDocument.four}]".toString(), json, 'The incorrect document was returned')
Dialect.POSTGRESQL -> { break
assertTrue(json.startsWith("["), "JSON should start with '[' ($json)") case Dialect.POSTGRESQL:
assertTrue(json.contains(docId("four")),"The incorrect document was returned ($json)") assertTrue(json.startsWith('['), "JSON should start with '[' ($json)")
assertTrue(json.endsWith("]"), "JSON should end with ']' ($json)") assertTrue(json.contains(docId('four')), "The incorrect document was returned ($json)")
} assertTrue(json.endsWith(']'), "JSON should end with ']' ($json)")
break
} }
} }
static void byFieldsMatchOrdered(ThrowawayDatabase db) { static void byFieldsMatchOrdered(ThrowawayDatabase db) {
JsonDocument.load(db) JsonDocument.load(db)
val json = db.conn.jsonByFields( String json = db.conn.jsonByFields(TEST_TABLE, List.of(Field.equal('value', 'purple')), null,
TEST_TABLE, listOf(Field.equal("value", "purple")), orderBy = listOf(Field.named("id")) List.of(Field.named('id')))
) switch (Configuration.dialect()) {
when (Configuration.dialect()) { case Dialect.SQLITE:
Dialect.SQLITE -> assertEquals( assertEquals("[${JsonDocument.five},${JsonDocument.four}]".toString(), json,
"[${JsonDocument.five},${JsonDocument.four}]", json, "The documents were not ordered correctly" 'The documents were not ordered correctly')
) break
case Dialect.POSTGRESQL:
Dialect.POSTGRESQL -> { int fiveIdx = json.indexOf(docId('five'))
val fiveIdx = json.indexOf(docId("five")) int fourIdx = json.indexOf(docId('four'))
val fourIdx = json.indexOf(docId("four")) assertTrue(json.startsWith('['), "JSON should start with '[' ($json)")
assertTrue(json.startsWith("["), "JSON should start with '[' ($json)")
assertTrue(fiveIdx >= 0, "Document 'five' not found ($json)") assertTrue(fiveIdx >= 0, "Document 'five' not found ($json)")
assertTrue(fourIdx >= 0, "Document 'four' not found ($json)") assertTrue(fourIdx >= 0, "Document 'four' not found ($json)")
assertTrue(fiveIdx < fourIdx, "Document 'five' should have been before 'four' ($json)") assertTrue(fiveIdx < fourIdx, "Document 'five' should have been before 'four' ($json)")
assertTrue(json.endsWith("]"), "JSON should end with ']' ($json)") assertTrue(json.endsWith(']'), "JSON should end with ']' ($json)")
} break
} }
} }
static void byFieldsMatchNumIn(ThrowawayDatabase db) { static void byFieldsMatchNumIn(ThrowawayDatabase db) {
JsonDocument.load(db) JsonDocument.load(db)
val json = db.conn.jsonByFields(TEST_TABLE, listOf(Field.any("numValue", listOf(2, 4, 6, 8)))) String json = db.conn.jsonByFields(TEST_TABLE, List.of(Field.any('numValue', List.of(2, 4, 6, 8))))
when (Configuration.dialect()) { switch (Configuration.dialect()) {
Dialect.SQLITE -> assertEquals("[${JsonDocument.three}]", json, "The incorrect document was returned") case Dialect.SQLITE:
Dialect.POSTGRESQL -> { assertEquals("[${JsonDocument.three}]".toString(), json, 'The incorrect document was returned')
assertTrue(json.startsWith("["), "JSON should start with '[' ($json)") break
assertTrue(json.contains(docId("three")), "The incorrect document was returned ($json)") case Dialect.POSTGRESQL:
assertTrue(json.endsWith("]"), "JSON should end with ']' ($json)") assertTrue(json.startsWith('['), "JSON should start with '[' ($json)")
} assertTrue(json.contains(docId('three')), "The incorrect document was returned ($json)")
assertTrue(json.endsWith(']'), "JSON should end with ']' ($json)")
break
} }
} }
static void byFieldsNoMatch(ThrowawayDatabase db) { static void byFieldsNoMatch(ThrowawayDatabase db) {
JsonDocument.load(db) JsonDocument.load(db)
assertEquals( assertEquals('[]', db.conn.jsonByFields(TEST_TABLE, List.of(Field.greater('numValue', 100))),
"[]", db.conn.jsonByFields(TEST_TABLE, listOf(Field.greater("numValue", 100))), 'There should have been no documents returned')
"There should have been no documents returned"
)
} }
static void byFieldsMatchInArray(ThrowawayDatabase db) { static void byFieldsMatchInArray(ThrowawayDatabase db) {
ArrayDocument.testDocuments.forEach { db.conn.insert(TEST_TABLE, it) } ArrayDocument.testDocuments.forEach { db.conn.insert(TEST_TABLE, it) }
val json = db.conn.jsonByFields(TEST_TABLE, listOf(Field.inArray("values", TEST_TABLE, listOf("c")))) String json = db.conn.jsonByFields(TEST_TABLE, List.of(Field.inArray('values', TEST_TABLE, List.of('c'))))
assertTrue(json.startsWith("["), "JSON should start with '[' ($json)") assertTrue(json.startsWith('['), "JSON should start with '[' ($json)")
assertTrue(json.contains(docId("first")), "The 'first' document was not found ($json)") assertTrue(json.contains(docId('first')), "The 'first' document was not found ($json)")
assertTrue(json.contains(docId("second")), "The 'second' document was not found ($json)") assertTrue(json.contains(docId('second')), "The 'second' document was not found ($json)")
assertTrue(json.endsWith("]"), "JSON should end with ']' ($json)") assertTrue(json.endsWith(']'), "JSON should end with ']' ($json)")
} }
static void byFieldsNoMatchInArray(ThrowawayDatabase db) { static void byFieldsNoMatchInArray(ThrowawayDatabase db) {
ArrayDocument.testDocuments.forEach { db.conn.insert(TEST_TABLE, it) } ArrayDocument.testDocuments.forEach { db.conn.insert(TEST_TABLE, it) }
assertEquals( assertEquals('[]', db.conn.jsonByFields(TEST_TABLE, List.of(Field.inArray('values', TEST_TABLE, List.of('j')))),
"[]", db.conn.jsonByFields( 'There should have been no documents returned')
TEST_TABLE, listOf(Field.inArray("values", TEST_TABLE, listOf("j")))
), "There should have been no documents returned"
)
} }
static void byContainsMatch(ThrowawayDatabase db) { static void byContainsMatch(ThrowawayDatabase db) {
JsonDocument.load(db) JsonDocument.load(db)
val json = db.conn.jsonByContains(TEST_TABLE, mapOf("value" to "purple")) String json = db.conn.jsonByContains(TEST_TABLE, Map.of('value', 'purple'))
assertTrue(json.startsWith("["), "JSON should start with '[' ($json)") assertTrue(json.startsWith('['), "JSON should start with '[' ($json)")
when (Configuration.dialect()) { switch (Configuration.dialect()) {
Dialect.SQLITE -> { case Dialect.SQLITE:
assertTrue(json.contains(JsonDocument.four), "Document 'four' not found ($json)") assertTrue(json.contains(JsonDocument.four), "Document 'four' not found ($json)")
assertTrue(json.contains(JsonDocument.five), "Document 'five' not found ($json)") assertTrue(json.contains(JsonDocument.five), "Document 'five' not found ($json)")
} break
Dialect.POSTGRESQL -> { case Dialect.POSTGRESQL:
assertTrue(json.contains(docId("four")), "Document 'four' not found ($json)") assertTrue(json.contains(docId('four')), "Document 'four' not found ($json)")
assertTrue(json.contains(docId("five")), "Document 'five' not found ($json)") assertTrue(json.contains(docId('five')), "Document 'five' not found ($json)")
} break
} }
assertTrue(json.endsWith("]"), "JSON should end with ']' ($json)") assertTrue(json.endsWith(']'), "JSON should end with ']' ($json)")
} }
static void byContainsMatchOrdered(ThrowawayDatabase db) { static void byContainsMatchOrdered(ThrowawayDatabase db) {
JsonDocument.load(db) JsonDocument.load(db)
val json = db.conn.jsonByContains( String json = db.conn.jsonByContains(TEST_TABLE, Map.of('sub', Map.of('foo', 'green')),
TEST_TABLE, mapOf("sub" to mapOf("foo" to "green")), listOf(Field.named("value")) List.of(Field.named('value')))
) switch (Configuration.dialect()) {
when (Configuration.dialect()) { case Dialect.SQLITE:
Dialect.SQLITE -> assertEquals( assertEquals("[${JsonDocument.two},${JsonDocument.four}]", json,
"[${JsonDocument.two},${JsonDocument.four}]", json, "The documents were not ordered correctly" 'The documents were not ordered correctly')
) break
Dialect.POSTGRESQL -> { case Dialect.POSTGRESQL:
val twoIdx = json.indexOf(docId("two")) int twoIdx = json.indexOf(docId('two'))
val fourIdx = json.indexOf(docId("four")) int fourIdx = json.indexOf(docId('four'))
assertTrue(json.startsWith("["), "JSON should start with '[' ($json)") assertTrue(json.startsWith('['), "JSON should start with '[' ($json)")
assertTrue(twoIdx >= 0, "Document 'two' not found ($json)") assertTrue(twoIdx >= 0, "Document 'two' not found ($json)")
assertTrue(fourIdx >= 0, "Document 'four' not found ($json)") assertTrue(fourIdx >= 0, "Document 'four' not found ($json)")
assertTrue(twoIdx < fourIdx, "Document 'two' should have been before 'four' ($json)") assertTrue(twoIdx < fourIdx, "Document 'two' should have been before 'four' ($json)")
assertTrue(json.endsWith("]"), "JSON should end with ']' ($json)") assertTrue(json.endsWith(']'), "JSON should end with ']' ($json)")
} break
} }
} }
static void byContainsNoMatch(ThrowawayDatabase db) { static void byContainsNoMatch(ThrowawayDatabase db) {
JsonDocument.load(db) JsonDocument.load(db)
assertEquals( assertEquals('[]', db.conn.jsonByContains(TEST_TABLE, Map.of('value', 'indigo')),
"[]", 'There should have been no documents returned')
db.conn.jsonByContains(TEST_TABLE, mapOf("value" to "indigo")),
"There should have been no documents returned"
)
} }
static void byJsonPathMatch(ThrowawayDatabase db) { static void byJsonPathMatch(ThrowawayDatabase db) {
JsonDocument.load(db) JsonDocument.load(db)
val json = db.conn.jsonByJsonPath(TEST_TABLE, "$.numValue ? (@ > 10)") String json = db.conn.jsonByJsonPath(TEST_TABLE, '$.numValue ? (@ > 10)')
assertTrue(json.startsWith("["), "JSON should start with '[' ($json)") assertTrue(json.startsWith('['), "JSON should start with '[' ($json)")
when (Configuration.dialect()) { switch (Configuration.dialect()) {
Dialect.SQLITE -> { case Dialect.SQLITE:
assertTrue(json.contains(JsonDocument.four), "Document 'four' not found ($json)") assertTrue(json.contains(JsonDocument.four), "Document 'four' not found ($json)")
assertTrue(json.contains(JsonDocument.five), "Document 'five' not found ($json)") assertTrue(json.contains(JsonDocument.five), "Document 'five' not found ($json)")
} break
Dialect.POSTGRESQL -> { case Dialect.POSTGRESQL:
assertTrue(json.contains(docId("four")), "Document 'four' not found ($json)") assertTrue(json.contains(docId('four')), "Document 'four' not found ($json)")
assertTrue(json.contains(docId("five")), "Document 'five' not found ($json)") assertTrue(json.contains(docId('five')), "Document 'five' not found ($json)")
} break
} }
assertTrue(json.endsWith("]"), "JSON should end with ']' ($json)") assertTrue(json.endsWith(']'), "JSON should end with ']' ($json)")
} }
static void byJsonPathMatchOrdered(ThrowawayDatabase db) { static void byJsonPathMatchOrdered(ThrowawayDatabase db) {
JsonDocument.load(db) JsonDocument.load(db)
val json = db.conn.jsonByJsonPath(TEST_TABLE, "$.numValue ? (@ > 10)", listOf(Field.named("id"))) String json = db.conn.jsonByJsonPath(TEST_TABLE, '$.numValue ? (@ > 10)', List.of(Field.named('id')))
when (Configuration.dialect()) { switch (Configuration.dialect()) {
Dialect.SQLITE -> assertEquals( case Dialect.SQLITE:
"[${JsonDocument.five},${JsonDocument.four}]", json, "The documents were not ordered correctly" assertEquals("[${JsonDocument.five},${JsonDocument.four}]", json,
) 'The documents were not ordered correctly')
break
Dialect.POSTGRESQL -> { case Dialect.POSTGRESQL:
val fiveIdx = json.indexOf(docId("five")) int fiveIdx = json.indexOf(docId('five'))
val fourIdx = json.indexOf(docId("four")) int fourIdx = json.indexOf(docId('four'))
assertTrue(json.startsWith("["), "JSON should start with '[' ($json)") assertTrue(json.startsWith('['), "JSON should start with '[' ($json)")
assertTrue(fiveIdx >= 0, "Document 'five' not found ($json)") assertTrue(fiveIdx >= 0, "Document 'five' not found ($json)")
assertTrue(fourIdx >= 0, "Document 'four' not found ($json)") assertTrue(fourIdx >= 0, "Document 'four' not found ($json)")
assertTrue(fiveIdx < fourIdx, "Document 'five' should have been before 'four' ($json)") assertTrue(fiveIdx < fourIdx, "Document 'five' should have been before 'four' ($json)")
assertTrue(json.endsWith("]"), "JSON should end with ']' ($json)") assertTrue(json.endsWith(']'), "JSON should end with ']' ($json)")
} break
} }
} }
static void byJsonPathNoMatch(ThrowawayDatabase db) { static void byJsonPathNoMatch(ThrowawayDatabase db) {
JsonDocument.load(db) JsonDocument.load(db)
assertEquals( assertEquals('[]', db.conn.jsonByJsonPath(TEST_TABLE, '$.numValue ? (@ > 100)'),
"[]", 'There should have been no documents returned')
db.conn.jsonByJsonPath(TEST_TABLE, "$.numValue ? (@ > 100)"),
"There should have been no documents returned"
)
} }
static void firstByFieldsMatchOne(ThrowawayDatabase db) { static void firstByFieldsMatchOne(ThrowawayDatabase db) {
JsonDocument.load(db) JsonDocument.load(db)
val json = db.conn.jsonFirstByFields(TEST_TABLE, listOf(Field.equal("value", "another"))) String json = db.conn.jsonFirstByFields(TEST_TABLE, List.of(Field.equal('value', 'another')))
when (Configuration.dialect()) { switch (Configuration.dialect()) {
Dialect.SQLITE -> assertEquals(JsonDocument.two, json, "The incorrect document was returned") case Dialect.SQLITE:
Dialect.POSTGRESQL -> assertTrue(json.contains(docId("two")), "The incorrect document was returned ($json)") assertEquals(JsonDocument.two, json, 'The incorrect document was returned')
break
case Dialect.POSTGRESQL:
assertTrue(json.contains(docId('two')), "The incorrect document was returned ($json)")
break
} }
} }
static void firstByFieldsMatchMany(ThrowawayDatabase db) { static void firstByFieldsMatchMany(ThrowawayDatabase db) {
JsonDocument.load(db) JsonDocument.load(db)
val json = db.conn.jsonFirstByFields(TEST_TABLE, listOf(Field.equal("sub.foo", "green"))) String json = db.conn.jsonFirstByFields(TEST_TABLE, List.of(Field.equal('sub.foo', 'green')))
when (Configuration.dialect()) { switch (Configuration.dialect()) {
Dialect.SQLITE -> assertTrue( case Dialect.SQLITE:
json.contains(JsonDocument.two) || json.contains(JsonDocument.four), assertTrue(json.contains(JsonDocument.two) || json.contains(JsonDocument.four),
"Expected document 'two' or 'four' ($json)" "Expected document 'two' or 'four' ($json)")
) break
Dialect.POSTGRESQL -> assertTrue( case Dialect.POSTGRESQL:
json.contains(docId("two")) || json.contains(docId("four")), assertTrue(json.contains(docId('two')) || json.contains(docId('four')),
"Expected document 'two' or 'four' ($json)" "Expected document 'two' or 'four' ($json)")
) break
} }
} }
static void firstByFieldsMatchOrdered(ThrowawayDatabase db) { static void firstByFieldsMatchOrdered(ThrowawayDatabase db) {
JsonDocument.load(db) JsonDocument.load(db)
val json = db.conn.jsonFirstByFields( String json = db.conn.jsonFirstByFields(TEST_TABLE, List.of(Field.equal('sub.foo', 'green')), null,
TEST_TABLE, listOf(Field.equal("sub.foo", "green")), orderBy = listOf(Field.named("n:numValue DESC")) List.of(Field.named('n:numValue DESC')))
) switch (Configuration.dialect()) {
when (Configuration.dialect()) { case Dialect.SQLITE:
Dialect.SQLITE -> assertEquals(JsonDocument.four, json, "An incorrect document was returned") assertEquals(JsonDocument.four, json, 'An incorrect document was returned')
Dialect.POSTGRESQL -> assertTrue(json.contains(docId("four")), "An incorrect document was returned ($json)") break
case Dialect.POSTGRESQL:
assertTrue(json.contains(docId('four')), "An incorrect document was returned ($json)")
break
} }
} }
static void firstByFieldsNoMatch(ThrowawayDatabase db) { static void firstByFieldsNoMatch(ThrowawayDatabase db) {
JsonDocument.load(db) JsonDocument.load(db)
assertEquals( assertEquals('{}', db.conn.jsonFirstByFields(TEST_TABLE, List.of(Field.equal('value', 'absent'))),
"{}", 'There should have been no document returned')
db.conn.jsonFirstByFields(TEST_TABLE, listOf(Field.equal("value", "absent"))),
"There should have been no document returned"
)
} }
static void firstByContainsMatchOne(ThrowawayDatabase db) { static void firstByContainsMatchOne(ThrowawayDatabase db) {
JsonDocument.load(db) JsonDocument.load(db)
val json = db.conn.jsonFirstByContains(TEST_TABLE, mapOf("value" to "FIRST!")) String json = db.conn.jsonFirstByContains(TEST_TABLE, Map.of('value', 'FIRST!'))
when (Configuration.dialect()) { switch (Configuration.dialect()) {
Dialect.SQLITE -> assertEquals(JsonDocument.one, json, "An incorrect document was returned") case Dialect.SQLITE:
Dialect.POSTGRESQL -> assertTrue(json.contains(docId("one")), "An incorrect document was returned ($json)") assertEquals(JsonDocument.one, json, 'An incorrect document was returned')
break
case Dialect.POSTGRESQL:
assertTrue(json.contains(docId('one')), "An incorrect document was returned ($json)")
break
} }
} }
static void firstByContainsMatchMany(ThrowawayDatabase db) { static void firstByContainsMatchMany(ThrowawayDatabase db) {
JsonDocument.load(db) JsonDocument.load(db)
val json = db.conn.jsonFirstByContains(TEST_TABLE, mapOf("value" to "purple")) String json = db.conn.jsonFirstByContains(TEST_TABLE, Map.of('value', 'purple'))
when (Configuration.dialect()) { switch (Configuration.dialect()) {
Dialect.SQLITE -> assertTrue( case Dialect.SQLITE:
json.contains(JsonDocument.four) || json.contains(JsonDocument.five), assertTrue(json.contains(JsonDocument.four) || json.contains(JsonDocument.five),
"Expected document 'four' or 'five' ($json)" "Expected document 'four' or 'five' ($json)")
) break
Dialect.POSTGRESQL -> assertTrue( case Dialect.POSTGRESQL:
json.contains(docId("four")) || json.contains(docId("five")), assertTrue( json.contains(docId('four')) || json.contains(docId('five')),
"Expected document 'four' or 'five' ($json)" "Expected document 'four' or 'five' ($json)")
) break
} }
} }
static void firstByContainsMatchOrdered(ThrowawayDatabase db) { static void firstByContainsMatchOrdered(ThrowawayDatabase db) {
JsonDocument.load(db) JsonDocument.load(db)
val json = db.conn.jsonFirstByContains( String json = db.conn.jsonFirstByContains(TEST_TABLE, Map.of('value', 'purple'),
TEST_TABLE, mapOf("value" to "purple"), listOf(Field.named("sub.bar NULLS FIRST")) List.of(Field.named('sub.bar NULLS FIRST')))
) switch (Configuration.dialect()) {
when (Configuration.dialect()) { case Dialect.SQLITE:
Dialect.SQLITE -> assertEquals(JsonDocument.five, json, "An incorrect document was returned") assertEquals(JsonDocument.five, json, 'An incorrect document was returned')
Dialect.POSTGRESQL -> assertTrue(json.contains(docId("five")), "An incorrect document was returned ($json)") break
case Dialect.POSTGRESQL:
assertTrue(json.contains(docId('five')), "An incorrect document was returned ($json)")
break
} }
} }
static void firstByContainsNoMatch(ThrowawayDatabase db) { static void firstByContainsNoMatch(ThrowawayDatabase db) {
JsonDocument.load(db) JsonDocument.load(db)
assertEquals( assertEquals('{}', db.conn.jsonFirstByContains(TEST_TABLE, Map.of('value', 'indigo')),
"{}", 'There should have been no document returned')
db.conn.jsonFirstByContains(TEST_TABLE, mapOf("value" to "indigo")),
"There should have been no document returned"
)
} }
static void firstByJsonPathMatchOne(ThrowawayDatabase db) { static void firstByJsonPathMatchOne(ThrowawayDatabase db) {
JsonDocument.load(db) JsonDocument.load(db)
val json = db.conn.jsonFirstByJsonPath(TEST_TABLE, "$.numValue ? (@ == 10)") String json = db.conn.jsonFirstByJsonPath(TEST_TABLE, '$.numValue ? (@ == 10)')
when (Configuration.dialect()) { switch (Configuration.dialect()) {
Dialect.SQLITE -> assertEquals(JsonDocument.two, json, "An incorrect document was returned") case Dialect.SQLITE:
Dialect.POSTGRESQL -> assertTrue(json.contains(docId("two")), "An incorrect document was returned ($json)") assertEquals(JsonDocument.two, json, 'An incorrect document was returned')
break
case Dialect.POSTGRESQL:
assertTrue(json.contains(docId('two')), "An incorrect document was returned ($json)")
break
} }
} }
static void firstByJsonPathMatchMany(ThrowawayDatabase db) { static void firstByJsonPathMatchMany(ThrowawayDatabase db) {
JsonDocument.load(db) JsonDocument.load(db)
val json = db.conn.jsonFirstByJsonPath(TEST_TABLE, "$.numValue ? (@ > 10)") String json = db.conn.jsonFirstByJsonPath(TEST_TABLE, '$.numValue ? (@ > 10)')
when (Configuration.dialect()) { switch (Configuration.dialect()) {
Dialect.SQLITE -> assertTrue( case Dialect.SQLITE:
json.contains(JsonDocument.four) || json.contains(JsonDocument.five), assertTrue(json.contains(JsonDocument.four) || json.contains(JsonDocument.five),
"Expected document 'four' or 'five' ($json)" "Expected document 'four' or 'five' ($json)")
) break
Dialect.POSTGRESQL -> assertTrue( case Dialect.POSTGRESQL:
json.contains(docId("four")) || json.contains(docId("five")), assertTrue(json.contains(docId('four')) || json.contains(docId('five')),
"Expected document 'four' or 'five' ($json)" "Expected document 'four' or 'five' ($json)")
) break
} }
} }
static void firstByJsonPathMatchOrdered(ThrowawayDatabase db) { static void firstByJsonPathMatchOrdered(ThrowawayDatabase db) {
JsonDocument.load(db) JsonDocument.load(db)
val json = db.conn.jsonFirstByJsonPath(TEST_TABLE, "$.numValue ? (@ > 10)", listOf(Field.named("id DESC"))) String json = db.conn.jsonFirstByJsonPath(TEST_TABLE, '$.numValue ? (@ > 10)', List.of(Field.named('id DESC')))
when (Configuration.dialect()) { switch (Configuration.dialect()) {
Dialect.SQLITE -> assertEquals(JsonDocument.four, json, "An incorrect document was returned") case Dialect.SQLITE:
Dialect.POSTGRESQL -> assertTrue(json.contains(docId("four")), "An incorrect document was returned ($json)") assertEquals(JsonDocument.four, json, 'An incorrect document was returned')
break
case Dialect.POSTGRESQL:
assertTrue(json.contains(docId('four')), "An incorrect document was returned ($json)")
break
} }
} }
static void firstByJsonPathNoMatch(ThrowawayDatabase db) { static void firstByJsonPathNoMatch(ThrowawayDatabase db) {
JsonDocument.load(db) JsonDocument.load(db)
assertEquals( assertEquals('{}', db.conn.jsonFirstByJsonPath(TEST_TABLE, '$.numValue ? (@ > 100)'),
"{}", 'There should have been no document returned')
db.conn.jsonFirstByJsonPath(TEST_TABLE, "$.numValue ? (@ > 100)"),
"There should have been no document returned"
)
} }
} }

View File

@ -2,10 +2,10 @@ package solutions.bitbadger.documents.groovy.tests.integration
class NumIdDocument { class NumIdDocument {
int key int key
String value String text
NumIdDocument(int key = 0, String value = "") { NumIdDocument(int key = 0, String text = "") {
this.key = key this.key = key
this.value = value this.text = text
} }
} }

View File

@ -0,0 +1,185 @@
package solutions.bitbadger.documents.groovy.tests.integration
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
/**
* PostgreSQL integration tests for the `Json` object / `json*` connection extension functions
*/
@DisplayName('Groovy | PostgreSQL: Json')
final class PostgreSQLJsonIT {
@Test
@DisplayName('all retrieves all documents')
void allDefault() {
new PgDB().withCloseable JsonFunctions.&allDefault
}
@Test
@DisplayName('all succeeds with an empty table')
void allEmpty() {
new PgDB().withCloseable JsonFunctions.&allEmpty
}
@Test
@DisplayName('byId retrieves a document via a string ID')
void byIdString() {
new PgDB().withCloseable JsonFunctions.&byIdString
}
@Test
@DisplayName('byId retrieves a document via a numeric ID')
void byIdNumber() {
new PgDB().withCloseable JsonFunctions.&byIdNumber
}
@Test
@DisplayName('byId returns null when a matching ID is not found')
void byIdNotFound() {
new PgDB().withCloseable JsonFunctions.&byIdNotFound
}
@Test
@DisplayName('byFields retrieves matching documents')
void byFieldsMatch() {
new PgDB().withCloseable JsonFunctions.&byFieldsMatch
}
@Test
@DisplayName('byFields retrieves ordered matching documents')
void byFieldsMatchOrdered() {
new PgDB().withCloseable JsonFunctions.&byFieldsMatchOrdered
}
@Test
@DisplayName('byFields retrieves matching documents with a numeric IN clause')
void byFieldsMatchNumIn() {
new PgDB().withCloseable JsonFunctions.&byFieldsMatchNumIn
}
@Test
@DisplayName('byFields succeeds when no documents match')
void byFieldsNoMatch() {
new PgDB().withCloseable JsonFunctions.&byFieldsNoMatch
}
@Test
@DisplayName('byFields retrieves matching documents with an IN_ARRAY comparison')
void byFieldsMatchInArray() {
new PgDB().withCloseable JsonFunctions.&byFieldsMatchInArray
}
@Test
@DisplayName('byFields succeeds when no documents match an IN_ARRAY comparison')
void byFieldsNoMatchInArray() {
new PgDB().withCloseable JsonFunctions.&byFieldsNoMatchInArray
}
@Test
@DisplayName('byContains retrieves matching documents')
void byContainsMatch() {
new PgDB().withCloseable JsonFunctions.&byContainsMatch
}
@Test
@DisplayName('byContains retrieves ordered matching documents')
void byContainsMatchOrdered() {
new PgDB().withCloseable JsonFunctions.&byContainsMatchOrdered
}
@Test
@DisplayName('byContains succeeds when no documents match')
void byContainsNoMatch() {
new PgDB().withCloseable JsonFunctions.&byContainsNoMatch
}
@Test
@DisplayName('byJsonPath retrieves matching documents')
void byJsonPathMatch() {
new PgDB().withCloseable JsonFunctions.&byJsonPathMatch
}
@Test
@DisplayName('byJsonPath retrieves ordered matching documents')
void byJsonPathMatchOrdered() {
new PgDB().withCloseable JsonFunctions.&byJsonPathMatchOrdered
}
@Test
@DisplayName('byJsonPath succeeds when no documents match')
void byJsonPathNoMatch() {
new PgDB().withCloseable JsonFunctions.&byJsonPathNoMatch
}
@Test
@DisplayName('firstByFields retrieves a matching document')
void firstByFieldsMatchOne() {
new PgDB().withCloseable JsonFunctions.&firstByFieldsMatchOne
}
@Test
@DisplayName('firstByFields retrieves a matching document among many')
void firstByFieldsMatchMany() {
new PgDB().withCloseable JsonFunctions.&firstByFieldsMatchMany
}
@Test
@DisplayName('firstByFields retrieves a matching document among many (ordered)')
void firstByFieldsMatchOrdered() {
new PgDB().withCloseable JsonFunctions.&firstByFieldsMatchOrdered
}
@Test
@DisplayName('firstByFields returns null when no document matches')
void firstByFieldsNoMatch() {
new PgDB().withCloseable JsonFunctions.&firstByFieldsNoMatch
}
@Test
@DisplayName('firstByContains retrieves a matching document')
void firstByContainsMatchOne() {
new PgDB().withCloseable JsonFunctions.&firstByContainsMatchOne
}
@Test
@DisplayName('firstByContains retrieves a matching document among many')
void firstByContainsMatchMany() {
new PgDB().withCloseable JsonFunctions.&firstByContainsMatchMany
}
@Test
@DisplayName('firstByContains retrieves a matching document among many (ordered)')
void firstByContainsMatchOrdered() {
new PgDB().withCloseable JsonFunctions.&firstByContainsMatchOrdered
}
@Test
@DisplayName('firstByContains returns null when no document matches')
void firstByContainsNoMatch() {
new PgDB().withCloseable JsonFunctions.&firstByContainsNoMatch
}
@Test
@DisplayName('firstByJsonPath retrieves a matching document')
void firstByJsonPathMatchOne() {
new PgDB().withCloseable JsonFunctions.&firstByJsonPathMatchOne
}
@Test
@DisplayName('firstByJsonPath retrieves a matching document among many')
void firstByJsonPathMatchMany() {
new PgDB().withCloseable JsonFunctions.&firstByJsonPathMatchMany
}
@Test
@DisplayName('firstByJsonPath retrieves a matching document among many (ordered)')
void firstByJsonPathMatchOrdered() {
new PgDB().withCloseable JsonFunctions.&firstByJsonPathMatchOrdered
}
@Test
@DisplayName('firstByJsonPath returns null when no document matches')
void firstByJsonPathNoMatch() {
new PgDB().withCloseable JsonFunctions.&firstByJsonPathNoMatch
}
}

View File

@ -0,0 +1,136 @@
package solutions.bitbadger.documents.groovy.tests.integration
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
import solutions.bitbadger.documents.DocumentException
import static org.junit.jupiter.api.Assertions.assertThrows
/**
* SQLite integration tests for the `Json` object / `json*` connection extension functions
*/
@DisplayName('Groovy | SQLite: Json')
final class SQLiteJsonIT {
@Test
@DisplayName('all retrieves all documents')
void allDefault() {
new SQLiteDB().withCloseable JsonFunctions.&allDefault
}
@Test
@DisplayName('all succeeds with an empty table')
void allEmpty() {
new SQLiteDB().withCloseable JsonFunctions.&allEmpty
}
@Test
@DisplayName('byId retrieves a document via a string ID')
void byIdString() {
new SQLiteDB().withCloseable JsonFunctions.&byIdString
}
@Test
@DisplayName('byId retrieves a document via a numeric ID')
void byIdNumber() {
new SQLiteDB().withCloseable JsonFunctions.&byIdNumber
}
@Test
@DisplayName('byId returns null when a matching ID is not found')
void byIdNotFound() {
new SQLiteDB().withCloseable JsonFunctions.&byIdNotFound
}
@Test
@DisplayName('byFields retrieves matching documents')
void byFieldsMatch() {
new SQLiteDB().withCloseable JsonFunctions.&byFieldsMatch
}
@Test
@DisplayName('byFields retrieves ordered matching documents')
void byFieldsMatchOrdered() {
new SQLiteDB().withCloseable JsonFunctions.&byFieldsMatchOrdered
}
@Test
@DisplayName('byFields retrieves matching documents with a numeric IN clause')
void byFieldsMatchNumIn() {
new SQLiteDB().withCloseable JsonFunctions.&byFieldsMatchNumIn
}
@Test
@DisplayName('byFields succeeds when no documents match')
void byFieldsNoMatch() {
new SQLiteDB().withCloseable JsonFunctions.&byFieldsNoMatch
}
@Test
@DisplayName('byFields retrieves matching documents with an IN_ARRAY comparison')
void byFieldsMatchInArray() {
new SQLiteDB().withCloseable JsonFunctions.&byFieldsMatchInArray
}
@Test
@DisplayName('byFields succeeds when no documents match an IN_ARRAY comparison')
void byFieldsNoMatchInArray() {
new SQLiteDB().withCloseable JsonFunctions.&byFieldsNoMatchInArray
}
@Test
@DisplayName('byContains fails')
void byContainsFails() {
new SQLiteDB().withCloseable { db ->
assertThrows(DocumentException) { JsonFunctions.byContainsMatch db }
}
}
@Test
@DisplayName('byJsonPath fails')
void byJsonPathFails() {
new SQLiteDB().withCloseable { db ->
assertThrows(DocumentException) { JsonFunctions.byJsonPathMatch db }
}
}
@Test
@DisplayName('firstByFields retrieves a matching document')
void firstByFieldsMatchOne() {
new SQLiteDB().withCloseable JsonFunctions.&firstByFieldsMatchOne
}
@Test
@DisplayName('firstByFields retrieves a matching document among many')
void firstByFieldsMatchMany() {
new SQLiteDB().withCloseable JsonFunctions.&firstByFieldsMatchMany
}
@Test
@DisplayName('firstByFields retrieves a matching document among many (ordered)')
void firstByFieldsMatchOrdered() {
new SQLiteDB().withCloseable JsonFunctions.&firstByFieldsMatchOrdered
}
@Test
@DisplayName('firstByFields returns null when no document matches')
void firstByFieldsNoMatch() {
new SQLiteDB().withCloseable JsonFunctions.&firstByFieldsNoMatch
}
@Test
@DisplayName('firstByContains fails')
void firstByContainsFails() {
new SQLiteDB().withCloseable { db ->
assertThrows(DocumentException) { JsonFunctions.firstByContainsMatchOne db }
}
}
@Test
@DisplayName('firstByJsonPath fails')
void firstByJsonPathFails() {
new SQLiteDB().withCloseable { db ->
assertThrows(DocumentException) { JsonFunctions.firstByJsonPathMatchOne db }
}
}
}