Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
46 lines (46 sloc) 1.24 KB
#' Run the DBI queries inside against a mocked db
#'
#' When testing with dittodb, wrap your tests in `with_mock_db({})` to use the
#' database fixtures. `dittodb` will look for fixtures in all entries of
#' [`.db_mock_paths`].
#'
#' Connections should be made inside of `with_mock_db()` because `dittodb` uses
#' the database name (given in `dbname` or `Database` argument of [`dbConnect`]
#'depending on the driver) to separate different fixtures.
#'
#' @param expr the expression to execute
#'
#' @return nothing
#' @importFrom testthat with_mock
#' @export
#'
#' @examples
#' \dontrun{
#' with_mock_db({
#' con <- dbConnect(
#' RPostgreSQL::PostgreSQL(),
#' dbname = "nycflights",
#' host = "127.0.0.1",
#' user = "travis",
#' password = ""
#' )
#'
#' test_that("We get one airline", {
#' one_airline <- dbGetQuery(
#' con,
#' "SELECT carrier, name FROM airlines LIMIT 1"
#' )
#' expect_is(one_airline, "data.frame")
#' expect_equal(nrow(one_airline), 1)
#' expect_equal(one_airline$carrier, "9E")
#' expect_equal(one_airline$name, "Endeavor Air Inc.")
#' })
#' })
#' }
with_mock_db <- function(expr) {
with_mock(
dbConnect = function(...) dbMockConnect(...),
.env = "DBI",
expr
)
}
You can’t perform that action at this time.