package solutions.bitbadger.documents.common.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.common.Configuration import solutions.bitbadger.documents.common.Dialect import solutions.bitbadger.documents.common.DocumentException import solutions.bitbadger.documents.common.Field import kotlin.test.assertEquals /** * Unit tests for the `Delete` object */ @DisplayName("Kotlin | Common | Query: Delete") 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(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(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 { 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 { Delete.byJsonPath(tbl) } } }