From 27a6cdfa3b6b1a4668206b8a4957ca492e0697f7 Mon Sep 17 00:00:00 2001 From: "Daniel J. Summers" Date: Fri, 28 Mar 2025 12:15:34 -0400 Subject: [PATCH] Fix JSON tests by implementation --- .../test/kotlin/integration/JsonFunctions.kt | 221 ++++++++++++------ 1 file changed, 154 insertions(+), 67 deletions(-) diff --git a/src/core/src/test/kotlin/integration/JsonFunctions.kt b/src/core/src/test/kotlin/integration/JsonFunctions.kt index 8b27176..77de5b8 100644 --- a/src/core/src/test/kotlin/integration/JsonFunctions.kt +++ b/src/core/src/test/kotlin/integration/JsonFunctions.kt @@ -48,6 +48,7 @@ object JsonFunctions { fun allDefault(db: ThrowawayDatabase) { JsonDocument.load(db) val json = db.conn.jsonAll(TEST_TABLE) + assertTrue(json.startsWith("["), "JSON should start with '[' ($json)") when (Configuration.dialect()) { Dialect.SQLITE -> { assertTrue(json.contains(JsonDocument.one), "Document 'one' not found in JSON ($json)") @@ -64,6 +65,7 @@ object JsonFunctions { assertTrue(json.indexOf(docId("five")) >= 0, "Document 'five' not found in JSON ($json)") } } + assertTrue(json.endsWith("]"), "JSON should end with ']' ($json)") } fun allEmpty(db: ThrowawayDatabase) = @@ -105,32 +107,48 @@ object JsonFunctions { TEST_TABLE, listOf(Field.any("value", listOf("blue", "purple")), Field.exists("sub")), FieldMatch.ALL ) when (Configuration.dialect()) { - Dialect.SQLITE -> assertEquals(JsonDocument.four, json, "The incorrect document was returned") - Dialect.POSTGRESQL -> assertTrue( - json.contains(docId("four")), - "The incorrect document was returned ($json)" - ) + Dialect.SQLITE -> assertEquals("[${JsonDocument.four}]", json, "The incorrect document was returned") + Dialect.POSTGRESQL -> { + assertTrue(json.startsWith("["), "JSON should start with '[' ($json)") + assertTrue(json.contains(docId("four")),"The incorrect document was returned ($json)") + assertTrue(json.endsWith("]"), "JSON should end with ']' ($json)") + } } } - // TODO: stopped here with Postgres/SQLite split - fun byFieldsMatchOrdered(db: ThrowawayDatabase) { JsonDocument.load(db) - assertEquals( - maybeJsonB("[${JsonDocument.five},${JsonDocument.four}]"), db.conn.jsonByFields( - TEST_TABLE, listOf(Field.equal("value", "purple")), orderBy = listOf(Field.named("id")) - ), "The documents were not ordered correctly" + val json = db.conn.jsonByFields( + TEST_TABLE, listOf(Field.equal("value", "purple")), orderBy = listOf(Field.named("id")) ) + when (Configuration.dialect()) { + Dialect.SQLITE -> assertEquals( + "[${JsonDocument.five},${JsonDocument.four}]", json, "The documents were not ordered correctly" + ) + + Dialect.POSTGRESQL -> { + val fiveIdx = json.indexOf(docId("five")) + val fourIdx = json.indexOf(docId("four")) + assertTrue(json.startsWith("["), "JSON should start with '[' ($json)") + assertTrue(fiveIdx >= 0, "Document 'five' not found ($json)") + assertTrue(fourIdx >= 0, "Document 'four' not found ($json)") + assertTrue(fiveIdx < fourIdx, "Document 'five' should have been before 'four' ($json)") + assertTrue(json.endsWith("]"), "JSON should end with ']' ($json)") + } + } } fun byFieldsMatchNumIn(db: ThrowawayDatabase) { JsonDocument.load(db) - assertEquals( - maybeJsonB("[${JsonDocument.three}]"), db.conn.jsonByFields( - TEST_TABLE, listOf(Field.any("numValue", listOf(2, 4, 6, 8))) - ), "The incorrect document was returned" - ) + val json = db.conn.jsonByFields(TEST_TABLE, listOf(Field.any("numValue", listOf(2, 4, 6, 8)))) + when (Configuration.dialect()) { + Dialect.SQLITE -> assertEquals("[${JsonDocument.three}]", json, "The incorrect document was returned") + Dialect.POSTGRESQL -> { + assertTrue(json.startsWith("["), "JSON should start with '[' ($json)") + assertTrue(json.contains(docId("three")), "The incorrect document was returned ($json)") + assertTrue(json.endsWith("]"), "JSON should end with ']' ($json)") + } + } } fun byFieldsNoMatch(db: ThrowawayDatabase) { @@ -144,8 +162,10 @@ object JsonFunctions { fun byFieldsMatchInArray(db: ThrowawayDatabase) { ArrayDocument.testDocuments.forEach { db.conn.insert(TEST_TABLE, it) } val json = db.conn.jsonByFields(TEST_TABLE, listOf(Field.inArray("values", TEST_TABLE, listOf("c")))) - assertTrue(json.contains(maybeJsonB("{\"id\":\"first\"")), "The 'first' document was not found ($json)") - assertTrue(json.contains(maybeJsonB("{\"id\":\"second\"")), "The 'second' document was not found ($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)") } fun byFieldsNoMatchInArray(db: ThrowawayDatabase) { @@ -160,17 +180,39 @@ object JsonFunctions { fun byContainsMatch(db: ThrowawayDatabase) { JsonDocument.load(db) val json = db.conn.jsonByContains(TEST_TABLE, mapOf("value" to "purple")) - assertTrue(json.contains(maybeJsonB(JsonDocument.four)), "Document 'four' not found ($json)") - assertTrue(json.contains(maybeJsonB(JsonDocument.five)), "Document 'five' not found ($json)") + assertTrue(json.startsWith("["), "JSON should start with '[' ($json)") + when (Configuration.dialect()) { + Dialect.SQLITE -> { + assertTrue(json.contains(JsonDocument.four), "Document 'four' not found ($json)") + assertTrue(json.contains(JsonDocument.five), "Document 'five' not found ($json)") + } + Dialect.POSTGRESQL -> { + assertTrue(json.contains(docId("four")), "Document 'four' not found ($json)") + assertTrue(json.contains(docId("five")), "Document 'five' not found ($json)") + } + } + assertTrue(json.endsWith("]"), "JSON should end with ']' ($json)") } fun byContainsMatchOrdered(db: ThrowawayDatabase) { JsonDocument.load(db) - assertEquals( - maybeJsonB("[${JsonDocument.two},${JsonDocument.four}]"), db.conn.jsonByContains( - TEST_TABLE, mapOf("sub" to mapOf("foo" to "green")), listOf(Field.named("value")) - ), "The documents were not ordered correctly" + val json = db.conn.jsonByContains( + TEST_TABLE, mapOf("sub" to mapOf("foo" to "green")), listOf(Field.named("value")) ) + when (Configuration.dialect()) { + Dialect.SQLITE -> assertEquals( + "[${JsonDocument.two},${JsonDocument.four}]", json, "The documents were not ordered correctly" + ) + Dialect.POSTGRESQL -> { + val twoIdx = json.indexOf(docId("two")) + val fourIdx = json.indexOf(docId("four")) + assertTrue(json.startsWith("["), "JSON should start with '[' ($json)") + assertTrue(twoIdx >= 0, "Document 'two' not found ($json)") + assertTrue(fourIdx >= 0, "Document 'four' not found ($json)") + assertTrue(twoIdx < fourIdx, "Document 'two' should have been before 'four' ($json)") + assertTrue(json.endsWith("]"), "JSON should end with ']' ($json)") + } + } } fun byContainsNoMatch(db: ThrowawayDatabase) { @@ -185,18 +227,38 @@ object JsonFunctions { fun byJsonPathMatch(db: ThrowawayDatabase) { JsonDocument.load(db) val json = db.conn.jsonByJsonPath(TEST_TABLE, "$.numValue ? (@ > 10)") - assertTrue(json.contains(maybeJsonB(JsonDocument.four)), "Document 'four' not found ($json)") - assertTrue(json.contains(maybeJsonB(JsonDocument.five)), "Document 'five' not found ($json)") + assertTrue(json.startsWith("["), "JSON should start with '[' ($json)") + when (Configuration.dialect()) { + Dialect.SQLITE -> { + assertTrue(json.contains(JsonDocument.four), "Document 'four' not found ($json)") + assertTrue(json.contains(JsonDocument.five), "Document 'five' not found ($json)") + } + Dialect.POSTGRESQL -> { + assertTrue(json.contains(docId("four")), "Document 'four' not found ($json)") + assertTrue(json.contains(docId("five")), "Document 'five' not found ($json)") + } + } + assertTrue(json.endsWith("]"), "JSON should end with ']' ($json)") } fun byJsonPathMatchOrdered(db: ThrowawayDatabase) { JsonDocument.load(db) val json = db.conn.jsonByJsonPath(TEST_TABLE, "$.numValue ? (@ > 10)", listOf(Field.named("id"))) - val fiveIdx = json.indexOf(docId("five")) - val fourIdx = json.indexOf(docId("four")) - assertTrue(fiveIdx >= 0, "Document 'five' not found ($json)") - assertTrue(fourIdx >= 0, "Document 'four' not found ($json)") - assertTrue(fiveIdx < fourIdx, "Document 'five' should have been before 'four' ($json)") + when (Configuration.dialect()) { + Dialect.SQLITE -> assertEquals( + "[${JsonDocument.five},${JsonDocument.four}]", json, "The documents were not ordered correctly" + ) + + Dialect.POSTGRESQL -> { + val fiveIdx = json.indexOf(docId("five")) + val fourIdx = json.indexOf(docId("four")) + assertTrue(json.startsWith("["), "JSON should start with '[' ($json)") + assertTrue(fiveIdx >= 0, "Document 'five' not found ($json)") + assertTrue(fourIdx >= 0, "Document 'four' not found ($json)") + assertTrue(fiveIdx < fourIdx, "Document 'five' should have been before 'four' ($json)") + assertTrue(json.endsWith("]"), "JSON should end with ']' ($json)") + } + } } fun byJsonPathNoMatch(db: ThrowawayDatabase) { @@ -211,25 +273,36 @@ object JsonFunctions { fun firstByFieldsMatchOne(db: ThrowawayDatabase) { JsonDocument.load(db) val json = db.conn.jsonFirstByFields(TEST_TABLE, listOf(Field.equal("value", "another"))) - assertTrue(json.contains(docId("two")), "The incorrect document was returned") + when (Configuration.dialect()) { + Dialect.SQLITE -> assertEquals(JsonDocument.two, json, "The incorrect document was returned") + Dialect.POSTGRESQL -> assertTrue(json.contains(docId("two")), "The incorrect document was returned ($json)") + } } fun firstByFieldsMatchMany(db: ThrowawayDatabase) { JsonDocument.load(db) val json = db.conn.jsonFirstByFields(TEST_TABLE, listOf(Field.equal("sub.foo", "green"))) - assertTrue( - json.contains(maybeJsonB(JsonDocument.two)) || json.contains(maybeJsonB(JsonDocument.four)), - "Expected document 'two' or 'four' ($json)" - ) + when (Configuration.dialect()) { + Dialect.SQLITE -> assertTrue( + json.contains(JsonDocument.two) || json.contains(JsonDocument.four), + "Expected document 'two' or 'four' ($json)" + ) + Dialect.POSTGRESQL -> assertTrue( + json.contains(docId("two")) || json.contains(docId("four")), + "Expected document 'two' or 'four' ($json)" + ) + } } fun firstByFieldsMatchOrdered(db: ThrowawayDatabase) { JsonDocument.load(db) - assertEquals( - maybeJsonB(JsonDocument.four), db.conn.jsonFirstByFields( - TEST_TABLE, listOf(Field.equal("sub.foo", "green")), orderBy = listOf(Field.named("n:numValue DESC")) - ), "An incorrect document was returned" + val json = db.conn.jsonFirstByFields( + TEST_TABLE, listOf(Field.equal("sub.foo", "green")), orderBy = listOf(Field.named("n:numValue DESC")) ) + when (Configuration.dialect()) { + Dialect.SQLITE -> assertEquals(JsonDocument.four, json, "An incorrect document was returned") + Dialect.POSTGRESQL -> assertTrue(json.contains(docId("four")), "An incorrect document was returned ($json)") + } } fun firstByFieldsNoMatch(db: ThrowawayDatabase) { @@ -243,29 +316,37 @@ object JsonFunctions { fun firstByContainsMatchOne(db: ThrowawayDatabase) { JsonDocument.load(db) - assertEquals( - maybeJsonB(JsonDocument.one), - db.conn.jsonFirstByContains(TEST_TABLE, mapOf("value" to "FIRST!")), - "An incorrect document was returned" - ) + val json = db.conn.jsonFirstByContains(TEST_TABLE, mapOf("value" to "FIRST!")) + when (Configuration.dialect()) { + Dialect.SQLITE -> assertEquals(JsonDocument.one, json, "An incorrect document was returned") + Dialect.POSTGRESQL -> assertTrue(json.contains(docId("one")), "An incorrect document was returned ($json)") + } } fun firstByContainsMatchMany(db: ThrowawayDatabase) { JsonDocument.load(db) val json = db.conn.jsonFirstByContains(TEST_TABLE, mapOf("value" to "purple")) - assertTrue( - json.contains(maybeJsonB(JsonDocument.four)) || json.contains(maybeJsonB(JsonDocument.five)), - "Expected document 'four' or 'five' ($json)" - ) + when (Configuration.dialect()) { + Dialect.SQLITE -> assertTrue( + json.contains(JsonDocument.four) || json.contains(JsonDocument.five), + "Expected document 'four' or 'five' ($json)" + ) + Dialect.POSTGRESQL -> assertTrue( + json.contains(docId("four")) || json.contains(docId("five")), + "Expected document 'four' or 'five' ($json)" + ) + } } fun firstByContainsMatchOrdered(db: ThrowawayDatabase) { JsonDocument.load(db) - assertEquals( - maybeJsonB(JsonDocument.five), db.conn.jsonFirstByContains( - TEST_TABLE, mapOf("value" to "purple"), listOf(Field.named("sub.bar NULLS FIRST")) - ), "An incorrect document was returned" + val json = db.conn.jsonFirstByContains( + TEST_TABLE, mapOf("value" to "purple"), listOf(Field.named("sub.bar NULLS FIRST")) ) + when (Configuration.dialect()) { + Dialect.SQLITE -> assertEquals(JsonDocument.five, json, "An incorrect document was returned") + Dialect.POSTGRESQL -> assertTrue(json.contains(docId("five")), "An incorrect document was returned ($json)") + } } fun firstByContainsNoMatch(db: ThrowawayDatabase) { @@ -279,29 +360,35 @@ object JsonFunctions { fun firstByJsonPathMatchOne(db: ThrowawayDatabase) { JsonDocument.load(db) - assertEquals( - maybeJsonB(JsonDocument.two), - db.conn.jsonFirstByJsonPath(TEST_TABLE, "$.numValue ? (@ == 10)"), - "An incorrect document was returned" - ) + val json = db.conn.jsonFirstByJsonPath(TEST_TABLE, "$.numValue ? (@ == 10)") + when (Configuration.dialect()) { + Dialect.SQLITE -> assertEquals(JsonDocument.two, json, "An incorrect document was returned") + Dialect.POSTGRESQL -> assertTrue(json.contains(docId("two")), "An incorrect document was returned ($json)") + } } fun firstByJsonPathMatchMany(db: ThrowawayDatabase) { JsonDocument.load(db) val json = db.conn.jsonFirstByJsonPath(TEST_TABLE, "$.numValue ? (@ > 10)") - assertTrue( - json.contains(maybeJsonB(JsonDocument.four)) || json.contains(maybeJsonB(JsonDocument.five)), - "Expected document 'four' or 'five' ($json)" - ) + when (Configuration.dialect()) { + Dialect.SQLITE -> assertTrue( + json.contains(JsonDocument.four) || json.contains(JsonDocument.five), + "Expected document 'four' or 'five' ($json)" + ) + Dialect.POSTGRESQL -> assertTrue( + json.contains(docId("four")) || json.contains(docId("five")), + "Expected document 'four' or 'five' ($json)" + ) + } } fun firstByJsonPathMatchOrdered(db: ThrowawayDatabase) { JsonDocument.load(db) - assertEquals( - maybeJsonB(JsonDocument.four), - db.conn.jsonFirstByJsonPath(TEST_TABLE, "$.numValue ? (@ > 10)", listOf(Field.named("id DESC"))), - "An incorrect document was returned" - ) + val json = db.conn.jsonFirstByJsonPath(TEST_TABLE, "$.numValue ? (@ > 10)", listOf(Field.named("id DESC"))) + when (Configuration.dialect()) { + Dialect.SQLITE -> assertEquals(JsonDocument.four, json, "An incorrect document was returned") + Dialect.POSTGRESQL -> assertTrue(json.contains(docId("four")), "An incorrect document was returned ($json)") + } } fun firstByJsonPathNoMatch(db: ThrowawayDatabase) {