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

import com.github.vokorm.AbstractDbMappingTests
import org.jdbi.v3.core.Handle
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
Expand All @@ -9,6 +10,7 @@ import kotlin.test.expect

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

/**
Expand Down
181 changes: 181 additions & 0 deletions src/test/kotlin/com/github/vokorm/AbstractDbMappingTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
@file:Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN")

package com.github.vokorm

import com.github.mvysny.dynatest.*
import org.jdbi.v3.core.mapper.reflect.FieldMapper
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import java.lang.IllegalStateException
import java.lang.Long
import java.time.Instant
import java.time.LocalDate
import java.util.*
import kotlin.test.expect

class Foo(var maritalStatus: String? = null)

abstract class AbstractDbMappingTests() {
@Test fun FindAll() {
expectList() { Person.findAll() }
val p = Person(name = "Zaphod", age = 42, ignored2 = Object())
p.save()
expect(true) { p.id != null }
p.ignored2 = null
expectList(p) { Person.findAll() }
}
@Nested inner class PersonTests {
@Nested inner class SaveTests {
@Test fun Save() {
val p = Person(name = "Albedo", age = 130)
p.save()
expectList("Albedo") { Person.findAll().map { it.name } }
p.name = "Rubedo"
p.save()
expectList("Rubedo") { Person.findAll().map { it.name } }
Person(name = "Nigredo", age = 130).save()
expectList("Rubedo", "Nigredo") { Person.findAll().map { it.name } }
}
@Test fun SaveEnum() {
val p = Person(name = "Zaphod", age = 42, maritalStatus = MaritalStatus.Divorced)
p.save()
expectList("Divorced") {
db {
handle.createQuery("select maritalStatus from Test").map(FieldMapper.of(Foo::class.java)).list().map { it.maritalStatus }
}
}
expect(p) { db { Person.findAll()[0] } }
}
@Test fun SaveLocalDate() {
val p = Person(name = "Zaphod", age = 42, dateOfBirth = LocalDate.of(1990, 1, 14))
p.save()
expect(LocalDate.of(1990, 1, 14)) { db { Person.findAll()[0].dateOfBirth!! } }
}
@Test fun `save date and instant`() {
val p = Person(name = "Zaphod", age = 20, created = Date(1000), modified = Instant.ofEpochMilli(120398123))
p.save()
expect(1000) { db { Person.findAll()[0].created!!.time } }
expect(Instant.ofEpochMilli(120398123)) { db { Person.findAll()[0].modified!! } }
}
@Test fun `updating non-existing row fails`() {
val p = Person(id = 15, name = "Zaphod", age = 20, created = Date(1000), modified = Instant.ofEpochMilli(120398123))
expectThrows(IllegalStateException::class, "We expected to update only one row but we updated 0 - perhaps there is no row with id 15?") {
p.save()
}
}
}
@Test fun delete() {
val p = Person(name = "Albedo", age = 130)
p.save()
p.delete()
expectList() { Person.findAll() }
}
@Test fun JsonSerializationIgnoresMeta() {
expect("""{"name":"Zaphod","age":42}""") { gson.toJson(Person(name = "Zaphod", age = 42)) }
}
@Test fun Meta() {
val meta = Person.meta
expect("Test") { meta.databaseTableName } // since Person is annotated with @Entity("Test")
expect("Test.id") { meta.idProperty[0].dbName.qualifiedName }
expect(Person::class.java) { meta.entityClass }
expect(Long::class.java) { meta.idProperty[0].valueType }
expect(
setOf(
"id",
"name",
"age",
"dateOfBirth",
"created",
"alive",
"maritalStatus",
"modified"
)
) { meta.persistedFieldDbNames.map { it.unqualifiedName } .toSet() }
}
}
@Nested inner class EntityWithAliasedIdTests {
@Test fun Save() {
val p = EntityWithAliasedId(name = "Albedo")
p.save()
expectList("Albedo") { EntityWithAliasedId.findAll().map { it.name } }
p.name = "Rubedo"
p.save()
expectList("Rubedo") { EntityWithAliasedId.findAll().map { it.name } }
EntityWithAliasedId(name = "Nigredo").save()
expectList("Rubedo", "Nigredo") { EntityWithAliasedId.findAll().map { it.name } }
}
@Test fun delete() {
val p = EntityWithAliasedId(name = "Albedo")
p.save()
p.delete()
expect(listOf()) { EntityWithAliasedId.findAll() }
}
@Test fun JsonSerializationIgnoresMeta() {
expect("""{"name":"Zaphod"}""") { gson.toJson(EntityWithAliasedId(name = "Zaphod")) }
}
@Test fun Meta() {
val meta = EntityWithAliasedId.meta
expect("EntityWithAliasedId") { meta.databaseTableName }
expect("myid") { meta.idProperty[0].dbName.unqualifiedName }
expect(EntityWithAliasedId::class.java) { meta.entityClass }
expect(Long::class.java) { meta.idProperty[0].valueType }
expect(setOf("myid", "name")) { meta.persistedFieldDbNames.map { it.unqualifiedName } .toSet() }
}
}
@Nested inner class NaturalPersonTests {
@Test fun `save fails`() {
val p = NaturalPerson(id = "12345678", name = "Albedo", bytes = byteArrayOf(5))
expectThrows<IllegalStateException>("We expected to update only one row but we updated 0 - perhaps there is no row with id 12345678?") {
p.save()
}
}
@Test fun Save() {
val p = NaturalPerson(id = "12345678", name = "Albedo", bytes = byteArrayOf(5))
p.create()
expectList("Albedo") { NaturalPerson.findAll().map { it.name } }
p.name = "Rubedo"
p.save()
expectList("Rubedo") { NaturalPerson.findAll().map { it.name } }
NaturalPerson(id = "aaa", name = "Nigredo", bytes = byteArrayOf(5)).create()
expectList("Rubedo", "Nigredo") { NaturalPerson.findAll().map { it.name } }
}
@Test fun delete() {
val p = NaturalPerson(id = "foo", name = "Albedo", bytes = byteArrayOf(5))
p.create()
p.delete()
expectList() { NaturalPerson.findAll() }
}
}
@Nested inner class LogRecordTests() {
@Test fun `save succeeds since create() auto-generates ID`() {
val p = LogRecord(text = "foo")
p.save()
expectList("foo") { LogRecord.findAll().map { it.text } }
}
@Test fun Save() {
val p = LogRecord(text = "Albedo")
p.save()
expectList("Albedo") { LogRecord.findAll().map { it.text } }
p.text = "Rubedo"
p.save()
expectList("Rubedo") { LogRecord.findAll().map { it.text } }
LogRecord(text = "Nigredo").save()
expect(setOf("Rubedo", "Nigredo")) { LogRecord.findAll().map { it.text } .toSet() }
}
@Test fun delete() {
val p = LogRecord(text = "foo")
p.save()
p.delete()
expectList() { LogRecord.findAll() }
}
}
@Nested inner class TypeMapping {
@Test fun `java enum to native db enum`() {
for (it in MaritalStatus.entries.plusNull) {
val id: kotlin.Long? = TypeMappingEntity(enumTest = it).run { save(); id }
val loaded = TypeMappingEntity.findById(id!!)!!
expect(it) { loaded.enumTest }
}
}
}
}
1 change: 0 additions & 1 deletion src/test/kotlin/com/github/vokorm/MappingTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import java.time.LocalDate
import java.util.*
import kotlin.test.expect

class Foo(var maritalStatus: String? = null)
@DynaTestDsl
fun DynaNodeGroup.dbMappingTests() {
test("FindAll") {
Expand Down

0 comments on commit 2df3045

Please sign in to comment.