From a2c9f96b32e614b34bd1403d5e17571a0cd6c046 Mon Sep 17 00:00:00 2001 From: "Daniel J. Summers" Date: Tue, 18 Mar 2025 23:19:58 -0400 Subject: [PATCH] Resolved closure/java.base open issue --- .../documents/groovy/AutoIdTest.groovy | 61 ++++++------- .../documents/groovy/ConfigurationTest.groovy | 5 +- .../documents/groovy/FieldTest.groovy | 26 +++--- .../documents/groovy/ParameterTest.groovy | 13 ++- .../groovy/jvm/ParametersTest.groovy | 86 +++++++++---------- .../integration/common/CountFunctions.groovy | 28 +++--- .../jvm/integration/postgresql/CountIT.groovy | 42 +++++++-- .../jvm/integration/sqlite/CountIT.groovy | 50 +++++++++++ .../groovy/query/CountQueryTest.groovy | 56 ++++++------ .../groovy/query/DefinitionQueryTest.groovy | 74 ++++++++-------- .../groovy/query/DeleteQueryTest.groovy | 59 ++++++------- .../groovy/query/DocumentQueryTest.groovy | 65 +++++++------- .../groovy/query/FindQueryTest.groovy | 63 +++++++------- .../groovy/query/PatchQueryTest.groovy | 61 ++++++------- .../groovy/query/QueryUtilsTest.groovy | 58 +++++++------ .../groovy/query/RemoveFieldsQueryTest.groovy | 64 +++++++------- .../documents/groovy/query/WhereTest.groovy | 56 ++++++------ .../groovy/support/JsonDocument.groovy | 4 +- 18 files changed, 462 insertions(+), 409 deletions(-) create mode 100644 src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/jvm/integration/sqlite/CountIT.groovy diff --git a/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/AutoIdTest.groovy b/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/AutoIdTest.groovy index 5a3e99d..1cecfa8 100644 --- a/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/AutoIdTest.groovy +++ b/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/AutoIdTest.groovy @@ -3,7 +3,7 @@ package solutions.bitbadger.documents.groovy import org.junit.jupiter.api.DisplayName import org.junit.jupiter.api.Test import solutions.bitbadger.documents.AutoId -//import solutions.bitbadger.documents.DocumentException +import solutions.bitbadger.documents.DocumentException import solutions.bitbadger.documents.groovy.support.* import static org.junit.jupiter.api.Assertions.* @@ -42,19 +42,17 @@ class AutoIdTest { assertNotEquals result1, result2, 'There should have been 2 different strings generated' } -// TODO: resolve java.base open issue -// @Test -// @DisplayName('needsAutoId fails for null document') -// void needsAutoIdFailsForNullDocument() { -// assertThrows(DocumentException) { AutoId.needsAutoId(AutoId.DISABLED, null, 'id') } -// } -// -// TODO: resolve java.base open issue -// @Test -// @DisplayName('needsAutoId fails for missing ID property') -// void needsAutoIdFailsForMissingId() { -// assertThrows(DocumentException) { AutoId.needsAutoId(AutoId.UUID, new IntIdClass(0), 'Id') } -// } + @Test + @DisplayName('needsAutoId fails for null document') + void needsAutoIdFailsForNullDocument() { + assertThrows(DocumentException) { AutoId.needsAutoId(AutoId.DISABLED, null, 'id') } + } + + @Test + @DisplayName('needsAutoId fails for missing ID property') + void needsAutoIdFailsForMissingId() { + assertThrows(DocumentException) { AutoId.needsAutoId(AutoId.UUID, new IntIdClass(0), 'Id') } + } @Test @DisplayName('needsAutoId returns false if disabled') @@ -118,12 +116,11 @@ class AutoIdTest { 'Number Auto ID with 2 should return false') } -// TODO: resolve java.base open issue -// @Test -// @DisplayName('needsAutoId fails for Number strategy and non-number ID') -// void needsAutoIdFailsForNumberWithStringId() { -// assertThrows(DocumentException) { AutoId.needsAutoId(AutoId.NUMBER, new StringIdClass(''), 'id') } -// } + @Test + @DisplayName('needsAutoId fails for Number strategy and non-number ID') + void needsAutoIdFailsForNumberWithStringId() { + assertThrows(DocumentException) { AutoId.needsAutoId(AutoId.NUMBER, new StringIdClass(''), 'id') } + } @Test @DisplayName('needsAutoId returns true for UUID strategy and blank ID') @@ -139,12 +136,11 @@ class AutoIdTest { 'UUID Auto ID with non-blank should return false') } -// TODO: resolve java.base open issue -// @Test -// @DisplayName('needsAutoId fails for UUID strategy and non-string ID') -// void needsAutoIdFailsForUUIDNonString() { -// assertThrows(DocumentException) { AutoId.needsAutoId(AutoId.UUID, new IntIdClass(5), 'id') } -// } + @Test + @DisplayName('needsAutoId fails for UUID strategy and non-string ID') + void needsAutoIdFailsForUUIDNonString() { + assertThrows(DocumentException) { AutoId.needsAutoId(AutoId.UUID, new IntIdClass(5), 'id') } + } @Test @DisplayName('needsAutoId returns true for Random String strategy and blank ID') @@ -160,12 +156,9 @@ class AutoIdTest { 'Random String Auto ID with non-blank should return false') } -// TODO: resolve java.base open issue -// @Test -// @DisplayName('needsAutoId fails for Random String strategy and non-string ID') -// void needsAutoIdFailsForRandomNonString() { -// assertThrows(DocumentException) { -// AutoId.needsAutoId(AutoId.RANDOM_STRING, new ShortIdClass((short) 55), 'id') -// } -// } + @Test + @DisplayName('needsAutoId fails for Random String strategy and non-string ID') + void needsAutoIdFailsForRandomNonString() { + assertThrows(DocumentException) { AutoId.needsAutoId(AutoId.RANDOM_STRING, new ShortIdClass((short) 55), 'id') } + } } diff --git a/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/ConfigurationTest.groovy b/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/ConfigurationTest.groovy index 9682c3f..87d7383 100644 --- a/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/ConfigurationTest.groovy +++ b/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/ConfigurationTest.groovy @@ -5,7 +5,7 @@ import org.junit.jupiter.api.Test import solutions.bitbadger.documents.AutoId import solutions.bitbadger.documents.Configuration import solutions.bitbadger.documents.Dialect -//import solutions.bitbadger.documents.DocumentException +import solutions.bitbadger.documents.DocumentException import static org.junit.jupiter.api.Assertions.* @@ -37,8 +37,7 @@ class ConfigurationTest { @DisplayName('Dialect is derived from connection string') void dialectIsDerived() { try { - // TODO: uncomment once java.base open issue resolved - //assertThrows(DocumentException) { Configuration.dialect() } + assertThrows(DocumentException) { Configuration.dialect() } Configuration.connectionString = 'jdbc:postgresql:db' assertEquals Dialect.POSTGRESQL, Configuration.dialect() } finally { diff --git a/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/FieldTest.groovy b/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/FieldTest.groovy index 25aa3ca..da2214e 100644 --- a/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/FieldTest.groovy +++ b/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/FieldTest.groovy @@ -4,7 +4,7 @@ import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.DisplayName import org.junit.jupiter.api.Test import solutions.bitbadger.documents.Dialect -//import solutions.bitbadger.documents.DocumentException +import solutions.bitbadger.documents.DocumentException import solutions.bitbadger.documents.Field import solutions.bitbadger.documents.FieldFormat import solutions.bitbadger.documents.Op @@ -28,12 +28,11 @@ class FieldTest { // ~~~ INSTANCE METHODS ~~~ -// TODO: fix java.base open issue -// @Test -// @DisplayName('withParameterName fails for invalid name') -// void withParamNameFails() { -// assertThrows(DocumentException) { Field.equal('it', '').withParameterName('2424') } -// } + @Test + @DisplayName('withParameterName fails for invalid name') + void withParamNameFails() { + assertThrows(DocumentException) { Field.equal('it', '').withParameterName('2424') } + } @Test @DisplayName('withParameterName works with colon prefix') @@ -351,7 +350,7 @@ class FieldTest { assertEquals 'Great', field.name, 'Field name not filled correctly' assertEquals Op.GREATER, field.comparison.op, 'Field comparison operation not filled correctly' assertEquals 'night', field.comparison.value, 'Field comparison value not filled correctly' - assertEquals('Field parameter name not filled correctly', ':yeah', field.parameterName) + assertEquals ':yeah', field.parameterName, 'Field parameter name not filled correctly' assertNull field.qualifier, 'The qualifier should have been null' } @@ -546,12 +545,11 @@ class FieldTest { assertNull field.qualifier, 'The qualifier should have been null' } -// TODO: fix java.base open issue -// @Test -// @DisplayName('static constructors fail for invalid parameter name') -// void staticCtorsFailOnParamName() { -// assertThrows(DocumentException) { Field.equal('a', 'b', "that ain't it, Jack...") } -// } + @Test + @DisplayName('static constructors fail for invalid parameter name') + void staticCtorsFailOnParamName() { + assertThrows(DocumentException) { Field.equal('a', 'b', "that ain't it, Jack...") } + } @Test @DisplayName('nameToPath creates a simple PostgreSQL SQL name') diff --git a/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/ParameterTest.groovy b/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/ParameterTest.groovy index 62320b7..76564b1 100644 --- a/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/ParameterTest.groovy +++ b/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/ParameterTest.groovy @@ -2,7 +2,7 @@ package solutions.bitbadger.documents.groovy import org.junit.jupiter.api.DisplayName import org.junit.jupiter.api.Test -//import solutions.bitbadger.documents.DocumentException +import solutions.bitbadger.documents.DocumentException import solutions.bitbadger.documents.Parameter import solutions.bitbadger.documents.ParameterType @@ -32,10 +32,9 @@ class ParameterTest { assertNull p.value, 'Parameter value was incorrect' } -// TODO: resolve java.base open issue -// @Test -// @DisplayName('Construction fails with incorrect prefix') -// void ctorFailsForPrefix() { -// assertThrows(DocumentException) { new Parameter('it', ParameterType.JSON, '') } -// } + @Test + @DisplayName('Construction fails with incorrect prefix') + void ctorFailsForPrefix() { + assertThrows(DocumentException) { new Parameter('it', ParameterType.JSON, '') } + } } diff --git a/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/jvm/ParametersTest.groovy b/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/jvm/ParametersTest.groovy index d5963fb..a8581a7 100644 --- a/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/jvm/ParametersTest.groovy +++ b/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/jvm/ParametersTest.groovy @@ -3,14 +3,14 @@ package solutions.bitbadger.documents.groovy.jvm import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.DisplayName import org.junit.jupiter.api.Test -//import solutions.bitbadger.documents.DocumentException +import solutions.bitbadger.documents.DocumentException import solutions.bitbadger.documents.Field import solutions.bitbadger.documents.Parameter import solutions.bitbadger.documents.ParameterType import solutions.bitbadger.documents.jvm.Parameters import solutions.bitbadger.documents.support.ForceDialect -import static groovy.test.GroovyAssert.* +import static org.junit.jupiter.api.Assertions.* /** * Unit tests for the `Parameters` object @@ -31,10 +31,10 @@ class ParametersTest { void nameFieldsNoChange() { def fields = List.of(Field.equal('a', '', ':test'), Field.exists('q'), Field.equal('b', '', ':me')) def named = Parameters.nameFields(fields).toList() - assertEquals('There should have been 3 fields in the list', fields.size(), named.size()) - assertSame('The first field should be the same', fields[0], named[0]) - assertSame('The second field should be the same', fields[1], named[1]) - assertSame('The third field should be the same', fields[2], named[2]) + assertEquals fields.size(), named.size(), 'There should have been 3 fields in the list' + assertSame fields[0], named[0], 'The first field should be the same' + assertSame fields[1], named[1], 'The second field should be the same' + assertSame fields[2], named[2], 'The third field should be the same' } @Test @@ -43,13 +43,13 @@ class ParametersTest { def fields = List.of(Field.equal('a', ''), Field.equal('e', '', ':hi'), Field.equal('b', ''), Field.notExists('z')) def named = Parameters.nameFields(fields).toList() - assertEquals('There should have been 4 fields in the list', fields.size(), named.size()) - assertNotSame('The first field should not be the same', fields[0], named[0]) - assertEquals('First parameter name incorrect', ':field0', named[0].parameterName) - assertSame('The second field should be the same', fields[1], named[1]) - assertNotSame('The third field should not be the same', fields[2], named[2]) - assertEquals('Third parameter name incorrect', ':field1', named[2].parameterName) - assertSame('The fourth field should be the same', fields[3], named[3]) + assertEquals fields.size(), named.size(), 'There should have been 4 fields in the list' + assertNotSame fields[0], named[0], 'The first field should not be the same' + assertEquals ':field0', named[0].parameterName, 'First parameter name incorrect' + assertSame fields[1], named[1], 'The second field should be the same' + assertNotSame fields[2], named[2], 'The third field should not be the same' + assertEquals ':field1', named[2].parameterName, 'Third parameter name incorrect' + assertSame fields[3], named[3], 'The fourth field should be the same' } @Test @@ -58,9 +58,8 @@ class ParametersTest { def parameters = List.of(new Parameter(':data', ParameterType.JSON, '{}'), new Parameter(':data_ext', ParameterType.STRING, '')) def query = 'SELECT data, data_ext FROM tbl WHERE data = :data AND data_ext = :data_ext AND more_data = :data' - assertEquals('Parameters not replaced correctly', - 'SELECT data, data_ext FROM tbl WHERE data = ? AND data_ext = ? AND more_data = ?', - Parameters.replaceNamesInQuery(query, parameters)) + assertEquals('SELECT data, data_ext FROM tbl WHERE data = ? AND data_ext = ? AND more_data = ?', + Parameters.replaceNamesInQuery(query, parameters), 'Parameters not replaced correctly') } @Test @@ -68,10 +67,10 @@ class ParametersTest { void fieldNamesSinglePostgres() { ForceDialect.postgres() def nameParams = Parameters.fieldNames(List.of('test')).toList() - assertEquals('There should be one name parameter', 1, nameParams.size()) - assertEquals('The parameter name is incorrect', ':name', nameParams[0].name) - assertEquals('The parameter type is incorrect', ParameterType.STRING, nameParams[0].type) - assertEquals('The parameter value is incorrect', '{test}', nameParams[0].value) + assertEquals 1, nameParams.size(), 'There should be one name parameter' + assertEquals ':name', nameParams[0].name, 'The parameter name is incorrect' + assertEquals ParameterType.STRING, nameParams[0].type, 'The parameter type is incorrect' + assertEquals '{test}', nameParams[0].value, 'The parameter value is incorrect' } @Test @@ -79,10 +78,10 @@ class ParametersTest { void fieldNamesMultiplePostgres() { ForceDialect.postgres() def nameParams = Parameters.fieldNames(List.of('test', 'this', 'today')).toList() - assertEquals('There should be one name parameter', 1, nameParams.size()) - assertEquals('The parameter name is incorrect', ':name', nameParams[0].name) - assertEquals('The parameter type is incorrect', ParameterType.STRING, nameParams[0].type) - assertEquals('The parameter value is incorrect', '{test,this,today}', nameParams[0].value) + assertEquals 1, nameParams.size(), 'There should be one name parameter' + assertEquals ':name', nameParams[0].name, 'The parameter name is incorrect' + assertEquals ParameterType.STRING, nameParams[0].type, 'The parameter type is incorrect' + assertEquals '{test,this,today}', nameParams[0].value, 'The parameter value is incorrect' } @Test @@ -90,10 +89,10 @@ class ParametersTest { void fieldNamesSingleSQLite() { ForceDialect.sqlite() def nameParams = Parameters.fieldNames(List.of('test')).toList() - assertEquals('There should be one name parameter', 1, nameParams.size()) - assertEquals('The parameter name is incorrect', ':name0', nameParams[0].name) - assertEquals('The parameter type is incorrect', ParameterType.STRING, nameParams[0].type) - assertEquals('The parameter value is incorrect', 'test', nameParams[0].value) + assertEquals 1, nameParams.size(), 'There should be one name parameter' + assertEquals ':name0', nameParams[0].name, 'The parameter name is incorrect' + assertEquals ParameterType.STRING, nameParams[0].type, 'The parameter type is incorrect' + assertEquals 'test', nameParams[0].value, 'The parameter value is incorrect' } @Test @@ -101,22 +100,21 @@ class ParametersTest { void fieldNamesMultipleSQLite() { ForceDialect.sqlite() def nameParams = Parameters.fieldNames(List.of('test', 'this', 'today')).toList() - assertEquals('There should be one name parameter', 3, nameParams.size()) - assertEquals('The first parameter name is incorrect', ':name0', nameParams[0].name) - assertEquals('The first parameter type is incorrect', ParameterType.STRING, nameParams[0].type) - assertEquals('The first parameter value is incorrect', 'test', nameParams[0].value) - assertEquals('The second parameter name is incorrect', ':name1', nameParams[1].name) - assertEquals('The second parameter type is incorrect', ParameterType.STRING, nameParams[1].type) - assertEquals('The second parameter value is incorrect', 'this', nameParams[1].value) - assertEquals('The third parameter name is incorrect', ':name2', nameParams[2].name) - assertEquals('The third parameter type is incorrect', ParameterType.STRING, nameParams[2].type) - assertEquals('The third parameter value is incorrect', 'today', nameParams[2].value) + assertEquals 3, nameParams.size(), 'There should be one name parameter' + assertEquals ':name0', nameParams[0].name, 'The first parameter name is incorrect' + assertEquals ParameterType.STRING, nameParams[0].type, 'The first parameter type is incorrect' + assertEquals 'test', nameParams[0].value, 'The first parameter value is incorrect' + assertEquals ':name1', nameParams[1].name, 'The second parameter name is incorrect' + assertEquals ParameterType.STRING, nameParams[1].type, 'The second parameter type is incorrect' + assertEquals 'this', nameParams[1].value, 'The second parameter value is incorrect' + assertEquals ':name2', nameParams[2].name, 'The third parameter name is incorrect' + assertEquals ParameterType.STRING, nameParams[2].type, 'The third parameter type is incorrect' + assertEquals 'today', nameParams[2].value, 'The third parameter value is incorrect' } -// TODO: resolve java.base open issue -// @Test -// @DisplayName('fieldNames fails if dialect not set') -// void fieldNamesFails() { -// assertThrows(DocumentException) { Parameters.fieldNames(List.of()) } -// } + @Test + @DisplayName('fieldNames fails if dialect not set') + void fieldNamesFails() { + assertThrows(DocumentException) { Parameters.fieldNames(List.of()) } + } } diff --git a/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/jvm/integration/common/CountFunctions.groovy b/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/jvm/integration/common/CountFunctions.groovy index 30899bb..673a7e4 100644 --- a/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/jvm/integration/common/CountFunctions.groovy +++ b/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/jvm/integration/common/CountFunctions.groovy @@ -6,48 +6,48 @@ import solutions.bitbadger.documents.support.ThrowawayDatabase import static solutions.bitbadger.documents.extensions.ConnExt.* import static solutions.bitbadger.documents.support.TypesKt.TEST_TABLE -import static groovy.test.GroovyAssert.* +import static org.junit.jupiter.api.Assertions.* class CountFunctions { static void all(ThrowawayDatabase db) { JsonDocument.load(db) - assertEquals('There should have been 5 documents in the table', 5L, countAll(db.conn, TEST_TABLE)) + assertEquals 5L, countAll(db.conn, TEST_TABLE), 'There should have been 5 documents in the table' } static void byFieldsNumeric(ThrowawayDatabase db) { JsonDocument.load(db) - assertEquals('There should have been 3 matching documents', 3L, - countByFields(db.conn, TEST_TABLE, List.of(Field.between('numValue', 10, 20)))) + assertEquals(3L, countByFields(db.conn, TEST_TABLE, List.of(Field.between('numValue', 10, 20))), + 'There should have been 3 matching documents') } static void byFieldsAlpha(ThrowawayDatabase db) { JsonDocument.load(db) - assertEquals('There should have been 1 matching document', 1L, - countByFields(db.conn, TEST_TABLE, List.of(Field.between('value', 'aardvark', 'apple')))) + assertEquals(1L, countByFields(db.conn, TEST_TABLE, List.of(Field.between('value', 'aardvark', 'apple'))), + 'There should have been 1 matching document') } static void byContainsMatch(ThrowawayDatabase db) { JsonDocument.load(db) - assertEquals('There should have been 2 matching documents', 2L, - countByContains(db.conn, TEST_TABLE, Map.of('value', 'purple'))) + assertEquals(2L, countByContains(db.conn, TEST_TABLE, Map.of('value', 'purple')), + 'There should have been 2 matching documents') } static void byContainsNoMatch(ThrowawayDatabase db) { JsonDocument.load(db) - assertEquals('There should have been no matching documents', 0L, - countByContains(db.conn, TEST_TABLE, Map.of('value', 'magenta'))) + assertEquals(0L, countByContains(db.conn, TEST_TABLE, Map.of('value', 'magenta')), + 'There should have been no matching documents') } static void byJsonPathMatch(ThrowawayDatabase db) { JsonDocument.load(db) - assertEquals('There should have been 2 matching documents', 2L, - countByJsonPath(db.conn, TEST_TABLE, '$.numValue ? (@ < 5)')) + assertEquals(2L, countByJsonPath(db.conn, TEST_TABLE, '$.numValue ? (@ < 5)'), + 'There should have been 2 matching documents') } static void byJsonPathNoMatch(ThrowawayDatabase db) { JsonDocument.load(db) - assertEquals('There should have been no matching documents', 0L, - countByJsonPath(db.conn, TEST_TABLE, '$.numValue ? (@ > 100)')) + assertEquals(0L, countByJsonPath(db.conn, TEST_TABLE, '$.numValue ? (@ > 100)'), + 'There should have been no matching documents') } } diff --git a/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/jvm/integration/postgresql/CountIT.groovy b/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/jvm/integration/postgresql/CountIT.groovy index b252bbb..49ad2fa 100644 --- a/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/jvm/integration/postgresql/CountIT.groovy +++ b/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/jvm/integration/postgresql/CountIT.groovy @@ -14,12 +14,42 @@ class CountIT { @Test @DisplayName("all counts all documents") void all() { - PgDB db = new PgDB() - try { - CountFunctions.all(db) - } finally { - db.close() - } + new PgDB().withCloseable { CountFunctions.all(it) } } + @Test + @DisplayName("byFields counts documents by a numeric value") + void byFieldsNumeric() { + new PgDB().withCloseable { CountFunctions.byFieldsNumeric(it) } + } + + @Test + @DisplayName("byFields counts documents by a alphanumeric value") + void byFieldsAlpha() { + new PgDB().withCloseable { CountFunctions.byFieldsAlpha(it) } + } + + @Test + @DisplayName("byContains counts documents when matches are found") + void byContainsMatch() { + new PgDB().withCloseable { CountFunctions.byContainsMatch(it) } + } + + @Test + @DisplayName("byContains counts documents when no matches are found") + void byContainsNoMatch() { + new PgDB().withCloseable { CountFunctions.byContainsNoMatch(it) } + } + + @Test + @DisplayName("byJsonPath counts documents when matches are found") + void byJsonPathMatch() { + new PgDB().withCloseable { CountFunctions.byJsonPathMatch(it) } + } + + @Test + @DisplayName("byJsonPath counts documents when no matches are found") + void byJsonPathNoMatch() { + new PgDB().withCloseable { CountFunctions.byJsonPathNoMatch(it) } + } } diff --git a/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/jvm/integration/sqlite/CountIT.groovy b/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/jvm/integration/sqlite/CountIT.groovy new file mode 100644 index 0000000..0dd4dad --- /dev/null +++ b/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/jvm/integration/sqlite/CountIT.groovy @@ -0,0 +1,50 @@ +package solutions.bitbadger.documents.groovy.jvm.integration.sqlite + +import org.junit.jupiter.api.DisplayName +import org.junit.jupiter.api.Test +import solutions.bitbadger.documents.DocumentException +import solutions.bitbadger.documents.groovy.jvm.integration.common.CountFunctions +import solutions.bitbadger.documents.jvm.integration.sqlite.SQLiteDB + +import static org.junit.jupiter.api.Assertions.assertThrows + +/** + * SQLite integration tests for the `Count` object / `count*` connection extension functions + */ +@DisplayName("JVM | Groovy | SQLite: Count") +class CountIT { + + @Test + @DisplayName("all counts all documents") + void all() { + new SQLiteDB().withCloseable { CountFunctions.all(it) } + } + + @Test + @DisplayName("byFields counts documents by a numeric value") + void byFieldsNumeric() { + new SQLiteDB().withCloseable { CountFunctions.byFieldsNumeric(it) } + } + + @Test + @DisplayName("byFields counts documents by a alphanumeric value") + void byFieldsAlpha() { + new SQLiteDB().withCloseable { CountFunctions.byFieldsAlpha(it) } + } + + @Test + @DisplayName("byContains fails") + void byContainsMatch() { + new SQLiteDB().withCloseable { db -> + assertThrows(DocumentException) { CountFunctions.byContainsMatch(db) } + } + } + + @Test + @DisplayName("byJsonPath fails") + void byJsonPathMatch() { + new SQLiteDB().withCloseable { db -> + assertThrows(DocumentException) { CountFunctions.byJsonPathMatch(db) } + } + } +} diff --git a/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/query/CountQueryTest.groovy b/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/query/CountQueryTest.groovy index 54d3d96..95d62b5 100644 --- a/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/query/CountQueryTest.groovy +++ b/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/query/CountQueryTest.groovy @@ -3,13 +3,13 @@ package solutions.bitbadger.documents.groovy.query import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.DisplayName import org.junit.jupiter.api.Test -//import solutions.bitbadger.documents.DocumentException +import solutions.bitbadger.documents.DocumentException import solutions.bitbadger.documents.Field import solutions.bitbadger.documents.query.CountQuery import solutions.bitbadger.documents.support.ForceDialect import static solutions.bitbadger.documents.support.TypesKt.TEST_TABLE -import static groovy.test.GroovyAssert.* +import static org.junit.jupiter.api.Assertions.* /** * Unit tests for the `Count` object @@ -28,59 +28,55 @@ class CountQueryTest { @Test @DisplayName('all generates correctly') void all() { - assertEquals('Count query not constructed correctly', "SELECT COUNT(*) AS it FROM $TEST_TABLE".toString(), - CountQuery.all(TEST_TABLE)) + assertEquals("SELECT COUNT(*) AS it FROM $TEST_TABLE".toString(), CountQuery.all(TEST_TABLE), + 'Count query not constructed correctly') } @Test @DisplayName('byFields generates correctly | PostgreSQL') void byFieldsPostgres() { ForceDialect.postgres() - assertEquals('Count query not constructed correctly', - "SELECT COUNT(*) AS it FROM $TEST_TABLE WHERE data->>'test' = :field0".toString(), - CountQuery.byFields(TEST_TABLE, List.of(Field.equal('test', '', ':field0')))) + assertEquals("SELECT COUNT(*) AS it FROM $TEST_TABLE WHERE data->>'test' = :field0".toString(), + CountQuery.byFields(TEST_TABLE, List.of(Field.equal('test', '', ':field0'))), + 'Count query not constructed correctly') } @Test @DisplayName('byFields generates correctly | SQLite') void byFieldsSQLite() { ForceDialect.sqlite() - assertEquals('Count query not constructed correctly', - "SELECT COUNT(*) AS it FROM $TEST_TABLE WHERE data->>'test' = :field0".toString(), - CountQuery.byFields(TEST_TABLE, List.of(Field.equal('test', '', ':field0')))) + assertEquals("SELECT COUNT(*) AS it FROM $TEST_TABLE WHERE data->>'test' = :field0".toString(), + CountQuery.byFields(TEST_TABLE, List.of(Field.equal('test', '', ':field0'))), + 'Count query not constructed correctly') } @Test @DisplayName('byContains generates correctly | PostgreSQL') void byContainsPostgres() { ForceDialect.postgres() - assertEquals('Count query not constructed correctly', - "SELECT COUNT(*) AS it FROM $TEST_TABLE WHERE data @> :criteria".toString(), - CountQuery.byContains(TEST_TABLE)) + assertEquals("SELECT COUNT(*) AS it FROM $TEST_TABLE WHERE data @> :criteria".toString(), + CountQuery.byContains(TEST_TABLE), 'Count query not constructed correctly') } -// TODO: resolve java.base open issue -// @Test -// @DisplayName('byContains fails | SQLite') -// void byContainsSQLite() { -// ForceDialect.sqlite() -// assertThrows(DocumentException) { CountQuery.byContains(TEST_TABLE) } -// } + @Test + @DisplayName('byContains fails | SQLite') + void byContainsSQLite() { + ForceDialect.sqlite() + assertThrows(DocumentException) { CountQuery.byContains(TEST_TABLE) } + } @Test @DisplayName('byJsonPath generates correctly | PostgreSQL') void byJsonPathPostgres() { ForceDialect.postgres() - assertEquals('Count query not constructed correctly', - "SELECT COUNT(*) AS it FROM $TEST_TABLE WHERE jsonb_path_exists(data, :path::jsonpath)".toString(), - CountQuery.byJsonPath(TEST_TABLE)) + assertEquals("SELECT COUNT(*) AS it FROM $TEST_TABLE WHERE jsonb_path_exists(data, :path::jsonpath)".toString(), + CountQuery.byJsonPath(TEST_TABLE), 'Count query not constructed correctly') } -// TODO: resolve java.base open issue -// @Test -// @DisplayName('byJsonPath fails | SQLite') -// void byJsonPathSQLite() { -// ForceDialect.sqlite() -// assertThrows(DocumentException) { CountQuery.byJsonPath(TEST_TABLE) } -// } + @Test + @DisplayName('byJsonPath fails | SQLite') + void byJsonPathSQLite() { + ForceDialect.sqlite() + assertThrows(DocumentException) { CountQuery.byJsonPath(TEST_TABLE) } + } } diff --git a/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/query/DefinitionQueryTest.groovy b/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/query/DefinitionQueryTest.groovy index bba2783..6ce06fb 100644 --- a/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/query/DefinitionQueryTest.groovy +++ b/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/query/DefinitionQueryTest.groovy @@ -4,13 +4,13 @@ import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.DisplayName import org.junit.jupiter.api.Test import solutions.bitbadger.documents.Dialect -//import solutions.bitbadger.documents.DocumentException +import solutions.bitbadger.documents.DocumentException import solutions.bitbadger.documents.DocumentIndex import solutions.bitbadger.documents.query.DefinitionQuery import solutions.bitbadger.documents.support.ForceDialect import static solutions.bitbadger.documents.support.TypesKt.TEST_TABLE -import static groovy.test.GroovyAssert.* +import static org.junit.jupiter.api.Assertions.* /** * Unit tests for the `Definition` object @@ -29,9 +29,8 @@ class DefinitionQueryTest { @Test @DisplayName('ensureTableFor generates correctly') void ensureTableFor() { - assertEquals('CREATE TABLE statement not constructed correctly', - 'CREATE TABLE IF NOT EXISTS my.table (data JSONB NOT NULL)', - DefinitionQuery.ensureTableFor('my.table', 'JSONB')) + assertEquals('CREATE TABLE IF NOT EXISTS my.table (data JSONB NOT NULL)', + DefinitionQuery.ensureTableFor('my.table', 'JSONB'), 'CREATE TABLE statement not constructed correctly') } @Test @@ -50,79 +49,80 @@ class DefinitionQueryTest { DefinitionQuery.ensureTable(TEST_TABLE)) } -// TODO: resolve java.base open issue -// @Test -// @DisplayName('ensureTable fails when no dialect is set') -// void ensureTableFailsUnknown() { -// assertThrows(DocumentException) { DefinitionQuery.ensureTable(TEST_TABLE) } -// } + @Test + @DisplayName('ensureTable fails when no dialect is set') + void ensureTableFailsUnknown() { + assertThrows(DocumentException) { DefinitionQuery.ensureTable(TEST_TABLE) } + } @Test @DisplayName('ensureKey generates correctly with schema') void ensureKeyWithSchema() { - assertEquals('CREATE INDEX for key statement with schema not constructed correctly', - "CREATE UNIQUE INDEX IF NOT EXISTS idx_table_key ON test.table ((data->>'id'))", - DefinitionQuery.ensureKey('test.table', Dialect.POSTGRESQL)) + assertEquals("CREATE UNIQUE INDEX IF NOT EXISTS idx_table_key ON test.table ((data->>'id'))", + DefinitionQuery.ensureKey('test.table', Dialect.POSTGRESQL), + 'CREATE INDEX for key statement with schema not constructed correctly') } @Test @DisplayName('ensureKey generates correctly without schema') void ensureKeyWithoutSchema() { - assertEquals('CREATE INDEX for key statement without schema not constructed correctly', + assertEquals( "CREATE UNIQUE INDEX IF NOT EXISTS idx_${TEST_TABLE}_key ON $TEST_TABLE ((data->>'id'))".toString(), - DefinitionQuery.ensureKey(TEST_TABLE, Dialect.SQLITE)) + DefinitionQuery.ensureKey(TEST_TABLE, Dialect.SQLITE), + 'CREATE INDEX for key statement without schema not constructed correctly') } @Test @DisplayName('ensureIndexOn generates multiple fields and directions') void ensureIndexOnMultipleFields() { - assertEquals('CREATE INDEX for multiple field statement not constructed correctly', - "CREATE INDEX IF NOT EXISTS idx_table_gibberish ON test.table ((data->>'taco'), (data->>'guac') DESC, (data->>'salsa') ASC)" - .toString(), + assertEquals( + "CREATE INDEX IF NOT EXISTS idx_table_gibberish ON test.table ((data->>'taco'), (data->>'guac') DESC, (data->>'salsa') ASC)", DefinitionQuery.ensureIndexOn('test.table', 'gibberish', List.of('taco', 'guac DESC', 'salsa ASC'), - Dialect.POSTGRESQL)) + Dialect.POSTGRESQL), + 'CREATE INDEX for multiple field statement not constructed correctly') } @Test @DisplayName('ensureIndexOn generates nested field | PostgreSQL') void ensureIndexOnNestedPostgres() { - assertEquals('CREATE INDEX for nested PostgreSQL field incorrect', - "CREATE INDEX IF NOT EXISTS idx_${TEST_TABLE}_nest ON $TEST_TABLE ((data#>>'{a,b,c}'))".toString(), - DefinitionQuery.ensureIndexOn(TEST_TABLE, 'nest', List.of('a.b.c'), Dialect.POSTGRESQL)) + assertEquals("CREATE INDEX IF NOT EXISTS idx_${TEST_TABLE}_nest ON $TEST_TABLE ((data#>>'{a,b,c}'))".toString(), + DefinitionQuery.ensureIndexOn(TEST_TABLE, 'nest', List.of('a.b.c'), Dialect.POSTGRESQL), + 'CREATE INDEX for nested PostgreSQL field incorrect') } @Test @DisplayName('ensureIndexOn generates nested field | SQLite') void ensureIndexOnNestedSQLite() { - assertEquals('CREATE INDEX for nested SQLite field incorrect', + assertEquals( "CREATE INDEX IF NOT EXISTS idx_${TEST_TABLE}_nest ON $TEST_TABLE ((data->'a'->'b'->>'c'))".toString(), - DefinitionQuery.ensureIndexOn(TEST_TABLE, 'nest', List.of('a.b.c'), Dialect.SQLITE)) + DefinitionQuery.ensureIndexOn(TEST_TABLE, 'nest', List.of('a.b.c'), Dialect.SQLITE), + 'CREATE INDEX for nested SQLite field incorrect') } @Test @DisplayName('ensureDocumentIndexOn generates Full | PostgreSQL') void ensureDocumentIndexOnFullPostgres() { ForceDialect.postgres() - assertEquals('CREATE INDEX for full document index incorrect', - "CREATE INDEX IF NOT EXISTS idx_${TEST_TABLE}_document ON $TEST_TABLE USING GIN (data)".toString(), - DefinitionQuery.ensureDocumentIndexOn(TEST_TABLE, DocumentIndex.FULL)) + assertEquals("CREATE INDEX IF NOT EXISTS idx_${TEST_TABLE}_document ON $TEST_TABLE USING GIN (data)".toString(), + DefinitionQuery.ensureDocumentIndexOn(TEST_TABLE, DocumentIndex.FULL), + 'CREATE INDEX for full document index incorrect') } @Test @DisplayName('ensureDocumentIndexOn generates Optimized | PostgreSQL') void ensureDocumentIndexOnOptimizedPostgres() { ForceDialect.postgres() - assertEquals('CREATE INDEX for optimized document index incorrect', + assertEquals( "CREATE INDEX IF NOT EXISTS idx_${TEST_TABLE}_document ON $TEST_TABLE USING GIN (data jsonb_path_ops)" .toString(), - DefinitionQuery.ensureDocumentIndexOn(TEST_TABLE, DocumentIndex.OPTIMIZED)) + DefinitionQuery.ensureDocumentIndexOn(TEST_TABLE, DocumentIndex.OPTIMIZED), + 'CREATE INDEX for optimized document index incorrect') } -// TODO: resolve java.base open issue -// @Test -// @DisplayName('ensureDocumentIndexOn fails | SQLite') -// void ensureDocumentIndexOnFailsSQLite() { -// ForceDialect.sqlite() -// assertThrows(DocumentException) { DefinitionQuery.ensureDocumentIndexOn(TEST_TABLE, DocumentIndex.FULL) } -// } + @Test + @DisplayName('ensureDocumentIndexOn fails | SQLite') + void ensureDocumentIndexOnFailsSQLite() { + ForceDialect.sqlite() + assertThrows(DocumentException) { DefinitionQuery.ensureDocumentIndexOn(TEST_TABLE, DocumentIndex.FULL) } + } } diff --git a/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/query/DeleteQueryTest.groovy b/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/query/DeleteQueryTest.groovy index 06fe0b7..4b16942 100644 --- a/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/query/DeleteQueryTest.groovy +++ b/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/query/DeleteQueryTest.groovy @@ -3,13 +3,13 @@ package solutions.bitbadger.documents.groovy.query import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.DisplayName import org.junit.jupiter.api.Test -//import solutions.bitbadger.documents.DocumentException +import solutions.bitbadger.documents.DocumentException import solutions.bitbadger.documents.Field import solutions.bitbadger.documents.query.DeleteQuery import solutions.bitbadger.documents.support.ForceDialect import static solutions.bitbadger.documents.support.TypesKt.TEST_TABLE -import static groovy.test.GroovyAssert.* +import static org.junit.jupiter.api.Assertions.* /** * Unit tests for the `Delete` object @@ -29,66 +29,63 @@ class DeleteQueryTest { @DisplayName('byId generates correctly | PostgreSQL') void byIdPostgres() { ForceDialect.postgres() - assertEquals('Delete query not constructed correctly', - "DELETE FROM $TEST_TABLE WHERE data->>'id' = :id".toString(), DeleteQuery.byId(TEST_TABLE)) + assertEquals("DELETE FROM $TEST_TABLE WHERE data->>'id' = :id".toString(), DeleteQuery.byId(TEST_TABLE), + 'Delete query not constructed correctly') } @Test @DisplayName('byId generates correctly | SQLite') void byIdSQLite() { ForceDialect.sqlite() - assertEquals('Delete query not constructed correctly', - "DELETE FROM $TEST_TABLE WHERE data->>'id' = :id".toString(), DeleteQuery.byId(TEST_TABLE)) + assertEquals("DELETE FROM $TEST_TABLE WHERE data->>'id' = :id".toString(), DeleteQuery.byId(TEST_TABLE), + 'Delete query not constructed correctly') } @Test @DisplayName('byFields generates correctly | PostgreSQL') void byFieldsPostgres() { ForceDialect.postgres() - assertEquals('Delete query not constructed correctly', - "DELETE FROM $TEST_TABLE WHERE data->>'a' = :b".toString(), - DeleteQuery.byFields(TEST_TABLE, List.of(Field.equal('a', '', ':b')))) + assertEquals("DELETE FROM $TEST_TABLE WHERE data->>'a' = :b".toString(), + DeleteQuery.byFields(TEST_TABLE, List.of(Field.equal('a', '', ':b'))), + 'Delete query not constructed correctly') } @Test @DisplayName('byFields generates correctly | SQLite') void byFieldsSQLite() { ForceDialect.sqlite() - assertEquals('Delete query not constructed correctly', - "DELETE FROM $TEST_TABLE WHERE data->>'a' = :b".toString(), - DeleteQuery.byFields(TEST_TABLE, List.of(Field.equal('a', '', ':b')))) + assertEquals("DELETE FROM $TEST_TABLE WHERE data->>'a' = :b".toString(), + DeleteQuery.byFields(TEST_TABLE, List.of(Field.equal('a', '', ':b'))), + 'Delete query not constructed correctly') } @Test @DisplayName('byContains generates correctly | PostgreSQL') void byContainsPostgres() { ForceDialect.postgres() - assertEquals('Delete query not constructed correctly', - "DELETE FROM $TEST_TABLE WHERE data @> :criteria".toString(), DeleteQuery.byContains(TEST_TABLE)) + assertEquals("DELETE FROM $TEST_TABLE WHERE data @> :criteria".toString(), DeleteQuery.byContains(TEST_TABLE), + 'Delete query not constructed correctly') } -// TODO: resolve java.base open issue -// @Test -// @DisplayName('byContains fails | SQLite') -// void byContainsSQLite() { -// ForceDialect.sqlite() -// assertThrows(DocumentException) { DeleteQuery.byContains(TEST_TABLE) } -// } + @Test + @DisplayName('byContains fails | SQLite') + void byContainsSQLite() { + ForceDialect.sqlite() + assertThrows(DocumentException) { DeleteQuery.byContains(TEST_TABLE) } + } @Test @DisplayName('byJsonPath generates correctly | PostgreSQL') void byJsonPathPostgres() { ForceDialect.postgres() - assertEquals('Delete query not constructed correctly', - "DELETE FROM $TEST_TABLE WHERE jsonb_path_exists(data, :path::jsonpath)".toString(), - DeleteQuery.byJsonPath(TEST_TABLE)) + assertEquals("DELETE FROM $TEST_TABLE WHERE jsonb_path_exists(data, :path::jsonpath)".toString(), + DeleteQuery.byJsonPath(TEST_TABLE), 'Delete query not constructed correctly') } -// TODO: resolve java.base open issue -// @Test -// @DisplayName('byJsonPath fails | SQLite') -// void byJsonPathSQLite() { -// ForceDialect.sqlite() -// assertThrows(DocumentException) { DeleteQuery.byJsonPath(TEST_TABLE) } -// } + @Test + @DisplayName('byJsonPath fails | SQLite') + void byJsonPathSQLite() { + ForceDialect.sqlite() + assertThrows(DocumentException) { DeleteQuery.byJsonPath(TEST_TABLE) } + } } diff --git a/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/query/DocumentQueryTest.groovy b/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/query/DocumentQueryTest.groovy index 1d7cc15..29edebb 100644 --- a/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/query/DocumentQueryTest.groovy +++ b/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/query/DocumentQueryTest.groovy @@ -5,12 +5,12 @@ import org.junit.jupiter.api.DisplayName import org.junit.jupiter.api.Test import solutions.bitbadger.documents.AutoId import solutions.bitbadger.documents.Configuration -//import solutions.bitbadger.documents.DocumentException +import solutions.bitbadger.documents.DocumentException import solutions.bitbadger.documents.query.DocumentQuery import solutions.bitbadger.documents.support.ForceDialect import static solutions.bitbadger.documents.support.TypesKt.TEST_TABLE -import static groovy.test.GroovyAssert.* +import static org.junit.jupiter.api.Assertions.* /** * Unit tests for the `Document` object @@ -62,20 +62,20 @@ class DocumentQueryTest { @DisplayName('insert generates auto UUID | PostgreSQL') void insertAutoUUIDPostgres() { ForceDialect.postgres() - def query = DocumentQuery.insert(TEST_TABLE, AutoId.UUID) - assertTrue("Query start not correct (actual: $query)", - query.startsWith("INSERT INTO $TEST_TABLE VALUES (:data::jsonb || '{\"id\":\"")) - assertTrue('Query end not correct', query.endsWith("\"}')")) + def query = DocumentQuery.insert TEST_TABLE, AutoId.UUID + assertTrue(query.startsWith("INSERT INTO $TEST_TABLE VALUES (:data::jsonb || '{\"id\":\""), + "Query start not correct (actual: $query)") + assertTrue query.endsWith("\"}')"), 'Query end not correct' } @Test @DisplayName('insert generates auto UUID | SQLite') void insertAutoUUIDSQLite() { ForceDialect.sqlite() - def query = DocumentQuery.insert(TEST_TABLE, AutoId.UUID) - assertTrue("Query start not correct (actual: $query)", - query.startsWith("INSERT INTO $TEST_TABLE VALUES (json_set(:data, '\$.id', '")) - assertTrue('Query end not correct', query.endsWith("'))")) + def query = DocumentQuery.insert TEST_TABLE, AutoId.UUID + assertTrue(query.startsWith("INSERT INTO $TEST_TABLE VALUES (json_set(:data, '\$.id', '"), + "Query start not correct (actual: $query)") + assertTrue query.endsWith("'))"), 'Query end not correct' } @Test @@ -84,13 +84,14 @@ class DocumentQueryTest { try { ForceDialect.postgres() Configuration.idStringLength = 8 - def query = DocumentQuery.insert(TEST_TABLE, AutoId.RANDOM_STRING) - assertTrue("Query start not correct (actual: $query)", - query.startsWith("INSERT INTO $TEST_TABLE VALUES (:data::jsonb || '{\"id\":\"")) - assertTrue('Query end not correct', query.endsWith("\"}')")) - assertEquals('Random string length incorrect', 8, + def query = DocumentQuery.insert TEST_TABLE, AutoId.RANDOM_STRING + assertTrue(query.startsWith("INSERT INTO $TEST_TABLE VALUES (:data::jsonb || '{\"id\":\""), + "Query start not correct (actual: $query)") + assertTrue query.endsWith("\"}')"), 'Query end not correct' + assertEquals(8, query.replace("INSERT INTO $TEST_TABLE VALUES (:data::jsonb || '{\"id\":\"", '') - .replace("\"}')", '').length()) + .replace("\"}')", '').length(), + 'Random string length incorrect') } finally { Configuration.idStringLength = 16 } @@ -100,36 +101,36 @@ class DocumentQueryTest { @DisplayName('insert generates auto random string | SQLite') void insertAutoRandomSQLite() { ForceDialect.sqlite() - def query = DocumentQuery.insert(TEST_TABLE, AutoId.RANDOM_STRING) - assertTrue("Query start not correct (actual: $query)", - query.startsWith("INSERT INTO $TEST_TABLE VALUES (json_set(:data, '\$.id', '")) - assertTrue('Query end not correct', query.endsWith("'))")) - assertEquals('Random string length incorrect', Configuration.idStringLength, + def query = DocumentQuery.insert TEST_TABLE, AutoId.RANDOM_STRING + assertTrue(query.startsWith("INSERT INTO $TEST_TABLE VALUES (json_set(:data, '\$.id', '"), + "Query start not correct (actual: $query)") + assertTrue query.endsWith("'))"), 'Query end not correct' + assertEquals(Configuration.idStringLength, query.replace("INSERT INTO $TEST_TABLE VALUES (json_set(:data, '\$.id', '", '').replace("'))", '') - .length()) + .length(), + 'Random string length incorrect') } -// TODO: resolve java.base open issue -// @Test -// @DisplayName('insert fails when no dialect is set') -// void insertFailsUnknown() { -// assertThrows(DocumentException) { DocumentQuery.insert(TEST_TABLE) } -// } + @Test + @DisplayName('insert fails when no dialect is set') + void insertFailsUnknown() { + assertThrows(DocumentException) { DocumentQuery.insert(TEST_TABLE) } + } @Test @DisplayName('save generates correctly') void save() { ForceDialect.postgres() - assertEquals('INSERT ON CONFLICT UPDATE statement not constructed correctly', + assertEquals( "INSERT INTO $TEST_TABLE VALUES (:data) ON CONFLICT ((data->>'id')) DO UPDATE SET data = EXCLUDED.data" .toString(), - DocumentQuery.save(TEST_TABLE)) + DocumentQuery.save(TEST_TABLE), 'INSERT ON CONFLICT UPDATE statement not constructed correctly') } @Test @DisplayName('update generates successfully') void update() { - assertEquals('Update query not constructed correctly', "UPDATE $TEST_TABLE SET data = :data".toString(), - DocumentQuery.update(TEST_TABLE)) + assertEquals("UPDATE $TEST_TABLE SET data = :data".toString(), DocumentQuery.update(TEST_TABLE), + 'Update query not constructed correctly') } } diff --git a/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/query/FindQueryTest.groovy b/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/query/FindQueryTest.groovy index 4f3502f..7d557c2 100644 --- a/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/query/FindQueryTest.groovy +++ b/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/query/FindQueryTest.groovy @@ -3,13 +3,13 @@ package solutions.bitbadger.documents.groovy.query import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.DisplayName import org.junit.jupiter.api.Test -//import solutions.bitbadger.documents.DocumentException +import solutions.bitbadger.documents.DocumentException import solutions.bitbadger.documents.Field import solutions.bitbadger.documents.query.FindQuery import solutions.bitbadger.documents.support.ForceDialect import static solutions.bitbadger.documents.support.TypesKt.TEST_TABLE -import static groovy.test.GroovyAssert.* +import static org.junit.jupiter.api.Assertions.* /** * Unit tests for the `Find` object @@ -28,74 +28,71 @@ class FindQueryTest { @Test @DisplayName('all generates correctly') void all() { - assertEquals('Find query not constructed correctly', "SELECT data FROM $TEST_TABLE".toString(), - FindQuery.all(TEST_TABLE)) + assertEquals("SELECT data FROM $TEST_TABLE".toString(), FindQuery.all(TEST_TABLE), + 'Find query not constructed correctly') } @Test @DisplayName('byId generates correctly | PostgreSQL') void byIdPostgres() { ForceDialect.postgres() - assertEquals('Find query not constructed correctly', - "SELECT data FROM $TEST_TABLE WHERE data->>'id' = :id".toString(), FindQuery.byId(TEST_TABLE)) + assertEquals("SELECT data FROM $TEST_TABLE WHERE data->>'id' = :id".toString(), FindQuery.byId(TEST_TABLE), + 'Find query not constructed correctly') } @Test @DisplayName('byId generates correctly | SQLite') void byIdSQLite() { ForceDialect.sqlite() - assertEquals('Find query not constructed correctly', - "SELECT data FROM $TEST_TABLE WHERE data->>'id' = :id".toString(), FindQuery.byId(TEST_TABLE)) + assertEquals("SELECT data FROM $TEST_TABLE WHERE data->>'id' = :id".toString(), FindQuery.byId(TEST_TABLE), + 'Find query not constructed correctly') } @Test @DisplayName('byFields generates correctly | PostgreSQL') void byFieldsPostgres() { ForceDialect.postgres() - assertEquals('Find query not constructed correctly', - "SELECT data FROM $TEST_TABLE WHERE data->>'a' = :b AND (data->>'c')::numeric < :d".toString(), - FindQuery.byFields(TEST_TABLE, List.of(Field.equal('a', '', ':b'), Field.less('c', 14, ':d')))) + assertEquals("SELECT data FROM $TEST_TABLE WHERE data->>'a' = :b AND (data->>'c')::numeric < :d".toString(), + FindQuery.byFields(TEST_TABLE, List.of(Field.equal('a', '', ':b'), Field.less('c', 14, ':d'))), + 'Find query not constructed correctly') } @Test @DisplayName('byFields generates correctly | SQLite') void byFieldsSQLite() { ForceDialect.sqlite() - assertEquals('Find query not constructed correctly', - "SELECT data FROM $TEST_TABLE WHERE data->>'a' = :b AND data->>'c' < :d".toString(), - FindQuery.byFields(TEST_TABLE, List.of(Field.equal('a', '', ':b'), Field.less('c', 14, ':d')))) + assertEquals("SELECT data FROM $TEST_TABLE WHERE data->>'a' = :b AND data->>'c' < :d".toString(), + FindQuery.byFields(TEST_TABLE, List.of(Field.equal('a', '', ':b'), Field.less('c', 14, ':d'))), + 'Find query not constructed correctly') } @Test @DisplayName('byContains generates correctly | PostgreSQL') void byContainsPostgres() { ForceDialect.postgres() - assertEquals('Find query not constructed correctly', - "SELECT data FROM $TEST_TABLE WHERE data @> :criteria".toString(), FindQuery.byContains(TEST_TABLE)) + assertEquals("SELECT data FROM $TEST_TABLE WHERE data @> :criteria".toString(), + FindQuery.byContains(TEST_TABLE), 'Find query not constructed correctly') } -// TODO: resolve java.base open issue -// @Test -// @DisplayName('byContains fails | SQLite') -// void byContainsSQLite() { -// ForceDialect.sqlite() -// assertThrows(DocumentException) { FindQuery.byContains(TEST_TABLE) } -// } + @Test + @DisplayName('byContains fails | SQLite') + void byContainsSQLite() { + ForceDialect.sqlite() + assertThrows(DocumentException) { FindQuery.byContains(TEST_TABLE) } + } @Test @DisplayName('byJsonPath generates correctly | PostgreSQL') void byJsonPathPostgres() { ForceDialect.postgres() - assertEquals('Find query not constructed correctly', - "SELECT data FROM $TEST_TABLE WHERE jsonb_path_exists(data, :path::jsonpath)".toString(), - FindQuery.byJsonPath(TEST_TABLE)) + assertEquals("SELECT data FROM $TEST_TABLE WHERE jsonb_path_exists(data, :path::jsonpath)".toString(), + FindQuery.byJsonPath(TEST_TABLE), 'Find query not constructed correctly') } -// TODO: resolve java.base open issue -// @Test -// @DisplayName('byJsonPath fails | SQLite') -// void byJsonPathSQLite() { -// ForceDialect.sqlite() -// assertThrows(DocumentException) { FindQuery.byJsonPath(TEST_TABLE) } -// } + @Test + @DisplayName('byJsonPath fails | SQLite') + void byJsonPathSQLite() { + ForceDialect.sqlite() + assertThrows(DocumentException) { FindQuery.byJsonPath(TEST_TABLE) } + } } diff --git a/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/query/PatchQueryTest.groovy b/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/query/PatchQueryTest.groovy index f34c7a7..c362109 100644 --- a/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/query/PatchQueryTest.groovy +++ b/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/query/PatchQueryTest.groovy @@ -3,13 +3,13 @@ package solutions.bitbadger.documents.groovy.query import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.DisplayName import org.junit.jupiter.api.Test -//import solutions.bitbadger.documents.DocumentException +import solutions.bitbadger.documents.DocumentException import solutions.bitbadger.documents.Field import solutions.bitbadger.documents.query.PatchQuery import solutions.bitbadger.documents.support.ForceDialect import static solutions.bitbadger.documents.support.TypesKt.TEST_TABLE -import static groovy.test.GroovyAssert.* +import static org.junit.jupiter.api.Assertions.* /** * Unit tests for the `Patch` object @@ -29,69 +29,64 @@ class PatchQueryTest { @DisplayName('byId generates correctly | PostgreSQL') void byIdPostgres() { ForceDialect.postgres() - assertEquals('Patch query not constructed correctly', - "UPDATE $TEST_TABLE SET data = data || :data WHERE data->>'id' = :id".toString(), - PatchQuery.byId(TEST_TABLE)) + assertEquals("UPDATE $TEST_TABLE SET data = data || :data WHERE data->>'id' = :id".toString(), + PatchQuery.byId(TEST_TABLE), 'Patch query not constructed correctly') } @Test @DisplayName('byId generates correctly | SQLite') void byIdSQLite() { ForceDialect.sqlite() - assertEquals('Patch query not constructed correctly', - "UPDATE $TEST_TABLE SET data = json_patch(data, json(:data)) WHERE data->>'id' = :id".toString(), - PatchQuery.byId(TEST_TABLE)) + assertEquals("UPDATE $TEST_TABLE SET data = json_patch(data, json(:data)) WHERE data->>'id' = :id".toString(), + PatchQuery.byId(TEST_TABLE), 'Patch query not constructed correctly') } @Test @DisplayName('byFields generates correctly | PostgreSQL') void byFieldsPostgres() { ForceDialect.postgres() - assertEquals('Patch query not constructed correctly', - "UPDATE $TEST_TABLE SET data = data || :data WHERE data->>'z' = :y".toString(), - PatchQuery.byFields(TEST_TABLE, List.of(Field.equal('z', '', ':y')))) + assertEquals("UPDATE $TEST_TABLE SET data = data || :data WHERE data->>'z' = :y".toString(), + PatchQuery.byFields(TEST_TABLE, List.of(Field.equal('z', '', ':y'))), + 'Patch query not constructed correctly') } @Test @DisplayName('byFields generates correctly | SQLite') void byFieldsSQLite() { ForceDialect.sqlite() - assertEquals('Patch query not constructed correctly', - "UPDATE $TEST_TABLE SET data = json_patch(data, json(:data)) WHERE data->>'z' = :y".toString(), - PatchQuery.byFields(TEST_TABLE, List.of(Field.equal('z', '', ':y')))) + assertEquals("UPDATE $TEST_TABLE SET data = json_patch(data, json(:data)) WHERE data->>'z' = :y".toString(), + PatchQuery.byFields(TEST_TABLE, List.of(Field.equal('z', '', ':y'))), + 'Patch query not constructed correctly') } @Test @DisplayName('byContains generates correctly | PostgreSQL') void byContainsPostgres() { ForceDialect.postgres() - assertEquals('Patch query not constructed correctly', - "UPDATE $TEST_TABLE SET data = data || :data WHERE data @> :criteria".toString(), - PatchQuery.byContains(TEST_TABLE)) + assertEquals("UPDATE $TEST_TABLE SET data = data || :data WHERE data @> :criteria".toString(), + PatchQuery.byContains(TEST_TABLE), 'Patch query not constructed correctly') } -// TODO: resolve java.base open issue -// @Test -// @DisplayName('byContains fails | SQLite') -// void byContainsSQLite() { -// ForceDialect.sqlite() -// assertThrows(DocumentException) { PatchQuery.byContains(TEST_TABLE) } -// } + @Test + @DisplayName('byContains fails | SQLite') + void byContainsSQLite() { + ForceDialect.sqlite() + assertThrows(DocumentException) { PatchQuery.byContains(TEST_TABLE) } + } @Test @DisplayName('byJsonPath generates correctly | PostgreSQL') void byJsonPathPostgres() { ForceDialect.postgres() - assertEquals('Patch query not constructed correctly', + assertEquals( "UPDATE $TEST_TABLE SET data = data || :data WHERE jsonb_path_exists(data, :path::jsonpath)".toString(), - PatchQuery.byJsonPath(TEST_TABLE)) + PatchQuery.byJsonPath(TEST_TABLE), 'Patch query not constructed correctly') } -// TODO: resolve java.base open issue -// @Test -// @DisplayName('byJsonPath fails | SQLite') -// void byJsonPathSQLite() { -// ForceDialect.sqlite() -// assertThrows(DocumentException) { PatchQuery.byJsonPath(TEST_TABLE) } -// } + @Test + @DisplayName('byJsonPath fails | SQLite') + void byJsonPathSQLite() { + ForceDialect.sqlite() + assertThrows(DocumentException) { PatchQuery.byJsonPath(TEST_TABLE) } + } } diff --git a/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/query/QueryUtilsTest.groovy b/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/query/QueryUtilsTest.groovy index 069bbec..0643482 100644 --- a/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/query/QueryUtilsTest.groovy +++ b/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/query/QueryUtilsTest.groovy @@ -9,7 +9,7 @@ import solutions.bitbadger.documents.FieldMatch import solutions.bitbadger.documents.query.QueryUtils import solutions.bitbadger.documents.support.ForceDialect -import static groovy.test.GroovyAssert.* +import static org.junit.jupiter.api.Assertions.* /** * Unit tests for the `QueryUtils` class @@ -28,28 +28,28 @@ class QueryUtilsTest { @Test @DisplayName('statementWhere generates correctly') void statementWhere() { - assertEquals('Statements not combined correctly', 'x WHERE y', QueryUtils.statementWhere('x', 'y')) + assertEquals 'x WHERE y', QueryUtils.statementWhere('x', 'y'), 'Statements not combined correctly' } @Test @DisplayName('byId generates a numeric ID query | PostgreSQL') void byIdNumericPostgres() { ForceDialect.postgres() - assertEquals("test WHERE (data->>'id')::numeric = :id", QueryUtils.byId('test', 9)) + assertEquals "test WHERE (data->>'id')::numeric = :id", QueryUtils.byId('test', 9) } @Test @DisplayName('byId generates an alphanumeric ID query | PostgreSQL') void byIdAlphaPostgres() { ForceDialect.postgres() - assertEquals("unit WHERE data->>'id' = :id", QueryUtils.byId('unit', '18')) + assertEquals "unit WHERE data->>'id' = :id", QueryUtils.byId('unit', '18') } @Test @DisplayName('byId generates ID query | SQLite') void byIdSQLite() { ForceDialect.sqlite() - assertEquals("yo WHERE data->>'id' = :id", QueryUtils.byId('yo', 27)) + assertEquals "yo WHERE data->>'id' = :id", QueryUtils.byId('yo', 27) } @Test @@ -89,71 +89,75 @@ class QueryUtilsTest { @Test @DisplayName('orderBy generates for no fields') void orderByNone() { - assertEquals('ORDER BY should have been blank (PostgreSQL)', '', QueryUtils.orderBy(List.of(), - Dialect.POSTGRESQL)) - assertEquals('ORDER BY should have been blank (SQLite)', '', QueryUtils.orderBy(List.of(), Dialect.SQLITE)) + assertEquals('', QueryUtils.orderBy(List.of(), Dialect.POSTGRESQL), + 'ORDER BY should have been blank (PostgreSQL)') + assertEquals '', QueryUtils.orderBy(List.of(), Dialect.SQLITE), 'ORDER BY should have been blank (SQLite)' } @Test @DisplayName('orderBy generates single, no direction | PostgreSQL') void orderBySinglePostgres() { - assertEquals('ORDER BY not constructed correctly', " ORDER BY data->>'TestField'", - QueryUtils.orderBy(List.of(Field.named('TestField')), Dialect.POSTGRESQL)) + assertEquals(" ORDER BY data->>'TestField'", + QueryUtils.orderBy(List.of(Field.named('TestField')), Dialect.POSTGRESQL), + 'ORDER BY not constructed correctly') } @Test @DisplayName('orderBy generates single, no direction | SQLite') void orderBySingleSQLite() { - assertEquals('ORDER BY not constructed correctly', " ORDER BY data->>'TestField'", - QueryUtils.orderBy(List.of(Field.named('TestField')), Dialect.SQLITE)) + assertEquals(" ORDER BY data->>'TestField'", + QueryUtils.orderBy(List.of(Field.named('TestField')), Dialect.SQLITE), + 'ORDER BY not constructed correctly') } @Test @DisplayName('orderBy generates multiple with direction | PostgreSQL') void orderByMultiplePostgres() { - assertEquals('ORDER BY not constructed correctly', - " ORDER BY data#>>'{Nested,Test,Field}' DESC, data->>'AnotherField', data->>'It' DESC", + assertEquals(" ORDER BY data#>>'{Nested,Test,Field}' DESC, data->>'AnotherField', data->>'It' DESC", QueryUtils.orderBy(List.of(Field.named('Nested.Test.Field DESC'), Field.named('AnotherField'), Field.named('It DESC')), - Dialect.POSTGRESQL)) + Dialect.POSTGRESQL), + 'ORDER BY not constructed correctly') } @Test @DisplayName('orderBy generates multiple with direction | SQLite') void orderByMultipleSQLite() { - assertEquals('ORDER BY not constructed correctly', - " ORDER BY data->'Nested'->'Test'->>'Field' DESC, data->>'AnotherField', data->>'It' DESC", + assertEquals(" ORDER BY data->'Nested'->'Test'->>'Field' DESC, data->>'AnotherField', data->>'It' DESC", QueryUtils.orderBy(List.of(Field.named('Nested.Test.Field DESC'), Field.named('AnotherField'), Field.named('It DESC')), - Dialect.SQLITE)) + Dialect.SQLITE), + 'ORDER BY not constructed correctly') } @Test @DisplayName('orderBy generates numeric ordering | PostgreSQL') void orderByNumericPostgres() { - assertEquals('ORDER BY not constructed correctly', " ORDER BY (data->>'Test')::numeric", - QueryUtils.orderBy(List.of(Field.named('n:Test')), Dialect.POSTGRESQL)) + assertEquals(" ORDER BY (data->>'Test')::numeric", + QueryUtils.orderBy(List.of(Field.named('n:Test')), Dialect.POSTGRESQL), + 'ORDER BY not constructed correctly') } @Test @DisplayName('orderBy generates numeric ordering | SQLite') void orderByNumericSQLite() { - assertEquals('ORDER BY not constructed correctly', " ORDER BY data->>'Test'", - QueryUtils.orderBy(List.of(Field.named('n:Test')), Dialect.SQLITE)) + assertEquals(" ORDER BY data->>'Test'", QueryUtils.orderBy(List.of(Field.named('n:Test')), Dialect.SQLITE), + 'ORDER BY not constructed correctly') } @Test @DisplayName('orderBy generates case-insensitive ordering | PostgreSQL') void orderByCIPostgres() { - assertEquals('ORDER BY not constructed correctly', " ORDER BY LOWER(data#>>'{Test,Field}') DESC NULLS FIRST", - QueryUtils.orderBy(List.of(Field.named('i:Test.Field DESC NULLS FIRST')), Dialect.POSTGRESQL)) + assertEquals(" ORDER BY LOWER(data#>>'{Test,Field}') DESC NULLS FIRST", + QueryUtils.orderBy(List.of(Field.named('i:Test.Field DESC NULLS FIRST')), Dialect.POSTGRESQL), + 'ORDER BY not constructed correctly') } @Test @DisplayName('orderBy generates case-insensitive ordering | SQLite') void orderByCISQLite() { - assertEquals('ORDER BY not constructed correctly', - " ORDER BY data->'Test'->>'Field' COLLATE NOCASE ASC NULLS LAST", - QueryUtils.orderBy(List.of(Field.named('i:Test.Field ASC NULLS LAST')), Dialect.SQLITE)) + assertEquals(" ORDER BY data->'Test'->>'Field' COLLATE NOCASE ASC NULLS LAST", + QueryUtils.orderBy(List.of(Field.named('i:Test.Field ASC NULLS LAST')), Dialect.SQLITE), + 'ORDER BY not constructed correctly') } } diff --git a/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/query/RemoveFieldsQueryTest.groovy b/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/query/RemoveFieldsQueryTest.groovy index 44acb89..546b06c 100644 --- a/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/query/RemoveFieldsQueryTest.groovy +++ b/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/query/RemoveFieldsQueryTest.groovy @@ -3,7 +3,7 @@ package solutions.bitbadger.documents.groovy.query import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.DisplayName import org.junit.jupiter.api.Test -//import solutions.bitbadger.documents.DocumentException +import solutions.bitbadger.documents.DocumentException import solutions.bitbadger.documents.Field import solutions.bitbadger.documents.Parameter import solutions.bitbadger.documents.ParameterType @@ -11,7 +11,7 @@ import solutions.bitbadger.documents.query.RemoveFieldsQuery import solutions.bitbadger.documents.support.ForceDialect import static solutions.bitbadger.documents.support.TypesKt.TEST_TABLE -import static groovy.test.GroovyAssert.* +import static org.junit.jupiter.api.Assertions.* /** * Unit tests for the `RemoveFields` object @@ -31,77 +31,77 @@ class RemoveFieldsQueryTest { @DisplayName('byId generates correctly | PostgreSQL') void byIdPostgres() { ForceDialect.postgres() - assertEquals('Remove Fields query not constructed correctly', - "UPDATE $TEST_TABLE SET data = data - :name::text[] WHERE data->>'id' = :id".toString(), - RemoveFieldsQuery.byId(TEST_TABLE, List.of(new Parameter(':name', ParameterType.STRING, '{a,z}')))) + assertEquals("UPDATE $TEST_TABLE SET data = data - :name::text[] WHERE data->>'id' = :id".toString(), + RemoveFieldsQuery.byId(TEST_TABLE, List.of(new Parameter(':name', ParameterType.STRING, '{a,z}'))), + 'Remove Fields query not constructed correctly') } @Test @DisplayName('byId generates correctly | SQLite') void byIdSQLite() { ForceDialect.sqlite() - assertEquals('Remove Field query not constructed correctly', + assertEquals( "UPDATE $TEST_TABLE SET data = json_remove(data, :name0, :name1) WHERE data->>'id' = :id".toString(), RemoveFieldsQuery.byId(TEST_TABLE, List.of(new Parameter(':name0', ParameterType.STRING, 'a'), - new Parameter(':name1', ParameterType.STRING, 'z')))) + new Parameter(':name1', ParameterType.STRING, 'z'))), + 'Remove Field query not constructed correctly') } @Test @DisplayName('byFields generates correctly | PostgreSQL') void byFieldsPostgres() { ForceDialect.postgres() - assertEquals('Remove Field query not constructed correctly', - "UPDATE $TEST_TABLE SET data = data - :name::text[] WHERE data->>'f' > :g".toString(), + assertEquals("UPDATE $TEST_TABLE SET data = data - :name::text[] WHERE data->>'f' > :g".toString(), RemoveFieldsQuery.byFields(TEST_TABLE, List.of(new Parameter(':name', ParameterType.STRING, '{b,c}')), - List.of(Field.greater('f', '', ':g')))) + List.of(Field.greater('f', '', ':g'))), + 'Remove Field query not constructed correctly') } @Test @DisplayName('byFields generates correctly | SQLite') void byFieldsSQLite() { ForceDialect.sqlite() - assertEquals('Remove Field query not constructed correctly', - "UPDATE $TEST_TABLE SET data = json_remove(data, :name0, :name1) WHERE data->>'f' > :g".toString(), + assertEquals("UPDATE $TEST_TABLE SET data = json_remove(data, :name0, :name1) WHERE data->>'f' > :g".toString(), RemoveFieldsQuery.byFields(TEST_TABLE, List.of(new Parameter(':name0', ParameterType.STRING, 'b'), new Parameter(':name1', ParameterType.STRING, 'c')), - List.of(Field.greater('f', '', ':g')))) + List.of(Field.greater('f', '', ':g'))), + 'Remove Field query not constructed correctly') } @Test @DisplayName('byContains generates correctly | PostgreSQL') void byContainsPostgres() { ForceDialect.postgres() - assertEquals('Remove Field query not constructed correctly', - "UPDATE $TEST_TABLE SET data = data - :name::text[] WHERE data @> :criteria".toString(), + assertEquals("UPDATE $TEST_TABLE SET data = data - :name::text[] WHERE data @> :criteria".toString(), RemoveFieldsQuery.byContains(TEST_TABLE, - List.of(new Parameter(':name', ParameterType.STRING, '{m,n}')))) + List.of(new Parameter(':name', ParameterType.STRING, '{m,n}'))), + 'Remove Field query not constructed correctly') } -// TODO: resolve java.base open issue -// @Test -// @DisplayName('byContains fails | SQLite') -// void byContainsSQLite() { -// ForceDialect.sqlite() -// assertThrows(DocumentException) { RemoveFieldsQuery.byContains(TEST_TABLE, List.of()) } -// } + @Test + @DisplayName('byContains fails | SQLite') + void byContainsSQLite() { + ForceDialect.sqlite() + assertThrows(DocumentException) { RemoveFieldsQuery.byContains(TEST_TABLE, List.of()) } + } @Test @DisplayName('byJsonPath generates correctly | PostgreSQL') void byJsonPathPostgres() { ForceDialect.postgres() - assertEquals('Remove Field query not constructed correctly', + assertEquals( "UPDATE $TEST_TABLE SET data = data - :name::text[] WHERE jsonb_path_exists(data, :path::jsonpath)" .toString(), RemoveFieldsQuery.byJsonPath(TEST_TABLE, - List.of(new Parameter(':name', ParameterType.STRING, '{o,p}')))) + List.of(new Parameter(':name', ParameterType.STRING, '{o,p}'))), + 'Remove Field query not constructed correctly') } -// TODO: resolve java.base open issue -// @Test -// @DisplayName('byJsonPath fails | SQLite') -// void byJsonPathSQLite() { -// ForceDialect.sqlite() -// assertThrows(DocumentException) { RemoveFieldsQuery.byJsonPath(TEST_TABLE, List.of()) } -// } + @Test + @DisplayName('byJsonPath fails | SQLite') + void byJsonPathSQLite() { + ForceDialect.sqlite() + assertThrows(DocumentException) { RemoveFieldsQuery.byJsonPath(TEST_TABLE, List.of()) } + } } diff --git a/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/query/WhereTest.groovy b/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/query/WhereTest.groovy index d65a115..e8fea87 100644 --- a/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/query/WhereTest.groovy +++ b/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/query/WhereTest.groovy @@ -3,13 +3,13 @@ package solutions.bitbadger.documents.groovy.query import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.DisplayName import org.junit.jupiter.api.Test -//import solutions.bitbadger.documents.DocumentException +import solutions.bitbadger.documents.DocumentException import solutions.bitbadger.documents.Field import solutions.bitbadger.documents.FieldMatch import solutions.bitbadger.documents.query.Where import solutions.bitbadger.documents.support.ForceDialect -import static groovy.test.GroovyAssert.* +import static org.junit.jupiter.api.Assertions.* /** * Unit tests for the `Where` object @@ -28,28 +28,28 @@ class WhereTest { @Test @DisplayName('byFields is blank when given no fields') void byFieldsBlankIfEmpty() { - assertEquals('', Where.byFields(List.of())) + assertEquals '', Where.byFields(List.of()) } @Test @DisplayName('byFields generates one numeric field | PostgreSQL') void byFieldsOneFieldPostgres() { ForceDialect.postgres() - assertEquals("(data->>'it')::numeric = :that", Where.byFields(List.of(Field.equal('it', 9, ':that')))) + assertEquals "(data->>'it')::numeric = :that", Where.byFields(List.of(Field.equal('it', 9, ':that'))) } @Test @DisplayName('byFields generates one alphanumeric field | PostgreSQL') void byFieldsOneAlphaFieldPostgres() { ForceDialect.postgres() - assertEquals("data->>'it' = :that", Where.byFields(List.of(Field.equal('it', '', ':that')))) + assertEquals "data->>'it' = :that", Where.byFields(List.of(Field.equal('it', '', ':that'))) } @Test @DisplayName('byFields generates one field | SQLite') void byFieldsOneFieldSQLite() { ForceDialect.sqlite() - assertEquals("data->>'it' = :that", Where.byFields(List.of(Field.equal('it', '', ':that')))) + assertEquals "data->>'it' = :that", Where.byFields(List.of(Field.equal('it', '', ':that'))) } @Test @@ -98,78 +98,76 @@ class WhereTest { @DisplayName('byId generates defaults for alphanumeric key | PostgreSQL') void byIdDefaultAlphaPostgres() { ForceDialect.postgres() - assertEquals("data->>'id' = :id", Where.byId()) + assertEquals "data->>'id' = :id", Where.byId() } @Test @DisplayName('byId generates defaults for numeric key | PostgreSQL') void byIdDefaultNumericPostgres() { ForceDialect.postgres() - assertEquals("(data->>'id')::numeric = :id", Where.byId(":id", 5)) + assertEquals "(data->>'id')::numeric = :id", Where.byId(":id", 5) } @Test @DisplayName('byId generates defaults | SQLite') void byIdDefaultSQLite() { ForceDialect.sqlite() - assertEquals("data->>'id' = :id", Where.byId()) + assertEquals "data->>'id' = :id", Where.byId() } @Test @DisplayName('byId generates named ID | PostgreSQL') void byIdDefaultNamedPostgres() { ForceDialect.postgres() - assertEquals("data->>'id' = :key", Where.byId(':key')) + assertEquals "data->>'id' = :key", Where.byId(':key') } @Test @DisplayName('byId generates named ID | SQLite') void byIdDefaultNamedSQLite() { ForceDialect.sqlite() - assertEquals("data->>'id' = :key", Where.byId(':key')) + assertEquals "data->>'id' = :key", Where.byId(':key') } @Test @DisplayName('jsonContains generates defaults | PostgreSQL') void jsonContainsDefaultPostgres() { ForceDialect.postgres() - assertEquals('data @> :criteria', Where.jsonContains()) + assertEquals 'data @> :criteria', Where.jsonContains() } @Test @DisplayName('jsonContains generates named parameter | PostgreSQL') void jsonContainsNamedPostgres() { ForceDialect.postgres() - assertEquals('data @> :it', Where.jsonContains(':it')) + assertEquals 'data @> :it', Where.jsonContains(':it') } -// TODO: resolve java.base open issue -// @Test -// @DisplayName('jsonContains fails | SQLite') -// void jsonContainsFailsSQLite() { -// ForceDialect.sqlite() -// assertThrows(DocumentException) { Where.jsonContains() } -// } + @Test + @DisplayName('jsonContains fails | SQLite') + void jsonContainsFailsSQLite() { + ForceDialect.sqlite() + assertThrows(DocumentException) { Where.jsonContains() } + } @Test @DisplayName('jsonPathMatches generates defaults | PostgreSQL') void jsonPathMatchDefaultPostgres() { ForceDialect.postgres() - assertEquals('jsonb_path_exists(data, :path::jsonpath)', Where.jsonPathMatches()) + assertEquals 'jsonb_path_exists(data, :path::jsonpath)', Where.jsonPathMatches() } @Test @DisplayName('jsonPathMatches generates named parameter | PostgreSQL') void jsonPathMatchNamedPostgres() { ForceDialect.postgres() - assertEquals('jsonb_path_exists(data, :jp::jsonpath)', Where.jsonPathMatches(':jp')) + assertEquals 'jsonb_path_exists(data, :jp::jsonpath)', Where.jsonPathMatches(':jp') } -// TODO: resolve java.base open issue -// @Test -// @DisplayName('jsonPathMatches fails | SQLite') -// void jsonPathFailsSQLite() { -// ForceDialect.sqlite() -// assertThrows(DocumentException) { Where.jsonPathMatches() } -// } + @Test + @DisplayName('jsonPathMatches fails | SQLite') + void jsonPathFailsSQLite() { + ForceDialect.sqlite() + assertThrows(DocumentException) { Where.jsonPathMatches() } + } } diff --git a/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/support/JsonDocument.groovy b/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/support/JsonDocument.groovy index 172daf1..4d94e97 100644 --- a/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/support/JsonDocument.groovy +++ b/src/jvm/src/test/groovy/solutions/bitbadger/documents/groovy/support/JsonDocument.groovy @@ -26,8 +26,6 @@ class JsonDocument { new JsonDocument("five", "purple", 18)) static void load(ThrowawayDatabase db, String tableName = TEST_TABLE) { - for (doc in testDocuments) { - Document.insert(tableName, doc, db.conn) - } + testDocuments.forEach { Document.insert(tableName, it, db.conn) } } }