Add common throwaway db; all ITs now work in both

This commit is contained in:
2025-02-27 17:32:00 -05:00
parent ad3b7d2316
commit 250e216ae8
18 changed files with 315 additions and 115 deletions

View File

@@ -14,35 +14,35 @@ class CustomIT {
@Test
@DisplayName("list succeeds with empty list")
fun listEmpty() =
PgDB().use { Custom.listEmpty(it.conn) }
PgDB().use(Custom::listEmpty)
@Test
@DisplayName("list succeeds with a non-empty list")
fun listAll() =
PgDB().use { Custom.listAll(it.conn) }
PgDB().use(Custom::listAll)
@Test
@DisplayName("single succeeds when document not found")
fun singleNone() =
PgDB().use { Custom.singleNone(it.conn) }
PgDB().use(Custom::singleNone)
@Test
@DisplayName("single succeeds when a document is found")
fun singleOne() =
PgDB().use { Custom.singleOne(it.conn) }
PgDB().use(Custom::singleOne)
@Test
@DisplayName("nonQuery makes changes")
fun nonQueryChanges() =
PgDB().use { Custom.nonQueryChanges(it.conn) }
PgDB().use(Custom::nonQueryChanges)
@Test
@DisplayName("nonQuery makes no changes when where clause matches nothing")
fun nonQueryNoChanges() =
PgDB().use { Custom.nonQueryNoChanges(it.conn) }
PgDB().use(Custom::nonQueryNoChanges)
@Test
@DisplayName("scalar succeeds")
fun scalar() =
PgDB().use { Custom.scalar(it.conn) }
PgDB().use(Custom::scalar)
}

View File

@@ -0,0 +1,22 @@
package solutions.bitbadger.documents.postgresql
import org.junit.jupiter.api.DisplayName
import solutions.bitbadger.documents.common.Definition
import kotlin.test.Test
/**
* PostgreSQL integration tests for the `Definition` object / `ensure*` connection extension functions
*/
@DisplayName("PostgreSQL - Definition")
class DefinitionIT {
@Test
@DisplayName("ensureTable creates table and index")
fun ensureTable() =
PgDB().use(Definition::ensureTable)
@Test
@DisplayName("ensureFieldIndex creates an index")
fun ensureFieldIndex() =
PgDB().use(Definition::ensureFieldIndex)
}

View File

@@ -13,15 +13,25 @@ class DocumentIT {
@Test
@DisplayName("insert works with default values")
fun insertDefault() =
PgDB().use { Document.insertDefault(it.conn) }
PgDB().use(Document::insertDefault)
@Test
@DisplayName("insert fails with duplicate key")
fun insertDupe() =
PgDB().use { Document.insertDupe(it.conn) }
PgDB().use(Document::insertDupe)
@Test
@DisplayName("insert succeeds with numeric auto IDs")
fun insertNumAutoId() =
PgDB().use { Document.insertNumAutoId(it.conn) }
PgDB().use(Document::insertNumAutoId)
@Test
@DisplayName("insert succeeds with UUID auto ID")
fun insertUUIDAutoId() =
PgDB().use(Document::insertUUIDAutoId)
@Test
@DisplayName("insert succeeds with random string auto ID")
fun insertStringAutoId() =
PgDB().use(Document::insertStringAutoId)
}

View File

@@ -1,14 +1,11 @@
package solutions.bitbadger.documents.postgresql
import solutions.bitbadger.documents.AutoId
import solutions.bitbadger.documents.Configuration
import solutions.bitbadger.documents.customNonQuery
import solutions.bitbadger.documents.ensureTable
import solutions.bitbadger.documents.*
/**
* A wrapper for a throwaway PostgreSQL database
*/
class PgDB : AutoCloseable {
class PgDB : ThrowawayDatabase {
private var dbName = ""
@@ -21,10 +18,10 @@ class PgDB : AutoCloseable {
Configuration.connectionString = connString(dbName)
}
val conn = Configuration.dbConn()
override val conn = Configuration.dbConn()
init {
conn.ensureTable(tableName)
conn.ensureTable(TEST_TABLE)
}
override fun close() {
@@ -36,10 +33,11 @@ class PgDB : AutoCloseable {
Configuration.connectionString = null
}
companion object {
override fun dbObjectExists(name: String) =
conn.customScalar("SELECT EXISTS (SELECT 1 FROM pg_class WHERE relname = :name) AS it",
listOf(Parameter(":name", ParameterType.STRING, name)), Results::toExists)
/** The table used for test documents */
val tableName = "test_table"
companion object {
/**
* Create a connection string for the given database