WIP on remove fields

This commit is contained in:
2025-02-22 13:30:27 -05:00
parent 07edc6ee43
commit 50de4fbf94
6 changed files with 257 additions and 3 deletions

View File

@@ -1,5 +1,6 @@
package solutions.bitbadger.documents
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.DisplayName
import kotlin.test.Test
import kotlin.test.assertEquals
@@ -12,6 +13,14 @@ import kotlin.test.assertSame
@DisplayName("Parameters")
class ParametersTest {
/**
* Clear the connection string (resets Dialect)
*/
@AfterEach
fun cleanUp() {
Configuration.dialectValue = null
}
@Test
@DisplayName("nameFields works with no changes")
fun nameFieldsNoChange() {
@@ -47,4 +56,54 @@ class ParametersTest {
assertEquals("SELECT data, data_ext FROM tbl WHERE data = ? AND data_ext = ? AND more_data = ?",
Parameters.replaceNamesInQuery(query, parameters), "Parameters not replaced correctly")
}
@Test
@DisplayName("fieldNames generates a single parameter (PostgreSQL)")
fun fieldNamesSinglePostgres() {
Configuration.dialectValue = Dialect.POSTGRESQL
val nameParams = Parameters.fieldNames(listOf("test"))
assertEquals(1, nameParams.size, "There should be one name parameter")
assertEquals(":name", nameParams[0].name, "The parameter name is incorrect")
assertEquals(ParameterType.STRING, nameParams[0].type, "The parameter type is incorrect")
assertEquals("test", nameParams[0].value, "The parameter value is incorrect")
}
@Test
@DisplayName("fieldNames generates multiple parameters (PostgreSQL)")
fun fieldNamesMultiplePostgres() {
Configuration.dialectValue = Dialect.POSTGRESQL
val nameParams = Parameters.fieldNames(listOf("test", "this", "today"))
assertEquals(1, nameParams.size, "There should be one name parameter")
assertEquals(":name", nameParams[0].name, "The parameter name is incorrect")
assertEquals(ParameterType.STRING, nameParams[0].type, "The parameter type is incorrect")
assertEquals("{test,this,today}", nameParams[0].value, "The parameter value is incorrect")
}
@Test
@DisplayName("fieldNames generates a single parameter (SQLite)")
fun fieldNamesSingleSQLite() {
Configuration.dialectValue = Dialect.SQLITE
val nameParams = Parameters.fieldNames(listOf("test"))
assertEquals(1, nameParams.size, "There should be one name parameter")
assertEquals(":name0", nameParams[0].name, "The parameter name is incorrect")
assertEquals(ParameterType.STRING, nameParams[0].type, "The parameter type is incorrect")
assertEquals("test", nameParams[0].value, "The parameter value is incorrect")
}
@Test
@DisplayName("fieldNames generates multiple parameters (SQLite)")
fun fieldNamesMultipleSQLite() {
Configuration.dialectValue = Dialect.SQLITE
val nameParams = Parameters.fieldNames(listOf("test", "this", "today"))
assertEquals(3, nameParams.size, "There should be one name parameter")
assertEquals(":name0", nameParams[0].name, "The first parameter name is incorrect")
assertEquals(ParameterType.STRING, nameParams[0].type, "The first parameter type is incorrect")
assertEquals("test", nameParams[0].value, "The first parameter value is incorrect")
assertEquals(":name1", nameParams[1].name, "The second parameter name is incorrect")
assertEquals(ParameterType.STRING, nameParams[1].type, "The second parameter type is incorrect")
assertEquals("this", nameParams[1].value, "The second parameter value is incorrect")
assertEquals(":name2", nameParams[2].name, "The third parameter name is incorrect")
assertEquals(ParameterType.STRING, nameParams[2].type, "The third parameter type is incorrect")
assertEquals("today", nameParams[2].value, "The third parameter value is incorrect")
}
}

View File

@@ -0,0 +1,102 @@
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 `Delete` object
*/
@DisplayName("Delete (Query)")
class DeleteTest {
/** Test table name */
private val tbl = "test_table"
/**
* Clear the connection string (resets Dialect)
*/
@AfterEach
fun cleanUp() {
Configuration.dialectValue = null
}
@Test
@DisplayName("byId generates correctly (PostgreSQL)")
fun byIdPostgres() {
Configuration.dialectValue = Dialect.POSTGRESQL
assertEquals(
"DELETE FROM $tbl WHERE data->>'id' = :id",
Delete.byId<String>(tbl), "Delete query not constructed correctly"
)
}
@Test
@DisplayName("byId generates correctly (SQLite)")
fun byIdSQLite() {
Configuration.dialectValue = Dialect.SQLITE
assertEquals(
"DELETE FROM $tbl WHERE data->>'id' = :id",
Delete.byId<String>(tbl), "Delete query not constructed correctly"
)
}
@Test
@DisplayName("byFields generates correctly (PostgreSQL)")
fun byFieldsPostgres() {
Configuration.dialectValue = Dialect.POSTGRESQL
assertEquals(
"DELETE FROM $tbl WHERE data->>'a' = :b", Delete.byFields(tbl, listOf(Field.equal("a", "", ":b"))),
"Delete query not constructed correctly"
)
}
@Test
@DisplayName("byFields generates correctly (SQLite)")
fun byFieldsSQLite() {
Configuration.dialectValue = Dialect.SQLITE
assertEquals(
"DELETE FROM $tbl WHERE data->>'a' = :b", Delete.byFields(tbl, listOf(Field.equal("a", "", ":b"))),
"Delete query not constructed correctly"
)
}
@Test
@DisplayName("byContains generates correctly (PostgreSQL)")
fun byContainsPostgres() {
Configuration.dialectValue = Dialect.POSTGRESQL
assertEquals(
"DELETE FROM $tbl WHERE data @> :criteria", Delete.byContains(tbl), "Delete query not constructed correctly"
)
}
@Test
@DisplayName("byContains fails (SQLite)")
fun byContainsSQLite() {
Configuration.dialectValue = Dialect.SQLITE
assertThrows<DocumentException> { Delete.byContains(tbl) }
}
@Test
@DisplayName("byJsonPath generates correctly (PostgreSQL)")
fun byJsonPathPostgres() {
Configuration.dialectValue = Dialect.POSTGRESQL
assertEquals(
"DELETE FROM $tbl WHERE jsonb_path_exists(data, :path::jsonpath)", Delete.byJsonPath(tbl),
"Delete query not constructed correctly"
)
}
@Test
@DisplayName("byJsonPath fails (SQLite)")
fun byJsonPathSQLite() {
Configuration.dialectValue = Dialect.SQLITE
assertThrows<DocumentException> { Delete.byJsonPath(tbl) }
}
}

View File

@@ -11,7 +11,7 @@ import solutions.bitbadger.documents.Field
import kotlin.test.assertEquals
/**
* Unit tests for the `Exists` object
* Unit tests for the `Find` object
*/
@DisplayName("Find (Query)")
class FindTest {