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 jsonb datetime tests #1780

Merged
merged 1 commit into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions exposed-java-time/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import org.gradle.api.tasks.testing.logging.TestLogEvent

plugins {
kotlin("jvm") apply true
kotlin("plugin.serialization") apply true
id("testWithDBs")
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package org.jetbrains.exposed

import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.descriptors.*
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.json.Json
import org.jetbrains.exposed.dao.id.IntIdTable
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.between
Expand All @@ -10,6 +16,7 @@ import org.jetbrains.exposed.sql.tests.DatabaseTestsBase
import org.jetbrains.exposed.sql.tests.TestDB
import org.jetbrains.exposed.sql.tests.currentDialectTest
import org.jetbrains.exposed.sql.tests.shared.assertEquals
import org.jetbrains.exposed.sql.tests.shared.assertTrue
import org.jetbrains.exposed.sql.vendors.*
import org.junit.Assert.fail
import org.junit.Test
Expand Down Expand Up @@ -261,6 +268,42 @@ open class JavaTimeBaseTest : DatabaseTestsBase() {
assertEquals(2, createdIn2023.size)
}
}

@Test
fun testDateTimeAsJsonB() {
val tester = object : Table("tester") {
val created = datetime("created")
val modified = jsonb<ModifierData>("modified", Json.Default)
}

withTables(excludeSettings = TestDB.allH2TestDB + TestDB.SQLITE + TestDB.SQLSERVER + TestDB.ORACLE, tester) {
val dateTimeNow = LocalDateTime.now()
tester.insert {
it[created] = dateTimeNow.minusYears(1)
it[modified] = ModifierData(1, dateTimeNow)
}
tester.insert {
it[created] = dateTimeNow.plusYears(1)
it[modified] = ModifierData(2, dateTimeNow)
}

val prefix = if (currentDialectTest is PostgreSQLDialect) "" else "."

// value extracted in same manner it is stored, a json string
val modifiedAsString = tester.modified.jsonExtract<String>("${prefix}timestamp")
val allModifiedAsString = tester.slice(modifiedAsString).selectAll()
assertTrue(allModifiedAsString.all { it[modifiedAsString] == dateTimeNow.toString() })

// PostgreSQL requires explicit type cast to timestamp for in-DB comparison
val dateModified = if (currentDialectTest is PostgreSQLDialect) {
tester.modified.jsonExtract<LocalDateTime>("${prefix}timestamp").castTo(JavaLocalDateTimeColumnType())
} else {
tester.modified.jsonExtract<LocalDateTime>("${prefix}timestamp")
}
val modifiedBeforeCreation = tester.select { dateModified less tester.created }.single()
assertEquals(2, modifiedBeforeCreation[tester.modified].userId)
}
}
}

fun <T : Temporal> assertEqualDateTime(d1: T?, d2: T?) {
Expand Down Expand Up @@ -337,3 +380,16 @@ object CitiesTime : IntIdTable("CitiesTime") {
val name = varchar("name", 50) // Column<String>
val local_time = datetime("local_time").nullable() // Column<datetime>
}

@Serializable
data class ModifierData(
val userId: Int,
@Serializable(with = DateTimeSerializer::class)
val timestamp: LocalDateTime
)

object DateTimeSerializer : KSerializer<LocalDateTime> {
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("LocalDateTime", PrimitiveKind.STRING)
override fun serialize(encoder: Encoder, value: LocalDateTime) = encoder.encodeString(value.toString())
override fun deserialize(decoder: Decoder): LocalDateTime = LocalDateTime.parse(decoder.decodeString())
}
1 change: 1 addition & 0 deletions exposed-jodatime/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import org.jetbrains.exposed.gradle.Versions

plugins {
kotlin("jvm") apply true
kotlin("plugin.serialization") apply true
id("testWithDBs")
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package org.jetbrains.exposed

import kotlinx.serialization.*
import kotlinx.serialization.descriptors.*
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.json.Json
import org.jetbrains.exposed.dao.id.IntIdTable
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.between
Expand All @@ -10,6 +15,7 @@ import org.jetbrains.exposed.sql.tests.DatabaseTestsBase
import org.jetbrains.exposed.sql.tests.TestDB
import org.jetbrains.exposed.sql.tests.currentDialectTest
import org.jetbrains.exposed.sql.tests.shared.assertEquals
import org.jetbrains.exposed.sql.tests.shared.assertTrue
import org.jetbrains.exposed.sql.vendors.*
import org.joda.time.DateTime
import org.joda.time.DateTimeZone
Expand Down Expand Up @@ -177,6 +183,42 @@ open class JodaTimeBaseTest : DatabaseTestsBase() {
assertEquals(2, createdIn2023.size)
}
}

@Test
fun testDateTimeAsJsonB() {
val tester = object : Table("tester") {
val created = datetime("created")
val modified = jsonb<ModifierData>("modified", Json.Default)
}

withTables(excludeSettings = TestDB.allH2TestDB + TestDB.SQLITE + TestDB.SQLSERVER + TestDB.ORACLE, tester) {
val dateTimeNow = DateTime.now()
tester.insert {
it[created] = dateTimeNow.minusYears(1)
it[modified] = ModifierData(1, dateTimeNow)
}
tester.insert {
it[created] = dateTimeNow.plusYears(1)
it[modified] = ModifierData(2, dateTimeNow)
}

val prefix = if (currentDialectTest is PostgreSQLDialect) "" else "."

// value extracted in same manner it is stored, a json string
val modifiedAsString = tester.modified.jsonExtract<String>("${prefix}timestamp")
val allModifiedAsString = tester.slice(modifiedAsString).selectAll()
assertTrue(allModifiedAsString.all { it[modifiedAsString] == dateTimeNow.toString() })

// PostgreSQL requires explicit type cast to timestamp for in-DB comparison
val dateModified = if (currentDialectTest is PostgreSQLDialect) {
tester.modified.jsonExtract<DateTime>("${prefix}timestamp").castTo(DateColumnType(true))
} else {
tester.modified.jsonExtract<DateTime>("${prefix}timestamp")
}
val modifiedBeforeCreation = tester.select { dateModified less tester.created }.single()
assertEquals(2, modifiedBeforeCreation[tester.modified].userId)
}
}
}

