Add JSON string custom functions

This commit is contained in:
2025-03-27 20:26:51 -04:00
parent 3990b40c38
commit 18ba1be191
28 changed files with 1012 additions and 16 deletions

View File

@@ -8,6 +8,7 @@ import solutions.bitbadger.documents.java.Results
import solutions.bitbadger.documents.query.CountQuery
import solutions.bitbadger.documents.query.DeleteQuery
import solutions.bitbadger.documents.query.FindQuery
import solutions.bitbadger.documents.query.QueryUtils
import static org.junit.jupiter.api.Assertions.*
import static solutions.bitbadger.documents.groovy.tests.Types.TEST_TABLE
@@ -27,6 +28,28 @@ final class CustomFunctions {
assertEquals 5, result.size(), 'There should have been 5 results'
}
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),
'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),
'A single document list was not represented correctly')
}
static void jsonArrayMany(ThrowawayDatabase db) {
ArrayDocument.testDocuments.forEach { db.conn.insert(TEST_TABLE, it) }
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),
'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(),
@@ -41,6 +64,18 @@ final class CustomFunctions {
'There should not have been a document returned')
}
static void jsonSingleNone(ThrowawayDatabase db) {
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),
'A single document was not represented correctly');
}
static void nonQueryChanges(ThrowawayDatabase db) {
JsonDocument.load db
assertEquals(5L, db.conn.customScalar(CountQuery.all(TEST_TABLE), List.of(), Long, Results.&toCount),

View File

@@ -0,0 +1,20 @@
package solutions.bitbadger.documents.groovy.tests.integration
import solutions.bitbadger.documents.Configuration
import solutions.bitbadger.documents.Dialect
final class JsonFunctions {
/**
* PostgreSQL, when returning JSONB as a string, has spaces after commas and colons delineating fields and values.
* This function will do a crude string replacement to match the target string based on the dialect being tested.
*
* @param json The JSON which should be returned
* @return The actual expected JSON based on the database being tested
*/
static String maybeJsonB(String json) {
Configuration.dialect() == Dialect.SQLITE
? json
: json.replace('":"', '": "').replace('","', '", "').replace('":[', '": [')
}
}

View File

@@ -21,6 +21,24 @@ final class PostgreSQLCustomIT {
new PgDB().withCloseable CustomFunctions.&listAll
}
@Test
@DisplayName('jsonArray succeeds with empty array')
void jsonArrayEmpty() {
new PgDB().withCloseable CustomFunctions.&jsonArrayEmpty
}
@Test
@DisplayName('jsonArray succeeds with a single-item array')
void jsonArraySingle() {
new PgDB().withCloseable CustomFunctions.&jsonArraySingle
}
@Test
@DisplayName('jsonArray succeeds with a multi-item array')
void jsonArrayMany() {
new PgDB().withCloseable CustomFunctions.&jsonArrayMany
}
@Test
@DisplayName('single succeeds when document not found')
void singleNone() {
@@ -33,6 +51,18 @@ final class PostgreSQLCustomIT {
new PgDB().withCloseable CustomFunctions.&singleOne
}
@Test
@DisplayName('jsonSingle succeeds when document not found')
void jsonSingleNone() {
new PgDB().withCloseable CustomFunctions.&jsonSingleNone
}
@Test
@DisplayName('jsonSingle succeeds when a document is found')
void jsonSingleOne() {
new PgDB().withCloseable CustomFunctions.&jsonSingleOne
}
@Test
@DisplayName('nonQuery makes changes')
void nonQueryChanges() {