Skip to content

Commit

Permalink
fix: EXPOSED-82 Inaccurate UShort column type mapping
Browse files Browse the repository at this point in the history
Add regression unit test for UShort column type
  • Loading branch information
bog-walk committed Jul 25, 2023
1 parent 4cc2a9d commit e0c9d12
Showing 1 changed file with 38 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package org.jetbrains.exposed.sql.tests.shared.types

import org.jetbrains.exposed.sql.SchemaUtils
import org.jetbrains.exposed.sql.Table
import org.jetbrains.exposed.sql.insert
import org.jetbrains.exposed.sql.selectAll
import org.jetbrains.exposed.sql.*
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.assertEqualCollections
import org.jetbrains.exposed.sql.tests.shared.assertEquals
import org.jetbrains.exposed.sql.tests.shared.assertFailAndRollback
import org.jetbrains.exposed.sql.tests.shared.assertTrue
Expand Down Expand Up @@ -75,7 +73,7 @@ class UnsignedColumnTypeTests : DatabaseTestsBase() {
assertEquals(number, result.single()[UShortTable.unsignedShort])

// test that column itself blocks same out-of-range value that compiler blocks
assertFailAndRollback("Check constraint violation or out-of-range error (MySQL/MariaDB)") {
assertFailAndRollback("Check constraint violation (or out-of-range error in MySQL/MariaDB)") {
val tableName = UShortTable.nameInDatabaseCase()
val columnName = UShortTable.unsignedShort.nameInDatabaseCase()
val outOfRangeValue = UShort.MAX_VALUE + 1u
Expand All @@ -84,6 +82,41 @@ class UnsignedColumnTypeTests : DatabaseTestsBase() {
}
}

@Test
fun testUShortTypeRegression() {
withDb(excludeSettings = listOf(TestDB.MYSQL, TestDB.MARIADB)) { testDb ->
try {
val tableName = UShortTable.nameInDatabaseCase()
val columnName = UShortTable.unsignedShort.nameInDatabaseCase()
exec("""CREATE TABLE ${addIfNotExistsIfSupported()}$tableName ($columnName SMALLINT NOT NULL)""")

val number1 = Short.MAX_VALUE.toUShort()
UShortTable.insert { it[unsignedShort] = number1 }

val result1 = UShortTable.select { UShortTable.unsignedShort eq number1 }.count()
assertEquals(1, result1)

// SMALLINT maps to INTEGER in SQLite and NUMBER(38) in Oracle, so they will not throw OoR error
if (testDb != TestDB.SQLITE && testDb != TestDB.ORACLE) {
val number2 = (Short.MAX_VALUE + 1).toUShort()
assertFailAndRollback("Out-of-range (OoR) error") {
UShortTable.insert { it[unsignedShort] = number2 }
assertEquals(0, UShortTable.select { UShortTable.unsignedShort less 0u })
}

// modify column to now have INT type
exec(UShortTable.unsignedShort.modifyStatement().first())
UShortTable.insert { it[unsignedShort] = number2 }

val result2 = UShortTable.selectAll().map { it[UShortTable.unsignedShort] }
assertEqualCollections(listOf(number1, number2), result2)
}
} finally {
SchemaUtils.drop(UShortTable)
}
}
}

@Test
fun testUIntColumnType() {
withTables(UIntTable) {
Expand Down

0 comments on commit e0c9d12

Please sign in to comment.