Vinicius Araujo
06/13/2019, 4:52 PMJonathan Mew
06/14/2019, 7:46 AMVinicius Araujo
06/14/2019, 2:49 PMJonathan Mew
06/14/2019, 3:01 PMDatabase
object) and creating a transaction.
The code creating the transaction doesn't have to supply a database if you only talk to a single database during your tests - just avoid connecting to the other database during testing. If you need to connect to both during testing, the code that creates a transaction will need access to a Database
object created by `connect`ing to the correct db.Vinicius Araujo
06/14/2019, 3:54 PMobject DatabaseFactory {
fun hikari(): HikariDataSource {
val config = HikariConfig().also {
it.username = sqlConfig.username
it.password = sqlConfig.password
it.jdbcUrl = sqlConfig.url
}
return HikariDataSource(config)
}
}
val connectSql by lazy {
Database.connect(DatabaseFactory.hikari())
}
suspend fun <T> dbQuery(block: () -> T): T =
withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
try {
connectSql
}catch (e: Exception) {
println(e.stackTrace)
throw ErroInternoException(codigo = 501, msgInterna = "Erro de conexao com banco de dados")
}
transaction { block() }
}
So at the service method I only use:
dbQuery { query inside }
Jonathan Mew
06/14/2019, 4:06 PMDatabase.connect()
in that @BeforeAllVinicius Araujo
06/14/2019, 7:33 PMJonathan Mew
06/15/2019, 8:52 PM