Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Add test coverage for column transforms #1687

Merged
merged 2 commits into from
Jul 24, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package org.jetbrains.exposed.sql.tests.shared.entities

import org.jetbrains.exposed.dao.IntEntity
import org.jetbrains.exposed.dao.IntEntityClass
import org.jetbrains.exposed.dao.id.EntityID
import org.jetbrains.exposed.dao.id.IntIdTable
import org.jetbrains.exposed.sql.Op
import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.tests.DatabaseTestsBase
import org.jetbrains.exposed.sql.tests.shared.assertEquals
import org.junit.Test

object TransformTables {
object Transformations : IntIdTable() {
val value = varchar("value", 50)
}
object NullableTransformations: IntIdTable() {
val value = varchar("nullable", 50).nullable()
}
class Transformation(id: EntityID<Int>) : IntEntity(id) {
companion object : IntEntityClass<Transformation>(Transformations)
var value by Transformations.value.transform(
toColumn = { "transformed-$it" },
toReal = { it.replace("transformed-", "") }
)
}
class NullableTransformation(id: EntityID<Int>) : IntEntity(id) {
companion object : IntEntityClass<NullableTransformation>(NullableTransformations)
var value by NullableTransformations.value.transform(
toColumn = { "transformed-$it" },
toReal = { it?.replace("transformed-", "") }
)
}
}
joc-a marked this conversation as resolved.
Show resolved Hide resolved

class ColumnWithTransformTest: DatabaseTestsBase() {

@Test fun `set and get value`() {
joc-a marked this conversation as resolved.
Show resolved Hide resolved
withTables(TransformTables.Transformations) {
val entity = TransformTables.Transformation.new {
value = "stuff"
}

assertEquals("stuff", entity.value)

val row = TransformTables.Transformations.select(Op.TRUE)
.first()

assertEquals("transformed-stuff", row[TransformTables.Transformations.value])
}
}

@Test fun `set and get nullable value - while present`() {
joc-a marked this conversation as resolved.
Show resolved Hide resolved
withTables(TransformTables.NullableTransformations) {
val entity = TransformTables.NullableTransformation.new {
value = "stuff"
}

assertEquals("stuff", entity.value)

val row = TransformTables.NullableTransformations.select(Op.TRUE)
.first()

assertEquals("transformed-stuff", row[TransformTables.NullableTransformations.value])
}
}

@Test fun `set and get nullable value - while absent`() {
joc-a marked this conversation as resolved.
Show resolved Hide resolved
withTables(TransformTables.NullableTransformations) {
val entity = TransformTables.NullableTransformation.new {}

assertEquals(null, entity.value)

val row = TransformTables.NullableTransformations.select(Op.TRUE)
.first()

assertEquals(null, row[TransformTables.NullableTransformations.value])
}
}
}
Loading