-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
inject helper message into postgres default check for each test run w…
…ith db connection fxn
- Loading branch information
Showing
1 changed file
with
12 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,21 @@ | ||
postgres_default <- function(...) { | ||
tryCatch({ | ||
# replaces RPostgres:::connect_default | ||
DBI::dbConnect(RPostgres::Postgres(), ...) | ||
}, error = function(...) { | ||
vars <- "try setting env vars: PGHOST, PGPORT, PGUSER, PGDATABASE" | ||
testthat::skip(glue("Test database not available. {vars}")) | ||
}) | ||
} | ||
|
||
#' Execute an R expression with access to a database connection. | ||
#' | ||
#' @details Copied from the RPostgres package, thank you! | ||
#' @details Copied unchanged from the RPostgres package, thank you! | ||
#' @param expr (expression) Any R expression. | ||
#' @param con (PqConnection) A database connection, by default. | ||
#' [dbConnect(RPostgres::Postgres())]. | ||
#' @return the return value of the evaluated `expr` | ||
with_database_connection <- function(expr, con = RPostgres::postgresDefault()) { | ||
with_database_connection <- function(expr, con = postgres_default()) { | ||
context <- list2env(list(con = con), parent = parent.frame()) | ||
eval(substitute(expr), envir = context) | ||
} |