Initial Development #1
@ -1,9 +1,6 @@
|
|||||||
package solutions.bitbadger.documents.kotlinx.tests.integration
|
package solutions.bitbadger.documents.kotlinx.tests.integration
|
||||||
|
|
||||||
import solutions.bitbadger.documents.*
|
import solutions.bitbadger.documents.*
|
||||||
import solutions.bitbadger.documents.java.extensions.countAll
|
|
||||||
import solutions.bitbadger.documents.java.extensions.insert
|
|
||||||
import solutions.bitbadger.documents.java.extensions.writeCustomJsonArray
|
|
||||||
import solutions.bitbadger.documents.kotlinx.Results
|
import solutions.bitbadger.documents.kotlinx.Results
|
||||||
import solutions.bitbadger.documents.kotlinx.extensions.*
|
import solutions.bitbadger.documents.kotlinx.extensions.*
|
||||||
import solutions.bitbadger.documents.kotlinx.tests.ArrayDocument
|
import solutions.bitbadger.documents.kotlinx.tests.ArrayDocument
|
||||||
@ -46,7 +43,7 @@ object CustomFunctions {
|
|||||||
fun jsonArraySingle(db: ThrowawayDatabase) {
|
fun jsonArraySingle(db: ThrowawayDatabase) {
|
||||||
db.conn.insert(TEST_TABLE, ArrayDocument("one", listOf("2", "3")))
|
db.conn.insert(TEST_TABLE, ArrayDocument("one", listOf("2", "3")))
|
||||||
assertEquals(
|
assertEquals(
|
||||||
JsonFunctions.maybeJsonB("[{\"id\":\"one\",\"values\":[\"2\",\"3\"]}]"),
|
JsonFunctions.maybeJsonB("""[{"id":"one","values":["2","3"]}]"""),
|
||||||
db.conn.customJsonArray(FindQuery.all(TEST_TABLE), listOf(), Results::jsonFromData),
|
db.conn.customJsonArray(FindQuery.all(TEST_TABLE), listOf(), Results::jsonFromData),
|
||||||
"A single document list was not represented correctly"
|
"A single document list was not represented correctly"
|
||||||
)
|
)
|
||||||
@ -55,9 +52,8 @@ object CustomFunctions {
|
|||||||
fun jsonArrayMany(db: ThrowawayDatabase) {
|
fun jsonArrayMany(db: ThrowawayDatabase) {
|
||||||
ArrayDocument.testDocuments.forEach { db.conn.insert(TEST_TABLE, it) }
|
ArrayDocument.testDocuments.forEach { db.conn.insert(TEST_TABLE, it) }
|
||||||
assertEquals(
|
assertEquals(
|
||||||
JsonFunctions.maybeJsonB("[{\"id\":\"first\",\"values\":[\"a\",\"b\",\"c\"]},"
|
JsonFunctions.maybeJsonB("""[{"id":"first","values":["a","b","c"]},"""
|
||||||
+ "{\"id\":\"second\",\"values\":[\"c\",\"d\",\"e\"]},"
|
+ """{"id":"second","values":["c","d","e"]},{"id":"third","values":["x","y","z"]}]"""),
|
||||||
+ "{\"id\":\"third\",\"values\":[\"x\",\"y\",\"z\"]}]"),
|
|
||||||
db.conn.customJsonArray(FindQuery.all(TEST_TABLE) + orderBy(listOf(Field.named("id"))), listOf(),
|
db.conn.customJsonArray(FindQuery.all(TEST_TABLE) + orderBy(listOf(Field.named("id"))), listOf(),
|
||||||
Results::jsonFromData),
|
Results::jsonFromData),
|
||||||
"A multiple document list was not represented correctly"
|
"A multiple document list was not represented correctly"
|
||||||
|
@ -121,7 +121,7 @@ object JsonFunctions {
|
|||||||
Configuration.idField = "key"
|
Configuration.idField = "key"
|
||||||
try {
|
try {
|
||||||
db.conn.insert(TEST_TABLE, NumIdDocument(18, "howdy"))
|
db.conn.insert(TEST_TABLE, NumIdDocument(18, "howdy"))
|
||||||
checkByIdString(db.conn.jsonById(TEST_TABLE, 18))
|
checkByIdNumber(db.conn.jsonById(TEST_TABLE, 18))
|
||||||
} finally {
|
} finally {
|
||||||
Configuration.idField = "id"
|
Configuration.idField = "id"
|
||||||
}
|
}
|
||||||
|
@ -8,16 +8,11 @@ import solutions.bitbadger.documents.kotlinx.tests.TEST_TABLE
|
|||||||
/**
|
/**
|
||||||
* A wrapper for a throwaway PostgreSQL database
|
* A wrapper for a throwaway PostgreSQL database
|
||||||
*/
|
*/
|
||||||
class PgDB : ThrowawayDatabase {
|
class PgDB : ThrowawayDatabase() {
|
||||||
|
|
||||||
private var dbName = ""
|
|
||||||
|
|
||||||
init {
|
init {
|
||||||
dbName = "throwaway_${AutoId.generateRandomString(8)}"
|
|
||||||
Configuration.connectionString = connString("postgres")
|
Configuration.connectionString = connString("postgres")
|
||||||
Configuration.dbConn().use {
|
Configuration.dbConn().use { it.customNonQuery("CREATE DATABASE $dbName") }
|
||||||
it.customNonQuery("CREATE DATABASE $dbName")
|
|
||||||
}
|
|
||||||
Configuration.connectionString = connString(dbName)
|
Configuration.connectionString = connString(dbName)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -30,9 +25,7 @@ class PgDB : ThrowawayDatabase {
|
|||||||
override fun close() {
|
override fun close() {
|
||||||
conn.close()
|
conn.close()
|
||||||
Configuration.connectionString = connString("postgres")
|
Configuration.connectionString = connString("postgres")
|
||||||
Configuration.dbConn().use {
|
Configuration.dbConn().use { it.customNonQuery("DROP DATABASE $dbName") }
|
||||||
it.customNonQuery("DROP DATABASE $dbName")
|
|
||||||
}
|
|
||||||
Configuration.connectionString = null
|
Configuration.connectionString = null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,12 +9,9 @@ import java.io.File
|
|||||||
/**
|
/**
|
||||||
* A wrapper for a throwaway SQLite database
|
* A wrapper for a throwaway SQLite database
|
||||||
*/
|
*/
|
||||||
class SQLiteDB : ThrowawayDatabase {
|
class SQLiteDB : ThrowawayDatabase() {
|
||||||
|
|
||||||
private var dbName = ""
|
|
||||||
|
|
||||||
init {
|
init {
|
||||||
dbName = "test-db-${AutoId.generateRandomString(8)}.db"
|
|
||||||
Configuration.connectionString = "jdbc:sqlite:$dbName"
|
Configuration.connectionString = "jdbc:sqlite:$dbName"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,14 +1,18 @@
|
|||||||
package solutions.bitbadger.documents.kotlinx.tests.integration
|
package solutions.bitbadger.documents.kotlinx.tests.integration
|
||||||
|
|
||||||
|
import solutions.bitbadger.documents.AutoId
|
||||||
import java.sql.Connection
|
import java.sql.Connection
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Common interface for PostgreSQL and SQLite throwaway databases
|
* Common interface for PostgreSQL and SQLite throwaway databases
|
||||||
*/
|
*/
|
||||||
interface ThrowawayDatabase : AutoCloseable {
|
abstract class ThrowawayDatabase : AutoCloseable {
|
||||||
|
|
||||||
|
/** The name of the throwaway database */
|
||||||
|
protected val dbName = "throwaway_${AutoId.generateRandomString(8)}"
|
||||||
|
|
||||||
/** The database connection for the throwaway database */
|
/** The database connection for the throwaway database */
|
||||||
val conn: Connection
|
abstract val conn: Connection
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Determine if a database object exists
|
* Determine if a database object exists
|
||||||
@ -16,5 +20,5 @@ interface ThrowawayDatabase : AutoCloseable {
|
|||||||
* @param name The name of the object whose existence should be checked
|
* @param name The name of the object whose existence should be checked
|
||||||
* @return True if the object exists, false if not
|
* @return True if the object exists, false if not
|
||||||
*/
|
*/
|
||||||
fun dbObjectExists(name: String): Boolean
|
abstract fun dbObjectExists(name: String): Boolean
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user