52 lines
1.3 KiB
Kotlin

package be.simplenotes.persistence
import be.simplenotes.config.DataSourceConfig
import io.micronaut.context.BeanContext
import org.flywaydb.core.Flyway
import org.junit.jupiter.api.AfterAll
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.parallel.ResourceLock
import javax.sql.DataSource
@ResourceLock("h2")
abstract class DbTest {
val beanContext = BeanContext.build()
inline fun <reified T> BeanContext.getBean(): T = getBean(T::class.java)
@BeforeAll
fun setComponent() {
beanContext
.registerSingleton(
DataSourceConfig(
jdbcUrl = "jdbc:h2:mem:regular;DB_CLOSE_DELAY=-1",
username = "h2",
password = "",
maximumPoolSize = 2,
connectionTimeout = 3000
)
)
.start()
}
@BeforeEach
fun beforeEach() {
val migration = beanContext.getBean<DbMigrations>()
val dataSource = beanContext.getBean<DataSource>()
Flyway.configure()
.dataSource(dataSource)
.load()
.clean()
migration.migrate()
}
@AfterAll
fun closeCtx() {
beanContext.close()
}
}