From b38474b36f3c7a5612e36cd07292dca6be7d3b59 Mon Sep 17 00:00:00 2001 From: Jocelyne Date: Thu, 8 Jun 2023 16:55:27 +0200 Subject: [PATCH] chore: move integration tests to separate file and create a separate test for SQLite with foreign key constraint enabled --- .../exposed/sql/tests/DatabaseTestsBase.kt | 2 +- .../sql/tests/shared/ForeignKeyTables.kt | 27 +++++++ .../sql/tests/shared/ddl/CreateTableTests.kt | 70 ++---------------- .../tests/sqlite/ForeignKeyConstraintTests.kt | 73 +++++++++++++++++++ 4 files changed, 107 insertions(+), 65 deletions(-) create mode 100644 exposed-tests/src/main/kotlin/org/jetbrains/exposed/sql/tests/shared/ForeignKeyTables.kt create mode 100644 exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/sqlite/ForeignKeyConstraintTests.kt diff --git a/exposed-tests/src/main/kotlin/org/jetbrains/exposed/sql/tests/DatabaseTestsBase.kt b/exposed-tests/src/main/kotlin/org/jetbrains/exposed/sql/tests/DatabaseTestsBase.kt index 83724fd85b..db7f2a5e65 100644 --- a/exposed-tests/src/main/kotlin/org/jetbrains/exposed/sql/tests/DatabaseTestsBase.kt +++ b/exposed-tests/src/main/kotlin/org/jetbrains/exposed/sql/tests/DatabaseTestsBase.kt @@ -46,7 +46,7 @@ enum class TestDB( H2_PSQL({ "jdbc:h2:mem:psql;MODE=PostgreSQL;DATABASE_TO_LOWER=TRUE;DEFAULT_NULL_ORDERING=HIGH;DB_CLOSE_DELAY=-1" }, "org.h2.Driver"), H2_ORACLE({ "jdbc:h2:mem:oracle;MODE=Oracle;DATABASE_TO_LOWER=TRUE;DEFAULT_NULL_ORDERING=HIGH;DB_CLOSE_DELAY=-1" }, "org.h2.Driver"), H2_SQLSERVER({ "jdbc:h2:mem:sqlserver;MODE=MSSQLServer;DB_CLOSE_DELAY=-1" }, "org.h2.Driver"), - SQLITE({ "jdbc:sqlite:file:test?foreign_keys=on&mode=memory&cache=shared" }, "org.sqlite.JDBC"), + SQLITE({ "jdbc:sqlite:file:test?mode=memory&cache=shared" }, "org.sqlite.JDBC"), MYSQL( connection = { if (runTestContainersMySQL()) { diff --git a/exposed-tests/src/main/kotlin/org/jetbrains/exposed/sql/tests/shared/ForeignKeyTables.kt b/exposed-tests/src/main/kotlin/org/jetbrains/exposed/sql/tests/shared/ForeignKeyTables.kt new file mode 100644 index 0000000000..f44660754b --- /dev/null +++ b/exposed-tests/src/main/kotlin/org/jetbrains/exposed/sql/tests/shared/ForeignKeyTables.kt @@ -0,0 +1,27 @@ +package org.jetbrains.exposed.sql.tests.shared + +import org.jetbrains.exposed.sql.ReferenceOption +import org.jetbrains.exposed.sql.Table + +object Category : Table("Category") { + val id = integer("id") + val name = varchar(name = "name", length = 20) + + override val primaryKey = PrimaryKey(id) +} + +const val DEFAULT_CATEGORY_ID = 0 + +object Item : Table("Item") { + val id = integer("id") + val name = varchar(name = "name", length = 20) + val categoryId = integer("categoryId") + .default(DEFAULT_CATEGORY_ID) + .references( + Category.id, + onDelete = ReferenceOption.SET_DEFAULT, + onUpdate = ReferenceOption.NO_ACTION + ) + + override val primaryKey = PrimaryKey(id) +} diff --git a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/ddl/CreateTableTests.kt b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/ddl/CreateTableTests.kt index 3c5777b3fd..72469e5077 100644 --- a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/ddl/CreateTableTests.kt +++ b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/ddl/CreateTableTests.kt @@ -4,11 +4,12 @@ import org.jetbrains.exposed.dao.id.IdTable import org.jetbrains.exposed.dao.id.IntIdTable import org.jetbrains.exposed.dao.id.LongIdTable import org.jetbrains.exposed.sql.* -import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq 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.inProperCase +import org.jetbrains.exposed.sql.tests.shared.Category +import org.jetbrains.exposed.sql.tests.shared.Item import org.jetbrains.exposed.sql.tests.shared.assertEqualCollections import org.jetbrains.exposed.sql.tests.shared.assertEquals import org.jetbrains.exposed.sql.tests.shared.assertTrue @@ -534,6 +535,7 @@ class CreateTableTests : DatabaseTestsBase() { val parent = object : Table("parent2") { val idA = integer("id_a") val idB = integer("id_b") + init { uniqueIndex(idA, idB) } @@ -574,35 +576,8 @@ class CreateTableTests : DatabaseTestsBase() { } @Test - fun testOnDeleteSetDefault() { - val Category = object : Table("Category") { - val id = integer("id") - val name = varchar(name = "name", length = 20) - - override val primaryKey = PrimaryKey(id) - } - - val defaultCategoryId = 0 - - val Item = object : Table("Item") { - val id = integer("id") - val name = varchar(name = "name", length = 20) - val categoryId = integer("categoryId") - .default(defaultCategoryId) - .references( - Category.id, - onDelete = ReferenceOption.SET_DEFAULT, - onUpdate = ReferenceOption.NO_ACTION - ) - - override val primaryKey = PrimaryKey(id) - } - - withDb(excludeSettings = listOf(TestDB.MARIADB, TestDB.MYSQL)) { testDb -> - println("testDb = $testDb") - println("version = ${this.db.version}") - addLogger(StdOutSqlLogger) - + fun createTableWithOnDeleteSetDefault() { + withDb(excludeSettings = listOf(TestDB.MARIADB, TestDB.MYSQL)) { val expected = listOf( "CREATE TABLE " + addIfNotExistsIfSupported() + "${this.identity(Item)} (" + "${Item.columns.joinToString { it.descriptionDdl(false) }}," + @@ -612,41 +587,8 @@ class CreateTableTests : DatabaseTestsBase() { " ON DELETE SET DEFAULT" + ")" ) - assertEqualCollections(Item.ddl, expected) - - SchemaUtils.create(Category, Item) - Category.insert { - it[id] = defaultCategoryId - it[name] = "Default" - } - - val saladsId = 1 - Category.insert { - it[id] = saladsId - it[name] = "Salads" - } - - val tabboulehId = 0 - Item.insert { - it[id] = tabboulehId - it[name] = "Tabbouleh" - it[categoryId] = saladsId - } - - assertEquals( - saladsId, - Item.select { Item.id eq tabboulehId }.single().also { - println("SELECT result = $it") - }[Item.categoryId] - ) - - Category.deleteWhere { Category.id eq saladsId } - - assertEquals( - defaultCategoryId, - Item.select { Item.id eq tabboulehId }.single()[Item.categoryId] - ) + assertEqualCollections(Item.ddl, expected) } } diff --git a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/sqlite/ForeignKeyConstraintTests.kt b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/sqlite/ForeignKeyConstraintTests.kt new file mode 100644 index 0000000000..171d524f8b --- /dev/null +++ b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/sqlite/ForeignKeyConstraintTests.kt @@ -0,0 +1,73 @@ +package org.jetbrains.exposed.sql.tests.sqlite + +import org.jetbrains.exposed.sql.Database +import org.jetbrains.exposed.sql.SchemaUtils +import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq +import org.jetbrains.exposed.sql.Transaction +import org.jetbrains.exposed.sql.deleteWhere +import org.jetbrains.exposed.sql.insert +import org.jetbrains.exposed.sql.select +import org.jetbrains.exposed.sql.tests.DatabaseTestsBase +import org.jetbrains.exposed.sql.tests.TestDB +import org.jetbrains.exposed.sql.tests.shared.Category +import org.jetbrains.exposed.sql.tests.shared.DEFAULT_CATEGORY_ID +import org.jetbrains.exposed.sql.tests.shared.Item +import org.jetbrains.exposed.sql.tests.shared.assertEquals +import org.jetbrains.exposed.sql.transactions.transaction +import org.junit.Assume +import org.junit.Test + +class ForeignKeyConstraintTests : DatabaseTestsBase() { + + @Test + fun `test ON DELETE SET DEFAULT for databases that support it without SQLite`() { + withDb(excludeSettings = listOf(TestDB.MARIADB, TestDB.MYSQL, TestDB.SQLITE)) { + testOnDeleteSetDefault() + } + } + + @Test + fun `test ON DELETE SET DEFAULT for SQLite`() { + Assume.assumeTrue(TestDB.SQLITE in TestDB.enabledInTests()) + + transaction(Database.connect("jdbc:sqlite:file:test?mode=memory&cache=shared&foreign_keys=on", user = "root", driver = "org.sqlite.JDBC")) { + testOnDeleteSetDefault() + } + } + + private fun Transaction.testOnDeleteSetDefault() { + SchemaUtils.create(Category, Item) + + Category.insert { + it[id] = DEFAULT_CATEGORY_ID + it[name] = "Default" + } + + val saladsId = 1 + Category.insert { + it[id] = saladsId + it[name] = "Salads" + } + + val tabboulehId = 0 + Item.insert { + it[id] = tabboulehId + it[name] = "Tabbouleh" + it[categoryId] = saladsId + } + + assertEquals( + saladsId, + Item.select { Item.id eq tabboulehId }.single().also { + println("SELECT result = $it") + }[Item.categoryId] + ) + + Category.deleteWhere { id eq saladsId } + + assertEquals( + DEFAULT_CATEGORY_ID, + Item.select { Item.id eq tabboulehId }.single()[Item.categoryId] + ) + } +}