Skip to content

Commit

Permalink
test: Properly drop all leaking test table objects (#1994)
Browse files Browse the repository at this point in the history
* test: Properly drop all leaking test table objects

As an example, if 'tests/shared/dml/SelectTests/testInList08()' is run on all containers
together via gradle task 'test', this results in false exceptions (duplicate key
violations) from certain databases: MySQL, PostgreSQL, MariaDB.

This occurs because it uses 2 table resources without closing them at the end
(usually handled via withTables()).

Other unit tests that don't drop tables were found and fixed.

Unused test parameters were also removed if found.

* test: Properly drop all leaking test table objects

Remove unused println functions
  • Loading branch information
bog-walk authored Feb 19, 2024
1 parent 2c8fd85 commit 1999d3f
Show file tree
Hide file tree
Showing 19 changed files with 60 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,8 @@ class DefaultsTest : DatabaseTestsBase() {
val row1 = testTable.selectAll().where { testTable.id eq id1 }.single()
assertEqualDateTime(nowWithTimeZone, row1[testTable.t1])
assertEqualDateTime(nowWithTimeZone, row1[testTable.t2])

SchemaUtils.drop(testTable)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,8 @@ open class JavaTimeBaseTest : DatabaseTestsBase() {
// Reset to original time zone as set up in DatabaseTestsBase init block
java.util.TimeZone.setDefault(java.util.TimeZone.getTimeZone(ZoneOffset.UTC))
assertEquals("UTC", ZoneId.systemDefault().id)

SchemaUtils.drop(testTable)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,8 @@ class JodaTimeDefaultsTest : JodaTimeBaseTest() {
val row1 = testTable.selectAll().where { testTable.id eq id1 }.single()
assertEqualDateTime(nowWithTimeZone, row1[testTable.t1])
assertEqualDateTime(nowWithTimeZone, row1[testTable.t2])

SchemaUtils.drop(testTable)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,8 @@ open class JodaTimeBaseTest : DatabaseTestsBase() {
// Assert equivalence in UTC when the same record is retrieved in different time zones
assertEqualDateTime(cairoNowRetrievedInUTCTimeZone, cairoNowRetrievedInTokyoTimeZone)
}

SchemaUtils.drop(testTable)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
@file:OptIn(ExperimentalTime::class)

package org.jetbrains.exposed.sql.kotlin.datetime

import kotlinx.datetime.*
Expand Down Expand Up @@ -35,7 +33,6 @@ import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertTrue
import kotlin.time.DurationUnit
import kotlin.time.ExperimentalTime
import kotlin.time.toDuration

fun now() = Clock.System.now().toLocalDateTime(TimeZone.currentSystemDefault())
Expand Down Expand Up @@ -453,6 +450,8 @@ class DefaultsTest : DatabaseTestsBase() {
val row1 = testTable.selectAll().where { testTable.id eq id1 }.single()
assertEqualDateTime(nowWithTimeZone, row1[testTable.t1])
assertEqualDateTime(nowWithTimeZone, row1[testTable.t2])

SchemaUtils.drop(testTable)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,8 @@ open class KotlinTimeBaseTest : DatabaseTestsBase() {
// Reset to original time zone as set up in DatabaseTestsBase init block
java.util.TimeZone.setDefault(java.util.TimeZone.getTimeZone(ZoneOffset.UTC))
assertEquals("UTC", ZoneId.systemDefault().id)

SchemaUtils.drop(testTable)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class EntityReferenceCacheTest : DatabaseTestsBase() {
var b1: EntityTests.Board by Delegates.notNull()
var p1: EntityTests.Post by Delegates.notNull()
var p2: EntityTests.Post by Delegates.notNull()
executeOnH2(EntityTests.Boards, EntityTests.Posts) {
executeOnH2(EntityTests.Boards, EntityTests.Posts, EntityTests.Categories) {
transaction(db) {
b1 = EntityTests.Board.new {
name = "test-board"
Expand Down Expand Up @@ -132,7 +132,7 @@ class EntityReferenceCacheTest : DatabaseTestsBase() {
var b1: EntityTests.Board by Delegates.notNull()
var p1: EntityTests.Post by Delegates.notNull()
var p2: EntityTests.Post by Delegates.notNull()
executeOnH2(EntityTests.Boards, EntityTests.Posts) {
executeOnH2(EntityTests.Boards, EntityTests.Posts, EntityTests.Categories) {
transaction(db) {
b1 = EntityTests.Board.new {
name = "test-board"
Expand Down Expand Up @@ -169,7 +169,7 @@ class EntityReferenceCacheTest : DatabaseTestsBase() {
var b1: EntityTests.Board by Delegates.notNull()
var p1: EntityTests.Post by Delegates.notNull()
var p2: EntityTests.Post by Delegates.notNull()
executeOnH2(EntityTests.Boards, EntityTests.Posts) {
executeOnH2(EntityTests.Boards, EntityTests.Posts, EntityTests.Categories) {
transaction(db) {
b1 = EntityTests.Board.new {
name = "test-board"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,30 @@ class H2Tests : DatabaseTestsBase() {
@Test
fun insertInH2() {
withDb(listOf(TestDB.H2_MYSQL, TestDB.H2)) {
SchemaUtils.drop(Testing)
SchemaUtils.create(Testing)
Testing.insert {
it[Testing.id] = 1
it[Testing.string] = "one"
it[id] = 1
it[string] = "one"
}

assertEquals("one", Testing.selectAll().where { Testing.id.eq(1) }.single()[Testing.string])

SchemaUtils.drop(Testing)
}
}

@Test
fun replaceAsInsertInH2() {
withDb(listOf(TestDB.H2_MYSQL, TestDB.H2_MARIADB)) {
SchemaUtils.drop(Testing)
SchemaUtils.create(Testing)
Testing.replace {
it[id] = 1
it[string] = "one"
}

assertEquals("one", Testing.selectAll().where { Testing.id.eq(1) }.single()[Testing.string])

SchemaUtils.drop(Testing)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class AliasesTests : DatabaseTestsBase() {

@Test
fun testJoinSubQuery01() {
withCitiesAndUsers { cities, users, userData ->
withCitiesAndUsers { _, users, _ ->
val expAlias = users.name.max().alias("m")
val usersAlias = users.select(users.cityId, expAlias).groupBy(users.cityId).alias("u2")
val resultRows = Join(users).join(usersAlias, JoinType.INNER, usersAlias[expAlias], users.name).selectAll().toList()
Expand All @@ -60,7 +60,7 @@ class AliasesTests : DatabaseTestsBase() {

@Test
fun testJoinSubQuery02() {
withCitiesAndUsers { cities, users, userData ->
withCitiesAndUsers { _, users, _ ->
val expAlias = users.name.max().alias("m")

val query = Join(users).joinQuery(on = { it[expAlias].eq(users.name) }) {
Expand All @@ -76,7 +76,7 @@ class AliasesTests : DatabaseTestsBase() {

@Test
fun `test wrap row with Aliased table`() {
withTables(EntityTestsData.XTable) {
withTables(EntityTestsData.XTable, EntityTestsData.YTable) {
val entity1 = EntityTestsData.XEntity.new {
this.b1 = false
}
Expand All @@ -94,7 +94,7 @@ class AliasesTests : DatabaseTestsBase() {

@Test
fun `test wrap row with Aliased query`() {
withTables(EntityTestsData.XTable) {
withTables(EntityTestsData.XTable, EntityTestsData.YTable) {
val entity1 = EntityTestsData.XEntity.new {
this.b1 = false
}
Expand All @@ -112,7 +112,7 @@ class AliasesTests : DatabaseTestsBase() {

@Test
fun `test aliased expression with aliased query`() {
withTables(EntityTestsData.XTable) {
withTables(EntityTestsData.XTable, EntityTestsData.YTable) {
val dataToInsert = listOf(true, true, false, true)
EntityTestsData.XTable.batchInsert(dataToInsert) {
this[EntityTestsData.XTable.b1] = it
Expand Down Expand Up @@ -140,7 +140,7 @@ class AliasesTests : DatabaseTestsBase() {
}

@Test fun `test alias for same table with join`() {
withTables(EntityTestsData.XTable) {
withTables(EntityTestsData.XTable, EntityTestsData.YTable) {
val table1Count = EntityTestsData.XTable.id.max().alias("t1max")
val table2Count = EntityTestsData.XTable.id.max().alias("t2max")
val t1Alias = EntityTestsData.XTable.select(table1Count).groupBy(EntityTestsData.XTable.b1).alias("t1")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class ConnectionTests : DatabaseTestsBase() {
)
}
assertEquals(expected, columnMetadata)

SchemaUtils.drop(People)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ 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.dml.DMLTestsData
import org.jetbrains.exposed.sql.transactions.TransactionManager
import org.jetbrains.exposed.sql.transactions.transaction
import org.jetbrains.exposed.sql.vendors.H2Dialect
Expand Down Expand Up @@ -213,9 +212,6 @@ class DDLTests : DatabaseTestsBase() {

withDb(TestDB.H2) {
assertEquals("CREATE TABLE IF NOT EXISTS ${"test_named_table".inProperCase()}", testTable.ddl)
DMLTestsData.Users.selectAll().where {
exists(DMLTestsData.UserData.selectAll().where { DMLTestsData.Users.id eq DMLTestsData.UserData.user_id })
}
}
}

Expand Down Expand Up @@ -360,6 +356,7 @@ class DDLTests : DatabaseTestsBase() {
if (h2Dialect.isSecondVersion && !isOracleMode) {
expect(Unit) {
SchemaUtils.create(testTable)
SchemaUtils.drop(testTable)
}
} else {
expectException<ExposedSQLException> {
Expand Down Expand Up @@ -667,7 +664,6 @@ class DDLTests : DatabaseTestsBase() {
}
}
else -> {
SchemaUtils.drop(testTable)
SchemaUtils.create(testTable)

testTable.insert {
Expand Down Expand Up @@ -938,6 +934,8 @@ class DDLTests : DatabaseTestsBase() {
testTable.select(concat).forEach {
assertEquals(it[concat], "1txt 1TXTMED 1txtlong")
}

SchemaUtils.drop(testTable)
}
}

Expand Down Expand Up @@ -1140,14 +1138,12 @@ class DDLTests : DatabaseTestsBase() {
val one = prepareSchemaForTest("one")
val two = prepareSchemaForTest("two")
withSchemas(two, one) {
SchemaUtils.drop(TableFromSchemeOne)
SchemaUtils.create(TableFromSchemeOne)

if (currentDialectTest is OracleDialect) {
exec("GRANT REFERENCES ON ${TableFromSchemeOne.tableName} to TWO")
}

SchemaUtils.drop(TableFromSchemeTwo)
SchemaUtils.create(TableFromSchemeTwo)
val idFromOne = TableFromSchemeOne.insertAndGetId { }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,5 +169,9 @@ class SchemaTests : DatabaseTestsBase() {
}
}
}

transaction {
SchemaUtils.dropSchema(schema)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,8 @@ class CreateMissingTablesAndColumnsTests : DatabaseTestsBase() {

assertTrue(T1.exists())
assertTrue(T2.exists())

SchemaUtils.drop(T1, T2)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import org.jetbrains.exposed.sql.tests.shared.assertEquals
import org.jetbrains.exposed.sql.tests.shared.assertTrue
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.util.*
import kotlin.test.assertFails
Expand Down Expand Up @@ -605,7 +604,6 @@ class CreateTableTests : DatabaseTestsBase() {
assertEquals(false, OneOneTable.exists())
SchemaUtils.createSchema(one)
SchemaUtils.create(OneOneTable)
println("${currentDialect.name}: ${currentDialectTest.allTablesNames()}")
assertEquals(true, OneTable.exists())
assertEquals(true, OneOneTable.exists())
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.jetbrains.exposed.sql.tests.shared.dml
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.tests.DatabaseTestsBase
import org.jetbrains.exposed.sql.tests.currentDialectTest
import org.jetbrains.exposed.sql.tests.shared.assertEqualLists
import org.jetbrains.exposed.sql.tests.shared.assertEquals
import org.jetbrains.exposed.sql.vendors.H2Dialect
import org.jetbrains.exposed.sql.vendors.H2Dialect.H2CompatibilityMode
Expand Down Expand Up @@ -126,12 +127,12 @@ class OrderByTests : DatabaseTestsBase() {
val result = cities
.selectAll()
.orderBy(expression, SortOrder.DESC)
.toList()
.map { it[cities.name] }

// Munich - 2 users
// St. Petersburg - 1 user
// Prague - 0 users
println(result)
assertEqualLists(result, listOf("Munich", "St. Petersburg", "Prague"))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ class SelectTests : DatabaseTestsBase() {
withCitiesAndUsers { _, users, _ ->
users.selectAll().where { not(users.id.eq("andrey")) }.forEach {
val userId = it[users.id]
val userName = it[users.name]
if (userId == "andrey") {
error("Unexpected user $userId")
}
Expand Down Expand Up @@ -211,7 +210,7 @@ class SelectTests : DatabaseTestsBase() {

@Test
fun testInList08() {
withTables(EntityTests.Posts) {
withTables(EntityTests.Posts, EntityTests.Boards, EntityTests.Categories) {
val board1 = EntityTests.Board.new {
this.name = "Board1"
}
Expand Down
Loading

0 comments on commit 1999d3f

Please sign in to comment.