Thread
#exposed
    Vinicius Araujo

    Vinicius Araujo

    3 years ago
    Does anyone have an example on how to use a H2 connection for unit testing, and use the default connection for real usage without setting the connection by parameter?
    j

    Jonathan Mew

    3 years ago
    What unit testing framework are you using? If you just have a single database you can set up the connection in an @BeforeAll rule and not supply the database to your transactions. Or perhaps I haven't quite understood the question...
    Vinicius Araujo

    Vinicius Araujo

    3 years ago
    You got it right. I'm using KotlinTest and it has this feature. I'll try it.
    The default (postgres) transaction is automatically created inside the service method, and I want my test to call this this method, but using the H2 connection instead. Not sure how to do this
    j

    Jonathan Mew

    3 years ago
    Do you ever need to communicate with the postgres database in your unit tests?
    It's perhaps helpful to draw a distinction between connecting to a database (building a
    Database
    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 connecting to the correct db.
    Vinicius Araujo

    Vinicius Araujo

    3 years ago
    I do not need to connect postgres from test. This is how I connect the DB:
    object 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 }
    I wish I could set the H2 DB from test without changing the default factory
    j

    Jonathan Mew

    3 years ago
    Sorry, I haven't got any experience with Hikari (nor have I actually used H2...). But I think you can do your connection any time before your first query, so perhaps you can separate it in a way that is reliably called by your application on startup, but isn't so close to your db access.
    then you can do an H2
    Database.connect()
    in that @BeforeAll
    and avoid calling into your DatabaseFactory in your tests
    Vinicius Araujo

    Vinicius Araujo

    3 years ago
    I got this! KotlinTest let me insert environment variables for the test runtime, so I put connection details that are overrided within the test, also created the db tables in the BeforeSpec you mentioned. Now working like a charm 🙂 Thanks for your help
    j

    Jonathan Mew

    3 years ago
    🎉