From 18866b3ff7da2cdf4e8bcc47ce15350582a4b6b7 Mon Sep 17 00:00:00 2001 From: "Daniel J. Summers" Date: Mon, 31 Mar 2025 20:04:43 -0400 Subject: [PATCH] Add JSON writing to Groovy impl --- .../tests/integration/CustomFunctions.groovy | 38 +- .../tests/integration/JsonFunctions.groovy | 490 +++++++++++++++--- .../integration/PostgreSQLCustomIT.groovy | 18 + .../tests/integration/PostgreSQLJsonIT.groovy | 174 +++++++ .../tests/integration/SQLiteCustomIT.groovy | 36 ++ .../tests/integration/SQLiteJsonIT.groovy | 122 +++++ 6 files changed, 795 insertions(+), 83 deletions(-) diff --git a/src/groovy/src/test/groovy/solutions/bitbadger/documents/groovy/tests/integration/CustomFunctions.groovy b/src/groovy/src/test/groovy/solutions/bitbadger/documents/groovy/tests/integration/CustomFunctions.groovy index 36a5bc9..70732bb 100644 --- a/src/groovy/src/test/groovy/solutions/bitbadger/documents/groovy/tests/integration/CustomFunctions.groovy +++ b/src/groovy/src/test/groovy/solutions/bitbadger/documents/groovy/tests/integration/CustomFunctions.groovy @@ -30,14 +30,14 @@ final class CustomFunctions { static void jsonArrayEmpty(ThrowawayDatabase db) { assertEquals(0L, db.conn.countAll(TEST_TABLE), 'The test table should be empty') - assertEquals('[]', db.conn.customJsonArray(FindQuery.all(TEST_TABLE), List.of(), Results::jsonFromData), + assertEquals('[]', db.conn.customJsonArray(FindQuery.all(TEST_TABLE), List.of(), Results.&jsonFromData), 'An empty list was not represented correctly'); } static void jsonArraySingle(ThrowawayDatabase db) { db.conn.insert(TEST_TABLE, new ArrayDocument("one", List.of("2", "3"))) assertEquals(JsonFunctions.maybeJsonB('[{"id":"one","values":["2","3"]}]'), - db.conn.customJsonArray(FindQuery.all(TEST_TABLE), List.of(), Results::jsonFromData), + db.conn.customJsonArray(FindQuery.all(TEST_TABLE), List.of(), Results.&jsonFromData), 'A single document list was not represented correctly') } @@ -46,10 +46,38 @@ final class CustomFunctions { assertEquals(JsonFunctions.maybeJsonB('[{"id":"first","values":["a","b","c"]},' + '{"id":"second","values":["c","d","e"]},{"id":"third","values":["x","y","z"]}]'), db.conn.customJsonArray(FindQuery.all(TEST_TABLE) + QueryUtils.orderBy(List.of(Field.named("id"))), - List.of(), Results::jsonFromData), + List.of(), Results.&jsonFromData), 'A multiple document list was not represented correctly') } + static void writeJsonArrayEmpty(ThrowawayDatabase db) { + assertEquals(0L, db.conn.countAll(TEST_TABLE), 'The test table should be empty') + def output = new StringWriter() + def writer = new PrintWriter(output) + db.conn.writeCustomJsonArray(FindQuery.all(TEST_TABLE), List.of(), writer, Results.&jsonFromData) + assertEquals("[]", output.toString(), 'An empty list was not represented correctly') + } + + static void writeJsonArraySingle(ThrowawayDatabase db) { + db.conn.insert(TEST_TABLE, new ArrayDocument("one", List.of("2", "3"))) + def output = new StringWriter() + def writer = new PrintWriter(output) + db.conn.writeCustomJsonArray(FindQuery.all(TEST_TABLE), List.of(), writer, Results.&jsonFromData) + assertEquals(JsonFunctions.maybeJsonB('[{"id":"one","values":["2","3"]}]'), output.toString(), + 'A single document list was not represented correctly') + } + + static void writeJsonArrayMany(ThrowawayDatabase db) { + ArrayDocument.testDocuments.forEach { db.conn.insert(TEST_TABLE, it) } + def output = new StringWriter() + def writer = new PrintWriter(output) + db.conn.writeCustomJsonArray(FindQuery.all(TEST_TABLE) + QueryUtils.orderBy(List.of(Field.named("id"))), + List.of(), writer, Results.&jsonFromData) + assertEquals(JsonFunctions.maybeJsonB('[{"id":"first","values":["a","b","c"]},' + + '{"id":"second","values":["c","d","e"]},{"id":"third","values":["x","y","z"]}]'), + output.toString(), "A multiple document list was not represented correctly") + } + static void singleNone(ThrowawayDatabase db) { assertFalse(db.conn.customSingle(FindQuery.all(TEST_TABLE), List.of(), JsonDocument, Results.&fromData) .isPresent(), @@ -65,14 +93,14 @@ final class CustomFunctions { } static void jsonSingleNone(ThrowawayDatabase db) { - assertEquals('{}', db.conn.customJsonSingle(FindQuery.all(TEST_TABLE), List.of(), Results::jsonFromData), + assertEquals('{}', db.conn.customJsonSingle(FindQuery.all(TEST_TABLE), List.of(), Results.&jsonFromData), 'An empty document was not represented correctly') } static void jsonSingleOne(ThrowawayDatabase db) { db.conn.insert(TEST_TABLE, new ArrayDocument("me", List.of("myself", "i"))) assertEquals(JsonFunctions.maybeJsonB('{"id":"me","values":["myself","i"]}'), - db.conn.customJsonSingle(FindQuery.all(TEST_TABLE), List.of(), Results::jsonFromData), + db.conn.customJsonSingle(FindQuery.all(TEST_TABLE), List.of(), Results.&jsonFromData), 'A single document was not represented correctly'); } 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 56c8b03..186c480 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 @@ -43,9 +43,7 @@ final class JsonFunctions { return maybeJsonB("{\"id\":\"$id\"") } - static void allDefault(ThrowawayDatabase db) { - JsonDocument.load(db) - String json = db.conn.jsonAll(TEST_TABLE) + private static void checkAllDefault(String json) { assertTrue(json.startsWith('['), "JSON should start with '[' ($json)") switch (Configuration.dialect()) { case Dialect.SQLITE: @@ -66,13 +64,35 @@ final class JsonFunctions { 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 allDefault(ThrowawayDatabase db) { + JsonDocument.load(db) + checkAllDefault(db.conn.jsonAll(TEST_TABLE)) } - static void byIdString(ThrowawayDatabase db) { + static void writeAllDefault(ThrowawayDatabase db) { JsonDocument.load(db) - String json = db.conn.jsonById(TEST_TABLE, 'two') + def output = new StringWriter() + def writer = new PrintWriter(output) + db.conn.writeJsonAll(TEST_TABLE, writer) + checkAllDefault(output.toString()) + } + + private static void checkAllEmpty(String json) { + assertEquals('[]', json, 'There should have been no documents returned') + } + + static void allEmpty(ThrowawayDatabase db) { + checkAllEmpty(db.conn.jsonAll(TEST_TABLE)) + } + + static void writeAllEmpty(ThrowawayDatabase db) { + def output = new StringWriter() + def writer = new PrintWriter(output) + db.conn.writeJsonAll(TEST_TABLE, writer) + checkAllEmpty(output.toString()) + } + + private static void checkByIdString(String json) { switch (Configuration.dialect()) { case Dialect.SQLITE: assertEquals(JsonDocument.two, json, 'An incorrect document was returned') @@ -83,26 +103,64 @@ final class JsonFunctions { } } + static void byIdString(ThrowawayDatabase db) { + JsonDocument.load(db) + checkByIdString(db.conn.jsonById(TEST_TABLE, 'two')) + } + + static void writeByIdString(ThrowawayDatabase db) { + JsonDocument.load(db) + def output = new StringWriter() + def writer = new PrintWriter(output) + db.conn.writeJsonById(TEST_TABLE, writer, 'two') + checkByIdString(output.toString()) + } + + private static void checkByIdNumber(String json) { + assertEquals(maybeJsonB('{"key":18,"text":"howdy"}'), json, 'The document should have been found by numeric ID') + } + static void byIdNumber(ThrowawayDatabase db) { Configuration.idField = 'key' try { 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') + checkByIdNumber(db.conn.jsonById(TEST_TABLE, 18)) } finally { 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') + static void writeByIdNumber(ThrowawayDatabase db) { + Configuration.idField = 'key' + try { + db.conn.insert(TEST_TABLE, new NumIdDocument(18, 'howdy')) + def output = new StringWriter() + def writer = new PrintWriter(output) + db.conn.writeJsonById(TEST_TABLE, writer, 18) + checkByIdNumber(output.toString()) + } finally { + Configuration.idField = 'id' + } } - static void byFieldsMatch(ThrowawayDatabase db) { + private static void checkByIdNotFound(String json) { + assertEquals('{}', json, 'There should have been no document returned') + } + + static void byIdNotFound(ThrowawayDatabase db) { JsonDocument.load(db) - String json = db.conn.jsonByFields(TEST_TABLE, List.of(Field.any('value', List.of('blue', 'purple')), - Field.exists('sub')), FieldMatch.ALL) + checkByIdNotFound(db.conn.jsonById(TEST_TABLE, 'x')) + } + + static void writeByIdNotFound(ThrowawayDatabase db) { + JsonDocument.load(db) + def output = new StringWriter() + def writer = new PrintWriter(output) + db.conn.writeJsonById(TEST_TABLE, writer, 'x') + checkByIdNotFound(output.toString()) + } + + private static void checkByFieldsMatch(String json) { switch (Configuration.dialect()) { case Dialect.SQLITE: assertEquals("[${JsonDocument.four}]".toString(), json, 'The incorrect document was returned') @@ -115,10 +173,22 @@ final class JsonFunctions { } } - static void byFieldsMatchOrdered(ThrowawayDatabase db) { + static void byFieldsMatch(ThrowawayDatabase db) { JsonDocument.load(db) - String json = db.conn.jsonByFields(TEST_TABLE, List.of(Field.equal('value', 'purple')), null, - List.of(Field.named('id'))) + checkByFieldsMatch(db.conn.jsonByFields(TEST_TABLE, List.of(Field.any('value', List.of('blue', 'purple')), + Field.exists('sub')), FieldMatch.ALL)) + } + + static void writeByFieldsMatch(ThrowawayDatabase db) { + JsonDocument.load(db) + def output = new StringWriter() + def writer = new PrintWriter(output) + db.conn.writeJsonByFields(TEST_TABLE, writer, List.of(Field.any('value', List.of('blue', 'purple')), + Field.exists('sub')), FieldMatch.ALL) + checkByFieldsMatch(output.toString()) + } + + private static checkByFieldsMatchOrdered(String json) { switch (Configuration.dialect()) { case Dialect.SQLITE: assertEquals("[${JsonDocument.five},${JsonDocument.four}]".toString(), json, @@ -136,9 +206,22 @@ final class JsonFunctions { } } - static void byFieldsMatchNumIn(ThrowawayDatabase db) { + static void byFieldsMatchOrdered(ThrowawayDatabase db) { JsonDocument.load(db) - String json = db.conn.jsonByFields(TEST_TABLE, List.of(Field.any('numValue', List.of(2, 4, 6, 8)))) + checkByFieldsMatchOrdered(db.conn.jsonByFields(TEST_TABLE, List.of(Field.equal('value', 'purple')), null, + List.of(Field.named('id')))) + } + + static void writeByFieldsMatchOrdered(ThrowawayDatabase db) { + JsonDocument.load(db) + def output = new StringWriter() + def writer = new PrintWriter(output) + db.conn.writeJsonByFields(TEST_TABLE, writer, List.of(Field.equal('value', 'purple')), null, + List.of(Field.named('id'))) + checkByFieldsMatchOrdered(output.toString()) + } + + private static void checkByFieldsMatchNumIn(String json) { switch (Configuration.dialect()) { case Dialect.SQLITE: assertEquals("[${JsonDocument.three}]".toString(), json, 'The incorrect document was returned') @@ -151,30 +234,76 @@ final class JsonFunctions { } } - static void byFieldsNoMatch(ThrowawayDatabase db) { + static void byFieldsMatchNumIn(ThrowawayDatabase db) { JsonDocument.load(db) - assertEquals('[]', db.conn.jsonByFields(TEST_TABLE, List.of(Field.greater('numValue', 100))), - 'There should have been no documents returned') + checkByFieldsMatchNumIn(db.conn.jsonByFields(TEST_TABLE, List.of(Field.any('numValue', List.of(2, 4, 6, 8))))) } - static void byFieldsMatchInArray(ThrowawayDatabase db) { - ArrayDocument.testDocuments.forEach { db.conn.insert(TEST_TABLE, it) } - String json = db.conn.jsonByFields(TEST_TABLE, List.of(Field.inArray('values', TEST_TABLE, List.of('c')))) + static void writeByFieldsMatchNumIn(ThrowawayDatabase db) { + JsonDocument.load(db) + def output = new StringWriter() + def writer = new PrintWriter(output) + db.conn.writeJsonByFields(TEST_TABLE, writer, List.of(Field.any('numValue', List.of(2, 4, 6, 8)))) + checkByFieldsMatchNumIn(output.toString()) + } + + private static void checkByFieldsNoMatch(String json) { + assertEquals('[]', json, 'There should have been no documents returned') + } + + static void byFieldsNoMatch(ThrowawayDatabase db) { + JsonDocument.load(db) + checkByFieldsNoMatch(db.conn.jsonByFields(TEST_TABLE, List.of(Field.greater('numValue', 100)))) + } + + static void writeByFieldsNoMatch(ThrowawayDatabase db) { + JsonDocument.load(db) + def output = new StringWriter() + def writer = new PrintWriter(output) + db.conn.writeJsonByFields(TEST_TABLE, writer, List.of(Field.greater('numValue', 100))) + checkByFieldsNoMatch(output.toString()) + } + + private static void checkByFieldsMatchInArray(String 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('second')), "The 'second' document was not found ($json)") assertTrue(json.endsWith(']'), "JSON should end with ']' ($json)") } - static void byFieldsNoMatchInArray(ThrowawayDatabase db) { + static void byFieldsMatchInArray(ThrowawayDatabase db) { ArrayDocument.testDocuments.forEach { db.conn.insert(TEST_TABLE, it) } - assertEquals('[]', db.conn.jsonByFields(TEST_TABLE, List.of(Field.inArray('values', TEST_TABLE, List.of('j')))), - 'There should have been no documents returned') + checkByFieldsMatchInArray(db.conn.jsonByFields(TEST_TABLE, + List.of(Field.inArray('values', TEST_TABLE, List.of('c'))))) } - static void byContainsMatch(ThrowawayDatabase db) { - JsonDocument.load(db) - String json = db.conn.jsonByContains(TEST_TABLE, Map.of('value', 'purple')) + static void writeByFieldsMatchInArray(ThrowawayDatabase db) { + ArrayDocument.testDocuments.forEach { db.conn.insert(TEST_TABLE, it) } + def output = new StringWriter() + def writer = new PrintWriter(output) + db.conn.writeJsonByFields(TEST_TABLE, writer, List.of(Field.inArray('values', TEST_TABLE, List.of('c')))) + checkByFieldsMatchInArray(output.toString()) + } + + private static void checkByFieldsNoMatchInArray(String json) { + assertEquals('[]', json, 'There should have been no documents returned') + } + + static void byFieldsNoMatchInArray(ThrowawayDatabase db) { + ArrayDocument.testDocuments.forEach { db.conn.insert(TEST_TABLE, it) } + checkByFieldsNoMatchInArray(db.conn.jsonByFields(TEST_TABLE, + List.of(Field.inArray('values', TEST_TABLE, List.of('j'))))) + } + + static void writeByFieldsNoMatchInArray(ThrowawayDatabase db) { + ArrayDocument.testDocuments.forEach { db.conn.insert(TEST_TABLE, it) } + def output = new StringWriter() + def writer = new PrintWriter(output) + db.conn.writeJsonByFields(TEST_TABLE, writer, List.of(Field.inArray('values', TEST_TABLE, List.of('j')))) + checkByFieldsNoMatchInArray(output.toString()) + } + + private static void checkByContainsMatch(String json) { assertTrue(json.startsWith('['), "JSON should start with '[' ($json)") switch (Configuration.dialect()) { case Dialect.SQLITE: @@ -189,10 +318,20 @@ final class JsonFunctions { assertTrue(json.endsWith(']'), "JSON should end with ']' ($json)") } - static void byContainsMatchOrdered(ThrowawayDatabase db) { + static void byContainsMatch(ThrowawayDatabase db) { JsonDocument.load(db) - String json = db.conn.jsonByContains(TEST_TABLE, Map.of('sub', Map.of('foo', 'green')), - List.of(Field.named('value'))) + checkByContainsMatch(db.conn.jsonByContains(TEST_TABLE, Map.of('value', 'purple'))) + } + + static void writeByContainsMatch(ThrowawayDatabase db) { + JsonDocument.load(db) + def output = new StringWriter() + def writer = new PrintWriter(output) + db.conn.writeJsonByContains(TEST_TABLE, writer, Map.of('value', 'purple')) + checkByContainsMatch(output.toString()) + } + + private static void checkByContainsMatchOrdered(String json) { switch (Configuration.dialect()) { case Dialect.SQLITE: assertEquals("[${JsonDocument.two},${JsonDocument.four}]", json, @@ -210,15 +349,39 @@ final class JsonFunctions { } } - static void byContainsNoMatch(ThrowawayDatabase db) { + static void byContainsMatchOrdered(ThrowawayDatabase db) { JsonDocument.load(db) - assertEquals('[]', db.conn.jsonByContains(TEST_TABLE, Map.of('value', 'indigo')), - 'There should have been no documents returned') + checkByContainsMatchOrdered(db.conn.jsonByContains(TEST_TABLE, Map.of('sub', Map.of('foo', 'green')), + List.of(Field.named('value')))) } - static void byJsonPathMatch(ThrowawayDatabase db) { + static void writeByContainsMatchOrdered(ThrowawayDatabase db) { JsonDocument.load(db) - String json = db.conn.jsonByJsonPath(TEST_TABLE, '$.numValue ? (@ > 10)') + def output = new StringWriter() + def writer = new PrintWriter(output) + db.conn.writeJsonByContains(TEST_TABLE, writer, Map.of('sub', Map.of('foo', 'green')), + List.of(Field.named('value'))) + checkByContainsMatchOrdered(output.toString()) + } + + private static void checkByContainsNoMatch(String json) { + assertEquals('[]', json, 'There should have been no documents returned') + } + + static void byContainsNoMatch(ThrowawayDatabase db) { + JsonDocument.load(db) + checkByContainsNoMatch(db.conn.jsonByContains(TEST_TABLE, Map.of('value', 'indigo'))) + } + + static void writeByContainsNoMatch(ThrowawayDatabase db) { + JsonDocument.load(db) + def output = new StringWriter() + def writer = new PrintWriter(output) + db.conn.writeJsonByContains(TEST_TABLE, writer, Map.of('value', 'indigo')) + checkByContainsNoMatch(output.toString()) + } + + private static void checkByJsonPathMatch(String json) { assertTrue(json.startsWith('['), "JSON should start with '[' ($json)") switch (Configuration.dialect()) { case Dialect.SQLITE: @@ -233,9 +396,20 @@ final class JsonFunctions { assertTrue(json.endsWith(']'), "JSON should end with ']' ($json)") } - static void byJsonPathMatchOrdered(ThrowawayDatabase db) { + static void byJsonPathMatch(ThrowawayDatabase db) { JsonDocument.load(db) - String json = db.conn.jsonByJsonPath(TEST_TABLE, '$.numValue ? (@ > 10)', List.of(Field.named('id'))) + checkByJsonPathMatch(db.conn.jsonByJsonPath(TEST_TABLE, '$.numValue ? (@ > 10)')) + } + + static void writeByJsonPathMatch(ThrowawayDatabase db) { + JsonDocument.load(db) + def output = new StringWriter() + def writer = new PrintWriter(output) + db.conn.writeJsonByJsonPath(TEST_TABLE, writer, '$.numValue ? (@ > 10)') + checkByJsonPathMatch(output.toString()) + } + + private static void checkByJsonPathMatchOrdered(String json) { switch (Configuration.dialect()) { case Dialect.SQLITE: assertEquals("[${JsonDocument.five},${JsonDocument.four}]", json, @@ -253,15 +427,38 @@ final class JsonFunctions { } } - static void byJsonPathNoMatch(ThrowawayDatabase db) { + static void byJsonPathMatchOrdered(ThrowawayDatabase db) { JsonDocument.load(db) - assertEquals('[]', db.conn.jsonByJsonPath(TEST_TABLE, '$.numValue ? (@ > 100)'), - 'There should have been no documents returned') + checkByJsonPathMatchOrdered(db.conn.jsonByJsonPath(TEST_TABLE, '$.numValue ? (@ > 10)', + List.of(Field.named('id')))) } - static void firstByFieldsMatchOne(ThrowawayDatabase db) { + static void writeByJsonPathMatchOrdered(ThrowawayDatabase db) { JsonDocument.load(db) - String json = db.conn.jsonFirstByFields(TEST_TABLE, List.of(Field.equal('value', 'another'))) + def output = new StringWriter() + def writer = new PrintWriter(output) + db.conn.writeJsonByJsonPath(TEST_TABLE, writer, '$.numValue ? (@ > 10)', List.of(Field.named('id'))) + checkByJsonPathMatchOrdered(output.toString()) + } + + private static void checkByJsonPathNoMatch(String json) { + assertEquals('[]', json, 'There should have been no documents returned') + } + + static void byJsonPathNoMatch(ThrowawayDatabase db) { + JsonDocument.load(db) + checkByJsonPathNoMatch(db.conn.jsonByJsonPath(TEST_TABLE, '$.numValue ? (@ > 100)')) + } + + static void writeByJsonPathNoMatch(ThrowawayDatabase db) { + JsonDocument.load(db) + def output = new StringWriter() + def writer = new PrintWriter(output) + db.conn.writeJsonByJsonPath(TEST_TABLE, writer, '$.numValue ? (@ > 100)') + checkByJsonPathNoMatch(output.toString()) + } + + private static void checkFirstByFieldsMatchOne(String json) { switch (Configuration.dialect()) { case Dialect.SQLITE: assertEquals(JsonDocument.two, json, 'The incorrect document was returned') @@ -272,13 +469,24 @@ final class JsonFunctions { } } - static void firstByFieldsMatchMany(ThrowawayDatabase db) { + static void firstByFieldsMatchOne(ThrowawayDatabase db) { JsonDocument.load(db) - String json = db.conn.jsonFirstByFields(TEST_TABLE, List.of(Field.equal('sub.foo', 'green'))) + checkFirstByFieldsMatchOne(db.conn.jsonFirstByFields(TEST_TABLE, List.of(Field.equal('value', 'another')))) + } + + static void writeFirstByFieldsMatchOne(ThrowawayDatabase db) { + JsonDocument.load(db) + def output = new StringWriter() + def writer = new PrintWriter(output) + db.conn.writeJsonFirstByFields(TEST_TABLE, writer, List.of(Field.equal('value', 'another'))) + checkFirstByFieldsMatchOne(output.toString()) + } + + private static void checkFirstByFieldsMatchMany(String json) { switch (Configuration.dialect()) { case Dialect.SQLITE: assertTrue(json.contains(JsonDocument.two) || json.contains(JsonDocument.four), - "Expected document 'two' or 'four' ($json)") + "Expected document 'two' or 'four' ($json)") break case Dialect.POSTGRESQL: assertTrue(json.contains(docId('two')) || json.contains(docId('four')), @@ -287,10 +495,20 @@ final class JsonFunctions { } } - static void firstByFieldsMatchOrdered(ThrowawayDatabase db) { + static void firstByFieldsMatchMany(ThrowawayDatabase db) { JsonDocument.load(db) - String json = db.conn.jsonFirstByFields(TEST_TABLE, List.of(Field.equal('sub.foo', 'green')), null, - List.of(Field.named('n:numValue DESC'))) + checkFirstByFieldsMatchMany(db.conn.jsonFirstByFields(TEST_TABLE, List.of(Field.equal('sub.foo', 'green')))) + } + + static void writeFirstByFieldsMatchMany(ThrowawayDatabase db) { + JsonDocument.load(db) + def output = new StringWriter() + def writer = new PrintWriter(output) + db.conn.writeJsonFirstByFields(TEST_TABLE, writer, List.of(Field.equal('sub.foo', 'green'))) + checkFirstByFieldsMatchMany(output.toString()) + } + + private static void checkFirstByFieldsMatchOrdered(String json) { switch (Configuration.dialect()) { case Dialect.SQLITE: assertEquals(JsonDocument.four, json, 'An incorrect document was returned') @@ -301,15 +519,39 @@ final class JsonFunctions { } } - static void firstByFieldsNoMatch(ThrowawayDatabase db) { + static void firstByFieldsMatchOrdered(ThrowawayDatabase db) { JsonDocument.load(db) - assertEquals('{}', db.conn.jsonFirstByFields(TEST_TABLE, List.of(Field.equal('value', 'absent'))), - 'There should have been no document returned') + checkFirstByFieldsMatchOrdered(db.conn.jsonFirstByFields(TEST_TABLE, List.of(Field.equal('sub.foo', 'green')), + null, List.of(Field.named('n:numValue DESC')))) } - static void firstByContainsMatchOne(ThrowawayDatabase db) { + static void writeFirstByFieldsMatchOrdered(ThrowawayDatabase db) { JsonDocument.load(db) - String json = db.conn.jsonFirstByContains(TEST_TABLE, Map.of('value', 'FIRST!')) + def output = new StringWriter() + def writer = new PrintWriter(output) + db.conn.writeJsonFirstByFields(TEST_TABLE, writer, List.of(Field.equal('sub.foo', 'green')), + null, List.of(Field.named('n:numValue DESC'))) + checkFirstByFieldsMatchOrdered(output.toString()) + } + + private static void checkFirstByFieldsNoMatch(String json) { + assertEquals('{}', json, 'There should have been no document returned') + } + + static void firstByFieldsNoMatch(ThrowawayDatabase db) { + JsonDocument.load(db) + checkFirstByFieldsNoMatch(db.conn.jsonFirstByFields(TEST_TABLE, List.of(Field.equal('value', 'absent')))) + } + + static void writeFirstByFieldsNoMatch(ThrowawayDatabase db) { + JsonDocument.load(db) + def output = new StringWriter() + def writer = new PrintWriter(output) + db.conn.writeJsonFirstByFields(TEST_TABLE, writer, List.of(Field.equal('value', 'absent'))) + checkFirstByFieldsNoMatch(output.toString()) + } + + private static void checkFirstByContainsMatchOne(String json) { switch (Configuration.dialect()) { case Dialect.SQLITE: assertEquals(JsonDocument.one, json, 'An incorrect document was returned') @@ -320,25 +562,46 @@ final class JsonFunctions { } } - static void firstByContainsMatchMany(ThrowawayDatabase db) { + static void firstByContainsMatchOne(ThrowawayDatabase db) { JsonDocument.load(db) - String json = db.conn.jsonFirstByContains(TEST_TABLE, Map.of('value', 'purple')) + checkFirstByContainsMatchOne(db.conn.jsonFirstByContains(TEST_TABLE, Map.of('value', 'FIRST!'))) + } + + static void writeFirstByContainsMatchOne(ThrowawayDatabase db) { + JsonDocument.load(db) + def output = new StringWriter() + def writer = new PrintWriter(output) + db.conn.writeJsonFirstByContains(TEST_TABLE, writer, Map.of('value', 'FIRST!')) + checkFirstByContainsMatchOne(output.toString()) + } + + private static void checkFirstByContainsMatchMany(String json) { switch (Configuration.dialect()) { case Dialect.SQLITE: assertTrue(json.contains(JsonDocument.four) || json.contains(JsonDocument.five), - "Expected document 'four' or 'five' ($json)") + "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)") + "Expected document 'four' or 'five' ($json)") break } } - static void firstByContainsMatchOrdered(ThrowawayDatabase db) { + static void firstByContainsMatchMany(ThrowawayDatabase db) { JsonDocument.load(db) - String json = db.conn.jsonFirstByContains(TEST_TABLE, Map.of('value', 'purple'), - List.of(Field.named('sub.bar NULLS FIRST'))) + checkFirstByContainsMatchMany(db.conn.jsonFirstByContains(TEST_TABLE, Map.of('value', 'purple'))) + } + + static void writeFirstByContainsMatchMany(ThrowawayDatabase db) { + JsonDocument.load(db) + def output = new StringWriter() + def writer = new PrintWriter(output) + db.conn.writeJsonFirstByContains(TEST_TABLE, writer, Map.of('value', 'purple')) + checkFirstByContainsMatchMany(output.toString()) + } + + private static void checkFirstByContainsMatchOrdered(String json) { switch (Configuration.dialect()) { case Dialect.SQLITE: assertEquals(JsonDocument.five, json, 'An incorrect document was returned') @@ -349,15 +612,39 @@ final class JsonFunctions { } } - static void firstByContainsNoMatch(ThrowawayDatabase db) { + static void firstByContainsMatchOrdered(ThrowawayDatabase db) { JsonDocument.load(db) - assertEquals('{}', db.conn.jsonFirstByContains(TEST_TABLE, Map.of('value', 'indigo')), - 'There should have been no document returned') + checkFirstByContainsMatchOrdered(db.conn.jsonFirstByContains(TEST_TABLE, Map.of('value', 'purple'), + List.of(Field.named('sub.bar NULLS FIRST')))) } - static void firstByJsonPathMatchOne(ThrowawayDatabase db) { + static void writeFirstByContainsMatchOrdered(ThrowawayDatabase db) { JsonDocument.load(db) - String json = db.conn.jsonFirstByJsonPath(TEST_TABLE, '$.numValue ? (@ == 10)') + def output = new StringWriter() + def writer = new PrintWriter(output) + db.conn.writeJsonFirstByContains(TEST_TABLE, writer, Map.of('value', 'purple'), + List.of(Field.named('sub.bar NULLS FIRST'))) + checkFirstByContainsMatchOrdered(output.toString()) + } + + private static void checkFirstByContainsNoMatch(String json) { + assertEquals('{}', json, 'There should have been no document returned') + } + + static void firstByContainsNoMatch(ThrowawayDatabase db) { + JsonDocument.load(db) + checkFirstByContainsNoMatch(db.conn.jsonFirstByContains(TEST_TABLE, Map.of('value', 'indigo'))) + } + + static void writeFirstByContainsNoMatch(ThrowawayDatabase db) { + JsonDocument.load(db) + def output = new StringWriter() + def writer = new PrintWriter(output) + db.conn.writeJsonFirstByContains(TEST_TABLE, writer, Map.of('value', 'indigo')) + checkFirstByContainsNoMatch(output.toString()) + } + + private static void checkFirstByJsonPathMatchOne(String json) { switch (Configuration.dialect()) { case Dialect.SQLITE: assertEquals(JsonDocument.two, json, 'An incorrect document was returned') @@ -368,24 +655,46 @@ final class JsonFunctions { } } - static void firstByJsonPathMatchMany(ThrowawayDatabase db) { + static void firstByJsonPathMatchOne(ThrowawayDatabase db) { JsonDocument.load(db) - String json = db.conn.jsonFirstByJsonPath(TEST_TABLE, '$.numValue ? (@ > 10)') + checkFirstByJsonPathMatchOne(db.conn.jsonFirstByJsonPath(TEST_TABLE, '$.numValue ? (@ == 10)')) + } + + static void writeFirstByJsonPathMatchOne(ThrowawayDatabase db) { + JsonDocument.load(db) + def output = new StringWriter() + def writer = new PrintWriter(output) + db.conn.writeJsonFirstByJsonPath(TEST_TABLE, writer, '$.numValue ? (@ == 10)') + checkFirstByJsonPathMatchOne(output.toString()) + } + + private static void checkFirstByJsonPathMatchMany(String json) { switch (Configuration.dialect()) { case Dialect.SQLITE: assertTrue(json.contains(JsonDocument.four) || json.contains(JsonDocument.five), - "Expected document 'four' or 'five' ($json)") + "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)") + "Expected document 'four' or 'five' ($json)") break } } - static void firstByJsonPathMatchOrdered(ThrowawayDatabase db) { + static void firstByJsonPathMatchMany(ThrowawayDatabase db) { JsonDocument.load(db) - String json = db.conn.jsonFirstByJsonPath(TEST_TABLE, '$.numValue ? (@ > 10)', List.of(Field.named('id DESC'))) + checkFirstByJsonPathMatchMany(db.conn.jsonFirstByJsonPath(TEST_TABLE, '$.numValue ? (@ > 10)')) + } + + static void writeFirstByJsonPathMatchMany(ThrowawayDatabase db) { + JsonDocument.load(db) + def output = new StringWriter() + def writer = new PrintWriter(output) + db.conn.writeJsonFirstByJsonPath(TEST_TABLE, writer, '$.numValue ? (@ > 10)') + checkFirstByJsonPathMatchMany(output.toString()) + } + + private static void checkFirstByJsonPathMatchOrdered(String json) { switch (Configuration.dialect()) { case Dialect.SQLITE: assertEquals(JsonDocument.four, json, 'An incorrect document was returned') @@ -396,9 +705,34 @@ final class JsonFunctions { } } + static void firstByJsonPathMatchOrdered(ThrowawayDatabase db) { + JsonDocument.load(db) + checkFirstByJsonPathMatchOrdered(db.conn.jsonFirstByJsonPath(TEST_TABLE, '$.numValue ? (@ > 10)', + List.of(Field.named('id DESC')))) + } + + static void writeFirstByJsonPathMatchOrdered(ThrowawayDatabase db) { + JsonDocument.load(db) + def output = new StringWriter() + def writer = new PrintWriter(output) + db.conn.writeJsonFirstByJsonPath(TEST_TABLE, writer, '$.numValue ? (@ > 10)', List.of(Field.named('id DESC'))) + checkFirstByJsonPathMatchOrdered(output.toString()) + } + + private static void checkFirstByJsonPathNoMatch(String json) { + assertEquals('{}', json, 'There should have been no document returned') + } + static void firstByJsonPathNoMatch(ThrowawayDatabase db) { JsonDocument.load(db) - assertEquals('{}', db.conn.jsonFirstByJsonPath(TEST_TABLE, '$.numValue ? (@ > 100)'), - 'There should have been no document returned') + checkFirstByJsonPathNoMatch(db.conn.jsonFirstByJsonPath(TEST_TABLE, '$.numValue ? (@ > 100)')) + } + + static void writeFirstByJsonPathNoMatch(ThrowawayDatabase db) { + JsonDocument.load(db) + def output = new StringWriter() + def writer = new PrintWriter(output) + db.conn.writeJsonFirstByJsonPath(TEST_TABLE, writer, '$.numValue ? (@ > 100)') + checkFirstByJsonPathNoMatch(output.toString()) } } diff --git a/src/groovy/src/test/groovy/solutions/bitbadger/documents/groovy/tests/integration/PostgreSQLCustomIT.groovy b/src/groovy/src/test/groovy/solutions/bitbadger/documents/groovy/tests/integration/PostgreSQLCustomIT.groovy index e052725..bca62d8 100644 --- a/src/groovy/src/test/groovy/solutions/bitbadger/documents/groovy/tests/integration/PostgreSQLCustomIT.groovy +++ b/src/groovy/src/test/groovy/solutions/bitbadger/documents/groovy/tests/integration/PostgreSQLCustomIT.groovy @@ -39,6 +39,24 @@ final class PostgreSQLCustomIT { new PgDB().withCloseable CustomFunctions.&jsonArrayMany } + @Test + @DisplayName('writeJsonArray succeeds with empty array') + void writeJsonArrayEmpty() { + new PgDB().withCloseable CustomFunctions.&writeJsonArrayEmpty + } + + @Test + @DisplayName('writeJsonArray succeeds with a single-item array') + void writeJsonArraySingle() { + new PgDB().withCloseable CustomFunctions.&writeJsonArraySingle + } + + @Test + @DisplayName('writeJsonArray succeeds with a multi-item array') + void writeJsonArrayMany() { + new PgDB().withCloseable CustomFunctions.&writeJsonArrayMany + } + @Test @DisplayName('single succeeds when document not found') void singleNone() { 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 index d0b61fa..690e401 100644 --- 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 @@ -182,4 +182,178 @@ final class PostgreSQLJsonIT { void firstByJsonPathNoMatch() { new PgDB().withCloseable JsonFunctions.&firstByJsonPathNoMatch } + + @Test + @DisplayName('writeAll retrieves all documents') + void writeAllDefault() { + new PgDB().withCloseable JsonFunctions.&writeAllDefault + } + + @Test + @DisplayName('writeAll succeeds with an empty table') + void writeAllEmpty() { + new PgDB().withCloseable JsonFunctions.&writeAllEmpty + } + + @Test + @DisplayName('writeById retrieves a document via a string ID') + void writeByIdString() { + new PgDB().withCloseable JsonFunctions.&writeByIdString + } + + @Test + @DisplayName('writeById retrieves a document via a numeric ID') + void writeByIdNumber() { + new PgDB().withCloseable JsonFunctions.&writeByIdNumber + } + + @Test + @DisplayName('writeById returns null when a matching ID is not found') + void writeByIdNotFound() { + new PgDB().withCloseable JsonFunctions.&writeByIdNotFound + } + + @Test + @DisplayName('writeByFields retrieves matching documents') + void writeByFieldsMatch() { + new PgDB().withCloseable JsonFunctions.&writeByFieldsMatch + } + + @Test + @DisplayName('writeByFields retrieves ordered matching documents') + void writeByFieldsMatchOrdered() { + new PgDB().withCloseable JsonFunctions.&writeByFieldsMatchOrdered + } + + @Test + @DisplayName('writeByFields retrieves matching documents with a numeric IN clause') + void writeByFieldsMatchNumIn() { + new PgDB().withCloseable JsonFunctions.&writeByFieldsMatchNumIn + } + + @Test + @DisplayName('writeByFields succeeds when no documents match') + void writeByFieldsNoMatch() { + new PgDB().withCloseable JsonFunctions.&writeByFieldsNoMatch + } + + @Test + @DisplayName('writeByFields retrieves matching documents with an IN_ARRAY comparison') + void writeByFieldsMatchInArray() { + new PgDB().withCloseable JsonFunctions.&writeByFieldsMatchInArray + } + + @Test + @DisplayName('writeByFields succeeds when no documents match an IN_ARRAY comparison') + void writeByFieldsNoMatchInArray() { + new PgDB().withCloseable JsonFunctions.&writeByFieldsNoMatchInArray + } + + @Test + @DisplayName('writeByContains retrieves matching documents') + void writeByContainsMatch() { + new PgDB().withCloseable JsonFunctions.&writeByContainsMatch + } + + @Test + @DisplayName('writeByContains retrieves ordered matching documents') + void writeByContainsMatchOrdered() { + new PgDB().withCloseable JsonFunctions.&writeByContainsMatchOrdered + } + + @Test + @DisplayName('writeByContains succeeds when no documents match') + void writeByContainsNoMatch() { + new PgDB().withCloseable JsonFunctions.&writeByContainsNoMatch + } + + @Test + @DisplayName('writeByJsonPath retrieves matching documents') + void writeByJsonPathMatch() { + new PgDB().withCloseable JsonFunctions.&writeByJsonPathMatch + } + + @Test + @DisplayName('writeByJsonPath retrieves ordered matching documents') + void writeByJsonPathMatchOrdered() { + new PgDB().withCloseable JsonFunctions.&writeByJsonPathMatchOrdered + } + + @Test + @DisplayName('writeByJsonPath succeeds when no documents match') + void writeByJsonPathNoMatch() { + new PgDB().withCloseable JsonFunctions.&writeByJsonPathNoMatch + } + + @Test + @DisplayName('writeFirstByFields retrieves a matching document') + void writeFirstByFieldsMatchOne() { + new PgDB().withCloseable JsonFunctions.&writeFirstByFieldsMatchOne + } + + @Test + @DisplayName('writeFirstByFields retrieves a matching document among many') + void writeFirstByFieldsMatchMany() { + new PgDB().withCloseable JsonFunctions.&writeFirstByFieldsMatchMany + } + + @Test + @DisplayName('writeFirstByFields retrieves a matching document among many (ordered)') + void writeFirstByFieldsMatchOrdered() { + new PgDB().withCloseable JsonFunctions.&writeFirstByFieldsMatchOrdered + } + + @Test + @DisplayName('writeFirstByFields returns null when no document matches') + void writeFirstByFieldsNoMatch() { + new PgDB().withCloseable JsonFunctions.&writeFirstByFieldsNoMatch + } + + @Test + @DisplayName('writeFirstByContains retrieves a matching document') + void writeFirstByContainsMatchOne() { + new PgDB().withCloseable JsonFunctions.&writeFirstByContainsMatchOne + } + + @Test + @DisplayName('writeFirstByContains retrieves a matching document among many') + void writeFirstByContainsMatchMany() { + new PgDB().withCloseable JsonFunctions.&writeFirstByContainsMatchMany + } + + @Test + @DisplayName('writeFirstByContains retrieves a matching document among many (ordered)') + void writeFirstByContainsMatchOrdered() { + new PgDB().withCloseable JsonFunctions.&writeFirstByContainsMatchOrdered + } + + @Test + @DisplayName('writeFirstByContains returns null when no document matches') + void writeFirstByContainsNoMatch() { + new PgDB().withCloseable JsonFunctions.&writeFirstByContainsNoMatch + } + + @Test + @DisplayName('writeFirstByJsonPath retrieves a matching document') + void writeFirstByJsonPathMatchOne() { + new PgDB().withCloseable JsonFunctions.&writeFirstByJsonPathMatchOne + } + + @Test + @DisplayName('writeFirstByJsonPath retrieves a matching document among many') + void writeFirstByJsonPathMatchMany() { + new PgDB().withCloseable JsonFunctions.&writeFirstByJsonPathMatchMany + } + + @Test + @DisplayName('writeFirstByJsonPath retrieves a matching document among many (ordered)') + void writeFirstByJsonPathMatchOrdered() { + new PgDB().withCloseable JsonFunctions.&writeFirstByJsonPathMatchOrdered + } + + @Test + @DisplayName('writeFirstByJsonPath returns null when no document matches') + void writeFirstByJsonPathNoMatch() { + new PgDB().withCloseable JsonFunctions.&writeFirstByJsonPathNoMatch + } } diff --git a/src/groovy/src/test/groovy/solutions/bitbadger/documents/groovy/tests/integration/SQLiteCustomIT.groovy b/src/groovy/src/test/groovy/solutions/bitbadger/documents/groovy/tests/integration/SQLiteCustomIT.groovy index 05d9f24..517e011 100644 --- a/src/groovy/src/test/groovy/solutions/bitbadger/documents/groovy/tests/integration/SQLiteCustomIT.groovy +++ b/src/groovy/src/test/groovy/solutions/bitbadger/documents/groovy/tests/integration/SQLiteCustomIT.groovy @@ -21,6 +21,42 @@ final class SQLiteCustomIT { new SQLiteDB().withCloseable CustomFunctions.&listAll } + @Test + @DisplayName('jsonArray succeeds with empty array') + void jsonArrayEmpty() { + new SQLiteDB().withCloseable CustomFunctions.&jsonArrayEmpty + } + + @Test + @DisplayName('jsonArray succeeds with a single-item array') + void jsonArraySingle() { + new SQLiteDB().withCloseable CustomFunctions.&jsonArraySingle + } + + @Test + @DisplayName('jsonArray succeeds with a multi-item array') + void jsonArrayMany() { + new SQLiteDB().withCloseable CustomFunctions.&jsonArrayMany + } + + @Test + @DisplayName('writeJsonArray succeeds with empty array') + void writeJsonArrayEmpty() { + new SQLiteDB().withCloseable CustomFunctions.&writeJsonArrayEmpty + } + + @Test + @DisplayName('writeJsonArray succeeds with a single-item array') + void writeJsonArraySingle() { + new SQLiteDB().withCloseable CustomFunctions.&writeJsonArraySingle + } + + @Test + @DisplayName('writeJsonArray succeeds with a multi-item array') + void writeJsonArrayMany() { + new SQLiteDB().withCloseable CustomFunctions.&writeJsonArrayMany + } + @Test @DisplayName('single succeeds when document not found') void singleNone() { 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 index 222bb86..3e81c59 100644 --- 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 @@ -133,4 +133,126 @@ final class SQLiteJsonIT { assertThrows(DocumentException) { JsonFunctions.firstByJsonPathMatchOne db } } } + + @Test + @DisplayName('writeAll retrieves all documents') + void writeAllDefault() { + new SQLiteDB().withCloseable JsonFunctions.&writeAllDefault + } + + @Test + @DisplayName('writeAll succeeds with an empty table') + void writeAllEmpty() { + new SQLiteDB().withCloseable JsonFunctions.&writeAllEmpty + } + + @Test + @DisplayName('writeById retrieves a document via a string ID') + void writeByIdString() { + new SQLiteDB().withCloseable JsonFunctions.&writeByIdString + } + + @Test + @DisplayName('writeById retrieves a document via a numeric ID') + void writeByIdNumber() { + new SQLiteDB().withCloseable JsonFunctions.&writeByIdNumber + } + + @Test + @DisplayName('writeById returns null when a matching ID is not found') + void writeByIdNotFound() { + new SQLiteDB().withCloseable JsonFunctions.&writeByIdNotFound + } + + @Test + @DisplayName('writeByFields retrieves matching documents') + void writeByFieldsMatch() { + new SQLiteDB().withCloseable JsonFunctions.&writeByFieldsMatch + } + + @Test + @DisplayName('writeByFields retrieves ordered matching documents') + void writeByFieldsMatchOrdered() { + new SQLiteDB().withCloseable JsonFunctions.&writeByFieldsMatchOrdered + } + + @Test + @DisplayName('writeByFields retrieves matching documents with a numeric IN clause') + void writeByFieldsMatchNumIn() { + new SQLiteDB().withCloseable JsonFunctions.&writeByFieldsMatchNumIn + } + + @Test + @DisplayName('writeByFields succeeds when no documents match') + void writeByFieldsNoMatch() { + new SQLiteDB().withCloseable JsonFunctions.&writeByFieldsNoMatch + } + + @Test + @DisplayName('writeByFields retrieves matching documents with an IN_ARRAY comparison') + void writeByFieldsMatchInArray() { + new SQLiteDB().withCloseable JsonFunctions.&writeByFieldsMatchInArray + } + + @Test + @DisplayName('writeByFields succeeds when no documents match an IN_ARRAY comparison') + void writeByFieldsNoMatchInArray() { + new SQLiteDB().withCloseable JsonFunctions.&writeByFieldsNoMatchInArray + } + + @Test + @DisplayName('writeByContains fails') + void writeByContainsFails() { + new SQLiteDB().withCloseable { db -> + assertThrows(DocumentException) { JsonFunctions.writeByContainsMatch db } + } + } + + @Test + @DisplayName('writeByJsonPath fails') + void writeByJsonPathFails() { + new SQLiteDB().withCloseable { db -> + assertThrows(DocumentException) { JsonFunctions.writeByJsonPathMatch db } + } + } + + @Test + @DisplayName('writeFirstByFields retrieves a matching document') + void writeFirstByFieldsMatchOne() { + new SQLiteDB().withCloseable JsonFunctions.&writeFirstByFieldsMatchOne + } + + @Test + @DisplayName('writeFirstByFields retrieves a matching document among many') + void writeFirstByFieldsMatchMany() { + new SQLiteDB().withCloseable JsonFunctions.&writeFirstByFieldsMatchMany + } + + @Test + @DisplayName('writeFirstByFields retrieves a matching document among many (ordered)') + void writeFirstByFieldsMatchOrdered() { + new SQLiteDB().withCloseable JsonFunctions.&writeFirstByFieldsMatchOrdered + } + + @Test + @DisplayName('writeFirstByFields returns null when no document matches') + void writeFirstByFieldsNoMatch() { + new SQLiteDB().withCloseable JsonFunctions.&writeFirstByFieldsNoMatch + } + + @Test + @DisplayName('writeFirstByContains fails') + void writeFirstByContainsFails() { + new SQLiteDB().withCloseable { db -> + assertThrows(DocumentException) { JsonFunctions.writeFirstByContainsMatchOne db } + } + } + + @Test + @DisplayName('writeFirstByJsonPath fails') + void writeFirstByJsonPathFails() { + new SQLiteDB().withCloseable { db -> + assertThrows(DocumentException) { JsonFunctions.writeFirstByJsonPathMatchOne db } + } + } }