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 6e59dc6 commit ba7a56a
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 1 deletion.
55 changes: 55 additions & 0 deletions src/test/kotlin/com/github/vokorm/AbstractDatabaseTests.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.github.vokorm

import org.jdbi.v3.core.Handle
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import java.io.IOException
import kotlin.test.expect

abstract class AbstractDatabaseTests(val info: DatabaseInfo) {
@Nested inner class DbFunTests : AbstractDbFunTests()
}

/**
* Tests the `db{}` method whether it manages transactions properly.
*/
abstract class AbstractDbFunTests() {
@Test fun verifyEntityManagerClosed() {
val em: Handle = db { handle }
expect(true) { em.connection.isClosed }
}
@Test fun exceptionRollsBack() {
assertThrows<IOException> {
db {
Person(name = "foo", age = 25).save()
expect(listOf(25)) { db { Person.findAll().map { it.age } } }
throw IOException("simulated")
}
}
expect(listOf()) { db { Person.findAll() } }
}
@Test fun commitInNestedDbBlocks() {
val person = db {
db {
db {
Person(name = "foo", age = 25).apply { save() }
}
}
}
expect(listOf(person)) { db { Person.findAll() } }
}
@Test fun exceptionRollsBackInNestedDbBlocks() {
assertThrows<IOException> {
db {
db {
db {
Person(name = "foo", age = 25).save()
throw IOException("simulated")
}
}
}
}
expect(listOf()) { Person.findAll() }
}
}
2 changes: 1 addition & 1 deletion src/test/kotlin/com/github/vokorm/Databases.kt
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ private fun DynaNodeGroup.usingDockerizedMariaDB() {
afterEach { clearDb() }
}

private fun clearDb() {
fun clearDb() {
Person.deleteAll()
EntityWithAliasedId.deleteAll()
NaturalPerson.deleteAll()
Expand Down
66 changes: 66 additions & 0 deletions src/test/kotlin/com/github/vokorm/H2DatabaseTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.github.vokorm

import com.gitlab.mvysny.jdbiorm.JdbiOrm
import com.gitlab.mvysny.jdbiorm.quirks.DatabaseVariant
import org.junit.jupiter.api.*
import kotlin.test.expect

/**
* Tests JDBI-ORM on H2.
*/
abstract class AbstractH2DatabaseTest {
companion object {
@BeforeAll
@JvmStatic
fun setupJdbi() {
hikari {
jdbcUrl = "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1"
username = "sa"
password = ""
}
}

@AfterAll
@JvmStatic
fun destroyJdbi() {
JdbiOrm.destroy()
}
}

@BeforeEach
fun setupDatabase() {
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
fun dropDatabase() {
db { ddl("DROP ALL OBJECTS") }
}

@Test
fun expectH2Variant() {
expect(DatabaseVariant.H2) { db { DatabaseVariant.from(handle) } }
}
}

class H2DatabaseTest : AbstractH2DatabaseTest() {
@Nested
inner class AllDatabaseTests : AbstractDatabaseTests(DatabaseInfo(DatabaseVariant.H2))
}
39 changes: 39 additions & 0 deletions src/test/kotlin/com/github/vokorm/TestUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,48 @@ package com.github.vokorm
import com.fatboyindustrial.gsonjavatime.Converters
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import org.junit.jupiter.api.Assumptions
import org.junit.jupiter.api.assertThrows
import org.testcontainers.DockerClientFactory
import java.io.ByteArrayOutputStream
import java.io.ObjectInputStream
import java.io.ObjectOutputStream
import java.io.Serializable
import kotlin.test.expect

val gson: Gson = GsonBuilder().registerJavaTimeAdapters().create()

private fun GsonBuilder.registerJavaTimeAdapters(): GsonBuilder = apply {
Converters.registerAll(this)
}

/**
* Expects that [actual] list of objects matches [expected] list of objects. Fails otherwise.
*/
fun <T> expectList(vararg expected: T, actual: ()->List<T>) {
expect(expected.toList(), actual)
}

inline fun <reified E: Throwable> expectThrows(msg: String, block: () -> Unit) {
val ex = assertThrows<E>(block)
expect(true) { ex.message!!.contains(msg) }
}

/**
* Clones this object by serialization and returns the deserialized clone.
* @return the clone of this
*/
fun <T : Serializable> T.cloneBySerialization(): T = javaClass.cast(serializeToBytes().deserialize())

inline fun <reified T: Serializable> ByteArray.deserialize(): T? = T::class.java.cast(
ObjectInputStream(inputStream()).readObject())

/**
* Serializes the object to a byte array
* @return the byte array containing this object serialized form.
*/
fun Serializable?.serializeToBytes(): ByteArray = ByteArrayOutputStream().also { ObjectOutputStream(it).writeObject(this) }.toByteArray()

fun assumeDockerAvailable() {
Assumptions.assumeTrue(DockerClientFactory.instance().isDockerAvailable(), "Docker not available")
}

0 comments on commit ba7a56a

Please sign in to comment.