Skip to content

Commit

Permalink
refactor testcontainers usage
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentlauvlwj committed May 29, 2022
1 parent cad4e8e commit 4838537
Show file tree
Hide file tree
Showing 19 changed files with 156 additions and 171 deletions.
2 changes: 1 addition & 1 deletion ktorm-core/src/test/kotlin/org/ktorm/BaseTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ abstract class BaseTest {

@Before
open fun init() {
database = Database.connect("jdbc:h2:mem:ktorm;DB_CLOSE_DELAY=-1", "org.h2.Driver", alwaysQuoteIdentifiers = true)
database = Database.connect("jdbc:h2:mem:ktorm;DB_CLOSE_DELAY=-1", alwaysQuoteIdentifiers = true)
execSqlScript("init-data.sql")
}

Expand Down
1 change: 0 additions & 1 deletion ktorm-core/src/test/resources/drop-data.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

drop table if exists "t_department";
drop table if exists "t_employee";
drop table if exists "t_employee0";
Expand Down
1 change: 0 additions & 1 deletion ktorm-core/src/test/resources/init-data.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

create table "t_department"(
"id" int not null primary key auto_increment,
"name" varchar(128) not null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import org.ktorm.database.Database
open class BaseGlobalTest : BaseTest() {

override fun init() {
database = Database.connectGlobally("jdbc:h2:mem:ktorm;DB_CLOSE_DELAY=-1", "org.h2.Driver", alwaysQuoteIdentifiers = true)
database = Database.connectGlobally("jdbc:h2:mem:ktorm;DB_CLOSE_DELAY=-1", alwaysQuoteIdentifiers = true)
execSqlScript("init-data.sql")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.ktorm.support.mysql

import org.ktorm.BaseTest
import org.ktorm.database.Database
import org.testcontainers.containers.MySQLContainer
import kotlin.concurrent.thread

abstract class BaseMySqlTest : BaseTest() {

override fun init() {
database = Database.connect(jdbcUrl, driverClassName, username, password)
execSqlScript("init-mysql-data.sql")
}

override fun destroy() {
execSqlScript("drop-mysql-data.sql")
}

companion object : MySQLContainer<Companion>("mysql:8") {
init {
// Start the container when it's first used.
start()
// Stop the container when the process exits.
Runtime.getRuntime().addShutdownHook(thread(start = false) { stop() })
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@ package org.ktorm.support.mysql

import org.hamcrest.CoreMatchers.equalTo
import org.hamcrest.MatcherAssert.assertThat
import org.junit.ClassRule
import org.junit.Test
import org.ktorm.BaseTest
import org.ktorm.database.Database
import org.ktorm.database.use
import org.ktorm.dsl.*
import org.ktorm.entity.*
import org.ktorm.jackson.json
import org.ktorm.schema.*
import org.testcontainers.containers.MySQLContainer
import java.time.LocalDate
import java.time.LocalDateTime
import java.util.concurrent.ExecutionException
Expand All @@ -22,24 +18,7 @@ import java.util.concurrent.TimeoutException
/**
* Created by vince on Dec 12, 2018.
*/
class MySqlTest : BaseTest() {

companion object {
class KMySqlContainer : MySQLContainer<KMySqlContainer>("mysql:8")

@ClassRule
@JvmField
val mysql = KMySqlContainer()
}

override fun init() {
database = Database.connect(mysql.jdbcUrl, mysql.driverClassName, mysql.username, mysql.password)
execSqlScript("init-mysql-data.sql")
}

override fun destroy() {
execSqlScript("drop-mysql-data.sql")
}
class CommonTest : BaseMySqlTest() {

@Test
fun testKeywordWrapping() {
Expand Down Expand Up @@ -434,7 +413,7 @@ class MySqlTest : BaseTest() {

@Test
fun testSchema() {
val t = object : Table<Department>("t_department", schema = mysql.databaseName) {
val t = object : Table<Department>("t_department", schema = databaseName) {
val id = int("id").primaryKey().bindTo { it.id }
val name = varchar("name").bindTo { it.name }
}
Expand Down
1 change: 0 additions & 1 deletion ktorm-support-mysql/src/test/resources/init-mysql-data.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

create table t_department(
id int not null primary key auto_increment,
name varchar(128) not null,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.ktorm.support.oracle

import org.ktorm.BaseTest
import org.ktorm.database.Database
import org.testcontainers.containers.OracleContainer
import kotlin.concurrent.thread

abstract class BaseOracleTest : BaseTest() {

override fun init() {
database = Database.connect(jdbcUrl, driverClassName, username, password, alwaysQuoteIdentifiers = true)
execSqlScript("init-oracle-data.sql")
}

override fun destroy() {
execSqlScript("drop-oracle-data.sql")
}

companion object : OracleContainer("zerda/oracle-database:11.2.0.2-xe") {
init {
// At least 1 GB memory is required by Oracle.
withCreateContainerCmdModifier { cmd -> cmd.hostConfig?.withShmSize((1 * 1024 * 1024 * 1024).toLong()) }
// Start the container when it's first used.
start()
// Stop the container when the process exits.
Runtime.getRuntime().addShutdownHook(thread(start = false) { stop() })
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package org.ktorm.support.oracle

import org.junit.ClassRule
import org.junit.Test
import org.ktorm.BaseTest
import org.ktorm.database.Database
import org.ktorm.database.use
import org.ktorm.dsl.*
import org.ktorm.entity.count
Expand All @@ -13,37 +10,11 @@ import org.ktorm.entity.sequenceOf
import org.ktorm.schema.Table
import org.ktorm.schema.int
import org.ktorm.schema.varchar
import org.testcontainers.containers.OracleContainer

/**
* Created by vince at Aug 01, 2020.
*/
class OracleTest : BaseTest() {
companion object {
const val TOTAL_RECORDS = 4
const val MINUS_ONE = -1
const val ZERO = 0
const val ONE = 1
const val TWO = 2
const val ID_1 = 1
const val ID_2 = 2
const val ID_3 = 3
const val ID_4 = 4

@ClassRule
@JvmField
val oracle: OracleContainer = OracleContainer("zerda/oracle-database:11.2.0.2-xe")
.withCreateContainerCmdModifier { cmd -> cmd.hostConfig?.withShmSize((1 * 1024 * 1024 * 1024).toLong()) }
}

override fun init() {
database = Database.connect(oracle.jdbcUrl, oracle.driverClassName, oracle.username, oracle.password, alwaysQuoteIdentifiers = true)
execSqlScript("init-oracle-data.sql")
}

override fun destroy() {
execSqlScript("drop-oracle-data.sql")
}
class CommonTest : BaseOracleTest() {

@Test
fun testKeywordWrapping() {
Expand Down Expand Up @@ -84,47 +55,47 @@ class OracleTest : BaseTest() {
*/
@Test
fun testBothLimitAndOffsetAreNotPositive() {
val query = database.from(Employees).select().orderBy(Employees.id.desc()).limit(ZERO, MINUS_ONE)
assert(query.totalRecords == TOTAL_RECORDS)
val query = database.from(Employees).select().orderBy(Employees.id.desc()).limit(0, -1)
assert(query.totalRecords == 4)

val ids = query.map { it[Employees.id] }
assert(ids == listOf(ID_4, ID_3, ID_2, ID_1))
assert(ids == listOf(4, 3, 2, 1))
}

/**
* Verifies that limit parameter works as expected.
*/
@Test
fun testLimitWithoutOffset() {
val query = database.from(Employees).select().orderBy(Employees.id.desc()).limit(TWO)
assert(query.totalRecords == TOTAL_RECORDS)
val query = database.from(Employees).select().orderBy(Employees.id.desc()).limit(2)
assert(query.totalRecords == 4)

val ids = query.map { it[Employees.id] }
assert(ids == listOf(ID_4, ID_3))
assert(ids == listOf(4, 3))
}

/**
* Verifies that offset parameter works as expected.
*/
@Test
fun testOffsetWithoutLimit() {
val query = database.from(Employees).select().orderBy(Employees.id.desc()).offset(TWO)
assert(query.totalRecords == TOTAL_RECORDS)
val query = database.from(Employees).select().orderBy(Employees.id.desc()).offset(2)
assert(query.totalRecords == 4)

val ids = query.map { it[Employees.id] }
assert(ids == listOf(ID_2, ID_1))
assert(ids == listOf(2, 1))
}

/**
* Verifies that limit and offset parameters work together as expected.
*/
@Test
fun testOffsetWithLimit() {
val query = database.from(Employees).select().orderBy(Employees.id.desc()).offset(TWO).limit(ONE)
assert(query.totalRecords == TOTAL_RECORDS)
val query = database.from(Employees).select().orderBy(Employees.id.desc()).offset(2).limit(1)
assert(query.totalRecords == 4)

val ids = query.map { it[Employees.id] }
assert(ids == listOf(ID_2))
assert(ids == listOf(2))
}

@Test
Expand All @@ -136,7 +107,7 @@ class OracleTest : BaseTest() {

@Test
fun testSchema() {
val t = object : Table<Department>("t_department", schema = oracle.username.uppercase()) {
val t = object : Table<Department>("t_department", schema = username.uppercase()) {
val id = int("id").primaryKey().bindTo { it.id }
val name = varchar("name").bindTo { it.name }
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

drop table "t_department";
drop table "t_employee";
drop table "t_employee0";
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

create table "t_department"(
"id" int not null primary key,
"name" varchar(128) not null,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.ktorm.support.sqlite

import org.ktorm.BaseTest
import org.ktorm.database.Database
import java.sql.Connection
import java.sql.DriverManager

abstract class BaseSQLiteTest : BaseTest() {
lateinit var connection: Connection

override fun init() {
connection = DriverManager.getConnection("jdbc:sqlite::memory:")

database = Database.connect {
object : Connection by connection {
override fun close() {
// do nothing...
}
}
}

execSqlScript("init-sqlite-data.sql")
}

override fun destroy() {
execSqlScript("drop-sqlite-data.sql")
connection.close()
}
}
Loading

0 comments on commit 4838537

Please sign in to comment.