Skip to content

Commit

Permalink
inject helper message into postgres default check for each test run w…
Browse files Browse the repository at this point in the history
…ith db connection fxn
  • Loading branch information
sckott committed Jan 3, 2025
1 parent c0ef212 commit a738528
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions tests/testthat/helper-rls.R
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)
}

0 comments on commit a738528

Please sign in to comment.