From 17e81de9e65dca3e68e489203908f99f8d68faf6 Mon Sep 17 00:00:00 2001 From: Andrew O'Hara Date: Fri, 21 Jul 2023 20:16:41 -0400 Subject: [PATCH] Add coverage for ColumnWithTransform --- .../entities/ColumnWithTransformTest.kt | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/entities/ColumnWithTransformTest.kt diff --git a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/entities/ColumnWithTransformTest.kt b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/entities/ColumnWithTransformTest.kt new file mode 100644 index 0000000000..3a61735063 --- /dev/null +++ b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/entities/ColumnWithTransformTest.kt @@ -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) : IntEntity(id) { + companion object : IntEntityClass(Transformations) + var value by Transformations.value.transform( + toColumn = { "transformed-$it" }, + toReal = { it.replace("transformed-", "") } + ) + } + class NullableTransformation(id: EntityID) : IntEntity(id) { + companion object : IntEntityClass(NullableTransformations) + var value by NullableTransformations.value.transform( + toColumn = { "transformed-$it" }, + toReal = { it?.replace("transformed-", "") } + ) + } +} + +class ColumnWithTransformTest: DatabaseTestsBase() { + + @Test fun `set and get value`() { + 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`() { + 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`() { + 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]) + } + } +}