This project now contains: - A generic JVM document library (with Kotlin extensions on the JDBC `Connection` object) - A Groovy library which adds extension methods to the `Connection` object - A Scala library, which uses native Scala collections and adds Scala-style extension methods to the `Connection` object - A Kotlin library which uses `kotlinx.serialization` (no reflection, reified generic types) along with `Connection` extensions Reviewed-on: #1
91 lines
2.7 KiB
Kotlin
91 lines
2.7 KiB
Kotlin
package solutions.bitbadger.documents.core.tests
|
|
|
|
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.DocumentException
|
|
import solutions.bitbadger.documents.Field
|
|
import solutions.bitbadger.documents.query.CountQuery
|
|
import kotlin.test.assertEquals
|
|
|
|
/**
|
|
* Unit tests for the `Count` object
|
|
*/
|
|
@DisplayName("Core | Kotlin | Query | CountQuery")
|
|
class CountQueryTest {
|
|
|
|
/**
|
|
* Clear the connection string (resets Dialect)
|
|
*/
|
|
@AfterEach
|
|
fun cleanUp() {
|
|
ForceDialect.none()
|
|
}
|
|
|
|
@Test
|
|
@DisplayName("all generates correctly")
|
|
fun all() =
|
|
assertEquals(
|
|
"SELECT COUNT(*) AS it FROM $TEST_TABLE",
|
|
CountQuery.all(TEST_TABLE),
|
|
"Count query not constructed correctly"
|
|
)
|
|
|
|
@Test
|
|
@DisplayName("byFields generates correctly | PostgreSQL")
|
|
fun byFieldsPostgres() {
|
|
ForceDialect.postgres()
|
|
assertEquals(
|
|
"SELECT COUNT(*) AS it FROM $TEST_TABLE WHERE data->>'test' = :field0",
|
|
CountQuery.byFields(TEST_TABLE, listOf(Field.equal("test", "", ":field0"))),
|
|
"Count query not constructed correctly"
|
|
)
|
|
}
|
|
|
|
@Test
|
|
@DisplayName("byFields generates correctly | SQLite")
|
|
fun byFieldsSQLite() {
|
|
ForceDialect.sqlite()
|
|
assertEquals(
|
|
"SELECT COUNT(*) AS it FROM $TEST_TABLE WHERE data->>'test' = :field0",
|
|
CountQuery.byFields(TEST_TABLE, listOf(Field.equal("test", "", ":field0"))),
|
|
"Count query not constructed correctly"
|
|
)
|
|
}
|
|
|
|
@Test
|
|
@DisplayName("byContains generates correctly | PostgreSQL")
|
|
fun byContainsPostgres() {
|
|
ForceDialect.postgres()
|
|
assertEquals(
|
|
"SELECT COUNT(*) AS it FROM $TEST_TABLE WHERE data @> :criteria", CountQuery.byContains(TEST_TABLE),
|
|
"Count query not constructed correctly"
|
|
)
|
|
}
|
|
|
|
@Test
|
|
@DisplayName("byContains fails | SQLite")
|
|
fun byContainsSQLite() {
|
|
ForceDialect.sqlite()
|
|
assertThrows<DocumentException> { CountQuery.byContains(TEST_TABLE) }
|
|
}
|
|
|
|
@Test
|
|
@DisplayName("byJsonPath generates correctly | PostgreSQL")
|
|
fun byJsonPathPostgres() {
|
|
ForceDialect.postgres()
|
|
assertEquals(
|
|
"SELECT COUNT(*) AS it FROM $TEST_TABLE WHERE jsonb_path_exists(data, :path::jsonpath)",
|
|
CountQuery.byJsonPath(TEST_TABLE), "Count query not constructed correctly"
|
|
)
|
|
}
|
|
|
|
@Test
|
|
@DisplayName("byJsonPath fails | SQLite")
|
|
fun byJsonPathSQLite() {
|
|
ForceDialect.sqlite()
|
|
assertThrows<DocumentException> { CountQuery.byJsonPath(TEST_TABLE) }
|
|
}
|
|
}
|