fun assertEqualDateTime(d1: DateTime?, d2: DateTime?) {
Expand All @@ -201,3 +243,16 @@ object CitiesTime : IntIdTable("CitiesTime") {
val name = varchar("name", 50) // Column<String>
val local_time = datetime("local_time").nullable() // Column<datetime>
}

@Serializable
data class ModifierData(
val userId: Int,
@Serializable(with = DateTimeSerializer::class)
val timestamp: DateTime
)

object DateTimeSerializer : KSerializer<DateTime> {
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("DateTime", PrimitiveKind.STRING)
override fun serialize(encoder: Encoder, value: DateTime) = encoder.encodeString(value.toString())
override fun deserialize(decoder: Decoder): DateTime = DateTime.parse(decoder.decodeString())
}
1 change: 1 addition & 0 deletions exposed-kotlin-datetime/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import org.gradle.api.tasks.testing.logging.TestLogEvent

plugins {
kotlin("jvm") apply true
kotlin("plugin.serialization") apply true
id("testWithDBs")
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.jetbrains.exposed.sql.kotlin.datetime

import kotlinx.datetime.*
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import org.jetbrains.exposed.dao.id.IntIdTable
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.between
Expand All @@ -10,6 +12,7 @@ import org.jetbrains.exposed.sql.tests.DatabaseTestsBase
import org.jetbrains.exposed.sql.tests.TestDB
import org.jetbrains.exposed.sql.tests.currentDialectTest
import org.jetbrains.exposed.sql.tests.shared.assertEquals
import org.jetbrains.exposed.sql.tests.shared.assertTrue
import org.jetbrains.exposed.sql.vendors.*
import org.junit.Assert.fail
import org.junit.Test
Expand Down Expand Up @@ -256,6 +259,46 @@ open class KotlinTimeBaseTest : DatabaseTestsBase() {
assertEquals(2, createdIn2023.size)
}
}

@Test
fun testDateTimeAsJsonB() {
val tester = object : Table("tester") {
val created = datetime("created")
val modified = jsonb<ModifierData>("modified", Json.Default)
}

withTables(excludeSettings = TestDB.allH2TestDB + TestDB.SQLITE + TestDB.SQLSERVER + TestDB.ORACLE, tester) {
val dateTimeNow = now()
tester.insert {
it[created] = dateTimeNow.date.minus(1, DateTimeUnit.YEAR).atTime(0, 0, 0)
it[modified] = ModifierData(1, dateTimeNow)
}
tester.insert {
it[created] = dateTimeNow.date.plus(1, DateTimeUnit.YEAR).atTime(0, 0, 0)
it[modified] = ModifierData(2, dateTimeNow)
}

val prefix = if (currentDialectTest is PostgreSQLDialect) "" else "."

// value extracted in same manner it is stored, a json string
val modifiedAsString = tester.modified.jsonExtract<String>("${prefix}timestamp")
val allModifiedAsString = tester.slice(modifiedAsString).selectAll()
assertTrue(allModifiedAsString.all { it[modifiedAsString] == dateTimeNow.toString() })
// value extracted as json, with implicit LocalDateTime serializer() performing conversions
val modifiedAsJson = tester.modified.jsonExtract<LocalDateTime>("${prefix}timestamp", toScalar = false)
val allModifiedAsJson = tester.slice(modifiedAsJson).selectAll()
assertTrue(allModifiedAsJson.all { it[modifiedAsJson] == dateTimeNow })

// PostgreSQL requires explicit type cast to timestamp for in-DB comparison
val dateModified = if (currentDialectTest is PostgreSQLDialect) {
tester.modified.jsonExtract<LocalDateTime>("${prefix}timestamp").castTo(KotlinLocalDateTimeColumnType())
} else {
tester.modified.jsonExtract<LocalDateTime>("${prefix}timestamp")
}
val modifiedBeforeCreation = tester.select { dateModified less tester.created }.single()
assertEquals(2, modifiedBeforeCreation[tester.modified].userId)
}
}
}

fun <T> assertEqualDateTime(d1: T?, d2: T?) {
Expand Down Expand Up @@ -332,3 +375,6 @@ object CitiesTime : IntIdTable("CitiesTime") {
val name = varchar("name", 50) // Column<String>
val local_time = datetime("local_time").nullable() // Column<datetime>
}

@Serializable
data class ModifierData(val userId: Int, val timestamp: LocalDateTime)