Skip to content

Commit

Permalink
fix: 🐛 verify function actually throws error (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
lwjohnst86 authored Aug 16, 2024
1 parent 48c8673 commit c018be6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
32 changes: 20 additions & 12 deletions R/verify-variables.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,34 @@
#' occurs, rather than within this function.
#'
#' @param data The dataset to check.
#' @param call The environment where the function is called, so that the error
#' traceback gives a more meaningful location.
#' @inheritParams get_required_variables
#'
#' @return Either TRUE if the verification passes, or a character string if
#' there is an error.
#' @return Either TRUE if the verification passes, or an error.
#' @keywords internal
#'
#' @examples
#' # TODO: Replace with simulated data.
#' example_bef_data <- tibble::tibble(pnr = 1, koen = 1, foed_dato = 1)
#' osdc:::verify_required_variables(example_bef_data, "bef")
verify_required_variables <- function(data, register) {
#' verify_required_variables(register_data$bef, "bef")
#' verify_required_variables(register_data$lpr_adm, "lpr_adm")
verify_required_variables <- function(data, register, call = rlang::caller_env()) {
checkmate::assert_choice(register, get_register_abbrev())
expected_variables <- sort(get_required_variables(register))
actual_variables <- sort(colnames(data))

# TODO: Consider using/looking into rlang::try_fetch() to provide contextual error messages.
expected_variables <- get_required_variables(register)

actual_variables <- colnames(data)

checkmate::check_names(
if (!checkmate::test_names(
x = actual_variables,
must.include = expected_variables
)
)) {
cli::cli_abort(
c(
"This function needs specific variables from the {.val {register}} register.",
"i" = "Variables required: {.val {expected_variables}}",
"x" = "Variables found: {.val {actual_variables}}"
),
call = call
)
}
return(invisible(TRUE))
}
12 changes: 7 additions & 5 deletions man/verify_required_variables.Rd

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

3 changes: 1 addition & 2 deletions tests/testthat/test-verify-variables.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ test_that("the required variables are present in the dataset", {
# When some of the variables are the required variables
expect_true(verify_required_variables(bef_complete_extra, "bef"))

# When it is a character output, it is a fail
expect_type(verify_required_variables(bef_incomplete, "bef"), "character")
expect_error(verify_required_variables(bef_incomplete, "bef"))
})


Expand Down

0 comments on commit c018be6

Please sign in to comment.