Skip to content

Commit

Permalink
chore: move integration tests to separate file and create a separate …
Browse files Browse the repository at this point in the history
…test for SQLite with foreign key constraint enabled
  • Loading branch information
joc-a committed Jun 9, 2023
1 parent 9d2c646 commit b38474b
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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) }}," +
Expand All @@ -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)
}
}

Expand Down
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]
)
}
}

0 comments on commit b38474b

Please sign in to comment.