https://kotlinlang.org logo
#mockk
Title
# mockk
t

tKw

12/12/2018, 4:02 PM
Hello all, has anyone had luck mocking Exposed with MockK? The team I am on have been trying to verify that a select query (or insert, update, etc.) is called. I will add the minified code I have setup (similar to our production code) and what we have so far for testing which is not working.
Copy code
import com.zaxxer.hikari.HikariConfig
import com.zaxxer.hikari.HikariDataSource
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.jetbrains.exposed.dao.IntIdTable
import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.ResultRow
import org.jetbrains.exposed.sql.Transaction
import org.jetbrains.exposed.sql.selectAll
import org.jetbrains.exposed.sql.transactions.transaction
import kotlin.coroutines.CoroutineContext


class DatabaseConnection(config: HikariConfig) {
    private val dispatcher: CoroutineContext = Dispatchers.Default
    private val connection: Database = Database.connect(HikariDataSource(config))

    suspend fun <T> query(transaction: Transaction.() -> T) =
        withContext(dispatcher) {
            transaction(db = connection, statement = transaction)
        }
}

interface DatabaseConnectionProvider {
    suspend fun <T> query(transaction: (Transaction.() -> T)): T
}

object DatabaseTest : DatabaseConnectionProvider {
    val config = HikariConfig().apply {
        driverClassName = "org.postgresql.Driver"
        jdbcUrl = "jdbc:<postgresql://localhost:5432/exposed>"
        username = "exposed"
        password = "exposed"
        maximumPoolSize = 3
        isAutoCommit = false
        transactionIsolation = "TRANSACTION_REPEATABLE_READ"
    }

    private val connection: DatabaseConnection = DatabaseConnection(config)

    override suspend fun <T> query(transaction: (Transaction.() -> T)) = connection.query(transaction)
}

object Peeps : IntIdTable("peeps") {
    data class Peep(val name: String, val role: String)

    val name = text("name")
    val role = text("role")

    fun toPeep(row: ResultRow): Peep = Peep(
        row[Peeps.name],
        row[Peeps.role]
    )
}

suspend fun main() {
    DatabaseTest.query {
        Peeps.selectAll().orderBy(Peeps.id).map { Peeps.toPeep(it) }.forEach { println("${it.name} - ${it.role}") }
    }
}
Copy code
import io.kotlintest.specs.DescribeSpec
import io.mockk.*
import kotlinx.coroutines.runBlocking
import org.jetbrains.exposed.sql.Transaction
import org.jetbrains.exposed.sql.selectAll
import org.jetbrains.exposed.sql.statements.InsertSelectStatement

class MainTests: DescribeSpec({
    describe("test migrateDatabase") {
        it("") {
            coEvery {
                DatabaseTest.query(any<Transaction.() -> Unit>())
            } returns mockk()

            runBlocking {
                DatabaseTest.query {
                    Peeps.selectAll().map { Peeps.toPeep(it) }.forEach { println(it.name) }
                }
            }

            coVerify {
                DatabaseTest.query { any<InsertSelectStatement>() }
            }
        }
    }
})
10 Views