diff --git a/src/test/kotlin/com/github/vokorm/AbstractDatabaseTests.kt b/src/test/kotlin/com/github/vokorm/AbstractDatabaseTests.kt index 8df03ab..5591618 100644 --- a/src/test/kotlin/com/github/vokorm/AbstractDatabaseTests.kt +++ b/src/test/kotlin/com/github/vokorm/AbstractDatabaseTests.kt @@ -1,6 +1,5 @@ package com.github.vokorm -import com.github.vokorm.AbstractDbDaoTests import org.jdbi.v3.core.Handle import org.junit.jupiter.api.Nested import org.junit.jupiter.api.Test @@ -12,6 +11,7 @@ abstract class AbstractDatabaseTests(val info: DatabaseInfo) { @Nested inner class DbFunTests : AbstractDbFunTests() @Nested inner class MappingTests : AbstractDbMappingTests() @Nested inner class DbDaoTests : AbstractDbDaoTests() + @Nested inner class FiltersTests : AbstractFiltersTest(info) } /** diff --git a/src/test/kotlin/com/github/vokorm/AbstractFiltersTest.kt b/src/test/kotlin/com/github/vokorm/AbstractFiltersTest.kt new file mode 100644 index 0000000..dafe3ff --- /dev/null +++ b/src/test/kotlin/com/github/vokorm/AbstractFiltersTest.kt @@ -0,0 +1,125 @@ +package com.github.vokorm + +import com.gitlab.mvysny.jdbiorm.quirks.DatabaseVariant +import org.junit.jupiter.api.Assumptions +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Nested +import org.junit.jupiter.api.Test + +abstract class AbstractFiltersTest(val info: DatabaseInfo) { + @Test fun `api test`() { + Person.findAll(Person::age.asc, Person::created.desc) + Person.findAllBy(Person::age.asc, Person::created.desc, condition = Person::age.exp.eq(5)) + } + + @Nested inner class `filter test` { + @BeforeEach fun preCreateTestEntities() { + // create a basic set of entities + Person(name = "Moby", age = 25).create() + Person(name = "Jerry", age = 26).create() + Person(name = "Paul", age = 27).create() + } + + @Test fun `eq filter test`() { + expectList() { + Person.findAllBy { Person::age eq 40 }.map { it.name } + } + expectList("Jerry") { + Person.findAllBy { Person::age eq 26 }.map { it.name } + } + } + + @Test fun `ne filter test`() { + expectList("Moby", "Jerry", "Paul") { + Person.findAllBy { Person::age ne 40 }.map { it.name } + } + expectList("Jerry", "Paul") { + Person.findAllBy { Person::age ne 25 }.map { it.name } + } + } + + @Test fun `le filter test`() { + expectList("Moby", "Jerry", "Paul") { + Person.findAllBy { Person::age le 40 }.map { it.name } + } + expectList("Moby", "Jerry") { + Person.findAllBy { Person::age le 26 }.map { it.name } + } + } + + @Test fun `lt filter test`() { + expectList("Moby", "Jerry", "Paul") { + Person.findAllBy { Person::age lt 40 }.map { it.name } + } + expectList("Moby") { + Person.findAllBy { Person::age lt 26 }.map { it.name } + } + } + + @Test fun `ge filter test`() { + expectList() { + Person.findAllBy { Person::age ge 40 }.map { it.name } + } + expectList("Jerry", "Paul") { + Person.findAllBy { Person::age ge 26 }.map { it.name } + } + } + + @Test fun `gt filter test`() { + expectList() { + Person.findAllBy { Person::age gt 40 }.map { it.name } + } + expectList("Paul") { + Person.findAllBy { Person::age gt 26 }.map { it.name } + } + } + + @Test fun `not filter test`() { + expectList("Moby", "Paul") { + Person.findAllBy { !(Person::age eq 26) }.map { it.name } + } + } + + @Test fun `in filter test`() { + expectList("Moby", "Jerry") { + Person.findAllBy { Person::age `in` listOf(25, 26, 28) }.map { it.name } + } + } + } + + @Nested inner class `full-text search` { + @BeforeEach fun assumeSupportsFullText() { + Assumptions.assumeTrue( + info.supportsFullText, + "This database doesn't support full-text search, skipping tests" + ) + } + @Test fun `smoke test`() { + Person.findAllBy(Person::name.exp.fullTextMatches("")) + Person.findAllBy(Person::name.exp.fullTextMatches("a")) + Person.findAllBy(Person::name.exp.fullTextMatches("the")) + Person.findAllBy(Person::name.exp.fullTextMatches("Moby")) + } + + @Test fun `blank filter matches all records`() { + val moby = Person(name = "Moby") + moby.create() + expectList(moby) { Person.findAllBy(Person::name.exp.fullTextMatches("")) } + } + + @Test fun `various queries matching-not matching Moby`() { + val moby = Person(name = "Moby") + moby.create() + expectList() { Person.findAllBy(Person::name.exp.fullTextMatches("foobar")) } + expectList(moby) { Person.findAllBy(Person::name.exp.fullTextMatches("Moby")) } + expectList() { Person.findAllBy(Person::name.exp.fullTextMatches("Jerry")) } + expectList() { Person.findAllBy(Person::name.exp.fullTextMatches("Jerry Moby")) } + } + + @Test fun `partial match`() { + val moby = Person(name = "Moby") + moby.create() + expectList(moby) { Person.findAllBy(Person::name.exp.fullTextMatches("Mob")) } + } + } +} diff --git a/src/test/kotlin/com/github/vokorm/CockroachDatabaseTest.kt b/src/test/kotlin/com/github/vokorm/CockroachDatabaseTest.kt index 7a2281e..7b2fce8 100644 --- a/src/test/kotlin/com/github/vokorm/CockroachDatabaseTest.kt +++ b/src/test/kotlin/com/github/vokorm/CockroachDatabaseTest.kt @@ -12,6 +12,7 @@ class CockroachDatabaseTest { @BeforeAll @JvmStatic fun setup() { + Assumptions.assumeTrue(!h2only) { "Only H2 tests are running now" } assumeDockerAvailable() container = diff --git a/src/test/kotlin/com/github/vokorm/ConnectionUtilsTest.kt b/src/test/kotlin/com/github/vokorm/ConnectionUtilsTest.kt index 72a6a99..b07be6d 100644 --- a/src/test/kotlin/com/github/vokorm/ConnectionUtilsTest.kt +++ b/src/test/kotlin/com/github/vokorm/ConnectionUtilsTest.kt @@ -1,31 +1,30 @@ package com.github.vokorm -import com.github.mvysny.dynatest.DynaTest +import org.junit.jupiter.api.Nested +import org.junit.jupiter.api.Test import java.time.LocalDate import kotlin.test.expect -class ConnectionUtilsTest : DynaTest({ - usingH2Database() - - group("one column") { - test("empty dump") { +class ConnectionUtilsTest : AbstractH2DatabaseTest() { + @Nested inner class `one column` { + @Test fun `empty dump`() { expect("ID\n--\n--0 row(s)\n") { db { handle.createQuery("select id from Test").dump() } } } - test("two rows") { + @Test fun `two rows`() { Person(name = "Chuck", age = 25, dateOfBirth = LocalDate.of(2000, 1, 1)).save() Person(name = "Duke", age = 40, dateOfBirth = LocalDate.of(1999, 1, 1)).save() expect("NAME\n----\nChuck\nDuke\n----2 row(s)\n") { db { handle.createQuery("select name from Test").dump() } } } } - group("multiple columns") { - test("empty dump") { + @Nested inner class `multiple columns`() { + @Test fun `empty dump`() { expect("""ID, NAME, AGE, DATEOFBIRTH, CREATED, MODIFIED, ALIVE, MARITALSTATUS ------------------------------------------------------------------- -------------------------------------------------------------------0 row(s) """) { db { handle.createQuery("select * from Test").dump() } } } - test("two rows") { + @Test fun `two rows`() { Person(name = "Chuck", age = 25, dateOfBirth = LocalDate.of(2000, 1, 1)).save() Person(name = "Duke", age = 40, dateOfBirth = LocalDate.of(1999, 1, 1)).save() expect("""ID, NAME, AGE, DATEOFBIRTH, ALIVE, MARITALSTATUS @@ -36,4 +35,4 @@ class ConnectionUtilsTest : DynaTest({ """) { db { handle.createQuery("select id, name, age, dateofbirth, alive, maritalstatus from Test").dump() } } } } -}) +} diff --git a/src/test/kotlin/com/github/vokorm/Databases.kt b/src/test/kotlin/com/github/vokorm/Databases.kt index 8c05a0a..86fa0ee 100644 --- a/src/test/kotlin/com/github/vokorm/Databases.kt +++ b/src/test/kotlin/com/github/vokorm/Databases.kt @@ -206,40 +206,6 @@ fun hikari(block: HikariConfig.() -> Unit) { @DynaTestDsl fun DynaNodeGroup.usingH2Database() { - beforeGroup { - hikari { - jdbcUrl = "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1" - username = "sa" - password = "" - } - } - - afterGroup { JdbiOrm.destroy() } - - beforeEach { - db { - ddl("DROP ALL OBJECTS") - ddl("""CREATE ALIAS IF NOT EXISTS FTL_INIT FOR "org.h2.fulltext.FullTextLucene.init";CALL FTL_INIT();""") - ddl("""create table Test ( - id bigint primary key auto_increment, - name varchar not null, - age integer not null, - dateOfBirth date, - created timestamp, - modified timestamp, - alive boolean, - maritalStatus varchar - )""") - ddl("""create table EntityWithAliasedId(myid bigint primary key auto_increment, name varchar not null)""") - ddl("""create table NaturalPerson(id varchar(10) primary key, name varchar(400) not null, bytes binary(16) not null)""") - ddl("""create table LogRecord(id UUID primary key, text varchar(400) not null)""") - ddl("""create table TypeMappingEntity(id bigint primary key auto_increment, enumTest ENUM('Single', 'Married', 'Divorced', 'Widowed'))""") - ddl("""CALL FTL_CREATE_INDEX('PUBLIC', 'TEST', 'NAME');""") - } - } - afterEach { - db { ddl("DROP ALL OBJECTS") } - } } fun PersistenceContext.ddl(@Language("sql") sql: String) { @@ -364,11 +330,6 @@ WITH CHANGE_TRACKING AUTO --Population type; """) @DynaTestDsl fun DynaNodeGroup.withAllDatabases(block: DynaNodeGroup.(DatabaseInfo)->Unit) { - group("H2") { - usingH2Database() - block(DatabaseInfo(DatabaseVariant.H2)) - } - if (System.getProperty("h2only").toBoolean()) { println("`h2only` system property specified, skipping PostgreSQL/MySQL/MariaDB/MSSQL tests") } else if (!DockerClientFactory.instance().isDockerAvailable) { diff --git a/src/test/kotlin/com/github/vokorm/FiltersTest.kt b/src/test/kotlin/com/github/vokorm/FiltersTest.kt index 635fc0c..5a3d9f1 100644 --- a/src/test/kotlin/com/github/vokorm/FiltersTest.kt +++ b/src/test/kotlin/com/github/vokorm/FiltersTest.kt @@ -2,7 +2,6 @@ package com.github.vokorm import com.github.mvysny.dynatest.DynaNodeGroup import com.github.mvysny.dynatest.DynaTestDsl -import com.github.mvysny.dynatest.expectList @DynaTestDsl fun DynaNodeGroup.dbFiltersTest(info: DatabaseInfo) { diff --git a/src/test/kotlin/com/github/vokorm/MariadbDatabaseTest.kt b/src/test/kotlin/com/github/vokorm/MariadbDatabaseTest.kt index 52793e6..8b00cdc 100644 --- a/src/test/kotlin/com/github/vokorm/MariadbDatabaseTest.kt +++ b/src/test/kotlin/com/github/vokorm/MariadbDatabaseTest.kt @@ -11,6 +11,7 @@ class MariaDBDatabaseTest { private lateinit var container: MariaDBContainer<*> @BeforeAll @JvmStatic fun setup() { + Assumptions.assumeTrue(!h2only) { "Only H2 tests are running now" } assumeDockerAvailable() container = MariaDBContainer("mariadb:${DatabaseVersions.mariadb}") diff --git a/src/test/kotlin/com/github/vokorm/MssqlDatabaseTest.kt b/src/test/kotlin/com/github/vokorm/MssqlDatabaseTest.kt index de5c7bc..2400e99 100644 --- a/src/test/kotlin/com/github/vokorm/MssqlDatabaseTest.kt +++ b/src/test/kotlin/com/github/vokorm/MssqlDatabaseTest.kt @@ -11,6 +11,7 @@ class MssqlDatabaseTest { private lateinit var container: MSSQLServerContainer<*> @BeforeAll @JvmStatic fun setup() { + Assumptions.assumeTrue(!h2only) { "Only H2 tests are running now" } assumeDockerAvailable() Assumptions.assumeTrue(isX86_64) { "MSSQL is only available on amd64: https://hub.docker.com/_/microsoft-mssql-server/ " } Assumptions.assumeTrue(isWindows) { "MSSQL tests fail to run on GitHub+Linux; don't know why, don't care" } diff --git a/src/test/kotlin/com/github/vokorm/MysqlDatabaseTest.kt b/src/test/kotlin/com/github/vokorm/MysqlDatabaseTest.kt index 88f1277..08f1985 100644 --- a/src/test/kotlin/com/github/vokorm/MysqlDatabaseTest.kt +++ b/src/test/kotlin/com/github/vokorm/MysqlDatabaseTest.kt @@ -13,6 +13,7 @@ class MysqlDatabaseTest { @BeforeAll @JvmStatic fun runMysqlContainer() { + Assumptions.assumeTrue(!h2only) { "Only H2 tests are running now" } assumeDockerAvailable() container = MySQLContainer("mysql:${DatabaseVersions.mysql}") diff --git a/src/test/kotlin/com/github/vokorm/PosgresqlDatabaseTest.kt b/src/test/kotlin/com/github/vokorm/PosgresqlDatabaseTest.kt index f295008..e66e06c 100644 --- a/src/test/kotlin/com/github/vokorm/PosgresqlDatabaseTest.kt +++ b/src/test/kotlin/com/github/vokorm/PosgresqlDatabaseTest.kt @@ -12,6 +12,7 @@ class PosgresqlDatabaseTest { @BeforeAll @JvmStatic fun setup() { + Assumptions.assumeTrue(!h2only) { "Only H2 tests are running now" } assumeDockerAvailable() container = diff --git a/src/test/kotlin/com/github/vokorm/TestUtils.kt b/src/test/kotlin/com/github/vokorm/TestUtils.kt index 855f943..c9881e2 100644 --- a/src/test/kotlin/com/github/vokorm/TestUtils.kt +++ b/src/test/kotlin/com/github/vokorm/TestUtils.kt @@ -17,6 +17,7 @@ import kotlin.test.expect val isX86_64: Boolean get() = System.getProperty("os.arch") == "amd64" val isWindows: Boolean get() = System.getProperty("os.name").contains("Windows", ignoreCase = true) +val h2only: Boolean get() = System.getProperty("h2only").toBoolean() val gson: Gson = GsonBuilder().registerJavaTimeAdapters().create()