111 lines
3.4 KiB
Kotlin
111 lines
3.4 KiB
Kotlin
package solutions.bitbadger.documents.query
|
|
|
|
import org.junit.jupiter.api.AfterEach
|
|
import org.junit.jupiter.api.DisplayName
|
|
import org.junit.jupiter.api.Test
|
|
import org.junit.jupiter.api.assertThrows
|
|
import solutions.bitbadger.documents.Configuration
|
|
import solutions.bitbadger.documents.Dialect
|
|
import solutions.bitbadger.documents.DocumentException
|
|
import solutions.bitbadger.documents.Field
|
|
import kotlin.test.assertEquals
|
|
|
|
/**
|
|
* Unit tests for the `Find` object
|
|
*/
|
|
@DisplayName("Find (Query)")
|
|
class FindTest {
|
|
|
|
/** Test table name */
|
|
private val tbl = "test_table"
|
|
|
|
/**
|
|
* Clear the connection string (resets Dialect)
|
|
*/
|
|
@AfterEach
|
|
fun cleanUp() {
|
|
Configuration.dialectValue = null
|
|
}
|
|
|
|
@Test
|
|
@DisplayName("all generates correctly")
|
|
fun all() =
|
|
assertEquals("SELECT data FROM $tbl", Find.all(tbl), "Find query not constructed correctly")
|
|
|
|
@Test
|
|
@DisplayName("byId generates correctly (PostgreSQL)")
|
|
fun byIdPostgres() {
|
|
Configuration.dialectValue = Dialect.POSTGRESQL
|
|
assertEquals(
|
|
"SELECT data FROM $tbl WHERE data->>'id' = :id",
|
|
Find.byId<String>(tbl), "Find query not constructed correctly"
|
|
)
|
|
}
|
|
|
|
@Test
|
|
@DisplayName("byId generates correctly (SQLite)")
|
|
fun byIdSQLite() {
|
|
Configuration.dialectValue = Dialect.SQLITE
|
|
assertEquals(
|
|
"SELECT data FROM $tbl WHERE data->>'id' = :id",
|
|
Find.byId<String>(tbl), "Find query not constructed correctly"
|
|
)
|
|
}
|
|
|
|
@Test
|
|
@DisplayName("byFields generates correctly (PostgreSQL)")
|
|
fun byFieldsPostgres() {
|
|
Configuration.dialectValue = Dialect.POSTGRESQL
|
|
assertEquals(
|
|
"SELECT data FROM $tbl WHERE data->>'a' = :b AND (data->>'c')::numeric < :d",
|
|
Find.byFields(tbl, listOf(Field.equal("a", "", ":b"), Field.less("c", 14, ":d"))),
|
|
"Find query not constructed correctly"
|
|
)
|
|
}
|
|
|
|
@Test
|
|
@DisplayName("byFields generates correctly (SQLite)")
|
|
fun byFieldsSQLite() {
|
|
Configuration.dialectValue = Dialect.SQLITE
|
|
assertEquals(
|
|
"SELECT data FROM $tbl WHERE data->>'a' = :b AND data->>'c' < :d",
|
|
Find.byFields(tbl, listOf(Field.equal("a", "", ":b"), Field.less("c", 14, ":d"))),
|
|
"Find query not constructed correctly"
|
|
)
|
|
}
|
|
|
|
@Test
|
|
@DisplayName("byContains generates correctly (PostgreSQL)")
|
|
fun byContainsPostgres() {
|
|
Configuration.dialectValue = Dialect.POSTGRESQL
|
|
assertEquals(
|
|
"SELECT data FROM $tbl WHERE data @> :criteria", Find.byContains(tbl),
|
|
"Find query not constructed correctly"
|
|
)
|
|
}
|
|
|
|
@Test
|
|
@DisplayName("byContains fails (SQLite)")
|
|
fun byContainsSQLite() {
|
|
Configuration.dialectValue = Dialect.SQLITE
|
|
assertThrows<DocumentException> { Find.byContains(tbl) }
|
|
}
|
|
|
|
@Test
|
|
@DisplayName("byJsonPath generates correctly (PostgreSQL)")
|
|
fun byJsonPathPostgres() {
|
|
Configuration.dialectValue = Dialect.POSTGRESQL
|
|
assertEquals(
|
|
"SELECT data FROM $tbl WHERE jsonb_path_exists(data, :path::jsonpath)", Find.byJsonPath(tbl),
|
|
"Find query not constructed correctly"
|
|
)
|
|
}
|
|
|
|
@Test
|
|
@DisplayName("byJsonPath fails (SQLite)")
|
|
fun byJsonPathSQLite() {
|
|
Configuration.dialectValue = Dialect.SQLITE
|
|
assertThrows<DocumentException> { Find.byJsonPath(tbl) }
|
|
}
|
|
}
|