Title
t

tKw

12/12/2018, 3:38 PM
Hello all! Has anyone had luck mocking Exposed using MockK?
I will attach some example code (similar to what we are trying to test).
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}") }
    }
}
Below is what we have so far for testing.
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>() }
            }
        }
    }
})
c

Czar

12/12/2018, 3:42 PM
"Edit message" button + triple backticks 🙂
🙂 1
t

tKw

12/12/2018, 3:45 PM
I think I need more coffee.
m

MrNiamh

12/12/2018, 3:57 PM
#mockk may have some answers too
👍 1