Skip to content

Commit

Permalink
fix: EXPOSED-412 Remove all the usage of isOldMySql function in tests (
Browse files Browse the repository at this point in the history
…#2126)

* fix: EXPOSED-412 Remove all the usage of isOldMySql function in tests
  • Loading branch information
obabichevjb authored Jun 19, 2024
1 parent 5898d2b commit c514f9f
Show file tree
Hide file tree
Showing 32 changed files with 618 additions and 673 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ subprojects {
}
}

testDb("mysql") {
testDb("mysql5") {
port = 3001
dialects("MYSQL_V5")
dependencies {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: "3.8"

services:
mysql:
mysql5:
platform: linux/x86_64
restart: always
image: mysql:5.7
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,44 +416,38 @@ class DefaultsTest : DatabaseTestsBase() {
else -> "NULL"
}

withDb(excludeSettings = listOf(TestDB.SQLITE, TestDB.MARIADB)) {
if (!isOldMySql()) {
SchemaUtils.create(testTable)

val timestampWithTimeZoneType = currentDialectTest.dataTypeProvider.timestampWithTimeZoneType()

val baseExpression = "CREATE TABLE " + addIfNotExistsIfSupported() +
"${"t".inProperCase()} (" +
"${"id".inProperCase()} ${currentDialectTest.dataTypeProvider.integerAutoincType()} PRIMARY KEY, " +
"${"t1".inProperCase()} $timestampWithTimeZoneType${testTable.t1.constraintNamePart()} ${timestampWithTimeZoneLiteral.itOrNull()}, " +
"${"t2".inProperCase()} $timestampWithTimeZoneType${testTable.t2.constraintNamePart()} ${timestampWithTimeZoneLiteral.itOrNull()}, " +
"${"t3".inProperCase()} $timestampWithTimeZoneType${testTable.t3.constraintNamePart()} ${CurrentTimestampWithTimeZone.itOrNull()}" +
")"

val expected = if (currentDialectTest is OracleDialect ||
currentDialectTest.h2Mode == H2Dialect.H2CompatibilityMode.Oracle
) {
arrayListOf(
"CREATE SEQUENCE t_id_seq START WITH 1 MINVALUE 1 MAXVALUE 9223372036854775807",
baseExpression
)
} else {
arrayListOf(baseExpression)
}
withTables(excludeSettings = TestDB.ALL_MARIADB + TestDB.MYSQL_V5 + TestDB.SQLITE, testTable) {
val timestampWithTimeZoneType = currentDialectTest.dataTypeProvider.timestampWithTimeZoneType()

assertEqualLists(expected, testTable.ddl)
val baseExpression = "CREATE TABLE " + addIfNotExistsIfSupported() +
"${"t".inProperCase()} (" +
"${"id".inProperCase()} ${currentDialectTest.dataTypeProvider.integerAutoincType()} PRIMARY KEY, " +
"${"t1".inProperCase()} $timestampWithTimeZoneType${testTable.t1.constraintNamePart()} ${timestampWithTimeZoneLiteral.itOrNull()}, " +
"${"t2".inProperCase()} $timestampWithTimeZoneType${testTable.t2.constraintNamePart()} ${timestampWithTimeZoneLiteral.itOrNull()}, " +
"${"t3".inProperCase()} $timestampWithTimeZoneType${testTable.t3.constraintNamePart()} ${CurrentTimestampWithTimeZone.itOrNull()}" +
")"

val expected = if (currentDialectTest is OracleDialect ||
currentDialectTest.h2Mode == H2Dialect.H2CompatibilityMode.Oracle
) {
arrayListOf(
"CREATE SEQUENCE t_id_seq START WITH 1 MINVALUE 1 MAXVALUE 9223372036854775807",
baseExpression
)
} else {
arrayListOf(baseExpression)
}

val id1 = testTable.insertAndGetId { }
assertEqualLists(expected, testTable.ddl)

val row1 = testTable.selectAll().where { testTable.id eq id1 }.single()
assertEqualDateTime(nowWithTimeZone, row1[testTable.t1])
assertEqualDateTime(nowWithTimeZone, row1[testTable.t2])
val dbDefault = row1[testTable.t3]
assertEquals(dbDefault.offset, nowWithTimeZone.offset)
assertTrue { dbDefault.toLocalDateTime() >= nowWithTimeZone.toLocalDateTime() }
val id1 = testTable.insertAndGetId { }

SchemaUtils.drop(testTable)
}
val row1 = testTable.selectAll().where { testTable.id eq id1 }.single()
assertEqualDateTime(nowWithTimeZone, row1[testTable.t1])
assertEqualDateTime(nowWithTimeZone, row1[testTable.t2])
val dbDefault = row1[testTable.t3]
assertEquals(dbDefault.offset, nowWithTimeZone.offset)
assertTrue { dbDefault.toLocalDateTime() >= nowWithTimeZone.toLocalDateTime() }
}
}

Expand Down Expand Up @@ -558,18 +552,10 @@ class DefaultsTest : DatabaseTestsBase() {

// SQLite does not support ALTER TABLE on a column that has a default value
// MariaDB does not support TIMESTAMP WITH TIME ZONE column type
val unsupportedDatabases = listOf(TestDB.SQLITE, TestDB.MARIADB)
withDb(excludeSettings = unsupportedDatabases) {
if (!isOldMySql()) {
try {
SchemaUtils.drop(tester)
SchemaUtils.create(tester)
val statements = SchemaUtils.addMissingColumnsStatements(tester)
assertEquals(0, statements.size)
} finally {
SchemaUtils.drop(tester)
}
}
val unsupportedDatabases = TestDB.ALL_MARIADB + listOf(TestDB.SQLITE, TestDB.MYSQL_V5)
withTables(excludeSettings = unsupportedDatabases, tester) {
val statements = SchemaUtils.addMissingColumnsStatements(tester)
assertEquals(0, statements.size)
}
}
}
220 changes: 105 additions & 115 deletions exposed-java-time/src/test/kotlin/org/jetbrains/exposed/JavaTimeTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import kotlin.test.assertEquals

open class JavaTimeBaseTest : DatabaseTestsBase() {

private val timestampWithTimeZoneUnsupportedDB = TestDB.ALL_MARIADB + TestDB.MYSQL_V5

@Test
fun javaTimeFunctions() {
withTables(CitiesTime) {
Expand Down Expand Up @@ -363,82 +365,72 @@ open class JavaTimeBaseTest : DatabaseTestsBase() {
val timestampWithTimeZone = timestampWithTimeZone("timestamptz-column")
}

withDb(excludeSettings = listOf(TestDB.MARIADB, TestDB.MYSQL_V8)) { testDB ->
if (!isOldMySql()) {
SchemaUtils.create(testTable)

// Cairo time zone
java.util.TimeZone.setDefault(java.util.TimeZone.getTimeZone("Africa/Cairo"))
assertEquals("Africa/Cairo", ZoneId.systemDefault().id)

val cairoNow = OffsetDateTime.now(ZoneId.systemDefault())

val cairoId = testTable.insertAndGetId {
it[timestampWithTimeZone] = cairoNow
}
withTables(excludeSettings = timestampWithTimeZoneUnsupportedDB, testTable) { testDB ->
// Cairo time zone
java.util.TimeZone.setDefault(java.util.TimeZone.getTimeZone("Africa/Cairo"))
assertEquals("Africa/Cairo", ZoneId.systemDefault().id)

val cairoNowInsertedInCairoTimeZone = testTable.selectAll().where { testTable.id eq cairoId }
.single()[testTable.timestampWithTimeZone]
val cairoNow = OffsetDateTime.now(ZoneId.systemDefault())

// UTC time zone
java.util.TimeZone.setDefault(java.util.TimeZone.getTimeZone(ZoneOffset.UTC))
assertEquals("UTC", ZoneId.systemDefault().id)
val cairoId = testTable.insertAndGetId {
it[timestampWithTimeZone] = cairoNow
}

val cairoNowRetrievedInUTCTimeZone = testTable.selectAll().where { testTable.id eq cairoId }
.single()[testTable.timestampWithTimeZone]
val cairoNowInsertedInCairoTimeZone = testTable.selectAll().where { testTable.id eq cairoId }
.single()[testTable.timestampWithTimeZone]

val utcID = testTable.insertAndGetId {
it[timestampWithTimeZone] = cairoNow
}
// UTC time zone
java.util.TimeZone.setDefault(java.util.TimeZone.getTimeZone(ZoneOffset.UTC))
assertEquals("UTC", ZoneId.systemDefault().id)

val cairoNowInsertedInUTCTimeZone = testTable.selectAll().where { testTable.id eq utcID }
.single()[testTable.timestampWithTimeZone]
val cairoNowRetrievedInUTCTimeZone = testTable.selectAll().where { testTable.id eq cairoId }
.single()[testTable.timestampWithTimeZone]

// Tokyo time zone
java.util.TimeZone.setDefault(java.util.TimeZone.getTimeZone("Asia/Tokyo"))
assertEquals("Asia/Tokyo", ZoneId.systemDefault().id)
val utcID = testTable.insertAndGetId {
it[timestampWithTimeZone] = cairoNow
}

val cairoNowRetrievedInTokyoTimeZone = testTable.selectAll().where { testTable.id eq cairoId }
.single()[testTable.timestampWithTimeZone]
val cairoNowInsertedInUTCTimeZone = testTable.selectAll().where { testTable.id eq utcID }
.single()[testTable.timestampWithTimeZone]

val tokyoID = testTable.insertAndGetId {
it[timestampWithTimeZone] = cairoNow
}
// Tokyo time zone
java.util.TimeZone.setDefault(java.util.TimeZone.getTimeZone("Asia/Tokyo"))
assertEquals("Asia/Tokyo", ZoneId.systemDefault().id)

val cairoNowInsertedInTokyoTimeZone = testTable.selectAll().where { testTable.id eq tokyoID }
.single()[testTable.timestampWithTimeZone]
val cairoNowRetrievedInTokyoTimeZone = testTable.selectAll().where { testTable.id eq cairoId }
.single()[testTable.timestampWithTimeZone]

// PostgreSQL and MySQL always store the timestamp in UTC, thereby losing the original time zone.
// To preserve the original time zone, store the time zone information in a separate column.
val isOriginalTimeZonePreserved = testDB !in listOf(
TestDB.POSTGRESQL,
TestDB.POSTGRESQLNG,
TestDB.MYSQL_V5
)
if (isOriginalTimeZonePreserved) {
// Assert that time zone is preserved when the same value is inserted in different time zones
assertEqualDateTime(cairoNow, cairoNowInsertedInCairoTimeZone)
assertEqualDateTime(cairoNow, cairoNowInsertedInUTCTimeZone)
assertEqualDateTime(cairoNow, cairoNowInsertedInTokyoTimeZone)

// Assert that time zone is preserved when the same record is retrieved in different time zones
assertEqualDateTime(cairoNow, cairoNowRetrievedInUTCTimeZone)
assertEqualDateTime(cairoNow, cairoNowRetrievedInTokyoTimeZone)
} else {
// Assert equivalence in UTC when the same value is inserted in different time zones
assertEqualDateTime(cairoNowInsertedInCairoTimeZone, cairoNowInsertedInUTCTimeZone)
assertEqualDateTime(cairoNowInsertedInUTCTimeZone, cairoNowInsertedInTokyoTimeZone)

// Assert equivalence in UTC when the same record is retrieved in different time zones
assertEqualDateTime(cairoNowRetrievedInUTCTimeZone, cairoNowRetrievedInTokyoTimeZone)
}
val tokyoID = testTable.insertAndGetId {
it[timestampWithTimeZone] = cairoNow
}

// Reset to original time zone as set up in DatabaseTestsBase init block
java.util.TimeZone.setDefault(java.util.TimeZone.getTimeZone(ZoneOffset.UTC))
assertEquals("UTC", ZoneId.systemDefault().id)
val cairoNowInsertedInTokyoTimeZone = testTable.selectAll().where { testTable.id eq tokyoID }
.single()[testTable.timestampWithTimeZone]

// PostgreSQL and MySQL always store the timestamp in UTC, thereby losing the original time zone.
// To preserve the original time zone, store the time zone information in a separate column.
val isOriginalTimeZonePreserved = testDB !in (TestDB.ALL_MYSQL + TestDB.ALL_POSTGRES)
if (isOriginalTimeZonePreserved) {
// Assert that time zone is preserved when the same value is inserted in different time zones
assertEqualDateTime(cairoNow, cairoNowInsertedInCairoTimeZone)
assertEqualDateTime(cairoNow, cairoNowInsertedInUTCTimeZone)
assertEqualDateTime(cairoNow, cairoNowInsertedInTokyoTimeZone)

// Assert that time zone is preserved when the same record is retrieved in different time zones
assertEqualDateTime(cairoNow, cairoNowRetrievedInUTCTimeZone)
assertEqualDateTime(cairoNow, cairoNowRetrievedInTokyoTimeZone)
} else {
// Assert equivalence in UTC when the same value is inserted in different time zones
assertEqualDateTime(cairoNowInsertedInCairoTimeZone, cairoNowInsertedInUTCTimeZone)
assertEqualDateTime(cairoNowInsertedInUTCTimeZone, cairoNowInsertedInTokyoTimeZone)

SchemaUtils.drop(testTable)
// Assert equivalence in UTC when the same record is retrieved in different time zones
assertEqualDateTime(cairoNowRetrievedInUTCTimeZone, cairoNowRetrievedInTokyoTimeZone)
}

// Reset to original time zone as set up in DatabaseTestsBase init block
java.util.TimeZone.setDefault(java.util.TimeZone.getTimeZone(ZoneOffset.UTC))
assertEquals("UTC", ZoneId.systemDefault().id)
}
}

Expand All @@ -448,7 +440,7 @@ open class JavaTimeBaseTest : DatabaseTestsBase() {
val timestampWithTimeZone = timestampWithTimeZone("timestamptz-column")
}

withDb(db = listOf(TestDB.MYSQL_V5, TestDB.MARIADB)) { testDB ->
withDb(db = timestampWithTimeZoneUnsupportedDB) {
expectException<UnsupportedByDialectException> {
SchemaUtils.create(testTable)
}
Expand All @@ -461,58 +453,56 @@ open class JavaTimeBaseTest : DatabaseTestsBase() {
val timestampWithTimeZone = timestampWithTimeZone("timestamptz-column")
}

withDb(excludeSettings = listOf(TestDB.MARIADB)) {
if (!isOldMySql()) {
try {
// UTC time zone
java.util.TimeZone.setDefault(java.util.TimeZone.getTimeZone(ZoneOffset.UTC))
assertEquals("UTC", ZoneId.systemDefault().id)

SchemaUtils.create(testTable)

val now = OffsetDateTime.parse("2023-05-04T05:04:01.700+00:00")
val nowId = testTable.insertAndGetId {
it[timestampWithTimeZone] = now
}

assertEquals(
now.toLocalDate(),
testTable.select(testTable.timestampWithTimeZone.date()).where { testTable.id eq nowId }
.single()[testTable.timestampWithTimeZone.date()]
)

assertEquals(
now.month.value,
testTable.select(testTable.timestampWithTimeZone.month()).where { testTable.id eq nowId }
.single()[testTable.timestampWithTimeZone.month()]
)

assertEquals(
now.dayOfMonth,
testTable.select(testTable.timestampWithTimeZone.day()).where { testTable.id eq nowId }
.single()[testTable.timestampWithTimeZone.day()]
)

assertEquals(
now.hour,
testTable.select(testTable.timestampWithTimeZone.hour()).where { testTable.id eq nowId }
.single()[testTable.timestampWithTimeZone.hour()]
)

assertEquals(
now.minute,
testTable.select(testTable.timestampWithTimeZone.minute()).where { testTable.id eq nowId }
.single()[testTable.timestampWithTimeZone.minute()]
)

assertEquals(
now.second,
testTable.select(testTable.timestampWithTimeZone.second()).where { testTable.id eq nowId }
.single()[testTable.timestampWithTimeZone.second()]
)
} finally {
SchemaUtils.drop(testTable)
withDb(excludeSettings = timestampWithTimeZoneUnsupportedDB) {
try {
// UTC time zone
java.util.TimeZone.setDefault(java.util.TimeZone.getTimeZone(ZoneOffset.UTC))
assertEquals("UTC", ZoneId.systemDefault().id)

SchemaUtils.create(testTable)

val now = OffsetDateTime.parse("2023-05-04T05:04:01.700+00:00")
val nowId = testTable.insertAndGetId {
it[timestampWithTimeZone] = now
}

assertEquals(
now.toLocalDate(),
testTable.select(testTable.timestampWithTimeZone.date()).where { testTable.id eq nowId }
.single()[testTable.timestampWithTimeZone.date()]
)

assertEquals(
now.month.value,
testTable.select(testTable.timestampWithTimeZone.month()).where { testTable.id eq nowId }
.single()[testTable.timestampWithTimeZone.month()]
)

assertEquals(
now.dayOfMonth,
testTable.select(testTable.timestampWithTimeZone.day()).where { testTable.id eq nowId }
.single()[testTable.timestampWithTimeZone.day()]
)

assertEquals(
now.hour,
testTable.select(testTable.timestampWithTimeZone.hour()).where { testTable.id eq nowId }
.single()[testTable.timestampWithTimeZone.hour()]
)

assertEquals(
now.minute,
testTable.select(testTable.timestampWithTimeZone.minute()).where { testTable.id eq nowId }
.single()[testTable.timestampWithTimeZone.minute()]
)

assertEquals(
now.second,
testTable.select(testTable.timestampWithTimeZone.second()).where { testTable.id eq nowId }
.single()[testTable.timestampWithTimeZone.second()]
)
} finally {
SchemaUtils.drop(testTable)
}
}
}
Expand Down
Loading

0 comments on commit c514f9f

Please sign in to comment.