From 6eb2beeeb1299e2abb69c3b53e0b979c2a4988d2 Mon Sep 17 00:00:00 2001 From: Jocelyne Date: Thu, 21 Sep 2023 18:16:43 +0100 Subject: [PATCH] chore: Reuse testing logic in ForeignKeyConstraintTests --- .../tests/sqlite/ForeignKeyConstraintTests.kt | 66 +++++-------------- 1 file changed, 16 insertions(+), 50 deletions(-) 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 index e40a2b8636..93d49463d1 100644 --- 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 @@ -72,7 +72,7 @@ class ForeignKeyConstraintTests : DatabaseTestsBase() { @Test fun `test ON DELETE RESTRICT for databases that support it without SQLite`() { withDb(excludeSettings = listOf(TestDB.SQLITE, TestDB.SQLSERVER)) { - testOnDeleteRestrict() + testRestrict(isTestingOnDelete = true) } } @@ -81,55 +81,14 @@ class ForeignKeyConstraintTests : DatabaseTestsBase() { Assume.assumeTrue(TestDB.SQLITE in TestDB.enabledDialects()) transaction(Database.connect("jdbc:sqlite:file:test?mode=memory&cache=shared&foreign_keys=on", user = "root", driver = "org.sqlite.JDBC")) { - testOnDeleteRestrict() - } - } - - private fun testOnDeleteRestrict() { - val country = object : Table("Country") { - val id = integer("id") - val name = varchar(name = "name", length = 20) - - override val primaryKey = PrimaryKey(id) - } - - val city = object : Table("City") { - val id = integer("id") - val name = varchar(name = "name", length = 20) - val countryId = integer("countryId") - .references( - country.id, - onDelete = ReferenceOption.RESTRICT - ) - - override val primaryKey = PrimaryKey(id) - } - - SchemaUtils.drop(country, city) - SchemaUtils.create(country, city) - - val lebanonId = 0 - country.insert { - it[id] = lebanonId - it[name] = "Lebanon" - } - - val beirutId = 0 - city.insert { - it[id] = beirutId - it[name] = "Beirut" - it[countryId] = 0 - } - - expectException { - country.deleteWhere { id eq lebanonId } + testRestrict(isTestingOnDelete = true) } } @Test fun `test ON UPDATE RESTRICT for databases that support it without SQLite`() { withDb(excludeSettings = listOf(TestDB.SQLITE, TestDB.SQLSERVER, TestDB.ORACLE)) { - testOnUpdateRestrict() + testRestrict(isTestingOnDelete = false) } } @@ -138,11 +97,11 @@ class ForeignKeyConstraintTests : DatabaseTestsBase() { Assume.assumeTrue(TestDB.SQLITE in TestDB.enabledDialects()) transaction(Database.connect("jdbc:sqlite:file:test?mode=memory&cache=shared&foreign_keys=on", user = "root", driver = "org.sqlite.JDBC")) { - testOnUpdateRestrict() + testRestrict(isTestingOnDelete = false) } } - private fun testOnUpdateRestrict() { + private fun testRestrict(isTestingOnDelete: Boolean) { val country = object : Table("Country") { val id = integer("id") val name = varchar(name = "name", length = 20) @@ -156,7 +115,8 @@ class ForeignKeyConstraintTests : DatabaseTestsBase() { val countryId = integer("countryId") .references( country.id, - onUpdate = ReferenceOption.RESTRICT + onDelete = if (isTestingOnDelete) ReferenceOption.RESTRICT else null, + onUpdate = if (isTestingOnDelete) null else ReferenceOption.RESTRICT, ) override val primaryKey = PrimaryKey(id) @@ -178,9 +138,15 @@ class ForeignKeyConstraintTests : DatabaseTestsBase() { it[countryId] = 0 } - expectException { - country.update({ country.id eq lebanonId }) { - it[id] = 1 + if (isTestingOnDelete) { + expectException { + country.deleteWhere { id eq lebanonId } + } + } else { + expectException { + country.update({ country.id eq lebanonId }) { + it[id] = 1 + } } } }