Skip to content

Commit

Permalink
test: Fix exposed-tests failing in SQL Server (#1801)
Browse files Browse the repository at this point in the history
ConditionsTests/testNullSafeEqualityOps()
- IS DISTINCT FROM operator is only supported as of SQL Server 2022 and Exposed
uses a docker image that does not yet support this version. It has been excluded.

InsertTests/batch insert number of inserted rows is accurate()
- SQL Server does not support insert or ignore (also fails on Oracle), so they
have been excluded from the test.
- Adjust test suite to use a list of databases that don't support insert or ignore.
- Adjust failing test to cover MySQL-related databases, which support insert or ignore.
  • Loading branch information
bog-walk authored Jul 27, 2023
1 parent 8b52c2f commit 55ed36b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import org.jetbrains.exposed.dao.id.IntIdTable
import org.jetbrains.exposed.exceptions.ExposedSQLException
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.shared.assertEqualLists
import org.jetbrains.exposed.sql.tests.shared.assertEquals
import org.jetbrains.exposed.sql.tests.shared.expectException
Expand All @@ -25,8 +26,8 @@ class ConditionsTests : DatabaseTestsBase() {
val number1 = integer("number_1").nullable()
val number2 = integer("number_2").nullable()
}

withTables(table) {
// remove SQL Server exclusion once test container supports SQL Server 2022
withTables(excludeSettings = listOf(TestDB.SQLSERVER), table) {
val sameNumberId = table.insert {
it[number1] = 0
it[number2] = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.statements.BatchInsertStatement
import org.jetbrains.exposed.sql.tests.DatabaseTestsBase
import org.jetbrains.exposed.sql.tests.TestDB
import org.jetbrains.exposed.sql.tests.currentTestDB
import org.jetbrains.exposed.sql.tests.shared.assertEqualLists
import org.jetbrains.exposed.sql.tests.shared.assertEquals
import org.jetbrains.exposed.sql.tests.shared.assertFailAndRollback
Expand Down Expand Up @@ -55,7 +56,7 @@ class InsertTests : DatabaseTestsBase() {
}
}

private val insertIgnoreSupportedDB = TestDB.values().toList() -
private val insertIgnoreUnsupportedDB = TestDB.values().toList() -
listOf(TestDB.SQLITE, TestDB.MYSQL, TestDB.H2_MYSQL, TestDB.POSTGRESQL, TestDB.POSTGRESQLNG, TestDB.H2_PSQL)

@Test
Expand All @@ -64,7 +65,7 @@ class InsertTests : DatabaseTestsBase() {
val name = varchar("foo", 10).uniqueIndex()
}

withTables(insertIgnoreSupportedDB, idTable) {
withTables(excludeSettings = insertIgnoreUnsupportedDB, idTable) {
idTable.insertIgnoreAndGetId {
it[idTable.name] = "1"
}
Expand Down Expand Up @@ -139,10 +140,7 @@ class InsertTests : DatabaseTestsBase() {
val name = varchar("foo", 10).uniqueIndex()
}

val insertIgnoreSupportedDB = TestDB.values().toList() -
listOf(TestDB.SQLITE, TestDB.MYSQL, TestDB.H2_MYSQL, TestDB.POSTGRESQL, TestDB.POSTGRESQLNG, TestDB.H2_PSQL)

withTables(insertIgnoreSupportedDB, idTable) {
withTables(excludeSettings = insertIgnoreUnsupportedDB, idTable) {
val insertedStatement = idTable.insertIgnore {
it[idTable.id] = EntityID(1, idTable)
it[idTable.name] = "1"
Expand Down Expand Up @@ -278,7 +276,6 @@ class InsertTests : DatabaseTestsBase() {
}

@Test fun testInsertWithExpression() {

val tbl = object : IntIdTable("testInsert") {
val nullableInt = integer("nullableIntCol").nullable()
val string = varchar("stringCol", 20)
Expand Down Expand Up @@ -315,7 +312,6 @@ class InsertTests : DatabaseTestsBase() {
}

@Test fun testInsertWithColumnExpression() {

val tbl1 = object : IntIdTable("testInsert1") {
val string1 = varchar("stringCol", 20)
}
Expand Down Expand Up @@ -357,7 +353,6 @@ class InsertTests : DatabaseTestsBase() {
// https://github.com/JetBrains/Exposed/issues/192
@Test fun testInsertWithColumnNamedWithKeyword() {
withTables(OrderedDataTable) {

val foo = OrderedData.new {
name = "foo"
order = 20
Expand Down Expand Up @@ -569,24 +564,34 @@ class InsertTests : DatabaseTestsBase() {
it[board] = nullableBoardId
}
}

}

class BatchInsertOnConflictDoNothing(
table: Table,
) : BatchInsertStatement(table) {
override fun prepareSQL(transaction: Transaction, prepared: Boolean) = buildString {
append(super.prepareSQL(transaction, prepared))
append(" ON CONFLICT (id) DO NOTHING")
val insertStatement = super.prepareSQL(transaction, prepared)
when (val db = currentTestDB) {
in TestDB.mySqlRelatedDB -> {
append("INSERT IGNORE ")
append(insertStatement.substringAfter("INSERT "))
}
else -> {
append(insertStatement)
val identifier = if (db == TestDB.H2_PSQL) "" else "(id) "
append(" ON CONFLICT ${identifier}DO NOTHING")
}
}
}
}

@Test fun `batch insert number of inserted rows is accurate`() {
@Test
fun testBatchInsertNumberOfInsertedRows() {
val tab = object : Table("tab") {
val id = varchar("id", 10).uniqueIndex()
}

withTables(TestDB.allH2TestDB + listOf(TestDB.MYSQL), tab) {
withTables(excludeSettings = insertIgnoreUnsupportedDB, tab) {
tab.insert { it[id] = "foo" }

val numInserted = BatchInsertOnConflictDoNothing(tab).run {
Expand Down

0 comments on commit 55ed36b

Please sign in to comment.