WIP on count impl; reworking comparisons

This commit is contained in:
2025-02-27 23:38:38 -05:00
parent 250e216ae8
commit a84c8289b1
20 changed files with 548 additions and 77 deletions

View File

@@ -14,82 +14,82 @@ class ComparisonTest {
@Test
@DisplayName("isNumeric is false for empty list of values")
fun isNumericFalseForEmptyList() =
assertFalse(Comparison(Op.IN, listOf<Int>()).isNumeric, "An IN with empty list should not be numeric")
assertFalse(InComparison(Op.IN, listOf<Int>()).isNumeric, "An IN with empty list should not be numeric")
@Test
@DisplayName("isNumeric is false for IN with strings")
fun isNumericFalseForStringsAndIn() =
assertFalse(Comparison(Op.IN, listOf("a", "b", "c")).isNumeric, "An IN with strings should not be numeric")
assertFalse(InComparison(Op.IN, listOf("a", "b", "c")).isNumeric, "An IN with strings should not be numeric")
@Test
@DisplayName("isNumeric is true for IN with bytes")
fun isNumericTrueForByteAndIn() =
assertTrue(Comparison(Op.IN, listOf<Byte>(4, 8)).isNumeric, "An IN with bytes should be numeric")
assertTrue(InComparison(Op.IN, listOf<Byte>(4, 8)).isNumeric, "An IN with bytes should be numeric")
@Test
@DisplayName("isNumeric is true for IN with shorts")
fun isNumericTrueForShortAndIn() =
assertTrue(Comparison(Op.IN, listOf<Short>(18, 22)).isNumeric, "An IN with shorts should be numeric")
assertTrue(InComparison(Op.IN, listOf<Short>(18, 22)).isNumeric, "An IN with shorts should be numeric")
@Test
@DisplayName("isNumeric is true for IN with ints")
fun isNumericTrueForIntAndIn() =
assertTrue(Comparison(Op.IN, listOf(7, 8, 9)).isNumeric, "An IN with ints should be numeric")
assertTrue(InComparison(Op.IN, listOf(7, 8, 9)).isNumeric, "An IN with ints should be numeric")
@Test
@DisplayName("isNumeric is true for IN with longs")
fun isNumericTrueForLongAndIn() =
assertTrue(Comparison(Op.IN, listOf(3L)).isNumeric, "An IN with longs should be numeric")
assertTrue(InComparison(Op.IN, listOf(3L)).isNumeric, "An IN with longs should be numeric")
@Test
@DisplayName("isNumeric is false for BETWEEN with strings")
fun isNumericFalseForStringsAndBetween() =
assertFalse(Comparison(Op.BETWEEN, Pair("eh", "zed")).isNumeric,
assertFalse(BetweenComparison(Op.BETWEEN, Pair("eh", "zed")).isNumeric,
"A BETWEEN with strings should not be numeric")
@Test
@DisplayName("isNumeric is true for BETWEEN with bytes")
fun isNumericTrueForByteAndBetween() =
assertTrue(Comparison(Op.BETWEEN, Pair<Byte, Byte>(7, 11)).isNumeric, "A BETWEEN with bytes should be numeric")
assertTrue(BetweenComparison(Op.BETWEEN, Pair<Byte, Byte>(7, 11)).isNumeric, "A BETWEEN with bytes should be numeric")
@Test
@DisplayName("isNumeric is true for BETWEEN with shorts")
fun isNumericTrueForShortAndBetween() =
assertTrue(Comparison(Op.BETWEEN, Pair<Short, Short>(0, 9)).isNumeric,
assertTrue(BetweenComparison(Op.BETWEEN, Pair<Short, Short>(0, 9)).isNumeric,
"A BETWEEN with shorts should be numeric")
@Test
@DisplayName("isNumeric is true for BETWEEN with ints")
fun isNumericTrueForIntAndBetween() =
assertTrue(Comparison(Op.BETWEEN, Pair(15, 44)).isNumeric, "A BETWEEN with ints should be numeric")
assertTrue(BetweenComparison(Op.BETWEEN, Pair(15, 44)).isNumeric, "A BETWEEN with ints should be numeric")
@Test
@DisplayName("isNumeric is true for BETWEEN with longs")
fun isNumericTrueForLongAndBetween() =
assertTrue(Comparison(Op.BETWEEN, Pair(9L, 12L)).isNumeric, "A BETWEEN with longs should be numeric")
assertTrue(BetweenComparison(Op.BETWEEN, Pair(9L, 12L)).isNumeric, "A BETWEEN with longs should be numeric")
@Test
@DisplayName("isNumeric is false for string value")
fun isNumericFalseForString() =
assertFalse(Comparison(Op.EQUAL, "80").isNumeric, "A string should not be numeric")
assertFalse(SingleComparison(Op.EQUAL, "80").isNumeric, "A string should not be numeric")
@Test
@DisplayName("isNumeric is true for byte value")
fun isNumericTrueForByte() =
assertTrue(Comparison(Op.EQUAL, 47.toByte()).isNumeric, "A byte should be numeric")
assertTrue(SingleComparison(Op.EQUAL, 47.toByte()).isNumeric, "A byte should be numeric")
@Test
@DisplayName("isNumeric is true for short value")
fun isNumericTrueForShort() =
assertTrue(Comparison(Op.EQUAL, 2.toShort()).isNumeric, "A short should be numeric")
assertTrue(SingleComparison(Op.EQUAL, 2.toShort()).isNumeric, "A short should be numeric")
@Test
@DisplayName("isNumeric is true for int value")
fun isNumericTrueForInt() =
assertTrue(Comparison(Op.EQUAL, 555).isNumeric, "An int should be numeric")
assertTrue(SingleComparison(Op.EQUAL, 555).isNumeric, "An int should be numeric")
@Test
@DisplayName("isNumeric is true for long value")
fun isNumericTrueForLong() =
assertTrue(Comparison(Op.EQUAL, 82L).isNumeric, "A long should be numeric")
assertTrue(SingleComparison(Op.EQUAL, 82L).isNumeric, "A long should be numeric")
}

View File

@@ -0,0 +1,24 @@
package solutions.bitbadger.documents
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
import kotlin.test.assertEquals
/**
* Unit tests for the `DocumentIndex` enum
*/
@DisplayName("Op")
class DocumentIndexTest {
@Test
@DisplayName("FULL uses proper SQL")
fun fullSQL() {
assertEquals("", DocumentIndex.FULL.sql, "The SQL for Full is incorrect")
}
@Test
@DisplayName("OPTIMIZED uses proper SQL")
fun optimizedSQL() {
assertEquals(" jsonb_path_ops", DocumentIndex.OPTIMIZED.sql, "The SQL for Optimized is incorrect")
}
}

View File

@@ -7,6 +7,7 @@ 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.DocumentIndex
import kotlin.test.assertEquals
/**
@@ -101,4 +102,33 @@ class DefinitionTest {
Definition.ensureIndexOn(tbl, "nest", listOf("a.b.c"), Dialect.SQLITE),
"CREATE INDEX for nested SQLite field incorrect"
)
@Test
@DisplayName("ensureDocumentIndexOn generates Full for PostgreSQL")
fun ensureDocumentIndexOnFullPostgres() {
Configuration.dialectValue = Dialect.POSTGRESQL
assertEquals(
"CREATE INDEX IF NOT EXISTS idx_${tbl}_document ON $tbl USING GIN (data)",
Definition.ensureDocumentIndexOn(tbl, DocumentIndex.FULL),
"CREATE INDEX for full document index incorrect"
)
}
@Test
@DisplayName("ensureDocumentIndexOn generates Optimized for PostgreSQL")
fun ensureDocumentIndexOnOptimizedPostgres() {
Configuration.dialectValue = Dialect.POSTGRESQL
assertEquals(
"CREATE INDEX IF NOT EXISTS idx_${tbl}_document ON $tbl USING GIN (data jsonb_path_ops)",
Definition.ensureDocumentIndexOn(tbl, DocumentIndex.OPTIMIZED),
"CREATE INDEX for optimized document index incorrect"
)
}
@Test
@DisplayName("ensureDocumentIndexOn fails for SQLite")
fun ensureDocumentIndexOnFailsSQLite() {
Configuration.dialectValue = Dialect.SQLITE
assertThrows<DocumentException> { Definition.ensureDocumentIndexOn(tbl, DocumentIndex.FULL) }
}
}