Initial Development #1

Merged
danieljsummers merged 88 commits from v1-rc into main 2025-04-16 01:29:20 +00:00
Showing only changes of commit 27a6cdfa3b - Show all commits

View File

@ -48,6 +48,7 @@ object JsonFunctions {
fun allDefault(db: ThrowawayDatabase) { fun allDefault(db: ThrowawayDatabase) {
JsonDocument.load(db) JsonDocument.load(db)
val json = db.conn.jsonAll(TEST_TABLE) val json = db.conn.jsonAll(TEST_TABLE)
assertTrue(json.startsWith("["), "JSON should start with '[' ($json)")
when (Configuration.dialect()) { when (Configuration.dialect()) {
Dialect.SQLITE -> { Dialect.SQLITE -> {
assertTrue(json.contains(JsonDocument.one), "Document 'one' not found in JSON ($json)") 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.indexOf(docId("five")) >= 0, "Document 'five' not found in JSON ($json)")
} }
} }
assertTrue(json.endsWith("]"), "JSON should end with ']' ($json)")
} }
fun allEmpty(db: ThrowawayDatabase) = fun allEmpty(db: ThrowawayDatabase) =
@ -105,32 +107,48 @@ object JsonFunctions {
TEST_TABLE, listOf(Field.any("value", listOf("blue", "purple")), Field.exists("sub")), FieldMatch.ALL TEST_TABLE, listOf(Field.any("value", listOf("blue", "purple")), Field.exists("sub")), FieldMatch.ALL
) )
when (Configuration.dialect()) { when (Configuration.dialect()) {
Dialect.SQLITE -> assertEquals(JsonDocument.four, json, "The incorrect document was returned") Dialect.SQLITE -> assertEquals("[${JsonDocument.four}]", json, "The incorrect document was returned")
Dialect.POSTGRESQL -> assertTrue( Dialect.POSTGRESQL -> {
json.contains(docId("four")), assertTrue(json.startsWith("["), "JSON should start with '[' ($json)")
"The incorrect document was returned ($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) { fun byFieldsMatchOrdered(db: ThrowawayDatabase) {
JsonDocument.load(db) JsonDocument.load(db)
assertEquals( val json = db.conn.jsonByFields(
maybeJsonB("[${JsonDocument.five},${JsonDocument.four}]"), db.conn.jsonByFields(
TEST_TABLE, listOf(Field.equal("value", "purple")), orderBy = listOf(Field.named("id")) TEST_TABLE, listOf(Field.equal("value", "purple")), orderBy = listOf(Field.named("id"))
), "The documents were not ordered correctly"
) )
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) { fun byFieldsMatchNumIn(db: ThrowawayDatabase) {
JsonDocument.load(db) JsonDocument.load(db)
assertEquals( val json = db.conn.jsonByFields(TEST_TABLE, listOf(Field.any("numValue", listOf(2, 4, 6, 8))))
maybeJsonB("[${JsonDocument.three}]"), db.conn.jsonByFields( when (Configuration.dialect()) {
TEST_TABLE, listOf(Field.any("numValue", listOf(2, 4, 6, 8))) Dialect.SQLITE -> assertEquals("[${JsonDocument.three}]", json, "The incorrect document was returned")
), "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) { fun byFieldsNoMatch(db: ThrowawayDatabase) {
@ -144,8 +162,10 @@ object JsonFunctions {
fun byFieldsMatchInArray(db: ThrowawayDatabase) { fun byFieldsMatchInArray(db: ThrowawayDatabase) {
ArrayDocument.testDocuments.forEach { db.conn.insert(TEST_TABLE, it) } ArrayDocument.testDocuments.forEach { db.conn.insert(TEST_TABLE, it) }
val json = db.conn.jsonByFields(TEST_TABLE, listOf(Field.inArray("values", TEST_TABLE, listOf("c")))) 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.startsWith("["), "JSON should start with '[' ($json)")
assertTrue(json.contains(maybeJsonB("{\"id\":\"second\"")), "The 'second' document was not found ($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) { fun byFieldsNoMatchInArray(db: ThrowawayDatabase) {
@ -160,17 +180,39 @@ object JsonFunctions {
fun byContainsMatch(db: ThrowawayDatabase) { fun byContainsMatch(db: ThrowawayDatabase) {
JsonDocument.load(db) JsonDocument.load(db)
val json = db.conn.jsonByContains(TEST_TABLE, mapOf("value" to "purple")) val json = db.conn.jsonByContains(TEST_TABLE, mapOf("value" to "purple"))
assertTrue(json.contains(maybeJsonB(JsonDocument.four)), "Document 'four' not found ($json)") assertTrue(json.startsWith("["), "JSON should start with '[' ($json)")
assertTrue(json.contains(maybeJsonB(JsonDocument.five)), "Document 'five' not found ($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) { fun byContainsMatchOrdered(db: ThrowawayDatabase) {
JsonDocument.load(db) JsonDocument.load(db)
assertEquals( val json = db.conn.jsonByContains(
maybeJsonB("[${JsonDocument.two},${JsonDocument.four}]"), db.conn.jsonByContains(
TEST_TABLE, mapOf("sub" to mapOf("foo" to "green")), listOf(Field.named("value")) TEST_TABLE, mapOf("sub" to mapOf("foo" to "green")), listOf(Field.named("value"))
), "The documents were not ordered correctly"
) )
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) { fun byContainsNoMatch(db: ThrowawayDatabase) {
@ -185,18 +227,38 @@ object JsonFunctions {
fun byJsonPathMatch(db: ThrowawayDatabase) { fun byJsonPathMatch(db: ThrowawayDatabase) {
JsonDocument.load(db) JsonDocument.load(db)
val json = db.conn.jsonByJsonPath(TEST_TABLE, "$.numValue ? (@ > 10)") val json = db.conn.jsonByJsonPath(TEST_TABLE, "$.numValue ? (@ > 10)")
assertTrue(json.contains(maybeJsonB(JsonDocument.four)), "Document 'four' not found ($json)") assertTrue(json.startsWith("["), "JSON should start with '[' ($json)")
assertTrue(json.contains(maybeJsonB(JsonDocument.five)), "Document 'five' not found ($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) { fun byJsonPathMatchOrdered(db: ThrowawayDatabase) {
JsonDocument.load(db) JsonDocument.load(db)
val json = db.conn.jsonByJsonPath(TEST_TABLE, "$.numValue ? (@ > 10)", listOf(Field.named("id"))) val json = db.conn.jsonByJsonPath(TEST_TABLE, "$.numValue ? (@ > 10)", listOf(Field.named("id")))
when (Configuration.dialect()) {
Dialect.SQLITE -> assertEquals(
"[${JsonDocument.five},${JsonDocument.four}]", json, "The documents were not ordered correctly"
)
Dialect.POSTGRESQL -> {
val fiveIdx = json.indexOf(docId("five")) val fiveIdx = json.indexOf(docId("five"))
val fourIdx = json.indexOf(docId("four")) val fourIdx = json.indexOf(docId("four"))
assertTrue(json.startsWith("["), "JSON should start with '[' ($json)")
assertTrue(fiveIdx >= 0, "Document 'five' not found ($json)") assertTrue(fiveIdx >= 0, "Document 'five' not found ($json)")
assertTrue(fourIdx >= 0, "Document 'four' not found ($json)") assertTrue(fourIdx >= 0, "Document 'four' not found ($json)")
assertTrue(fiveIdx < fourIdx, "Document 'five' should have been before 'four' ($json)") assertTrue(fiveIdx < fourIdx, "Document 'five' should have been before 'four' ($json)")
assertTrue(json.endsWith("]"), "JSON should end with ']' ($json)")
}
}
} }
fun byJsonPathNoMatch(db: ThrowawayDatabase) { fun byJsonPathNoMatch(db: ThrowawayDatabase) {
@ -211,25 +273,36 @@ object JsonFunctions {
fun firstByFieldsMatchOne(db: ThrowawayDatabase) { fun firstByFieldsMatchOne(db: ThrowawayDatabase) {
JsonDocument.load(db) JsonDocument.load(db)
val json = db.conn.jsonFirstByFields(TEST_TABLE, listOf(Field.equal("value", "another"))) 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) { fun firstByFieldsMatchMany(db: ThrowawayDatabase) {
JsonDocument.load(db) JsonDocument.load(db)
val json = db.conn.jsonFirstByFields(TEST_TABLE, listOf(Field.equal("sub.foo", "green"))) val json = db.conn.jsonFirstByFields(TEST_TABLE, listOf(Field.equal("sub.foo", "green")))
assertTrue( when (Configuration.dialect()) {
json.contains(maybeJsonB(JsonDocument.two)) || json.contains(maybeJsonB(JsonDocument.four)), Dialect.SQLITE -> assertTrue(
json.contains(JsonDocument.two) || json.contains(JsonDocument.four),
"Expected document 'two' or 'four' ($json)" "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) { fun firstByFieldsMatchOrdered(db: ThrowawayDatabase) {
JsonDocument.load(db) JsonDocument.load(db)
assertEquals( val json = db.conn.jsonFirstByFields(
maybeJsonB(JsonDocument.four), db.conn.jsonFirstByFields(
TEST_TABLE, listOf(Field.equal("sub.foo", "green")), orderBy = listOf(Field.named("n:numValue DESC")) TEST_TABLE, listOf(Field.equal("sub.foo", "green")), orderBy = listOf(Field.named("n:numValue DESC"))
), "An incorrect document was returned"
) )
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) { fun firstByFieldsNoMatch(db: ThrowawayDatabase) {
@ -243,29 +316,37 @@ object JsonFunctions {
fun firstByContainsMatchOne(db: ThrowawayDatabase) { fun firstByContainsMatchOne(db: ThrowawayDatabase) {
JsonDocument.load(db) JsonDocument.load(db)
assertEquals( val json = db.conn.jsonFirstByContains(TEST_TABLE, mapOf("value" to "FIRST!"))
maybeJsonB(JsonDocument.one), when (Configuration.dialect()) {
db.conn.jsonFirstByContains(TEST_TABLE, mapOf("value" to "FIRST!")), Dialect.SQLITE -> assertEquals(JsonDocument.one, json, "An incorrect document was returned")
"An incorrect document was returned" Dialect.POSTGRESQL -> assertTrue(json.contains(docId("one")), "An incorrect document was returned ($json)")
) }
} }
fun firstByContainsMatchMany(db: ThrowawayDatabase) { fun firstByContainsMatchMany(db: ThrowawayDatabase) {
JsonDocument.load(db) JsonDocument.load(db)
val json = db.conn.jsonFirstByContains(TEST_TABLE, mapOf("value" to "purple")) val json = db.conn.jsonFirstByContains(TEST_TABLE, mapOf("value" to "purple"))
assertTrue( when (Configuration.dialect()) {
json.contains(maybeJsonB(JsonDocument.four)) || json.contains(maybeJsonB(JsonDocument.five)), Dialect.SQLITE -> assertTrue(
json.contains(JsonDocument.four) || json.contains(JsonDocument.five),
"Expected document 'four' or 'five' ($json)" "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) { fun firstByContainsMatchOrdered(db: ThrowawayDatabase) {
JsonDocument.load(db) JsonDocument.load(db)
assertEquals( val json = db.conn.jsonFirstByContains(
maybeJsonB(JsonDocument.five), db.conn.jsonFirstByContains(
TEST_TABLE, mapOf("value" to "purple"), listOf(Field.named("sub.bar NULLS FIRST")) TEST_TABLE, mapOf("value" to "purple"), listOf(Field.named("sub.bar NULLS FIRST"))
), "An incorrect document was returned"
) )
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) { fun firstByContainsNoMatch(db: ThrowawayDatabase) {
@ -279,29 +360,35 @@ object JsonFunctions {
fun firstByJsonPathMatchOne(db: ThrowawayDatabase) { fun firstByJsonPathMatchOne(db: ThrowawayDatabase) {
JsonDocument.load(db) JsonDocument.load(db)
assertEquals( val json = db.conn.jsonFirstByJsonPath(TEST_TABLE, "$.numValue ? (@ == 10)")
maybeJsonB(JsonDocument.two), when (Configuration.dialect()) {
db.conn.jsonFirstByJsonPath(TEST_TABLE, "$.numValue ? (@ == 10)"), Dialect.SQLITE -> assertEquals(JsonDocument.two, json, "An incorrect document was returned")
"An incorrect document was returned" Dialect.POSTGRESQL -> assertTrue(json.contains(docId("two")), "An incorrect document was returned ($json)")
) }
} }
fun firstByJsonPathMatchMany(db: ThrowawayDatabase) { fun firstByJsonPathMatchMany(db: ThrowawayDatabase) {
JsonDocument.load(db) JsonDocument.load(db)
val json = db.conn.jsonFirstByJsonPath(TEST_TABLE, "$.numValue ? (@ > 10)") val json = db.conn.jsonFirstByJsonPath(TEST_TABLE, "$.numValue ? (@ > 10)")
assertTrue( when (Configuration.dialect()) {
json.contains(maybeJsonB(JsonDocument.four)) || json.contains(maybeJsonB(JsonDocument.five)), Dialect.SQLITE -> assertTrue(
json.contains(JsonDocument.four) || json.contains(JsonDocument.five),
"Expected document 'four' or 'five' ($json)" "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) { fun firstByJsonPathMatchOrdered(db: ThrowawayDatabase) {
JsonDocument.load(db) JsonDocument.load(db)
assertEquals( val json = db.conn.jsonFirstByJsonPath(TEST_TABLE, "$.numValue ? (@ > 10)", listOf(Field.named("id DESC")))
maybeJsonB(JsonDocument.four), when (Configuration.dialect()) {
db.conn.jsonFirstByJsonPath(TEST_TABLE, "$.numValue ? (@ > 10)", listOf(Field.named("id DESC"))), Dialect.SQLITE -> assertEquals(JsonDocument.four, json, "An incorrect document was returned")
"An incorrect document was returned" Dialect.POSTGRESQL -> assertTrue(json.contains(docId("four")), "An incorrect document was returned ($json)")
) }
} }
fun firstByJsonPathNoMatch(db: ThrowawayDatabase) { fun firstByJsonPathNoMatch(db: ThrowawayDatabase) {