Initial Development #1

Merged
danieljsummers merged 88 commits from v1-rc into main 2025-04-16 01:29:20 +00:00
5 changed files with 39 additions and 43 deletions
Showing only changes of commit ad3b7d2316 - Show all commits

View File

@ -40,4 +40,4 @@ data class JsonDocument(val id: String, val value: String, val numValue: Int, va
} }
/** The test table name to use for integration tests */ /** The test table name to use for integration tests */
val testTableName = "test_table" const val TEST_TABLE = "test_table"

View File

@ -14,66 +14,66 @@ import kotlin.test.assertNull
object Custom { object Custom {
fun listEmpty(conn: Connection) { fun listEmpty(conn: Connection) {
JsonDocument.load(conn, testTableName) JsonDocument.load(conn, TEST_TABLE)
conn.customNonQuery("DELETE FROM $testTableName") conn.customNonQuery("DELETE FROM $TEST_TABLE")
val result = conn.customList<JsonDocument>(Find.all(testTableName), mapFunc = Results::fromData) val result = conn.customList<JsonDocument>(Find.all(TEST_TABLE), mapFunc = Results::fromData)
assertEquals(0, result.size, "There should have been no results") assertEquals(0, result.size, "There should have been no results")
} }
fun listAll(conn: Connection) { fun listAll(conn: Connection) {
JsonDocument.load(conn, testTableName) JsonDocument.load(conn, TEST_TABLE)
val result = conn.customList<JsonDocument>(Find.all(testTableName), mapFunc = Results::fromData) val result = conn.customList<JsonDocument>(Find.all(TEST_TABLE), mapFunc = Results::fromData)
assertEquals(5, result.size, "There should have been 5 results") assertEquals(5, result.size, "There should have been 5 results")
} }
fun singleNone(conn: Connection) = fun singleNone(conn: Connection) =
assertNull( assertNull(
conn.customSingle(Find.all(testTableName), mapFunc = Results::fromData), conn.customSingle(Find.all(TEST_TABLE), mapFunc = Results::fromData),
"There should not have been a document returned" "There should not have been a document returned"
) )
fun singleOne(conn: Connection) { fun singleOne(conn: Connection) {
JsonDocument.load(conn, testTableName) JsonDocument.load(conn, TEST_TABLE)
assertNotNull( assertNotNull(
conn.customSingle<JsonDocument>(Find.all(testTableName), mapFunc = Results::fromData), conn.customSingle<JsonDocument>(Find.all(TEST_TABLE), mapFunc = Results::fromData),
"There should not have been a document returned" "There should not have been a document returned"
) )
} }
fun nonQueryChanges(conn: Connection) { fun nonQueryChanges(conn: Connection) {
JsonDocument.load(conn, testTableName) JsonDocument.load(conn, TEST_TABLE)
assertEquals( assertEquals(
5L, conn.customScalar(Count.all(testTableName), mapFunc = Results::toCount), 5L, conn.customScalar(Count.all(TEST_TABLE), mapFunc = Results::toCount),
"There should have been 5 documents in the table" "There should have been 5 documents in the table"
) )
conn.customNonQuery("DELETE FROM $testTableName") conn.customNonQuery("DELETE FROM $TEST_TABLE")
assertEquals( assertEquals(
0L, conn.customScalar(Count.all(testTableName), mapFunc = Results::toCount), 0L, conn.customScalar(Count.all(TEST_TABLE), mapFunc = Results::toCount),
"There should have been no documents in the table" "There should have been no documents in the table"
) )
} }
fun nonQueryNoChanges(conn: Connection) { fun nonQueryNoChanges(conn: Connection) {
JsonDocument.load(conn, testTableName) JsonDocument.load(conn, TEST_TABLE)
assertEquals( assertEquals(
5L, conn.customScalar(Count.all(testTableName), mapFunc = Results::toCount), 5L, conn.customScalar(Count.all(TEST_TABLE), mapFunc = Results::toCount),
"There should have been 5 documents in the table" "There should have been 5 documents in the table"
) )
conn.customNonQuery( conn.customNonQuery(
"DELETE FROM $testTableName WHERE data->>'id' = :id", "DELETE FROM $TEST_TABLE WHERE data->>'id' = :id",
listOf(Parameter(":id", ParameterType.STRING, "eighty-two")) listOf(Parameter(":id", ParameterType.STRING, "eighty-two"))
) )
assertEquals( assertEquals(
5L, conn.customScalar(Count.all(testTableName), mapFunc = Results::toCount), 5L, conn.customScalar(Count.all(TEST_TABLE), mapFunc = Results::toCount),
"There should still have been 5 documents in the table" "There should still have been 5 documents in the table"
) )
} }
fun scalar(conn: Connection) { fun scalar(conn: Connection) {
JsonDocument.load(conn, testTableName) JsonDocument.load(conn, TEST_TABLE)
assertEquals( assertEquals(
3L, 3L,
conn.customScalar("SELECT 3 AS it FROM $testTableName LIMIT 1", mapFunc = Results::toCount), conn.customScalar("SELECT 3 AS it FROM $TEST_TABLE LIMIT 1", mapFunc = Results::toCount),
"The number 3 should have been returned" "The number 3 should have been returned"
) )
} }

View File

@ -1,8 +1,6 @@
package solutions.bitbadger.documents.common package solutions.bitbadger.documents.common
import org.junit.jupiter.api.DisplayName
import solutions.bitbadger.documents.* import solutions.bitbadger.documents.*
import solutions.bitbadger.documents.sqlite.SQLiteDB
import java.sql.Connection import java.sql.Connection
import kotlin.test.assertEquals import kotlin.test.assertEquals
import kotlin.test.fail import kotlin.test.fail
@ -13,18 +11,18 @@ import kotlin.test.fail
object Document { object Document {
fun insertDefault(conn: Connection) { fun insertDefault(conn: Connection) {
assertEquals(0L, conn.countAll(testTableName), "There should be no documents in the table") assertEquals(0L, conn.countAll(TEST_TABLE), "There should be no documents in the table")
val doc = JsonDocument("turkey", "", 0, SubDocument("gobble", "gobble")) val doc = JsonDocument("turkey", "", 0, SubDocument("gobble", "gobble"))
conn.insert(testTableName, doc) conn.insert(TEST_TABLE, doc)
val after = conn.findAll<JsonDocument>(testTableName) val after = conn.findAll<JsonDocument>(TEST_TABLE)
assertEquals(1, after.size, "There should be one document in the table") assertEquals(1, after.size, "There should be one document in the table")
assertEquals(doc, after[0], "The document should be what was inserted") assertEquals(doc, after[0], "The document should be what was inserted")
} }
fun insertDupe(conn: Connection) { fun insertDupe(conn: Connection) {
conn.insert(testTableName, JsonDocument("a", "", 0, null)) conn.insert(TEST_TABLE, JsonDocument("a", "", 0, null))
try { try {
conn.insert(testTableName, JsonDocument("a", "b", 22, null)) conn.insert(TEST_TABLE, JsonDocument("a", "b", 22, null))
fail("Inserting a document with a duplicate key should have thrown an exception") fail("Inserting a document with a duplicate key should have thrown an exception")
} catch (_: Exception) { } catch (_: Exception) {
// yay // yay
@ -35,14 +33,14 @@ object Document {
try { try {
Configuration.autoIdStrategy = AutoId.NUMBER Configuration.autoIdStrategy = AutoId.NUMBER
Configuration.idField = "key" Configuration.idField = "key"
assertEquals(0L, conn.countAll(SQLiteDB.tableName), "There should be no documents in the table") assertEquals(0L, conn.countAll(TEST_TABLE), "There should be no documents in the table")
conn.insert(SQLiteDB.tableName, NumIdDocument(0, "one")) conn.insert(TEST_TABLE, NumIdDocument(0, "one"))
conn.insert(SQLiteDB.tableName, NumIdDocument(0, "two")) conn.insert(TEST_TABLE, NumIdDocument(0, "two"))
conn.insert(SQLiteDB.tableName, NumIdDocument(77, "three")) conn.insert(TEST_TABLE, NumIdDocument(77, "three"))
conn.insert(SQLiteDB.tableName, NumIdDocument(0, "four")) conn.insert(TEST_TABLE, NumIdDocument(0, "four"))
val after = conn.findAll<NumIdDocument>(SQLiteDB.tableName, listOf(Field.named("key"))) val after = conn.findAll<NumIdDocument>(TEST_TABLE, listOf(Field.named("key")))
assertEquals(4, after.size, "There should have been 4 documents returned") assertEquals(4, after.size, "There should have been 4 documents returned")
assertEquals( assertEquals(
"1|2|77|78", after.joinToString("|") { it.key.toString() }, "1|2|77|78", after.joinToString("|") { it.key.toString() },

View File

@ -21,7 +21,7 @@ class DefinitionIT {
* @return True if the item exists in the given database, false if not * @return True if the item exists in the given database, false if not
*/ */
private fun itExists(item: String, conn: Connection) = private fun itExists(item: String, conn: Connection) =
conn.customScalar("SELECT EXISTS (SELECT 1 FROM ${SQLiteDB.catalog} WHERE name = :name) AS it", conn.customScalar("SELECT EXISTS (SELECT 1 FROM ${SQLiteDB.CATALOG} WHERE name = :name) AS it",
listOf(Parameter(":name", ParameterType.STRING, item)), Results::toExists) listOf(Parameter(":name", ParameterType.STRING, item)), Results::toExists)
@Test @Test
@ -39,8 +39,8 @@ class DefinitionIT {
@DisplayName("ensureFieldIndex creates an index") @DisplayName("ensureFieldIndex creates an index")
fun ensureFieldIndex() = fun ensureFieldIndex() =
SQLiteDB().use { db -> SQLiteDB().use { db ->
assertFalse(itExists("idx_${SQLiteDB.tableName}_test", db.conn), "The test index should not exist") assertFalse(itExists("idx_${TEST_TABLE}_test", db.conn), "The test index should not exist")
db.conn.ensureFieldIndex(SQLiteDB.tableName, "test", listOf("id", "category")) db.conn.ensureFieldIndex(TEST_TABLE, "test", listOf("id", "category"))
assertTrue(itExists("idx_${SQLiteDB.tableName}_test", db.conn), "The test index should now exist") assertTrue(itExists("idx_${TEST_TABLE}_test", db.conn), "The test index should now exist")
} }
} }

View File

@ -2,6 +2,7 @@ package solutions.bitbadger.documents.sqlite
import solutions.bitbadger.documents.AutoId import solutions.bitbadger.documents.AutoId
import solutions.bitbadger.documents.Configuration import solutions.bitbadger.documents.Configuration
import solutions.bitbadger.documents.TEST_TABLE
import solutions.bitbadger.documents.ensureTable import solutions.bitbadger.documents.ensureTable
import java.io.File import java.io.File
@ -10,7 +11,7 @@ import java.io.File
*/ */
class SQLiteDB : AutoCloseable { class SQLiteDB : AutoCloseable {
private var dbName = ""; private var dbName = ""
init { init {
dbName = "test-db-${AutoId.generateRandomString(8)}.db" dbName = "test-db-${AutoId.generateRandomString(8)}.db"
@ -20,7 +21,7 @@ class SQLiteDB : AutoCloseable {
val conn = Configuration.dbConn() val conn = Configuration.dbConn()
init { init {
conn.ensureTable(tableName) conn.ensureTable(TEST_TABLE)
} }
override fun close() { override fun close() {
@ -31,9 +32,6 @@ class SQLiteDB : AutoCloseable {
companion object { companion object {
/** The catalog table for SQLite's schema */ /** The catalog table for SQLite's schema */
val catalog = "sqlite_master" const val CATALOG = "sqlite_master"
/** The table used for test documents */
val tableName = "test_table"
} }
} }