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