Skip to content

Commit

Permalink
Migrate tests from DynaTest to JUnit5
Browse files Browse the repository at this point in the history
  • Loading branch information
mvysny committed Jan 31, 2025
1 parent 07e9762 commit 6512f2f
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 52 deletions.
2 changes: 1 addition & 1 deletion src/test/kotlin/com/github/vokorm/AbstractDatabaseTests.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
}

/**
Expand Down
125 changes: 125 additions & 0 deletions src/test/kotlin/com/github/vokorm/AbstractFiltersTest.kt
Original file line number Diff line number Diff line change
@@ -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")) }
}
}
}
1 change: 1 addition & 0 deletions src/test/kotlin/com/github/vokorm/CockroachDatabaseTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class CockroachDatabaseTest {
@BeforeAll
@JvmStatic
fun setup() {
Assumptions.assumeTrue(!h2only) { "Only H2 tests are running now" }
assumeDockerAvailable()

container =
Expand Down
21 changes: 10 additions & 11 deletions src/test/kotlin/com/github/vokorm/ConnectionUtilsTest.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -36,4 +35,4 @@ class ConnectionUtilsTest : DynaTest({
""") { db { handle.createQuery("select id, name, age, dateofbirth, alive, maritalstatus from Test").dump() } }
}
}
})
}
39 changes: 0 additions & 39 deletions src/test/kotlin/com/github/vokorm/Databases.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
1 change: 0 additions & 1 deletion src/test/kotlin/com/github/vokorm/FiltersTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions src/test/kotlin/com/github/vokorm/MariadbDatabaseTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down
1 change: 1 addition & 0 deletions src/test/kotlin/com/github/vokorm/MssqlDatabaseTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
1 change: 1 addition & 0 deletions src/test/kotlin/com/github/vokorm/MysqlDatabaseTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down
1 change: 1 addition & 0 deletions src/test/kotlin/com/github/vokorm/PosgresqlDatabaseTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class PosgresqlDatabaseTest {
@BeforeAll
@JvmStatic
fun setup() {
Assumptions.assumeTrue(!h2only) { "Only H2 tests are running now" }
assumeDockerAvailable()

container =
Expand Down
1 change: 1 addition & 0 deletions src/test/kotlin/com/github/vokorm/TestUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit 6512f2f

Please sign in to comment.