with_mock_db: Run the DBI queries inside against a mocked db

Description Usage Arguments Details Value Examples

View source: R/mock-db.R

Description

When testing with dbtest, wrap your tests in with_mock_db({}) to use the database fixtures. dbtest will look for fixtures in all entries of .mockPaths.

Usage

1

Arguments

expr

the expression to execute

Details

Connections should be made inside of with_mock_db() because dbtest uses the database name (given in dbname or Database argument of dbConnect depending on the driver) to separate different fixtures.

Value

nothing

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
## Not run: 
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.")
  })
})

## End(Not run)

pachamaltese/dbtest documentation built on Dec. 3, 2019, 11:08 p.m.