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: Fix failing exposed-tests in SQL Server #1801

Merged
merged 1 commit into from
Jul 27, 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
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
Loading