-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate tests from DynaTest to JUnit5
- Loading branch information
Showing
4 changed files
with
161 additions
and
1 deletion.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
src/test/kotlin/com/github/vokorm/AbstractDatabaseTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters