Skip to content

Commit

Permalink
Add test for Issue #12
Browse files Browse the repository at this point in the history
  • Loading branch information
mvysny committed Feb 25, 2019
1 parent 6a4209e commit 279892d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ dependencies {
testCompile("ch.qos.logback:logback-classic:1.2.3")
testCompile("com.h2database:h2:1.4.197")

testCompile("org.postgresql:postgresql:42.2.1")
testCompile("org.postgresql:postgresql:42.2.5")
testCompile("org.zeroturnaround:zt-exec:1.10")
testCompile("mysql:mysql-connector-java:5.1.47")
testCompile("org.mariadb.jdbc:mariadb-java-client:2.3.0")
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/com/github/vokorm/Mapping.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import javax.validation.ValidationException
import kotlin.reflect.KProperty1

/**
* Optional annotation which allows you to change the table name.
* Optional annotation which allows you to change the database table name.
* @property dbname the database table name; defaults to an empty string which will use the [Class.getSimpleName] as the table name.
*/
@Target(AnnotationTarget.CLASS)
Expand Down Expand Up @@ -119,7 +119,7 @@ interface Entity<ID: Any> : Serializable {
create(validate = false) // no need to validate again
} else {
val fields = meta.persistedFieldDbNames - meta.idProperty.dbColumnName
con.createQuery("update ${meta.databaseTableName} set ${fields.map { "$it = :$it" }.joinToString()} where ${meta.idProperty.dbColumnName} = :${meta.idProperty.dbColumnName}")
con.createQuery("update ${meta.databaseTableName} set ${fields.joinToString { "$it = :$it" }} where ${meta.idProperty.dbColumnName} = :${meta.idProperty.dbColumnName}")
.bindAliased(this@Entity)
.setColumnMappings(meta.getSql2oColumnMappings())
.executeUpdate()
Expand Down
20 changes: 18 additions & 2 deletions src/test/kotlin/com/github/vokorm/Databases.kt
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,23 @@ interface UuidEntity : Entity<UUID> {
/**
* Demoes app-generated UUID ids. Note how [create] is overridden to auto-generate the ID, so that [save] works properly.
*
* Warning: do NOT add any additional fields in here, since that would mysteriously make the compiler generate
* `void setId(UUID)` instead of `void setId(Object)` and we wouldn't test the metadata hook that fixes this issue.
* Warning: do NOT add any additional fields in here, since that would make java place synthetic `setId(Object) before
* `setId(UUID)` and we wouldn't test the metadata hook that fixes this issue.
*/
data class LogRecord(override var id: UUID? = null, var text: String) : UuidEntity {
companion object : Dao<LogRecord>
}

/**
* Tests all sorts of type mapping:
* @property enumTest tests Java Enum mapping to native database enum mapping: https://github.com/mvysny/vok-orm/issues/12
*/
data class TypeMappingEntity(override var id: Long? = null,
var enumTest: MaritalStatus? = null
) : Entity<Long> {
companion object : Dao<TypeMappingEntity>
}

private fun DynaNodeGroup.usingDockerizedPosgresql(databasePort: Int) {
check(Docker.isPresent) { "Docker not available" }
beforeGroup { Docker.startPostgresql(port = databasePort) }
Expand All @@ -114,6 +124,8 @@ private fun DynaNodeGroup.usingDockerizedPosgresql(databasePort: Int) {
ddl("""create table if not exists EntityWithAliasedId(myid bigserial primary key, name varchar(400) not null)""")
ddl("""create table if not exists NaturalPerson(id varchar(10) primary key, name varchar(400) not null, bytes bytea not null)""")
ddl("""create table if not exists LogRecord(id UUID primary key, text varchar(400) not null)""")
ddl("""CREATE TYPE marital_status AS ENUM ('Single', 'Married', 'Widowed', 'Divorced')""")
ddl("""CREATE TABLE IF NOT EXISTS TypeMappingEntity(id bigserial primary key, enumTest marital_status)""")
}
}

Expand Down Expand Up @@ -156,6 +168,7 @@ fun DynaNodeGroup.usingDockerizedMysql(databasePort: Int) {
ddl("""create table if not exists EntityWithAliasedId(myid bigint primary key auto_increment, name varchar(400) not null)""")
ddl("""create table if not exists NaturalPerson(id varchar(10) primary key, name varchar(400) not null, bytes binary(16) not null)""")
ddl("""create table if not exists LogRecord(id binary(16) primary key, text varchar(400) not null)""")
ddl("""create table TypeMappingEntity(id bigint primary key auto_increment, enumTest ENUM('Single', 'Married', 'Divorced', 'Widowed'))""")
}
}

Expand All @@ -167,6 +180,7 @@ fun DynaNodeGroup.usingDockerizedMysql(databasePort: Int) {
EntityWithAliasedId.deleteAll()
NaturalPerson.deleteAll()
LogRecord.deleteAll()
TypeMappingEntity.deleteAll()
}
beforeEach { clearDb() }
afterEach { clearDb() }
Expand Down Expand Up @@ -200,6 +214,7 @@ fun DynaNodeGroup.usingH2Database() {
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'))""")
}
}
afterEach {
Expand Down Expand Up @@ -239,6 +254,7 @@ private fun DynaNodeGroup.usingDockerizedMariaDB(databasePort: Int) {
ddl("""create table if not exists EntityWithAliasedId(myid bigint primary key auto_increment, name varchar(400) not null)""")
ddl("""create table if not exists NaturalPerson(id varchar(10) primary key, name varchar(400) not null, bytes binary(16) not null)""")
ddl("""create table if not exists LogRecord(id binary(16) primary key, text varchar(400) not null)""")
ddl("""create table TypeMappingEntity(id bigint primary key auto_increment, enumTest ENUM('Single', 'Married', 'Divorced', 'Widowed'))""")
}
}

Expand Down
10 changes: 10 additions & 0 deletions src/test/kotlin/com/github/vokorm/MappingTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,17 @@ class MappingTest : DynaTest({
expectList() { LogRecord.findAll() }
}
}
group("TypeMapping") {
test("java enum to native db enum") {
for (it in MaritalStatus.values().plusNull) {
val id: kotlin.Long? = TypeMappingEntity(enumTest = it).run { save(); id }
val loaded = TypeMappingEntity.findById(id!!)!!
expect(it) { loaded.enumTest }
}
}
}
}
})

val Instant.withZeroNanos: Instant get() = with(ChronoField.NANO_OF_SECOND, get(ChronoField.MILLI_OF_SECOND).toLong() * 1000000)
val <T> Array<T>.plusNull: List<T?> get() = toList<T?>() + listOf(null)

0 comments on commit 279892d

Please sign in to comment.