From a4b3c618d9db27aaee9e99c8f9da384ad6a99e12 Mon Sep 17 00:00:00 2001 From: Stefan Bundfuss Date: Wed, 30 Oct 2024 16:57:22 +0000 Subject: [PATCH 01/10] #468 multiple_units: extend required_unit argument --- R/assertions.R | 47 ++++++++++++++++++++++++++++++++++++++-------- man/assert_unit.Rd | 39 ++++++++++++++++++++++++++++++++------ 2 files changed, 72 insertions(+), 14 deletions(-) diff --git a/R/assertions.R b/R/assertions.R index 10d8751b..114ef47b 100644 --- a/R/assertions.R +++ b/R/assertions.R @@ -1099,9 +1099,18 @@ assert_function <- function(arg, #' Checks if a parameter (`PARAMCD`) in a dataset is provided in the expected #' unit. #' -#' @param dataset A `data.frame` +#' @param dataset Dataset to be checked +#' +#' The variable `PARAMCD` and those used in `get_unit_expr` are expected. +#' #' @param param Parameter code of the parameter to check -#' @param required_unit Expected unit +#' @param required_unit Expected unit(s) +#' +#' If the argument is set to `NULL`, it is checked only whether the unit is +#' unique. +#' +#' *Permitted Values*: A character vector or `NULL` +#' #' @param get_unit_expr Expression used to provide the unit of `param` #' #' @inheritParams assert_logical_scalar @@ -1111,9 +1120,12 @@ assert_function <- function(arg, #' @family assertion #' #' @return -#' The function throws an error if the unit variable differs from the -#' unit for any observation of the parameter in the input dataset. Otherwise, the -#' dataset is returned invisibly. +#' The function throws an error +#' - if there is more than one non-missing unit in the dataset or +#' - if the unit variable differs from the expected unit for any observation of +#' the parameter in the input dataset. +#' +#' Otherwise, the dataset is returned invisibly. #' #' @export #' @@ -1127,9 +1139,24 @@ assert_function <- function(arg, #' ) #' #' assert_unit(advs, param = "WEIGHT", required_unit = "kg", get_unit_expr = VSSTRESU) +#' +#' try( +#' assert_unit(advs, param = "WEIGHT", required_unit = c("g", "mg"), get_unit_expr = VSSTRESU) +#' ) +#' +#' # Checking uniqueness of unit only +#' advs <- tribble( +#' ~USUBJID, ~VSTESTCD, ~VSTRESN, ~VSSTRESU, ~PARAMCD, ~AVAL, +#' "P01", "WEIGHT", 80.1, "kg", "WEIGHT", 80.1, +#' "P02", "WEIGHT", 85.7, "g", "WEIGHT", 85.7 +#' ) +#' +#' try( +#' assert_unit(advs, param = "WEIGHT", get_unit_expr = VSSTRESU) +#' ) assert_unit <- function(dataset, param, - required_unit, + required_unit = NULL, get_unit_expr, arg_name = rlang::caller_arg(required_unit), message = NULL, @@ -1137,7 +1164,7 @@ assert_unit <- function(dataset, call = parent.frame()) { assert_data_frame(dataset, required_vars = exprs(PARAMCD)) assert_character_scalar(param) - assert_character_scalar(required_unit) + assert_character_vector(required_unit, optional = TRUE) get_unit_expr <- enexpr(get_unit_expr) units <- dataset %>% @@ -1157,7 +1184,11 @@ assert_unit <- function(dataset, class = c(class, "assert-admiraldev") ) } - if (tolower(units) != tolower(required_unit)) { + + if (!is.null(required_unit) && !(tolower(units) %in% tolower(required_unit))) { + # change cli `.val` to end with OR instead of AND + divid <- cli_div(theme = list(.val = list("vec-last" = ", or ", "vec_sep2" = " or "))) + message <- message %||% "It is expected that {.val {param}} has unit of {.val {required_unit}}. diff --git a/man/assert_unit.Rd b/man/assert_unit.Rd index 4762b616..f59b2a6b 100644 --- a/man/assert_unit.Rd +++ b/man/assert_unit.Rd @@ -7,7 +7,7 @@ assert_unit( dataset, param, - required_unit, + required_unit = NULL, get_unit_expr, arg_name = rlang::caller_arg(required_unit), message = NULL, @@ -16,11 +16,18 @@ assert_unit( ) } \arguments{ -\item{dataset}{A \code{data.frame}} +\item{dataset}{Dataset to be checked + +The variable \code{PARAMCD} and those used in \code{get_unit_expr} are expected.} \item{param}{Parameter code of the parameter to check} -\item{required_unit}{Expected unit} +\item{required_unit}{Expected unit(s) + +If the argument is set to \code{NULL}, it is checked only whether the unit is +unique. + +\emph{Permitted Values}: A character vector or \code{NULL}} \item{get_unit_expr}{Expression used to provide the unit of \code{param}} @@ -47,9 +54,14 @@ respectively not display any call or hard-code a code to display. For more information about error calls, see \ifelse{html}{\link[rlang:topic-error-call]{Including function calls in error messages}}{\link[rlang:topic-error-call]{Including function calls in error messages}}.} } \value{ -The function throws an error if the unit variable differs from the -unit for any observation of the parameter in the input dataset. Otherwise, the -dataset is returned invisibly. +The function throws an error +\itemize{ +\item if there is more than one non-missing unit in the dataset or +\item if the unit variable differs from the expected unit for any observation of +the parameter in the input dataset. +} + +Otherwise, the dataset is returned invisibly. } \description{ Checks if a parameter (\code{PARAMCD}) in a dataset is provided in the expected @@ -65,6 +77,21 @@ advs <- tribble( ) assert_unit(advs, param = "WEIGHT", required_unit = "kg", get_unit_expr = VSSTRESU) + +try( + assert_unit(advs, param = "WEIGHT", required_unit = c("g", "mg"), get_unit_expr = VSSTRESU) +) + +# Checking uniqueness of unit only +advs <- tribble( + ~USUBJID, ~VSTESTCD, ~VSTRESN, ~VSSTRESU, ~PARAMCD, ~AVAL, + "P01", "WEIGHT", 80.1, "kg", "WEIGHT", 80.1, + "P02", "WEIGHT", 85.7, "g", "WEIGHT", 85.7 +) + +try( + assert_unit(advs, param = "WEIGHT", get_unit_expr = VSSTRESU) +) } \seealso{ Checks for valid input and returns warning or errors messages: From 07a7327a0cf2d29fa15db423f2c02351ce373ccf Mon Sep 17 00:00:00 2001 From: Stefan Bundfuss Date: Thu, 31 Oct 2024 11:42:14 +0000 Subject: [PATCH 02/10] #468 multiple_units: update tests and NEWS --- NEWS.md | 4 + R/assertions.R | 26 ++++- man/assert_unit.Rd | 9 +- tests/testthat/_snaps/assertions.md | 40 ++++--- tests/testthat/test-assertions.R | 166 +++++++++++++++++----------- 5 files changed, 159 insertions(+), 86 deletions(-) diff --git a/NEWS.md b/NEWS.md index be4cbab7..2cf16bcf 100644 --- a/NEWS.md +++ b/NEWS.md @@ -2,6 +2,10 @@ ## Updates of Existing Functions +- The `required_unit` argument of `assert_unit()` has been enhanced. It is now +possible to specify more than one unit or not specifying it. In the latter case +only the uniqueness of the unit is checked. (#468) + ## Breaking Changes - The following functions are entering the next phase of the deprecation process: (#459) diff --git a/R/assertions.R b/R/assertions.R index 114ef47b..37c87c60 100644 --- a/R/assertions.R +++ b/R/assertions.R @@ -1141,14 +1141,19 @@ assert_function <- function(arg, #' assert_unit(advs, param = "WEIGHT", required_unit = "kg", get_unit_expr = VSSTRESU) #' #' try( -#' assert_unit(advs, param = "WEIGHT", required_unit = c("g", "mg"), get_unit_expr = VSSTRESU) +#' assert_unit( +#' advs, +#' param = "WEIGHT", +#' required_unit = c("g", "mg"), +#' get_unit_expr = VSSTRESU +#' ) #' ) #' #' # Checking uniqueness of unit only #' advs <- tribble( #' ~USUBJID, ~VSTESTCD, ~VSTRESN, ~VSSTRESU, ~PARAMCD, ~AVAL, #' "P01", "WEIGHT", 80.1, "kg", "WEIGHT", 80.1, -#' "P02", "WEIGHT", 85.7, "g", "WEIGHT", 85.7 +#' "P02", "WEIGHT", 85700, "g", "WEIGHT", 85700 #' ) #' #' try( @@ -1167,8 +1172,21 @@ assert_unit <- function(dataset, assert_character_vector(required_unit, optional = TRUE) get_unit_expr <- enexpr(get_unit_expr) - units <- dataset %>% - mutate(`_unit` = !!get_unit_expr) %>% + tryCatch( + data_unit <- mutate(dataset, `_unit` = !!get_unit_expr), + error = function(cnd) { + cli_abort( + message = + c("Extracting units using expression {.code {get_unit_expr}} specified for {.arg get_unit_expr} failed!", + "See error message below:", + conditionMessage(cnd) + ), + call = parent.frame(n = 4), + class = c(class, "assert-admiraldev", class(cnd)) + ) + } + ) + units <- data_unit %>% filter(PARAMCD == param & !is.na(`_unit`)) %>% pull(`_unit`) %>% unique() diff --git a/man/assert_unit.Rd b/man/assert_unit.Rd index f59b2a6b..e62250e0 100644 --- a/man/assert_unit.Rd +++ b/man/assert_unit.Rd @@ -79,14 +79,19 @@ advs <- tribble( assert_unit(advs, param = "WEIGHT", required_unit = "kg", get_unit_expr = VSSTRESU) try( - assert_unit(advs, param = "WEIGHT", required_unit = c("g", "mg"), get_unit_expr = VSSTRESU) + assert_unit( + advs, + param = "WEIGHT", + required_unit = c("g", "mg"), + get_unit_expr = VSSTRESU + ) ) # Checking uniqueness of unit only advs <- tribble( ~USUBJID, ~VSTESTCD, ~VSTRESN, ~VSSTRESU, ~PARAMCD, ~AVAL, "P01", "WEIGHT", 80.1, "kg", "WEIGHT", 80.1, - "P02", "WEIGHT", 85.7, "g", "WEIGHT", 85.7 + "P02", "WEIGHT", 85700, "g", "WEIGHT", 85700 ) try( diff --git a/tests/testthat/_snaps/assertions.md b/tests/testthat/_snaps/assertions.md index 771f4752..ec3b3fc8 100644 --- a/tests/testthat/_snaps/assertions.md +++ b/tests/testthat/_snaps/assertions.md @@ -323,15 +323,15 @@ Error in `example_fun()`: ! "x" and "y" are not arguments of the function specified for `arg`. -# assert_unit Test 57: error if there are multiple units in the input dataset +# assert_unit Test 58: error if multiple units in the input dataset Code - assert_unit(advs, param = "WEIGHT", required_unit = "kg", get_unit_expr = VSSTRESU) + assert_unit(advs, param = "WEIGHT", get_unit_expr = VSSTRESU) Condition Error: ! Multiple units "kg" and "lb" found for "WEIGHT". Please review and update the units. -# assert_unit Test 58: error if unexpected unit in the input dataset +# assert_unit Test 59: error if unexpected unit in the input dataset Code assert_unit(advs, param = "WEIGHT", required_unit = "lb", get_unit_expr = VSSTRESU) @@ -339,7 +339,17 @@ Error: ! It is expected that "WEIGHT" has unit of "lb". In the input dataset the unit is "kg". -# assert_param_does_not_exist Test 59: error if parameter exists in the input dataset +# assert_unit Test 60: error if get_unit_expr invalid + + Code + assert_unit(advs, param = "WEIGHT", required_unit = "kg", get_unit_expr = VSTRESU) + Condition + Error in `assert_unit()`: + ! Extracting units using expression `VSTRESU` specified for `get_unit_expr` failed! + See error message below: + i In argument: `_unit = VSTRESU`. Caused by error: ! object 'VSTRESU' not found + +# assert_param_does_not_exist Test 61: error if parameter exists in the input dataset Code assert_param_does_not_exist(advs, param = "WEIGHT") @@ -347,7 +357,7 @@ Error: ! The parameter code "WEIGHT" already exists in dataset `advs`. -# assert_varval_list Test 61: error if `arg` is not a list of var-value expressions +# assert_varval_list Test 63: error if `arg` is not a list of var-value expressions Code example_fun(c("USUBJID", "PARAMCD", "VISIT")) @@ -356,7 +366,7 @@ ! Argument `arg` must be a named list of expressions where each element is a symbol, character scalar, numeric scalar, an expression, or NA, but is a character vector. i To create a list of expressions use `exprs()`. -# assert_varval_list Test 62: error if `arg` is not a list of var-value expressions +# assert_varval_list Test 64: error if `arg` is not a list of var-value expressions Code example_fun(exprs(USUBJID, PARAMCD, NULL)) @@ -365,7 +375,7 @@ ! Argument `arg` must be a list of expressions where each element is a symbol, character scalar, numeric scalar, an expression, or NA, but is a list. i To create a list of expressions use `exprs()`. -# assert_varval_list Test 63: error if `required_elements` are missing from `arg` +# assert_varval_list Test 65: error if `required_elements` are missing from `arg` Code example_fun(exprs(DTHSEQ = AESEQ)) @@ -373,7 +383,7 @@ Error in `example_fun()`: ! The following required elements are missing from argument `arg`: "DTHDOM". -# assert_varval_list Test 65: error if `accept_expr` is TRUE and value is invalid +# assert_varval_list Test 67: error if `accept_expr` is TRUE and value is invalid Code example_fun(exprs(DTHSEQ = TRUE)) @@ -382,7 +392,7 @@ ! The elements of the list in argument `arg` must be a symbol, character scalar, numeric scalar, an expression, or NA. i "DTHSEQ" = `TRUE` is of type -# assert_varval_list Test 66: error if `accept_expr` is FALSE and value is invalid +# assert_varval_list Test 68: error if `accept_expr` is FALSE and value is invalid Code example_fun(exprs(DTHSEQ = exprs())) @@ -391,7 +401,7 @@ ! The elements of the list in argument `arg` must be a symbol, character scalar, numeric scalar, or NA. i "DTHSEQ" = `exprs()` is of type -# assert_list_element Test 75: error if the elements do not fulfill the condition +# assert_list_element Test 77: error if the elements do not fulfill the condition Code assert_list_element(list(list(var = expr(DTHDT), val = 1), list(var = expr( @@ -403,7 +413,7 @@ ! List element "val" must be `>=0` in argument `input`: i But, `input[[2]]$val = -1`, and `input[[3]]$val = -2` -# assert_one_to_one Test 76: error if there is a one to many mapping +# assert_one_to_one Test 78: error if there is a one to many mapping Code assert_one_to_one(dm, exprs(DOMAIN), exprs(USUBJID)) @@ -412,7 +422,7 @@ ! For some values of "DOMAIN" there is more than one value of "USUBJID" i Call `admiral::get_one_to_many_dataset()` to get all one-to-many values. -# assert_date_var Test 79: error if variable is not a date or datetime variable +# assert_date_var Test 81: error if variable is not a date or datetime variable Code example_fun(dataset = my_data, var = USUBJID) @@ -420,7 +430,7 @@ Error in `example_fun()`: ! Column "USUBJID" in dataset `dataset` must be a date or datetime, but is a character vector. -# assert_date_vector Test 83: error if `arg` is NULL and optional is FALSE +# assert_date_vector Test 85: error if `arg` is NULL and optional is FALSE Code example_fun(NULL) @@ -428,7 +438,7 @@ Error in `example_fun()`: ! Argument `arg` must be a date or datetime, but is NULL. -# assert_atomic_vector Test 84: error if input is not atomic vector +# assert_atomic_vector Test 86: error if input is not atomic vector Code assert_atomic_vector(x) @@ -436,7 +446,7 @@ Error: ! Argument `x` must be an atomic vector, but is a list. -# assert_same_type Test 86: error if different type +# assert_same_type Test 88: error if different type Code assert_same_type(true_value, false_value, missing_value) diff --git a/tests/testthat/test-assertions.R b/tests/testthat/test-assertions.R index d6ed0857..a408ce5b 100644 --- a/tests/testthat/test-assertions.R +++ b/tests/testthat/test-assertions.R @@ -990,8 +990,26 @@ test_that("assert_unit Test 56: no error if the parameter is provided in the exp ) }) -## Test 57: error if there are multiple units in the input dataset ---- -test_that("assert_unit Test 57: error if there are multiple units in the input dataset", { +## Test 57: no error for multiple expected units ---- +test_that("assert_unit Test 57: no error for multiple expected units", { + advs <- dplyr::tribble( + ~USUBJID, ~VSTESTCD, ~VSTRESN, ~VSSTRESU, ~PARAMCD, ~AVAL, + "P01", "WEIGHT", 80.1, "kg", "WEIGHT", 80.1, + "P02", "WEIGHT", 85.7, "kg", "WEIGHT", 85.7 + ) + + expect_invisible( + assert_unit( + advs, + param = "WEIGHT", + required_unit = c("kg", "lb"), + get_unit_expr = VSSTRESU + ) + ) +}) + +## Test 58: error if multiple units in the input dataset ---- +test_that("assert_unit Test 58: error if multiple units in the input dataset", { advs <- dplyr::tribble( ~USUBJID, ~VSTESTCD, ~VSTRESN, ~VSSTRESU, ~PARAMCD, ~AVAL, "P01", "WEIGHT", 80.1, "kg", "WEIGHT", 80.1, @@ -999,18 +1017,18 @@ test_that("assert_unit Test 57: error if there are multiple units in the input d ) expect_error( - assert_unit(advs, param = "WEIGHT", required_unit = "kg", get_unit_expr = VSSTRESU), + assert_unit(advs, param = "WEIGHT", get_unit_expr = VSSTRESU), class = "assert_unit" ) expect_snapshot( - assert_unit(advs, param = "WEIGHT", required_unit = "kg", get_unit_expr = VSSTRESU), + assert_unit(advs, param = "WEIGHT", get_unit_expr = VSSTRESU), error = TRUE ) }) -## Test 58: error if unexpected unit in the input dataset ---- -test_that("assert_unit Test 58: error if unexpected unit in the input dataset", { - advs <- dplyr::tribble( +## Test 59: error if unexpected unit in the input dataset ---- +test_that("assert_unit Test 59: error if unexpected unit in the input dataset", { + advs <- tibble::tribble( ~USUBJID, ~VSTESTCD, ~VSTRESN, ~VSSTRESU, ~PARAMCD, ~AVAL, "P01", "WEIGHT", 80.1, "kg", "WEIGHT", 80.1, "P02", "WEIGHT", 85.7, "kg", "WEIGHT", 85.7 @@ -1026,9 +1044,27 @@ test_that("assert_unit Test 58: error if unexpected unit in the input dataset", ) }) +## Test 60: error if get_unit_expr invalid ---- +test_that("assert_unit Test 60: error if get_unit_expr invalid", { + advs <- tibble::tribble( + ~USUBJID, ~VSTESTCD, ~VSTRESN, ~VSSTRESU, ~PARAMCD, ~AVAL, + "P01", "WEIGHT", 80.1, "kg", "WEIGHT", 80.1, + "P02", "WEIGHT", 85.7, "kg", "WEIGHT", 85.7 + ) + + expect_error( + assert_unit(advs, param = "WEIGHT", required_unit = "kg", get_unit_expr = VSTRESU), + class = "assert_unit" + ) + expect_snapshot( + assert_unit(advs, param = "WEIGHT", required_unit = "kg", get_unit_expr = VSTRESU), + error = TRUE + ) +}) + # assert_param_does_not_exist ---- -## Test 59: error if parameter exists in the input dataset ---- -test_that("assert_param_does_not_exist Test 59: error if parameter exists in the input dataset", { +## Test 61: error if parameter exists in the input dataset ---- +test_that("assert_param_does_not_exist Test 61: error if parameter exists in the input dataset", { advs <- dplyr::tribble( ~USUBJID, ~VSTESTCD, ~VSTRESN, ~VSSTRESU, ~PARAMCD, ~AVAL, "P01", "WEIGHT", 80.1, "kg", "WEIGHT", 80.1, @@ -1046,8 +1082,8 @@ test_that("assert_param_does_not_exist Test 59: error if parameter exists in the ) }) -## Test 60: no error if the parameter exists in the dataset ---- -test_that("assert_param_does_not_exist Test 60: no error if the parameter exists in the dataset", { +## Test 62: no error if the parameter exists in the dataset ---- +test_that("assert_param_does_not_exist Test 62: no error if the parameter exists in the dataset", { advs <- dplyr::tribble( ~USUBJID, ~VSTESTCD, ~VSTRESN, ~VSSTRESU, ~PARAMCD, ~AVAL, "P01", "WEIGHT", 80.1, "kg", "WEIGHT", 80.1, @@ -1060,8 +1096,8 @@ test_that("assert_param_does_not_exist Test 60: no error if the parameter exists }) # assert_varval_list ---- -## Test 61: error if `arg` is not a list of var-value expressions ---- -test_that("assert_varval_list Test 61: error if `arg` is not a list of var-value expressions", { +## Test 63: error if `arg` is not a list of var-value expressions ---- +test_that("assert_varval_list Test 63: error if `arg` is not a list of var-value expressions", { example_fun <- function(arg) { assert_varval_list(arg, accept_var = FALSE) } @@ -1076,8 +1112,8 @@ test_that("assert_varval_list Test 61: error if `arg` is not a list of var-value ) }) -## Test 62: error if `arg` is not a list of var-value expressions ---- -test_that("assert_varval_list Test 62: error if `arg` is not a list of var-value expressions", { +## Test 64: error if `arg` is not a list of var-value expressions ---- +test_that("assert_varval_list Test 64: error if `arg` is not a list of var-value expressions", { example_fun <- function(arg) { assert_varval_list(arg, accept_var = TRUE) } @@ -1092,8 +1128,8 @@ test_that("assert_varval_list Test 62: error if `arg` is not a list of var-value ) }) -## Test 63: error if `required_elements` are missing from `arg` ---- -test_that("assert_varval_list Test 63: error if `required_elements` are missing from `arg`", { +## Test 65: error if `required_elements` are missing from `arg` ---- +test_that("assert_varval_list Test 65: error if `required_elements` are missing from `arg`", { example_fun <- function(arg) { assert_varval_list(arg, required_elements = "DTHDOM") } @@ -1108,8 +1144,8 @@ test_that("assert_varval_list Test 63: error if `required_elements` are missing ) }) -## Test 64: no error if `arg` is NULL and optional is TRUE ---- -test_that("assert_varval_list Test 64: no error if `arg` is NULL and optional is TRUE", { +## Test 66: no error if `arg` is NULL and optional is TRUE ---- +test_that("assert_varval_list Test 66: no error if `arg` is NULL and optional is TRUE", { example_fun <- function(arg) { assert_varval_list(arg, optional = TRUE) } @@ -1119,8 +1155,8 @@ test_that("assert_varval_list Test 64: no error if `arg` is NULL and optional is ) }) -## Test 65: error if `accept_expr` is TRUE and value is invalid ---- -test_that("assert_varval_list Test 65: error if `accept_expr` is TRUE and value is invalid", { +## Test 67: error if `accept_expr` is TRUE and value is invalid ---- +test_that("assert_varval_list Test 67: error if `accept_expr` is TRUE and value is invalid", { example_fun <- function(arg) { assert_varval_list(arg, accept_expr = TRUE) } @@ -1135,8 +1171,8 @@ test_that("assert_varval_list Test 65: error if `accept_expr` is TRUE and value ) }) -## Test 66: error if `accept_expr` is FALSE and value is invalid ---- -test_that("assert_varval_list Test 66: error if `accept_expr` is FALSE and value is invalid", { +## Test 68: error if `accept_expr` is FALSE and value is invalid ---- +test_that("assert_varval_list Test 68: error if `accept_expr` is FALSE and value is invalid", { example_fun <- function(arg) { assert_varval_list(arg, accept_expr = FALSE) } @@ -1151,8 +1187,8 @@ test_that("assert_varval_list Test 66: error if `accept_expr` is FALSE and value ) }) -## Test 67: no error if an argument is a variable-value list ---- -test_that("assert_varval_list Test 67: no error if an argument is a variable-value list", { +## Test 69: no error if an argument is a variable-value list ---- +test_that("assert_varval_list Test 69: no error if an argument is a variable-value list", { example_fun <- function(arg) { assert_varval_list(arg) } @@ -1163,24 +1199,24 @@ test_that("assert_varval_list Test 67: no error if an argument is a variable-val }) # assert_expr_list ---- -## Test 68: error if `arg` is not a list of expressions ---- -test_that("assert_expr_list Test 68: error if `arg` is not a list of expressions", { +## Test 70: error if `arg` is not a list of expressions ---- +test_that("assert_expr_list Test 70: error if `arg` is not a list of expressions", { expect_error( assert_expr_list(arg <- c("USUBJID", "PARAMCD", "VISIT")), class = "assert_expr_list" ) }) -## Test 69: error if `arg` is not a named list of expressions ---- -test_that("assert_expr_list Test 69: error if `arg` is not a named list of expressions", { +## Test 71: error if `arg` is not a named list of expressions ---- +test_that("assert_expr_list Test 71: error if `arg` is not a named list of expressions", { expect_error( assert_expr_list(arg <- exprs(USUBJID, PARAMCD, NULL), named = TRUE), regexp = "argument must be named" ) }) -## Test 70: error if `required_elements` are missing from `arg` ---- -test_that("assert_expr_list Test 70: error if `required_elements` are missing from `arg`", { +## Test 72: error if `required_elements` are missing from `arg` ---- +test_that("assert_expr_list Test 72: error if `required_elements` are missing from `arg`", { expect_error( assert_expr_list( arg <- exprs(DTHSEQ = AESEQ), @@ -1190,23 +1226,23 @@ test_that("assert_expr_list Test 70: error if `required_elements` are missing fr ) }) -## Test 71: no error if `arg` is NULL and optional is TRUE ---- -test_that("assert_expr_list Test 71: no error if `arg` is NULL and optional is TRUE", { +## Test 73: no error if `arg` is NULL and optional is TRUE ---- +test_that("assert_expr_list Test 73: no error if `arg` is NULL and optional is TRUE", { expect_invisible( assert_expr_list(NULL, optional = TRUE) ) }) -## Test 72: error if element is invalid ---- -test_that("assert_expr_list Test 72: error if element is invalid", { +## Test 74: error if element is invalid ---- +test_that("assert_expr_list Test 74: error if element is invalid", { expect_error( assert_expr_list(arg <- exprs(DTHSEQ = !!mean)), class = "assert_expr_list" ) }) -## Test 73: no error if argument is valid ---- -test_that("assert_expr_list Test 73: no error if argument is valid", { +## Test 75: no error if argument is valid ---- +test_that("assert_expr_list Test 75: no error if argument is valid", { expect_invisible( assert_expr_list(arg <- exprs( DTHDOM = "AE", @@ -1217,8 +1253,8 @@ test_that("assert_expr_list Test 73: no error if argument is valid", { }) # assert_list_element ---- -## Test 74: no error if the elements fulfill a certain condition ---- -test_that("assert_list_element Test 74: no error if the elements fulfill a certain condition", { +## Test 76: no error if the elements fulfill a certain condition ---- +test_that("assert_list_element Test 76: no error if the elements fulfill a certain condition", { expect_invisible( assert_list_element( list( @@ -1232,8 +1268,8 @@ test_that("assert_list_element Test 74: no error if the elements fulfill a certa ) }) -## Test 75: error if the elements do not fulfill the condition ---- -test_that("assert_list_element Test 75: error if the elements do not fulfill the condition", { +## Test 77: error if the elements do not fulfill the condition ---- +test_that("assert_list_element Test 77: error if the elements do not fulfill the condition", { expect_error( assert_list_element( list( @@ -1264,8 +1300,8 @@ test_that("assert_list_element Test 75: error if the elements do not fulfill the }) # assert_one_to_one ---- -## Test 76: error if there is a one to many mapping ---- -test_that("assert_one_to_one Test 76: error if there is a one to many mapping", { +## Test 78: error if there is a one to many mapping ---- +test_that("assert_one_to_one Test 78: error if there is a one to many mapping", { dm <- dplyr::tribble( ~DOMAIN, ~USUBJID, "DM", "01-701-1015", @@ -1287,8 +1323,8 @@ test_that("assert_one_to_one Test 76: error if there is a one to many mapping", admiraldev_environment$one_to_many <- NULL }) -## Test 77: error if there is a many to one mapping ---- -test_that("assert_one_to_one Test 77: error if there is a many to one mapping", { +## Test 79: error if there is a many to one mapping ---- +test_that("assert_one_to_one Test 79: error if there is a many to one mapping", { dm <- dplyr::tribble( ~DOMAIN, ~USUBJID, "DM", "01-701-1015", @@ -1304,8 +1340,8 @@ test_that("assert_one_to_one Test 77: error if there is a many to one mapping", admiraldev_environment$many_to_one <- NULL }) -## Test 78: dataset is returned invisible if one-to-one ---- -test_that("assert_one_to_one Test 78: dataset is returned invisible if one-to-one", { +## Test 80: dataset is returned invisible if one-to-one ---- +test_that("assert_one_to_one Test 80: dataset is returned invisible if one-to-one", { df <- dplyr::tribble( ~SPECIES, ~SPECIESN, "DOG", 1L, @@ -1321,8 +1357,8 @@ test_that("assert_one_to_one Test 78: dataset is returned invisible if one-to-on }) # assert_date_var ---- -## Test 79: error if variable is not a date or datetime variable ---- -test_that("assert_date_var Test 79: error if variable is not a date or datetime variable", { +## Test 81: error if variable is not a date or datetime variable ---- +test_that("assert_date_var Test 81: error if variable is not a date or datetime variable", { example_fun <- function(dataset, var) { var <- assert_symbol(enexpr(var)) assert_date_var(dataset = dataset, var = !!var) @@ -1350,18 +1386,18 @@ test_that("assert_date_var Test 79: error if variable is not a date or datetime }) # assert_date_vector ---- -## Test 80: returns error if input vector is not a date formatted ---- -test_that("assert_date_vector Test 80: returns error if input vector is not a date formatted", { +## Test 82: returns error if input vector is not a date formatted ---- +test_that("assert_date_vector Test 82: returns error if input vector is not a date formatted", { expect_error(assert_date_vector("2018-08-23")) }) -## Test 81: returns invisible if input is date formatted ---- -test_that("assert_date_vector Test 81: returns invisible if input is date formatted", { +## Test 83: returns invisible if input is date formatted ---- +test_that("assert_date_vector Test 83: returns invisible if input is date formatted", { expect_invisible(assert_date_vector(as.Date("2022-10-25"))) }) -## Test 82: no error if `arg` is NULL and optional is TRUE ---- -test_that("assert_date_vector Test 82: no error if `arg` is NULL and optional is TRUE", { +## Test 84: no error if `arg` is NULL and optional is TRUE ---- +test_that("assert_date_vector Test 84: no error if `arg` is NULL and optional is TRUE", { example_fun <- function(arg) { assert_date_vector(arg, optional = TRUE) } @@ -1371,8 +1407,8 @@ test_that("assert_date_vector Test 82: no error if `arg` is NULL and optional is ) }) -## Test 83: error if `arg` is NULL and optional is FALSE ---- -test_that("assert_date_vector Test 83: error if `arg` is NULL and optional is FALSE", { +## Test 85: error if `arg` is NULL and optional is FALSE ---- +test_that("assert_date_vector Test 85: error if `arg` is NULL and optional is FALSE", { example_fun <- function(arg) { assert_date_vector(arg, optional = FALSE) } @@ -1389,8 +1425,8 @@ test_that("assert_date_vector Test 83: error if `arg` is NULL and optional is FA # assert_atomic_vector ---- -## Test 84: error if input is not atomic vector ---- -test_that("assert_atomic_vector Test 84: error if input is not atomic vector", { +## Test 86: error if input is not atomic vector ---- +test_that("assert_atomic_vector Test 86: error if input is not atomic vector", { x <- list("a", "a", "b", "c", "d", "d", 1, 1, 4) expect_error(assert_atomic_vector(x), class = "assert_atomic_vector") expect_snapshot( @@ -1400,15 +1436,15 @@ test_that("assert_atomic_vector Test 84: error if input is not atomic vector", { }) # assert_same_type ---- -## Test 85: no error if same type ---- -test_that("assert_same_type Test 85: no error if same type", { +## Test 87: no error if same type ---- +test_that("assert_same_type Test 87: no error if same type", { true_value <- "Y" false_value <- "N" expect_invisible(assert_same_type(true_value, false_value)) }) -## Test 86: error if different type ---- -test_that("assert_same_type Test 86: error if different type", { +## Test 88: error if different type ---- +test_that("assert_same_type Test 88: error if different type", { true_value <- "Y" false_value <- "N" missing_value <- 0 @@ -1424,8 +1460,8 @@ test_that("assert_same_type Test 86: error if different type", { ) }) -## Test 87: works as intended ---- -test_that("assert_same_type Test 87: works as intended", { +## Test 89: works as intended ---- +test_that("assert_same_type Test 89: works as intended", { expect_equal( valid_time_units(), c("years", "months", "days", "hours", "minutes", "seconds") From 2da0fd007991e88d0d4ff56ab523a232097b1ad0 Mon Sep 17 00:00:00 2001 From: Stefan Bundfuss Date: Thu, 31 Oct 2024 11:44:58 +0000 Subject: [PATCH 03/10] #468 multiple_units: update man --- R/assertions.R | 2 +- man/assert_unit.Rd | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/R/assertions.R b/R/assertions.R index 37c87c60..0e808c5b 100644 --- a/R/assertions.R +++ b/R/assertions.R @@ -1130,7 +1130,7 @@ assert_function <- function(arg, #' @export #' #' @examples -#' library(dplyr) +#' library(tibble) #' #' advs <- tribble( #' ~USUBJID, ~VSTESTCD, ~VSTRESN, ~VSSTRESU, ~PARAMCD, ~AVAL, diff --git a/man/assert_unit.Rd b/man/assert_unit.Rd index e62250e0..71461b6e 100644 --- a/man/assert_unit.Rd +++ b/man/assert_unit.Rd @@ -68,7 +68,7 @@ Checks if a parameter (\code{PARAMCD}) in a dataset is provided in the expected unit. } \examples{ -library(dplyr) +library(tibble) advs <- tribble( ~USUBJID, ~VSTESTCD, ~VSTRESN, ~VSSTRESU, ~PARAMCD, ~AVAL, @@ -91,7 +91,7 @@ try( advs <- tribble( ~USUBJID, ~VSTESTCD, ~VSTRESN, ~VSSTRESU, ~PARAMCD, ~AVAL, "P01", "WEIGHT", 80.1, "kg", "WEIGHT", 80.1, - "P02", "WEIGHT", 85700, "g", "WEIGHT", 85700 + "P02", "WEIGHT", 85700, "g", "WEIGHT", 85700 ) try( From 85e1c5ddeca8ee6daef2429c1df3fa0c42e2a31f Mon Sep 17 00:00:00 2001 From: Stefan Bundfuss Date: Thu, 31 Oct 2024 11:55:01 +0000 Subject: [PATCH 04/10] #468 multiple_units: style files --- R/assertions.R | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/R/assertions.R b/R/assertions.R index 0e808c5b..e097ecfd 100644 --- a/R/assertions.R +++ b/R/assertions.R @@ -1177,12 +1177,13 @@ assert_unit <- function(dataset, error = function(cnd) { cli_abort( message = - c("Extracting units using expression {.code {get_unit_expr}} specified for {.arg get_unit_expr} failed!", + c( + "Extracting units using expression {.code {get_unit_expr}} specified for {.arg get_unit_expr} failed!", "See error message below:", conditionMessage(cnd) ), call = parent.frame(n = 4), - class = c(class, "assert-admiraldev", class(cnd)) + class = c(class, "assert-admiraldev", class(cnd)) ) } ) From 997d5ff5aadcf22599bc8c9d617b6125cf99bad1 Mon Sep 17 00:00:00 2001 From: Stefan Bundfuss Date: Thu, 31 Oct 2024 12:06:14 +0000 Subject: [PATCH 05/10] #468 multiple_units: fix R-CMD check --- DESCRIPTION | 2 +- R/assertions.R | 7 +++++-- man/assert_unit.Rd | 2 +- tests/testthat/test-assertions.R | 4 ++-- 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 510b16a7..825d1288 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -35,7 +35,7 @@ BugReports: https://github.com/pharmaverse/admiraldev/issues Depends: R (>= 4.0) Imports: - cli, + cli (>= 3.0.0), dplyr (>= 1.0.5), glue (>= 1.6.0), lifecycle (>= 0.1.0), diff --git a/R/assertions.R b/R/assertions.R index e097ecfd..2d92bfd4 100644 --- a/R/assertions.R +++ b/R/assertions.R @@ -1130,7 +1130,7 @@ assert_function <- function(arg, #' @export #' #' @examples -#' library(tibble) +#' library(dplyr) #' #' advs <- tribble( #' ~USUBJID, ~VSTESTCD, ~VSTRESN, ~VSSTRESU, ~PARAMCD, ~AVAL, @@ -1178,7 +1178,10 @@ assert_unit <- function(dataset, cli_abort( message = c( - "Extracting units using expression {.code {get_unit_expr}} specified for {.arg get_unit_expr} failed!", + paste( + "Extracting units using expression {.code {get_unit_expr}} specified", + "for {.arg get_unit_expr} failed!" + ), "See error message below:", conditionMessage(cnd) ), diff --git a/man/assert_unit.Rd b/man/assert_unit.Rd index 71461b6e..9ecae817 100644 --- a/man/assert_unit.Rd +++ b/man/assert_unit.Rd @@ -68,7 +68,7 @@ Checks if a parameter (\code{PARAMCD}) in a dataset is provided in the expected unit. } \examples{ -library(tibble) +library(dplyr) advs <- tribble( ~USUBJID, ~VSTESTCD, ~VSTRESN, ~VSSTRESU, ~PARAMCD, ~AVAL, diff --git a/tests/testthat/test-assertions.R b/tests/testthat/test-assertions.R index a408ce5b..568d2c9f 100644 --- a/tests/testthat/test-assertions.R +++ b/tests/testthat/test-assertions.R @@ -1028,7 +1028,7 @@ test_that("assert_unit Test 58: error if multiple units in the input dataset", { ## Test 59: error if unexpected unit in the input dataset ---- test_that("assert_unit Test 59: error if unexpected unit in the input dataset", { - advs <- tibble::tribble( + advs <- dplyr::tribble( ~USUBJID, ~VSTESTCD, ~VSTRESN, ~VSSTRESU, ~PARAMCD, ~AVAL, "P01", "WEIGHT", 80.1, "kg", "WEIGHT", 80.1, "P02", "WEIGHT", 85.7, "kg", "WEIGHT", 85.7 @@ -1046,7 +1046,7 @@ test_that("assert_unit Test 59: error if unexpected unit in the input dataset", ## Test 60: error if get_unit_expr invalid ---- test_that("assert_unit Test 60: error if get_unit_expr invalid", { - advs <- tibble::tribble( + advs <- dplyr::tribble( ~USUBJID, ~VSTESTCD, ~VSTRESN, ~VSSTRESU, ~PARAMCD, ~AVAL, "P01", "WEIGHT", 80.1, "kg", "WEIGHT", 80.1, "P02", "WEIGHT", 85.7, "kg", "WEIGHT", 85.7 From 4c4d10d1258f4f928b809513b8bd1f4ffb8d7441 Mon Sep 17 00:00:00 2001 From: Stefan Bundfuss Date: Thu, 31 Oct 2024 12:07:28 +0000 Subject: [PATCH 06/10] #468 multiple_units: style files --- R/assertions.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/assertions.R b/R/assertions.R index 2d92bfd4..8dcb249a 100644 --- a/R/assertions.R +++ b/R/assertions.R @@ -1181,7 +1181,7 @@ assert_unit <- function(dataset, paste( "Extracting units using expression {.code {get_unit_expr}} specified", "for {.arg get_unit_expr} failed!" - ), + ), "See error message below:", conditionMessage(cnd) ), From 839c2fab0548aa3c201cd9aace2e7a1b524d1030 Mon Sep 17 00:00:00 2001 From: Stefan Bundfuss Date: Thu, 31 Oct 2024 16:58:53 +0000 Subject: [PATCH 07/10] #468 multiple_units: fix case of all units NA --- R/assertions.R | 6 +++--- man/assert_unit.Rd | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/R/assertions.R b/R/assertions.R index 8dcb249a..dc9ed80a 100644 --- a/R/assertions.R +++ b/R/assertions.R @@ -1107,7 +1107,7 @@ assert_function <- function(arg, #' @param required_unit Expected unit(s) #' #' If the argument is set to `NULL`, it is checked only whether the unit is -#' unique. +#' unique within the parameter. #' #' *Permitted Values*: A character vector or `NULL` #' @@ -1195,7 +1195,7 @@ assert_unit <- function(dataset, pull(`_unit`) %>% unique() - if (length(units) != 1L) { + if (length(units) > 1L) { message <- message %||% "Multiple units {.val {units}} found for {.val {param}}. Please review and update the units." @@ -1207,7 +1207,7 @@ assert_unit <- function(dataset, ) } - if (!is.null(required_unit) && !(tolower(units) %in% tolower(required_unit))) { + if (!is.null(required_unit) && tolower(units) %notin% tolower(required_unit)) { # change cli `.val` to end with OR instead of AND divid <- cli_div(theme = list(.val = list("vec-last" = ", or ", "vec_sep2" = " or "))) diff --git a/man/assert_unit.Rd b/man/assert_unit.Rd index 9ecae817..a2497c32 100644 --- a/man/assert_unit.Rd +++ b/man/assert_unit.Rd @@ -25,7 +25,7 @@ The variable \code{PARAMCD} and those used in \code{get_unit_expr} are expected. \item{required_unit}{Expected unit(s) If the argument is set to \code{NULL}, it is checked only whether the unit is -unique. +unique within the parameter. \emph{Permitted Values}: A character vector or \code{NULL}} From ca0aebf4aa08a03b41e57fa8339df7923ae05842 Mon Sep 17 00:00:00 2001 From: Stefan Bundfuss Date: Thu, 31 Oct 2024 17:20:27 +0000 Subject: [PATCH 08/10] #468 multiple_units: fix case of all units NA --- R/assertions.R | 3 +- tests/testthat/_snaps/assertions.md | 30 +++--- tests/testthat/test-assertions.R | 146 ++++++++++++++++------------ 3 files changed, 99 insertions(+), 80 deletions(-) diff --git a/R/assertions.R b/R/assertions.R index dc9ed80a..6761d883 100644 --- a/R/assertions.R +++ b/R/assertions.R @@ -1207,7 +1207,8 @@ assert_unit <- function(dataset, ) } - if (!is.null(required_unit) && tolower(units) %notin% tolower(required_unit)) { + if (!is.null(required_unit) && length(units) > 0 && + tolower(units) %notin% tolower(required_unit)) { # change cli `.val` to end with OR instead of AND divid <- cli_div(theme = list(.val = list("vec-last" = ", or ", "vec_sep2" = " or "))) diff --git a/tests/testthat/_snaps/assertions.md b/tests/testthat/_snaps/assertions.md index ec3b3fc8..4262f87c 100644 --- a/tests/testthat/_snaps/assertions.md +++ b/tests/testthat/_snaps/assertions.md @@ -323,7 +323,7 @@ Error in `example_fun()`: ! "x" and "y" are not arguments of the function specified for `arg`. -# assert_unit Test 58: error if multiple units in the input dataset +# assert_unit Test 59: error if multiple units in the input dataset Code assert_unit(advs, param = "WEIGHT", get_unit_expr = VSSTRESU) @@ -331,7 +331,7 @@ Error: ! Multiple units "kg" and "lb" found for "WEIGHT". Please review and update the units. -# assert_unit Test 59: error if unexpected unit in the input dataset +# assert_unit Test 60: error if unexpected unit in the input dataset Code assert_unit(advs, param = "WEIGHT", required_unit = "lb", get_unit_expr = VSSTRESU) @@ -339,7 +339,7 @@ Error: ! It is expected that "WEIGHT" has unit of "lb". In the input dataset the unit is "kg". -# assert_unit Test 60: error if get_unit_expr invalid +# assert_unit Test 61: error if get_unit_expr invalid Code assert_unit(advs, param = "WEIGHT", required_unit = "kg", get_unit_expr = VSTRESU) @@ -349,7 +349,7 @@ See error message below: i In argument: `_unit = VSTRESU`. Caused by error: ! object 'VSTRESU' not found -# assert_param_does_not_exist Test 61: error if parameter exists in the input dataset +# assert_param_does_not_exist Test 62: error if parameter exists in the input dataset Code assert_param_does_not_exist(advs, param = "WEIGHT") @@ -357,7 +357,7 @@ Error: ! The parameter code "WEIGHT" already exists in dataset `advs`. -# assert_varval_list Test 63: error if `arg` is not a list of var-value expressions +# assert_varval_list Test 64: error if `arg` is not a list of var-value expressions Code example_fun(c("USUBJID", "PARAMCD", "VISIT")) @@ -366,7 +366,7 @@ ! Argument `arg` must be a named list of expressions where each element is a symbol, character scalar, numeric scalar, an expression, or NA, but is a character vector. i To create a list of expressions use `exprs()`. -# assert_varval_list Test 64: error if `arg` is not a list of var-value expressions +# assert_varval_list Test 65: error if `arg` is not a list of var-value expressions Code example_fun(exprs(USUBJID, PARAMCD, NULL)) @@ -375,7 +375,7 @@ ! Argument `arg` must be a list of expressions where each element is a symbol, character scalar, numeric scalar, an expression, or NA, but is a list. i To create a list of expressions use `exprs()`. -# assert_varval_list Test 65: error if `required_elements` are missing from `arg` +# assert_varval_list Test 66: error if `required_elements` are missing from `arg` Code example_fun(exprs(DTHSEQ = AESEQ)) @@ -383,7 +383,7 @@ Error in `example_fun()`: ! The following required elements are missing from argument `arg`: "DTHDOM". -# assert_varval_list Test 67: error if `accept_expr` is TRUE and value is invalid +# assert_varval_list Test 68: error if `accept_expr` is TRUE and value is invalid Code example_fun(exprs(DTHSEQ = TRUE)) @@ -392,7 +392,7 @@ ! The elements of the list in argument `arg` must be a symbol, character scalar, numeric scalar, an expression, or NA. i "DTHSEQ" = `TRUE` is of type -# assert_varval_list Test 68: error if `accept_expr` is FALSE and value is invalid +# assert_varval_list Test 69: error if `accept_expr` is FALSE and value is invalid Code example_fun(exprs(DTHSEQ = exprs())) @@ -401,7 +401,7 @@ ! The elements of the list in argument `arg` must be a symbol, character scalar, numeric scalar, or NA. i "DTHSEQ" = `exprs()` is of type -# assert_list_element Test 77: error if the elements do not fulfill the condition +# assert_list_element Test 78: error if the elements do not fulfill the condition Code assert_list_element(list(list(var = expr(DTHDT), val = 1), list(var = expr( @@ -413,7 +413,7 @@ ! List element "val" must be `>=0` in argument `input`: i But, `input[[2]]$val = -1`, and `input[[3]]$val = -2` -# assert_one_to_one Test 78: error if there is a one to many mapping +# assert_one_to_one Test 79: error if there is a one to many mapping Code assert_one_to_one(dm, exprs(DOMAIN), exprs(USUBJID)) @@ -422,7 +422,7 @@ ! For some values of "DOMAIN" there is more than one value of "USUBJID" i Call `admiral::get_one_to_many_dataset()` to get all one-to-many values. -# assert_date_var Test 81: error if variable is not a date or datetime variable +# assert_date_var Test 82: error if variable is not a date or datetime variable Code example_fun(dataset = my_data, var = USUBJID) @@ -430,7 +430,7 @@ Error in `example_fun()`: ! Column "USUBJID" in dataset `dataset` must be a date or datetime, but is a character vector. -# assert_date_vector Test 85: error if `arg` is NULL and optional is FALSE +# assert_date_vector Test 86: error if `arg` is NULL and optional is FALSE Code example_fun(NULL) @@ -438,7 +438,7 @@ Error in `example_fun()`: ! Argument `arg` must be a date or datetime, but is NULL. -# assert_atomic_vector Test 86: error if input is not atomic vector +# assert_atomic_vector Test 87: error if input is not atomic vector Code assert_atomic_vector(x) @@ -446,7 +446,7 @@ Error: ! Argument `x` must be an atomic vector, but is a list. -# assert_same_type Test 88: error if different type +# assert_same_type Test 89: error if different type Code assert_same_type(true_value, false_value, missing_value) diff --git a/tests/testthat/test-assertions.R b/tests/testthat/test-assertions.R index 568d2c9f..c5628f94 100644 --- a/tests/testthat/test-assertions.R +++ b/tests/testthat/test-assertions.R @@ -1008,8 +1008,26 @@ test_that("assert_unit Test 57: no error for multiple expected units", { ) }) -## Test 58: error if multiple units in the input dataset ---- -test_that("assert_unit Test 58: error if multiple units in the input dataset", { +## Test 58: no error if all units NA ---- +test_that("assert_unit Test 58: no error if all units NA", { + advs <- dplyr::tribble( + ~USUBJID, ~VSTESTCD, ~VSTRESN, ~VSSTRESU, ~PARAMCD, ~AVAL, + "P01", "RATIO", 80.1, NA_character_, "WEIGHT", 80.1, + "P02", "RATIO", 85.7, NA_character_, "WEIGHT", 85.7 + ) + + expect_invisible( + assert_unit( + advs, + param = "RATIO", + required_unit = NA_character_, + get_unit_expr = VSSTRESU + ) + ) +}) + +## Test 59: error if multiple units in the input dataset ---- +test_that("assert_unit Test 59: error if multiple units in the input dataset", { advs <- dplyr::tribble( ~USUBJID, ~VSTESTCD, ~VSTRESN, ~VSSTRESU, ~PARAMCD, ~AVAL, "P01", "WEIGHT", 80.1, "kg", "WEIGHT", 80.1, @@ -1026,8 +1044,8 @@ test_that("assert_unit Test 58: error if multiple units in the input dataset", { ) }) -## Test 59: error if unexpected unit in the input dataset ---- -test_that("assert_unit Test 59: error if unexpected unit in the input dataset", { +## Test 60: error if unexpected unit in the input dataset ---- +test_that("assert_unit Test 60: error if unexpected unit in the input dataset", { advs <- dplyr::tribble( ~USUBJID, ~VSTESTCD, ~VSTRESN, ~VSSTRESU, ~PARAMCD, ~AVAL, "P01", "WEIGHT", 80.1, "kg", "WEIGHT", 80.1, @@ -1044,8 +1062,8 @@ test_that("assert_unit Test 59: error if unexpected unit in the input dataset", ) }) -## Test 60: error if get_unit_expr invalid ---- -test_that("assert_unit Test 60: error if get_unit_expr invalid", { +## Test 61: error if get_unit_expr invalid ---- +test_that("assert_unit Test 61: error if get_unit_expr invalid", { advs <- dplyr::tribble( ~USUBJID, ~VSTESTCD, ~VSTRESN, ~VSSTRESU, ~PARAMCD, ~AVAL, "P01", "WEIGHT", 80.1, "kg", "WEIGHT", 80.1, @@ -1063,8 +1081,8 @@ test_that("assert_unit Test 60: error if get_unit_expr invalid", { }) # assert_param_does_not_exist ---- -## Test 61: error if parameter exists in the input dataset ---- -test_that("assert_param_does_not_exist Test 61: error if parameter exists in the input dataset", { +## Test 62: error if parameter exists in the input dataset ---- +test_that("assert_param_does_not_exist Test 62: error if parameter exists in the input dataset", { advs <- dplyr::tribble( ~USUBJID, ~VSTESTCD, ~VSTRESN, ~VSSTRESU, ~PARAMCD, ~AVAL, "P01", "WEIGHT", 80.1, "kg", "WEIGHT", 80.1, @@ -1082,8 +1100,8 @@ test_that("assert_param_does_not_exist Test 61: error if parameter exists in the ) }) -## Test 62: no error if the parameter exists in the dataset ---- -test_that("assert_param_does_not_exist Test 62: no error if the parameter exists in the dataset", { +## Test 63: no error if the parameter exists in the dataset ---- +test_that("assert_param_does_not_exist Test 63: no error if the parameter exists in the dataset", { advs <- dplyr::tribble( ~USUBJID, ~VSTESTCD, ~VSTRESN, ~VSSTRESU, ~PARAMCD, ~AVAL, "P01", "WEIGHT", 80.1, "kg", "WEIGHT", 80.1, @@ -1096,8 +1114,8 @@ test_that("assert_param_does_not_exist Test 62: no error if the parameter exists }) # assert_varval_list ---- -## Test 63: error if `arg` is not a list of var-value expressions ---- -test_that("assert_varval_list Test 63: error if `arg` is not a list of var-value expressions", { +## Test 64: error if `arg` is not a list of var-value expressions ---- +test_that("assert_varval_list Test 64: error if `arg` is not a list of var-value expressions", { example_fun <- function(arg) { assert_varval_list(arg, accept_var = FALSE) } @@ -1112,8 +1130,8 @@ test_that("assert_varval_list Test 63: error if `arg` is not a list of var-value ) }) -## Test 64: error if `arg` is not a list of var-value expressions ---- -test_that("assert_varval_list Test 64: error if `arg` is not a list of var-value expressions", { +## Test 65: error if `arg` is not a list of var-value expressions ---- +test_that("assert_varval_list Test 65: error if `arg` is not a list of var-value expressions", { example_fun <- function(arg) { assert_varval_list(arg, accept_var = TRUE) } @@ -1128,8 +1146,8 @@ test_that("assert_varval_list Test 64: error if `arg` is not a list of var-value ) }) -## Test 65: error if `required_elements` are missing from `arg` ---- -test_that("assert_varval_list Test 65: error if `required_elements` are missing from `arg`", { +## Test 66: error if `required_elements` are missing from `arg` ---- +test_that("assert_varval_list Test 66: error if `required_elements` are missing from `arg`", { example_fun <- function(arg) { assert_varval_list(arg, required_elements = "DTHDOM") } @@ -1144,8 +1162,8 @@ test_that("assert_varval_list Test 65: error if `required_elements` are missing ) }) -## Test 66: no error if `arg` is NULL and optional is TRUE ---- -test_that("assert_varval_list Test 66: no error if `arg` is NULL and optional is TRUE", { +## Test 67: no error if `arg` is NULL and optional is TRUE ---- +test_that("assert_varval_list Test 67: no error if `arg` is NULL and optional is TRUE", { example_fun <- function(arg) { assert_varval_list(arg, optional = TRUE) } @@ -1155,8 +1173,8 @@ test_that("assert_varval_list Test 66: no error if `arg` is NULL and optional is ) }) -## Test 67: error if `accept_expr` is TRUE and value is invalid ---- -test_that("assert_varval_list Test 67: error if `accept_expr` is TRUE and value is invalid", { +## Test 68: error if `accept_expr` is TRUE and value is invalid ---- +test_that("assert_varval_list Test 68: error if `accept_expr` is TRUE and value is invalid", { example_fun <- function(arg) { assert_varval_list(arg, accept_expr = TRUE) } @@ -1171,8 +1189,8 @@ test_that("assert_varval_list Test 67: error if `accept_expr` is TRUE and value ) }) -## Test 68: error if `accept_expr` is FALSE and value is invalid ---- -test_that("assert_varval_list Test 68: error if `accept_expr` is FALSE and value is invalid", { +## Test 69: error if `accept_expr` is FALSE and value is invalid ---- +test_that("assert_varval_list Test 69: error if `accept_expr` is FALSE and value is invalid", { example_fun <- function(arg) { assert_varval_list(arg, accept_expr = FALSE) } @@ -1187,8 +1205,8 @@ test_that("assert_varval_list Test 68: error if `accept_expr` is FALSE and value ) }) -## Test 69: no error if an argument is a variable-value list ---- -test_that("assert_varval_list Test 69: no error if an argument is a variable-value list", { +## Test 70: no error if an argument is a variable-value list ---- +test_that("assert_varval_list Test 70: no error if an argument is a variable-value list", { example_fun <- function(arg) { assert_varval_list(arg) } @@ -1199,24 +1217,24 @@ test_that("assert_varval_list Test 69: no error if an argument is a variable-val }) # assert_expr_list ---- -## Test 70: error if `arg` is not a list of expressions ---- -test_that("assert_expr_list Test 70: error if `arg` is not a list of expressions", { +## Test 71: error if `arg` is not a list of expressions ---- +test_that("assert_expr_list Test 71: error if `arg` is not a list of expressions", { expect_error( assert_expr_list(arg <- c("USUBJID", "PARAMCD", "VISIT")), class = "assert_expr_list" ) }) -## Test 71: error if `arg` is not a named list of expressions ---- -test_that("assert_expr_list Test 71: error if `arg` is not a named list of expressions", { +## Test 72: error if `arg` is not a named list of expressions ---- +test_that("assert_expr_list Test 72: error if `arg` is not a named list of expressions", { expect_error( assert_expr_list(arg <- exprs(USUBJID, PARAMCD, NULL), named = TRUE), regexp = "argument must be named" ) }) -## Test 72: error if `required_elements` are missing from `arg` ---- -test_that("assert_expr_list Test 72: error if `required_elements` are missing from `arg`", { +## Test 73: error if `required_elements` are missing from `arg` ---- +test_that("assert_expr_list Test 73: error if `required_elements` are missing from `arg`", { expect_error( assert_expr_list( arg <- exprs(DTHSEQ = AESEQ), @@ -1226,23 +1244,23 @@ test_that("assert_expr_list Test 72: error if `required_elements` are missing fr ) }) -## Test 73: no error if `arg` is NULL and optional is TRUE ---- -test_that("assert_expr_list Test 73: no error if `arg` is NULL and optional is TRUE", { +## Test 74: no error if `arg` is NULL and optional is TRUE ---- +test_that("assert_expr_list Test 74: no error if `arg` is NULL and optional is TRUE", { expect_invisible( assert_expr_list(NULL, optional = TRUE) ) }) -## Test 74: error if element is invalid ---- -test_that("assert_expr_list Test 74: error if element is invalid", { +## Test 75: error if element is invalid ---- +test_that("assert_expr_list Test 75: error if element is invalid", { expect_error( assert_expr_list(arg <- exprs(DTHSEQ = !!mean)), class = "assert_expr_list" ) }) -## Test 75: no error if argument is valid ---- -test_that("assert_expr_list Test 75: no error if argument is valid", { +## Test 76: no error if argument is valid ---- +test_that("assert_expr_list Test 76: no error if argument is valid", { expect_invisible( assert_expr_list(arg <- exprs( DTHDOM = "AE", @@ -1253,8 +1271,8 @@ test_that("assert_expr_list Test 75: no error if argument is valid", { }) # assert_list_element ---- -## Test 76: no error if the elements fulfill a certain condition ---- -test_that("assert_list_element Test 76: no error if the elements fulfill a certain condition", { +## Test 77: no error if the elements fulfill a certain condition ---- +test_that("assert_list_element Test 77: no error if the elements fulfill a certain condition", { expect_invisible( assert_list_element( list( @@ -1268,8 +1286,8 @@ test_that("assert_list_element Test 76: no error if the elements fulfill a certa ) }) -## Test 77: error if the elements do not fulfill the condition ---- -test_that("assert_list_element Test 77: error if the elements do not fulfill the condition", { +## Test 78: error if the elements do not fulfill the condition ---- +test_that("assert_list_element Test 78: error if the elements do not fulfill the condition", { expect_error( assert_list_element( list( @@ -1300,8 +1318,8 @@ test_that("assert_list_element Test 77: error if the elements do not fulfill the }) # assert_one_to_one ---- -## Test 78: error if there is a one to many mapping ---- -test_that("assert_one_to_one Test 78: error if there is a one to many mapping", { +## Test 79: error if there is a one to many mapping ---- +test_that("assert_one_to_one Test 79: error if there is a one to many mapping", { dm <- dplyr::tribble( ~DOMAIN, ~USUBJID, "DM", "01-701-1015", @@ -1323,8 +1341,8 @@ test_that("assert_one_to_one Test 78: error if there is a one to many mapping", admiraldev_environment$one_to_many <- NULL }) -## Test 79: error if there is a many to one mapping ---- -test_that("assert_one_to_one Test 79: error if there is a many to one mapping", { +## Test 80: error if there is a many to one mapping ---- +test_that("assert_one_to_one Test 80: error if there is a many to one mapping", { dm <- dplyr::tribble( ~DOMAIN, ~USUBJID, "DM", "01-701-1015", @@ -1340,8 +1358,8 @@ test_that("assert_one_to_one Test 79: error if there is a many to one mapping", admiraldev_environment$many_to_one <- NULL }) -## Test 80: dataset is returned invisible if one-to-one ---- -test_that("assert_one_to_one Test 80: dataset is returned invisible if one-to-one", { +## Test 81: dataset is returned invisible if one-to-one ---- +test_that("assert_one_to_one Test 81: dataset is returned invisible if one-to-one", { df <- dplyr::tribble( ~SPECIES, ~SPECIESN, "DOG", 1L, @@ -1357,8 +1375,8 @@ test_that("assert_one_to_one Test 80: dataset is returned invisible if one-to-on }) # assert_date_var ---- -## Test 81: error if variable is not a date or datetime variable ---- -test_that("assert_date_var Test 81: error if variable is not a date or datetime variable", { +## Test 82: error if variable is not a date or datetime variable ---- +test_that("assert_date_var Test 82: error if variable is not a date or datetime variable", { example_fun <- function(dataset, var) { var <- assert_symbol(enexpr(var)) assert_date_var(dataset = dataset, var = !!var) @@ -1386,18 +1404,18 @@ test_that("assert_date_var Test 81: error if variable is not a date or datetime }) # assert_date_vector ---- -## Test 82: returns error if input vector is not a date formatted ---- -test_that("assert_date_vector Test 82: returns error if input vector is not a date formatted", { +## Test 83: returns error if input vector is not a date formatted ---- +test_that("assert_date_vector Test 83: returns error if input vector is not a date formatted", { expect_error(assert_date_vector("2018-08-23")) }) -## Test 83: returns invisible if input is date formatted ---- -test_that("assert_date_vector Test 83: returns invisible if input is date formatted", { +## Test 84: returns invisible if input is date formatted ---- +test_that("assert_date_vector Test 84: returns invisible if input is date formatted", { expect_invisible(assert_date_vector(as.Date("2022-10-25"))) }) -## Test 84: no error if `arg` is NULL and optional is TRUE ---- -test_that("assert_date_vector Test 84: no error if `arg` is NULL and optional is TRUE", { +## Test 85: no error if `arg` is NULL and optional is TRUE ---- +test_that("assert_date_vector Test 85: no error if `arg` is NULL and optional is TRUE", { example_fun <- function(arg) { assert_date_vector(arg, optional = TRUE) } @@ -1407,8 +1425,8 @@ test_that("assert_date_vector Test 84: no error if `arg` is NULL and optional is ) }) -## Test 85: error if `arg` is NULL and optional is FALSE ---- -test_that("assert_date_vector Test 85: error if `arg` is NULL and optional is FALSE", { +## Test 86: error if `arg` is NULL and optional is FALSE ---- +test_that("assert_date_vector Test 86: error if `arg` is NULL and optional is FALSE", { example_fun <- function(arg) { assert_date_vector(arg, optional = FALSE) } @@ -1425,8 +1443,8 @@ test_that("assert_date_vector Test 85: error if `arg` is NULL and optional is FA # assert_atomic_vector ---- -## Test 86: error if input is not atomic vector ---- -test_that("assert_atomic_vector Test 86: error if input is not atomic vector", { +## Test 87: error if input is not atomic vector ---- +test_that("assert_atomic_vector Test 87: error if input is not atomic vector", { x <- list("a", "a", "b", "c", "d", "d", 1, 1, 4) expect_error(assert_atomic_vector(x), class = "assert_atomic_vector") expect_snapshot( @@ -1436,15 +1454,15 @@ test_that("assert_atomic_vector Test 86: error if input is not atomic vector", { }) # assert_same_type ---- -## Test 87: no error if same type ---- -test_that("assert_same_type Test 87: no error if same type", { +## Test 88: no error if same type ---- +test_that("assert_same_type Test 88: no error if same type", { true_value <- "Y" false_value <- "N" expect_invisible(assert_same_type(true_value, false_value)) }) -## Test 88: error if different type ---- -test_that("assert_same_type Test 88: error if different type", { +## Test 89: error if different type ---- +test_that("assert_same_type Test 89: error if different type", { true_value <- "Y" false_value <- "N" missing_value <- 0 @@ -1460,8 +1478,8 @@ test_that("assert_same_type Test 88: error if different type", { ) }) -## Test 89: works as intended ---- -test_that("assert_same_type Test 89: works as intended", { +## Test 90: works as intended ---- +test_that("assert_same_type Test 90: works as intended", { expect_equal( valid_time_units(), c("years", "months", "days", "hours", "minutes", "seconds") From 12d81dd2affe5ed66a6f54e2437f5e918ad4c675 Mon Sep 17 00:00:00 2001 From: Stefan Bundfuss Date: Tue, 5 Nov 2024 10:16:12 +0000 Subject: [PATCH 09/10] #468 multiple_units: update NEWS --- NEWS.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/NEWS.md b/NEWS.md index 2cf16bcf..94b64fbf 100644 --- a/NEWS.md +++ b/NEWS.md @@ -3,8 +3,8 @@ ## Updates of Existing Functions - The `required_unit` argument of `assert_unit()` has been enhanced. It is now -possible to specify more than one unit or not specifying it. In the latter case -only the uniqueness of the unit is checked. (#468) +possible to specify more than one unit or not specify it at all. In the latter +case only the uniqueness of the unit is checked. (#468) ## Breaking Changes From 7f138b42cf151442664ce30e8732d88ad78e40b3 Mon Sep 17 00:00:00 2001 From: Stefan Bundfuss Date: Tue, 5 Nov 2024 15:36:35 +0000 Subject: [PATCH 10/10] #468 multiple_units: trigger workflows