Initial Development #1
@ -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 */
|
||||
val testTableName = "test_table"
|
||||
const val TEST_TABLE = "test_table"
|
||||
|
@ -14,66 +14,66 @@ import kotlin.test.assertNull
|
||||
object Custom {
|
||||
|
||||
fun listEmpty(conn: Connection) {
|
||||
JsonDocument.load(conn, testTableName)
|
||||
conn.customNonQuery("DELETE FROM $testTableName")
|
||||
val result = conn.customList<JsonDocument>(Find.all(testTableName), mapFunc = Results::fromData)
|
||||
JsonDocument.load(conn, TEST_TABLE)
|
||||
conn.customNonQuery("DELETE FROM $TEST_TABLE")
|
||||
val result = conn.customList<JsonDocument>(Find.all(TEST_TABLE), mapFunc = Results::fromData)
|
||||
assertEquals(0, result.size, "There should have been no results")
|
||||
}
|
||||
|
||||
fun listAll(conn: Connection) {
|
||||
JsonDocument.load(conn, testTableName)
|
||||
val result = conn.customList<JsonDocument>(Find.all(testTableName), mapFunc = Results::fromData)
|
||||
JsonDocument.load(conn, TEST_TABLE)
|
||||
val result = conn.customList<JsonDocument>(Find.all(TEST_TABLE), mapFunc = Results::fromData)
|
||||
assertEquals(5, result.size, "There should have been 5 results")
|
||||
}
|
||||
|
||||
fun singleNone(conn: Connection) =
|
||||
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"
|
||||
)
|
||||
|
||||
fun singleOne(conn: Connection) {
|
||||
JsonDocument.load(conn, testTableName)
|
||||
JsonDocument.load(conn, TEST_TABLE)
|
||||
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"
|
||||
)
|
||||
}
|
||||
|
||||
fun nonQueryChanges(conn: Connection) {
|
||||
JsonDocument.load(conn, testTableName)
|
||||
JsonDocument.load(conn, TEST_TABLE)
|
||||
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"
|
||||
)
|
||||
conn.customNonQuery("DELETE FROM $testTableName")
|
||||
conn.customNonQuery("DELETE FROM $TEST_TABLE")
|
||||
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"
|
||||
)
|
||||
}
|
||||
|
||||
fun nonQueryNoChanges(conn: Connection) {
|
||||
JsonDocument.load(conn, testTableName)
|
||||
JsonDocument.load(conn, TEST_TABLE)
|
||||
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"
|
||||
)
|
||||
conn.customNonQuery(
|
||||
"DELETE FROM $testTableName WHERE data->>'id' = :id",
|
||||
"DELETE FROM $TEST_TABLE WHERE data->>'id' = :id",
|
||||
listOf(Parameter(":id", ParameterType.STRING, "eighty-two"))
|
||||
)
|
||||
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"
|
||||
)
|
||||
}
|
||||
|
||||
fun scalar(conn: Connection) {
|
||||
JsonDocument.load(conn, testTableName)
|
||||
JsonDocument.load(conn, TEST_TABLE)
|
||||
assertEquals(
|
||||
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"
|
||||
)
|
||||
}
|
||||
|
@ -1,8 +1,6 @@
|
||||
package solutions.bitbadger.documents.common
|
||||
|
||||
import org.junit.jupiter.api.DisplayName
|
||||
import solutions.bitbadger.documents.*
|
||||
import solutions.bitbadger.documents.sqlite.SQLiteDB
|
||||
import java.sql.Connection
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.fail
|
||||
@ -13,18 +11,18 @@ import kotlin.test.fail
|
||||
object Document {
|
||||
|
||||
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"))
|
||||
conn.insert(testTableName, doc)
|
||||
val after = conn.findAll<JsonDocument>(testTableName)
|
||||
conn.insert(TEST_TABLE, doc)
|
||||
val after = conn.findAll<JsonDocument>(TEST_TABLE)
|
||||
assertEquals(1, after.size, "There should be one document in the table")
|
||||
assertEquals(doc, after[0], "The document should be what was inserted")
|
||||
}
|
||||
|
||||
fun insertDupe(conn: Connection) {
|
||||
conn.insert(testTableName, JsonDocument("a", "", 0, null))
|
||||
conn.insert(TEST_TABLE, JsonDocument("a", "", 0, null))
|
||||
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")
|
||||
} catch (_: Exception) {
|
||||
// yay
|
||||
@ -35,14 +33,14 @@ object Document {
|
||||
try {
|
||||
Configuration.autoIdStrategy = AutoId.NUMBER
|
||||
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(SQLiteDB.tableName, NumIdDocument(0, "two"))
|
||||
conn.insert(SQLiteDB.tableName, NumIdDocument(77, "three"))
|
||||
conn.insert(SQLiteDB.tableName, NumIdDocument(0, "four"))
|
||||
conn.insert(TEST_TABLE, NumIdDocument(0, "one"))
|
||||
conn.insert(TEST_TABLE, NumIdDocument(0, "two"))
|
||||
conn.insert(TEST_TABLE, NumIdDocument(77, "three"))
|
||||
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(
|
||||
"1|2|77|78", after.joinToString("|") { it.key.toString() },
|
||||
|
@ -21,7 +21,7 @@ class DefinitionIT {
|
||||
* @return True if the item exists in the given database, false if not
|
||||
*/
|
||||
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)
|
||||
|
||||
@Test
|
||||
@ -39,8 +39,8 @@ class DefinitionIT {
|
||||
@DisplayName("ensureFieldIndex creates an index")
|
||||
fun ensureFieldIndex() =
|
||||
SQLiteDB().use { db ->
|
||||
assertFalse(itExists("idx_${SQLiteDB.tableName}_test", db.conn), "The test index should not exist")
|
||||
db.conn.ensureFieldIndex(SQLiteDB.tableName, "test", listOf("id", "category"))
|
||||
assertTrue(itExists("idx_${SQLiteDB.tableName}_test", db.conn), "The test index should now exist")
|
||||
assertFalse(itExists("idx_${TEST_TABLE}_test", db.conn), "The test index should not exist")
|
||||
db.conn.ensureFieldIndex(TEST_TABLE, "test", listOf("id", "category"))
|
||||
assertTrue(itExists("idx_${TEST_TABLE}_test", db.conn), "The test index should now exist")
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package solutions.bitbadger.documents.sqlite
|
||||
|
||||
import solutions.bitbadger.documents.AutoId
|
||||
import solutions.bitbadger.documents.Configuration
|
||||
import solutions.bitbadger.documents.TEST_TABLE
|
||||
import solutions.bitbadger.documents.ensureTable
|
||||
import java.io.File
|
||||
|
||||
@ -10,7 +11,7 @@ import java.io.File
|
||||
*/
|
||||
class SQLiteDB : AutoCloseable {
|
||||
|
||||
private var dbName = "";
|
||||
private var dbName = ""
|
||||
|
||||
init {
|
||||
dbName = "test-db-${AutoId.generateRandomString(8)}.db"
|
||||
@ -20,7 +21,7 @@ class SQLiteDB : AutoCloseable {
|
||||
val conn = Configuration.dbConn()
|
||||
|
||||
init {
|
||||
conn.ensureTable(tableName)
|
||||
conn.ensureTable(TEST_TABLE)
|
||||
}
|
||||
|
||||
override fun close() {
|
||||
@ -31,9 +32,6 @@ class SQLiteDB : AutoCloseable {
|
||||
companion object {
|
||||
|
||||
/** The catalog table for SQLite's schema */
|
||||
val catalog = "sqlite_master"
|
||||
|
||||
/** The table used for test documents */
|
||||
val tableName = "test_table"
|
||||
const val CATALOG = "sqlite_master"
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user