Skip to content

Commit

Permalink
#183: Error handling for the connection object being made before star…
Browse files Browse the repository at this point in the history
…t_db_capturing() (#186)
  • Loading branch information
KoderKow authored Apr 9, 2024
1 parent d2aa156 commit 7005e25
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 0 deletions.
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
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# dittodb (development version)
* Improved error messages for connections made before running `start_db_capture()`. (#183)
* Added to the `dbplyr` unique table names that are ignored to work with the newest `dbplyr` table names. (#188)

# dittodb 0.1.7
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), regex_db),
new = expect_error(expect_error(dbListTables(con)), regex_db)
)

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

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

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

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

0 comments on commit 7005e25

Please sign in to comment.