When testing with dittodb, wrap your tests in with_mock_path({})
to use the
database fixtures located in other directories. dittodb
will look for
fixtures in the directory specified by the user, which can be a temporary
or permanent location.
Examples
# Only run if RSQLite and testthat are available
if (check_for_pkg("RSQLite", message) & check_for_pkg("testthat", message)) {
with_mock_path(
system.file("nycflight_mocks", package = "dittodb"),
with_mock_db({
con <- DBI::dbConnect(
RSQLite::SQLite(),
dbname = "nycflights"
)
one_airline <- dbGetQuery(
con,
"SELECT carrier, name FROM airlines LIMIT 1"
)
testthat::test_that("We get one airline", {
testthat::expect_s3_class(one_airline, "data.frame")
testthat::expect_equal(nrow(one_airline), 1)
testthat::expect_equal(one_airline$carrier, "9E")
testthat::expect_equal(one_airline$name, "Endeavor Air Inc.")
})
one_airline
})
)
}
#> Test passed 🌈
#> carrier name
#> 1 9E Endeavor Air Inc.