2021-02-27 18:07:22 +01:00

45 lines
1019 B
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 javax.sql.DataSource
abstract class DbTest {
abstract fun dataSourceConfig(): DataSourceConfig
val beanContext = BeanContext
.build()
inline fun <reified T> BeanContext.getBean(): T = getBean(T::class.java)
@BeforeAll
fun setComponent() {
beanContext
.registerSingleton(dataSourceConfig())
.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()
}
}