84 lines
3.0 KiB
Kotlin

package solutions.bitbadger.documents.integration.common
import solutions.bitbadger.documents.*
import solutions.bitbadger.documents.integration.JsonDocument
import solutions.bitbadger.documents.integration.TEST_TABLE
import solutions.bitbadger.documents.integration.ThrowawayDatabase
import solutions.bitbadger.documents.query.Count
import solutions.bitbadger.documents.query.Delete
import solutions.bitbadger.documents.query.Find
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertNull
/**
* Integration tests for the `Custom` object
*/
object Custom {
fun listEmpty(db: ThrowawayDatabase) {
JsonDocument.load(db)
db.conn.deleteByFields(TEST_TABLE, listOf(Field.exists(Configuration.idField)))
val result = db.conn.customList<JsonDocument>(Find.all(TEST_TABLE), mapFunc = Results::fromData)
assertEquals(0, result.size, "There should have been no results")
}
fun listAll(db: ThrowawayDatabase) {
JsonDocument.load(db)
val result = db.conn.customList<JsonDocument>(Find.all(TEST_TABLE), mapFunc = Results::fromData)
assertEquals(5, result.size, "There should have been 5 results")
}
fun singleNone(db: ThrowawayDatabase) =
assertNull(
db.conn.customSingle(Find.all(TEST_TABLE), mapFunc = Results::fromData),
"There should not have been a document returned"
)
fun singleOne(db: ThrowawayDatabase) {
JsonDocument.load(db)
assertNotNull(
db.conn.customSingle<JsonDocument>(Find.all(TEST_TABLE), mapFunc = Results::fromData),
"There should not have been a document returned"
)
}
fun nonQueryChanges(db: ThrowawayDatabase) {
JsonDocument.load(db)
assertEquals(
5L, db.conn.customScalar(Count.all(TEST_TABLE), mapFunc = Results::toCount),
"There should have been 5 documents in the table"
)
db.conn.customNonQuery("DELETE FROM $TEST_TABLE")
assertEquals(
0L, db.conn.customScalar(Count.all(TEST_TABLE), mapFunc = Results::toCount),
"There should have been no documents in the table"
)
}
fun nonQueryNoChanges(db: ThrowawayDatabase) {
JsonDocument.load(db)
assertEquals(
5L, db.conn.customScalar(Count.all(TEST_TABLE), mapFunc = Results::toCount),
"There should have been 5 documents in the table"
)
db.conn.customNonQuery(
Delete.byId(TEST_TABLE, "eighty-two"),
listOf(Parameter(":id", ParameterType.STRING, "eighty-two"))
)
assertEquals(
5L, db.conn.customScalar(Count.all(TEST_TABLE), mapFunc = Results::toCount),
"There should still have been 5 documents in the table"
)
}
fun scalar(db: ThrowawayDatabase) {
JsonDocument.load(db)
assertEquals(
3L,
db.conn.customScalar("SELECT 3 AS it FROM $TEST_TABLE LIMIT 1", mapFunc = Results::toCount),
"The number 3 should have been returned"
)
}
}