Skip to content

Commit

Permalink
chore: modify test to include integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
joc-a committed Jun 8, 2023
1 parent 5eb1a3b commit e9ead06
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package org.jetbrains.exposed.sql

import org.jetbrains.exposed.sql.transactions.TransactionManager
import org.jetbrains.exposed.sql.vendors.*
import org.jetbrains.exposed.sql.vendors.H2Dialect
import org.jetbrains.exposed.sql.vendors.MariaDBDialect
import org.jetbrains.exposed.sql.vendors.MysqlDialect
import org.jetbrains.exposed.sql.vendors.OracleDialect
import org.jetbrains.exposed.sql.vendors.currentDialect
import org.jetbrains.exposed.sql.vendors.currentDialectIfAvailable
import org.jetbrains.exposed.sql.vendors.h2Mode
import org.jetbrains.exposed.sql.vendors.inProperCase
import java.sql.DatabaseMetaData

Expand Down Expand Up @@ -110,24 +115,46 @@ data class ForeignKeyConstraint(
from.joinToString("_") { it.name }
}__${target.joinToString("_") { it.name }}"
).inProperCase()

internal val foreignKeyPart: String
get() = buildString {
if (fkName.isNotBlank()) {
append("CONSTRAINT $fkName ")
}
append("FOREIGN KEY ($fromColumns) REFERENCES $targetTableName($targetColumns)")
if (deleteRule != ReferenceOption.NO_ACTION) {
if (deleteRule == ReferenceOption.SET_DEFAULT && (currentDialect as? MysqlDialect)?.isMysql8 == false) {
exposedLogger.warn("This MySQL version doesn't support FOREIGN KEY with SET DEFAULT reference option with ON DELETE clause. Please check your $fromTableName table.")
if (deleteRule == ReferenceOption.SET_DEFAULT) {
when (currentDialect) {
is MysqlDialect -> exposedLogger.warn(
"MySQL doesn't support FOREIGN KEY with SET DEFAULT reference option with ON DELETE clause. " +
"Please check your $fromTableName table."
)
is MariaDBDialect -> exposedLogger.warn(
"MariaDB doesn't support FOREIGN KEY with SET DEFAULT reference option with ON DELETE clause. " +
"Please check your $fromTableName table."
)
else -> append(" ON DELETE $deleteRule")
}
} else {
append(" ON DELETE $deleteRule")
}
}
if (updateRule != ReferenceOption.NO_ACTION) {
if (currentDialect is OracleDialect || currentDialect.h2Mode == H2Dialect.H2CompatibilityMode.Oracle) {
exposedLogger.warn("Oracle doesn't support FOREIGN KEY with ON UPDATE clause. Please check your $fromTableName table.")
} else if (updateRule == ReferenceOption.SET_DEFAULT && (currentDialect as? MysqlDialect)?.isMysql8 == false) {
exposedLogger.warn("This MySQL version doesn't support FOREIGN KEY with SET DEFAULT reference option with ON UPDATE clause. Please check your $fromTableName table.")
} else if (updateRule == ReferenceOption.SET_DEFAULT) {
when (currentDialect) {
is MysqlDialect -> exposedLogger.warn(
"MySQL doesn't support FOREIGN KEY with SET DEFAULT reference option with ON UPDATE clause. " +
"Please check your $fromTableName table."
)
is MariaDBDialect ->
exposedLogger.warn(
"MariaDB doesn't support FOREIGN KEY with SET DEFAULT reference option with ON UPDATE clause. " +
"Please check your $fromTableName table."
)
else -> append(" ON UPDATE $updateRule")
}
} else {
append(" ON UPDATE $updateRule")
}
Expand Down
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?mode=memory&cache=shared" }, "org.sqlite.JDBC"),
SQLITE({ "jdbc:sqlite:file:test?foreign_keys=on&mode=memory&cache=shared" }, "org.sqlite.JDBC"),
MYSQL(
connection = {
if (runTestContainersMySQL()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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
Expand All @@ -15,7 +16,6 @@ import org.jetbrains.exposed.sql.transactions.TransactionManager
import org.jetbrains.exposed.sql.vendors.SQLiteDialect
import org.jetbrains.exposed.sql.vendors.currentDialect
import org.junit.Test
import java.math.BigDecimal
import java.util.*
import kotlin.test.assertFails

Expand Down Expand Up @@ -575,7 +575,7 @@ class CreateTableTests : DatabaseTestsBase() {

@Test
fun testOnDeleteSetDefault() {
val category = object : Table("Category") {
val Category = object : Table("Category") {
val id = integer("id")
val name = varchar(name = "name", length = 20)

Expand All @@ -584,36 +584,69 @@ class CreateTableTests : DatabaseTestsBase() {

val defaultCategoryId = 0

val item = object : Table("Item") {
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,
onUpdate = ReferenceOption.SET_DEFAULT,
onDelete = ReferenceOption.SET_DEFAULT
Category.id,
onDelete = ReferenceOption.SET_DEFAULT,
onUpdate = ReferenceOption.NO_ACTION
)

override val primaryKey = PrimaryKey(id)
}

withTables(category, item) { testDb ->
val isSetDefaultSupported = testDb != TestDB.MYSQL || this.db.isVersionCovers(BigDecimal("8.0"))
val onDeleteSetDefaultPart = if (isSetDefaultSupported) " ON DELETE SET DEFAULT" else ""
val onUpdateSetDefaultPart = if (isSetDefaultSupported && testDb !in listOf(TestDB.ORACLE, TestDB.H2_ORACLE)) " ON UPDATE SET DEFAULT" else ""
withDb(excludeSettings = listOf(TestDB.MARIADB, TestDB.MYSQL)) { testDb ->
println("testDb = $testDb")
println("version = ${this.db.version}")
addLogger(StdOutSqlLogger)

val expected = listOf(
"CREATE TABLE " + addIfNotExistsIfSupported() + "${this.identity(item)} (" +
"${item.columns.joinToString { it.descriptionDdl(false) }}," +
"CREATE TABLE " + addIfNotExistsIfSupported() + "${this.identity(Item)} (" +
"${Item.columns.joinToString { it.descriptionDdl(false) }}," +
" CONSTRAINT ${"fk_Item_categoryId__id".inProperCase()}" +
" FOREIGN KEY (${this.identity(item.categoryId)})" +
" REFERENCES ${this.identity(category)}(${this.identity(category.id)})" +
onDeleteSetDefaultPart +
onUpdateSetDefaultPart +
" FOREIGN KEY (${this.identity(Item.categoryId)})" +
" REFERENCES ${this.identity(Category)}(${this.identity(Category.id)})" +
" ON DELETE SET DEFAULT" +
")"
)
assertEqualCollections(item.ddl, expected)
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]
)
}
}

Expand Down

0 comments on commit e9ead06

Please sign in to comment.