-
Notifications
You must be signed in to change notification settings - Fork 696
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: move integration tests to separate file and create a separate …
…test for SQLite with foreign key constraint enabled
- Loading branch information
Showing
4 changed files
with
107 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
exposed-tests/src/main/kotlin/org/jetbrains/exposed/sql/tests/shared/ForeignKeyTables.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
...tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/sqlite/ForeignKeyConstraintTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] | ||
) | ||
} | ||
} |