From a7385289075a8e8fa2daff7a0a4c89d044f0bf7e Mon Sep 17 00:00:00 2001 From: Scott Chamberlain Date: Fri, 3 Jan 2025 10:26:32 -0800 Subject: [PATCH] inject helper message into postgres default check for each test run with db connection fxn --- tests/testthat/helper-rls.R | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/tests/testthat/helper-rls.R b/tests/testthat/helper-rls.R index 423c1cc..d28703d 100644 --- a/tests/testthat/helper-rls.R +++ b/tests/testthat/helper-rls.R @@ -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) }