Skip to content

Commit

Permalink
refactor: EXPOSED-555 Extract common test code
Browse files Browse the repository at this point in the history
  • Loading branch information
RenanKummer committed Nov 26, 2024
1 parent 7e13c34 commit 3a76a15
Showing 1 changed file with 15 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import org.jetbrains.exposed.sql.transactions.TransactionManager
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
import org.jetbrains.exposed.sql.transactions.transaction
import org.junit.Assert
import org.junit.Assert.assertNotNull
import org.junit.Assume
import org.junit.Test
import java.sql.Connection
import kotlin.test.assertNotNull

class ConnectionPoolTests : LogDbInTestName() {
private val hikariDataSourcePG by lazy {
Expand Down Expand Up @@ -64,24 +64,13 @@ class ConnectionPoolTests : LogDbInTestName() {
fun testReadOnlyModeWithHikariAndPostgres() {
Assume.assumeTrue(TestDB.POSTGRESQL in TestDB.enabledDialects())

val testTable = object : IntIdTable("HIKARI_TESTER") { }

fun Transaction.getReadOnlyMode(): Boolean {
val mode = exec("SHOW transaction_read_only;") {
it.next()
it.getBoolean(1)
}
assertNotNull(mode)
return mode
}

// read only mode should be set directly by hikari config
transaction(db = hikariPG) {
assertTrue(getReadOnlyMode())

// table cannot be created in read-only mode
expectException<ExposedSQLException> {
SchemaUtils.create(testTable)
SchemaUtils.create(TestTable)
}
}

Expand All @@ -90,8 +79,8 @@ class ConnectionPoolTests : LogDbInTestName() {
Assert.assertFalse(getReadOnlyMode())

// table can now be created and dropped
SchemaUtils.create(testTable)
SchemaUtils.drop(testTable)
SchemaUtils.create(TestTable)
SchemaUtils.drop(TestTable)
}

TransactionManager.closeAndUnregister(hikariPG)
Expand All @@ -103,15 +92,6 @@ class ConnectionPoolTests : LogDbInTestName() {

val testTable = object : IntIdTable("HIKARI_TESTER") { }

fun Transaction.getReadOnlyMode(): Boolean {
val mode = exec("SHOW transaction_read_only;") {
it.next()
it.getBoolean(1)
}
assertNotNull(mode)
return mode
}

// read only mode should be set directly by hikari config
newSuspendedTransaction(db = hikariPG) {
assertTrue(getReadOnlyMode())
Expand All @@ -134,3 +114,14 @@ class ConnectionPoolTests : LogDbInTestName() {
TransactionManager.closeAndUnregister(hikariPG)
}
}

private val TestTable = object : IntIdTable("HIKARI_TESTER") { }

private fun Transaction.getReadOnlyMode(): Boolean {
val mode = exec("SHOW transaction_read_only;") {
it.next()
it.getBoolean(1)
}
assertNotNull(mode)
return mode == true
}

0 comments on commit 3a76a15

Please sign in to comment.