Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#183: Error handling for the connection object being made before start_db_capturing() #186

Merged
merged 25 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
export(.db_mock_paths)
export(.dittodb_env)
export(capture_db_requests)
export(check_db_path)
export(check_for_pkg)
export(clean_statement)
export(dbMockConnect)
Expand Down
28 changes: 28 additions & 0 deletions R/capture-requests.R
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ dbConnectTrace <- quote({
})

dbSendQueryTrace <- quote({
check_db_path(.dittodb_env)

if (dittodb_debug_level(2)) {
message(
"The statement: \n", statement,
Expand All @@ -222,6 +224,7 @@ dbSendQueryTrace <- quote({
})

dbListTablesTrace <- quote({
check_db_path(.dittodb_env)
thing <- returnValue()
dput(
thing,
Expand All @@ -231,6 +234,7 @@ dbListTablesTrace <- quote({
})

dbListFieldsTrace <- quote({
check_db_path(.dittodb_env)
thing <- returnValue()
name <- sanitize_table_id(name, ...)
dput(
Expand All @@ -241,6 +245,7 @@ dbListFieldsTrace <- quote({
})

dbExistsTableTrace <- quote({
check_db_path(.dittodb_env)
thing <- returnValue()
name <- sanitize_table_id(name, ...)
dput(
Expand Down Expand Up @@ -429,3 +434,26 @@ get_redactor <- function() {

return(NULL)
}

#' Check dittodb environment path
#'
#' This function should generally not be used, but must be exported for the
#' query recording function to work properly
#'
#' @param .dittodb_env Environment object
#'
#' @return `NULL`, invisibly.
#' @export
check_db_path <- function(.dittodb_env) {
if (is.null(.dittodb_env$db_path)) {
rlang::abort(
message = c("Database capture failed",
"*" = "The database connection object was created before calling 'start_db_capturing()'",
"*" = "Please ensure the connection is created after calling 'start_db_capturing()'."
),
call = rlang::caller_env()
)
}

return(invisible())
}
18 changes: 18 additions & 0 deletions man/check_db_path.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions tests/testthat/test-a-recording.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,41 @@ test_that("We can specify the path when starting capture", {
dbDisconnect(con)
stop_db_capturing()
})

test_that("dbGetQuery error checking", {
# Check to make sure con was not created before start_db_capturing()
suppressMessages(con <- nycflights13_create_sqlite(verbose = FALSE))

start_db_capturing()

# Testthat sets this to "tests/testthat//_memory_"
# Setting this to NULL so it will mimic a developers experience
.dittodb_env$db_path <- NULL

regex_db <- "^Database capture failed"

error_get_query <- expect_error(
object = dbGetQuery(con, "SELECT * FROM airlines"),
regexp = regex_db
)

testthat_transition(
old = expect_error(dbListTables(con)),
new = expect_error(expect_error(dbListTables(con)), regex_db)
KoderKow marked this conversation as resolved.
Show resolved Hide resolved
)

testthat_transition(
old = expect_error(dbListFields(con, "airlines")),
new = expect_error(expect_error(dbListFields(con, "airlines")), regex_db)
)

testthat_transition(
old = expect_error(dbExistsTable(con, "airlines")),
new = expect_error(expect_error(dbExistsTable(con, "airlines")), regex_db)
)

error_tbl <- expect_error(dplyr::tbl(con, "airlines"))

suppressWarnings(dbDisconnect(con))
stop_db_capturing()
})