diff --git a/NAMESPACE b/NAMESPACE index 30a076c0..89a75598 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -54,6 +54,7 @@ importFrom(dplyr,ungroup) importFrom(glue,glue) importFrom(glue,glue_collapse) importFrom(graphics,stem) +importFrom(haven,read_xpt) importFrom(haven,write_xpt) importFrom(lifecycle,deprecated) importFrom(magrittr,"%>%") diff --git a/R/xportr-package.R b/R/xportr-package.R index ce107099..417e58b8 100644 --- a/R/xportr-package.R +++ b/R/xportr-package.R @@ -102,7 +102,7 @@ #' @aliases xportr-package #' #' @importFrom lifecycle deprecated -#' @importFrom haven write_xpt +#' @importFrom haven write_xpt read_xpt #' @importFrom rlang abort warn inform with_options local_options .data := sym #' %||% #' @importFrom dplyr left_join bind_cols filter select rename rename_with n diff --git a/tests/testthat/test-deprecation.R b/tests/testthat/test-deprecation.R new file mode 100644 index 00000000..23d7c55a --- /dev/null +++ b/tests/testthat/test-deprecation.R @@ -0,0 +1,100 @@ +## Test 1: xportr_df_label: deprecated metacore gives an error ---- +test_that("deprecation Test 1: xportr_df_label: deprecated metacore gives an error", { + local_options(lifecycle_verbosity = "quiet") + df <- data.frame(x = "a", y = "b") + df_meta <- data.frame(dataset = "df", label = "Label") + + expect_error(xportr_df_label(df, metacore = df_meta)) +}) + +## Test 2: xportr_format: deprecated metacore gives an error ---- +test_that("deprecation Test 2: xportr_format: deprecated metacore gives an error", { + local_options(lifecycle_verbosity = "quiet") + df <- data.frame(x = 1, y = 2) + df_meta <- data.frame( + dataset = "df", + variable = "x", + format = "date9." + ) + + expect_error(xportr_format(df, metacore = df_meta)) +}) + +## Test 3: xportr_label: using the deprecated metacore argument gives an error ---- +test_that( + "deprecation Test 3: xportr_label: using the deprecated metacore argument gives an error", + { + local_options(lifecycle_verbosity = "quiet") + + df <- data.frame(x = "a", y = "b") + df_meta <- + data.frame( + dataset = "df", + variable = "x", + label = "foo" + ) + + expect_error(xportr_label(df, metacore = df_meta)) + } +) + +## Test 4: xportr_length: using the deprecated metacore argument gives an error ---- +test_that( + "deprecation Test 4: xportr_length: using the deprecated metacore argument gives an error", + { + local_options(lifecycle_verbosity = "quiet") + df <- data.frame(x = "a", y = "b") + df_meta <- data.frame( + dataset = "df", + variable = c("x", "y"), + type = c("text", "text"), + length = c(1, 2) + ) + + expect_error(xportr_length(df, metacore = df_meta)) + } +) + +## Test 5: xportr_order: using the deprecated metacore argument gives an error ---- +test_that( + "deprecation Test 5: xportr_order: using the deprecated metacore argument gives an error", + { + local_options(lifecycle_verbosity = "quiet") + + df <- data.frame( + c = 1:5, + a = "a", + d = 5:1, + b = LETTERS[1:5] + ) + df_meta <- data.frame( + dataset = "DOMAIN", + variable = letters[1:4], + order = 1:4 + ) + + expect_error(xportr_order(df, metacore = df_meta, domain = "DOMAIN")) + } +) + +## Test 6: xportr_type: using the deprecated metacore argument gives an error ---- +test_that( + "deprecation Test 6: xportr_type: using the deprecated metacore argument gives an error", + { + local_options(lifecycle_verbosity = "quiet") + df <- data.frame( + Subj = as.character(c(123, 456, 789, "", NA, NA_integer_)), + Different = c("a", "b", "c", "", NA, NA_character_), + Val = c("1", "2", "3", "", NA, NA_character_), + Param = c("param1", "param2", "param3", "", NA, NA_character_) + ) + df_meta <- data.frame( + dataset = "df", + variable = c("Subj", "Param", "Val", "NotUsed"), + type = c("numeric", "character", "numeric", "character"), + format = NA + ) + + expect_error(xportr_type(df, metacore = df_meta)) + } +) diff --git a/tests/testthat/test-depreciation.R b/tests/testthat/test-depreciation.R deleted file mode 100644 index d1eb0cd2..00000000 --- a/tests/testthat/test-depreciation.R +++ /dev/null @@ -1,72 +0,0 @@ -test_that("xportr_df_label: deprecated metacore gives an error", { - local_options(lifecycle_verbosity = "quiet") - df <- data.frame(x = "a", y = "b") - df_meta <- data.frame(dataset = "df", label = "Label") - - expect_error(xportr_df_label(df, metacore = df_meta)) -}) - -test_that("xportr_format: deprecated metacore gives an error", { - local_options(lifecycle_verbosity = "quiet") - df <- data.frame(x = 1, y = 2) - df_meta <- data.frame( - dataset = "df", - variable = "x", - format = "date9." - ) - - expect_error(xportr_format(df, metacore = df_meta)) -}) - -test_that("xportr_label: using the deprecated metacore argument gives an error", { - local_options(lifecycle_verbosity = "quiet") - - df <- data.frame(x = "a", y = "b") - df_meta <- data.frame(dataset = "df", variable = "x", label = "foo") - - expect_error(xportr_label(df, metacore = df_meta)) -}) - -test_that("xportr_length: using the deprecated metacore argument gives an error", { - local_options(lifecycle_verbosity = "quiet") - df <- data.frame(x = "a", y = "b") - df_meta <- data.frame( - dataset = "df", - variable = c("x", "y"), - type = c("text", "text"), - length = c(1, 2) - ) - - expect_error(xportr_length(df, metacore = df_meta)) -}) - -test_that("xportr_order: using the deprecated metacore argument gives an error", { - local_options(lifecycle_verbosity = "quiet") - - df <- data.frame(c = 1:5, a = "a", d = 5:1, b = LETTERS[1:5]) - df_meta <- data.frame( - dataset = "DOMAIN", - variable = letters[1:4], - order = 1:4 - ) - - expect_error(xportr_order(df, metacore = df_meta, domain = "DOMAIN")) -}) - -test_that("xportr_type: using the deprecated metacore argument gives an error", { - local_options(lifecycle_verbosity = "quiet") - df <- data.frame( - Subj = as.character(c(123, 456, 789, "", NA, NA_integer_)), - Different = c("a", "b", "c", "", NA, NA_character_), - Val = c("1", "2", "3", "", NA, NA_character_), - Param = c("param1", "param2", "param3", "", NA, NA_character_) - ) - df_meta <- data.frame( - dataset = "df", - variable = c("Subj", "Param", "Val", "NotUsed"), - type = c("numeric", "character", "numeric", "character"), - format = NA - ) - - expect_error(xportr_type(df, metacore = df_meta)) -}) diff --git a/tests/testthat/test-df_label.R b/tests/testthat/test-df_label.R index 1a0cfdd8..ebf3cbac 100644 --- a/tests/testthat/test-df_label.R +++ b/tests/testthat/test-df_label.R @@ -1,4 +1,5 @@ -test_that("xportr_df_label: error when metadata is not set", { +## Test 1: xportr_df_label: error when metadata is not set ---- +test_that("df_label Test 1: xportr_df_label: error when metadata is not set", { adsl <- minimal_table() expect_error( diff --git a/tests/testthat/test-format.R b/tests/testthat/test-format.R index 7769cd10..c3e9bd09 100644 --- a/tests/testthat/test-format.R +++ b/tests/testthat/test-format.R @@ -1,4 +1,6 @@ -test_that("xportr_format: error when metadata is not set", { +# xportr_format ---- +## Test 1: xportr_format: error when metadata is not set ---- +test_that("format Test 1: error when metadata is not set", { adsl <- data.frame( USUBJID = c(1001, 1002, 1003), BRTHDT = c(1, 1, 2) @@ -10,7 +12,8 @@ test_that("xportr_format: error when metadata is not set", { ) }) -test_that("xportr_format: Gets warning when metadata has multiple rows with same variable", { +## Test 2: xportr_format: Gets warning when metadata has multiple rows with same variable ---- +test_that("format Test 2: Gets warning when metadata has multiple rows with same variable", { # This test uses the (2) functions below to reduce code duplication # All `expect_*` are being called inside the functions # @@ -20,7 +23,8 @@ test_that("xportr_format: Gets warning when metadata has multiple rows with same multiple_vars_in_spec_helper2(xportr_format) }) -test_that("xportr_format: Works as expected with only one domain in metadata", { +## Test 3: xportr_format: Works as expected with only one domain in metadata ---- +test_that("format Test 3: Works as expected with only one domain in metadata", { adsl <- data.frame( USUBJID = c(1001, 1002, 1003), BRTHDT = c(1, 1, 2) @@ -35,7 +39,8 @@ test_that("xportr_format: Works as expected with only one domain in metadata", { expect_silent(xportr_format(adsl, metadata)) }) -test_that("xportr_format: Variable ending in DT should produce a warning if no format", { +## Test 4: xportr_format: Variable ending in DT should produce a warning if no format ---- +test_that("format Test 4: Variable ending in DT should produce a warning if no format", { adsl <- data.frame( USUBJID = c(1001, 1002, 1003), BRTHDT = c(1, 1, 2) @@ -54,7 +59,8 @@ test_that("xportr_format: Variable ending in DT should produce a warning if no f ) }) -test_that("xportr_format: Variable ending in TM should produce an error if no format", { +## Test 5: xportr_format: Variable ending in TM should produce an error if no format ---- +test_that("format Test 5: Variable ending in TM should produce an error if no format", { adsl <- data.frame( USUBJID = c(1001, 1002, 1003), BRTHTM = c(1, 1, 2) @@ -73,7 +79,8 @@ test_that("xportr_format: Variable ending in TM should produce an error if no fo ) }) -test_that("xportr_format: Variable ending in DTM should produce a warning if no format", { +## Test 6: xportr_format: Variable ending in DTM should produce a warning if no format ---- +test_that("format Test 6: Variable ending in DTM should produce a warning if no format", { adsl <- data.frame( USUBJID = c(1001, 1002, 1003), BRTHDTM = c(1, 1, 2) @@ -114,7 +121,8 @@ test_that( } ) -test_that("xportr_format: If a variable is character then a warning should be produced if format is > 32 in length", { +## Test 7: xportr_format: If a variable is character then a warning should be produced if format is > 32 in length ---- +test_that("format Test 7: If a variable is character then a warning should be produced if format is > 32 in length", { # nolint adsl <- data.frame( USUBJID = c("1001", "1002", "1003"), BRTHDT = c(1, 1, 2) @@ -141,26 +149,31 @@ test_that("xportr_format: If a variable is character then a warning should be pr ) }) -test_that("xportr_format: If a variable is numeric then an error should be produced if a format starts with `$`", { - adsl <- data.frame( - USUBJID = c(1001, 1002, 1003), - BRTHDT = c(1, 1, 2) - ) +## Test 8: xportr_format: If a variable is numeric then an error should be produced if a format starts with `$` ---- +test_that( + "format Test 8: If a variable is numeric then an error should be produced if a format starts with `$`", + { # nolint + adsl <- data.frame( # nolint + USUBJID = c(1001, 1002, 1003), + BRTHDT = c(1, 1, 2) + ) - metadata <- data.frame( - dataset = c("adsl", "adsl"), - variable = c("USUBJID", "BRTHDT"), - format = c("$4.", "DATE9.") - ) + metadata <- data.frame( # nolint + dataset = c("adsl", "adsl"), + variable = c("USUBJID", "BRTHDT"), + format = c("$4.", "DATE9.") + ) - expect_error( - xportr_format(adsl, metadata, verbose = "stop"), - regexp = "(xportr::xportr_format) `USUBJID` is a numeric variable and should not have a `$` prefix.", - fixed = TRUE - ) -}) + expect_error( # nolint + xportr_format(adsl, metadata, verbose = "stop"), + regexp = "(xportr::xportr_format) `USUBJID` is a numeric variable and should not have a `$` prefix.", + fixed = TRUE + ) + } +) -test_that("xportr_format: If a variable is numeric then a warning should be produced if format is > 32 in length", { +## Test 9: xportr_format: If a variable is numeric then a warning should be produced if format is > 32 in length ---- #nolint +test_that("format Test 9: If a variable is numeric then a warning should be produced if format is > 32 in length", { # nolint adsl <- data.frame( USUBJID = c(1001, 1002, 1003), BRTHDT = c(1, 1, 2) diff --git a/tests/testthat/test-label.R b/tests/testthat/test-label.R index bb0a9e3b..0155a49e 100644 --- a/tests/testthat/test-label.R +++ b/tests/testthat/test-label.R @@ -1,4 +1,5 @@ -test_that("xportr_label: error when metadata is not set", { +## Test 1: xportr_label: error when metadata is not set ---- +test_that("label Test 1: xportr_label: error when metadata is not set", { df <- data.frame( Subj = as.character(123, 456, 789), Different = c("a", "b", "c"), @@ -6,24 +7,28 @@ test_that("xportr_label: error when metadata is not set", { Param = c("param1", "param2", "param3") ) - expect_error( - xportr_label(df), + expect_error(xportr_label(df), regexp = "Must be of type 'data.frame', 'Metacore' or set via 'xportr_metadata\\(\\)'" ) }) -test_that("xportr_label: Gets warning when metadata has multiple rows with same variable", { - # This test uses the (2) functions below to reduce code duplication - # All `expect_*` are being called inside the functions - # - # Checks that message appears when xportr.domain_name is invalid - multiple_vars_in_spec_helper(xportr_label) - # Checks that message doesn't appear when xportr.domain_name is valid - multiple_vars_in_spec_helper2(xportr_label) -}) +## Test 2: xportr_label: Gets warning when metadata has multiple rows with same variable ---- +test_that( + "label Test 2: xportr_label: Gets warning when metadata has multiple rows with same variable", + { + # This test uses the (2) functions below to reduce code duplication + # All `expect_*` are being called inside the functions + # + # Checks that message appears when xportr.domain_name is invalid + multiple_vars_in_spec_helper(xportr_label) + # Checks that message doesn't appear when xportr.domain_name is valid + multiple_vars_in_spec_helper2(xportr_label) + } +) -test_that("xportr_label: Works as expected with only one domain in metadata", { +## Test 3: xportr_label: Works as expected with only one domain in metadata ---- +test_that("label Test 3: xportr_label: Works as expected with only one domain in metadata", { adsl <- data.frame( USUBJID = c(1001, 1002, 1003), BRTHDT = c(1, 1, 2) diff --git a/tests/testthat/test-length.R b/tests/testthat/test-length.R index 12fce410..0ffb1c9e 100644 --- a/tests/testthat/test-length.R +++ b/tests/testthat/test-length.R @@ -5,9 +5,16 @@ #' * Result of call will create SAS default length attribute (`width` for each #' variable) -test_that("xportr_length: Accepts valid domain names in metadata object", { +# xportr_length +## Test 1: xportr_length: Accepts valid domain names in metadata object ---- +test_that("length Test 1: Accepts valid domain names in metadata object", { adsl <- minimal_table(30) - metadata <- minimal_metadata(dataset = TRUE, length = TRUE, var_names = colnames(adsl)) + metadata <- + minimal_metadata( + dataset = TRUE, + length = TRUE, + var_names = colnames(adsl) + ) # Setup temporary options with active verbose local_options(xportr.length_verbose = "message") @@ -43,28 +50,39 @@ test_that("xportr_length: Accepts valid domain names in metadata object", { expect_equal(30) }) -test_that("xportr_length: CDISC data frame is being piped after another xportr function", { - adsl <- minimal_table(30) - metadata <- minimal_metadata( - dataset = TRUE, length = TRUE, type = TRUE, format = TRUE, var_names = colnames(adsl) - ) - - # Setup temporary options with active verbose - local_options(xportr.length_verbose = "message") +## Test 2: xportr_length: CDISC data frame is being piped after another xportr function ---- +test_that( + "length Test 2: CDISC data frame is being piped after another xportr function", + { + adsl <- minimal_table(30) + metadata <- minimal_metadata( + dataset = TRUE, + length = TRUE, + type = TRUE, + format = TRUE, + var_names = colnames(adsl) + ) - adsl %>% - xportr_type(metadata, domain = "adsl", verbose = "message") %>% - xportr_length(metadata) %>% - expect_silent() %>% - expect_attr_width(metadata$length) %>% - attr("_xportr.df_arg_") %>% - expect_equal("adsl") -}) + # Setup temporary options with active verbose + local_options(xportr.length_verbose = "message") + + adsl %>% + xportr_type(metadata, domain = "adsl", verbose = "message") %>% + xportr_length(metadata) %>% + expect_silent() %>% + expect_attr_width(metadata$length) %>% + attr("_xportr.df_arg_") %>% + expect_equal("adsl") + } +) -test_that("xportr_length: Impute character lengths based on class", { +## Test 3: xportr_length: Impute character lengths based on class ---- +test_that("length Test 3: xportr_length: Impute character lengths based on class", { adsl <- minimal_table(30, cols = c("x", "b")) metadata <- minimal_metadata( - dataset = TRUE, length = TRUE, var_names = colnames(adsl) + dataset = TRUE, + length = TRUE, + var_names = colnames(adsl) ) %>% mutate(length = length - 1) @@ -99,9 +117,15 @@ test_that("xportr_length: Impute character lengths based on class", { expect_attr_width(c(7, 199, 200, 200, 8)) }) -test_that("xportr_length: Throws message when variables not present in metadata", { +## Test 4: xportr_length: Throws message when variables not present in metadata ---- +test_that("length Test 4: xportr_length: Throws message when variables not present in metadata", { adsl <- minimal_table(30, cols = c("x", "y")) - metadata <- minimal_metadata(dataset = TRUE, length = TRUE, var_names = c("x")) + metadata <- + minimal_metadata( + dataset = TRUE, + length = TRUE, + var_names = c("x") + ) # Setup temporary options with `verbose = "message"` local_options(xportr.length_verbose = "message") @@ -115,31 +139,28 @@ test_that("xportr_length: Throws message when variables not present in metadata" expect_message(regexp = "Problem with `y`") }) -test_that("xportr_length: Metacore instance can be used", { +## Test 5: xportr_length: Metacore instance can be used ---- +test_that("length Test 5: Metacore instance can be used", { skip_if_not_installed("metacore") adsl <- minimal_table(30, cols = c("x", "b")) # Build a minimal metacore object - metadata <- suppressMessages( - suppressWarnings( - metacore::metacore( - ds_spec = dplyr::tibble( - dataset = "ADSL" - ), - ds_vars = dplyr::tibble( - dataset = "ADSL", - variable = colnames(adsl) - ), - var_spec = minimal_metadata( - length = TRUE, - type = TRUE, - label = TRUE, - format = TRUE, - order = TRUE - ) + metadata <- suppressMessages(suppressWarnings( + metacore::metacore( + ds_spec = dplyr::tibble(dataset = "ADSL"), + ds_vars = dplyr::tibble( + dataset = "ADSL", + variable = colnames(adsl) + ), + var_spec = minimal_metadata( + length = TRUE, + type = TRUE, + label = TRUE, + format = TRUE, + order = TRUE ) ) - ) + )) # Test metacore parameter with `metacore` class instead of data.frame xportr_length(adsl, metadata, domain = "adsl", verbose = "message") %>% @@ -149,38 +170,43 @@ test_that("xportr_length: Metacore instance can be used", { expect_attr_width(metadata$length) }) -test_that("xportr_length: Domain not in character format", { +## Test 6: xportr_length: Domain not in character format ---- +test_that("length Test 6: Domain not in character format", { skip_if_not_installed("readxl") require(haven, quietly = TRUE) require(readxl, quietly = TRUE) - ADAE <- read_sas(system.file("extdata", "adae.sas7bdat", package = "xportr")) - met <- read_excel(system.file("specs", "ADaM_spec.xlsx", package = "xportr"), 3) + ADAE <- + read_sas(system.file("extdata", "adae.sas7bdat", package = "xportr")) + met <- + read_excel(system.file("specs", "ADaM_spec.xlsx", package = "xportr"), 3) - expect_error( - xportr_length(ADAE, met, domain = ADAE, verbose = "none") - ) + expect_error(xportr_length(ADAE, met, domain = ADAE, verbose = "none")) }) -test_that("xportr_length: error when metadata is not set", { +## Test 7: xportr_length: error when metadata is not set ---- +test_that("length Test 7: error when metadata is not set", { adsl <- minimal_table(30) - expect_error( - xportr_length(adsl), + expect_error(xportr_length(adsl), regexp = "Must be of type 'data.frame', 'Metacore' or set via 'xportr_metadata\\(\\)'" ) }) -test_that("xportr_length: Gets warning when metadata has multiple rows with same variable", { - # This test uses the (2) functions below to reduce code duplication - # All `expect_*` are being called inside the functions - # - # Checks that message appears when xportr.domain_name is invalid - multiple_vars_in_spec_helper(xportr_length) - # Checks that message doesn't appear when xportr.domain_name is valid - multiple_vars_in_spec_helper2(xportr_length) -}) +## Test 8: xportr_length: Gets warning when metadata has multiple rows with same variable ---- +test_that( + "length Test 8: Gets warning when metadata has multiple rows with same variable", + { + # This test uses the (2) functions below to reduce code duplication + # All `expect_*` are being called inside the functions + # + # Checks that message appears when xportr.domain_name is invalid + multiple_vars_in_spec_helper(xportr_length) + # Checks that message doesn't appear when xportr.domain_name is valid + multiple_vars_in_spec_helper2(xportr_length) + } +) meta_example <- data.frame( dataset = "df", @@ -193,23 +219,30 @@ df <- data.frame( WEIGHT = c(85, 45, 121) ) -test_that("xportr_length: length assigned as expected from metadata or data", { +## Test 9: xportr_length: length assigned as expected from metadata or data ---- +test_that("length Test 9: length assigned as expected from metadata or data", { result <- df %>% xportr_length(meta_example, domain = "df", length_source = "metadata") %>% expect_attr_width(c(10, 8)) - - result <- df %>% - xportr_length(meta_example, domain = "df", length_source = "data") %>% - expect_attr_width(c(3, 8)) + suppressMessages( + result <- df %>% + xportr_length(meta_example, domain = "df", length_source = "data") %>% + expect_attr_width(c(3, 8)) + ) }) -test_that("xportr_length: Gets message when length in metadata longer than data length", { - result <- df %>% - xportr_length(meta_example, domain = "df", length_source = "data") %>% - expect_message() -}) +## Test 10: xportr_length: Gets message when length in metadata longer than data length ---- +test_that( + "length Test 10: Gets message when length in metadata longer than data length", + { + result <- df %>% + xportr_length(meta_example, domain = "df", length_source = "data") %>% + expect_message() + } +) -test_that("xportr_length: Works as expected with only one domain in metadata", { +## Test 11: xportr_length: Works as expected with only one domain in metadata ---- +test_that("length Test 11: Works as expected with only one domain in metadata", { adsl <- data.frame( USUBJID = c(1001, 1002, 1003), BRTHDT = c(1, 1, 2) diff --git a/tests/testthat/test-messages.R b/tests/testthat/test-messages.R index 1da3e004..4b798ae0 100644 --- a/tests/testthat/test-messages.R +++ b/tests/testthat/test-messages.R @@ -1,6 +1,8 @@ #' Test `R/messages.R` functions -test_that("xportr_logger: Type parameter will create correct message type", { +# xportr_logger ---- +## Test 1: xportr_logger: Type parameter will create correct message type ---- +test_that("messages Test 1: Type parameter will create correct message type", { xportr_logger("A message", type = "none") %>% expect_silent() @@ -18,7 +20,9 @@ test_that("xportr_logger: Type parameter will create correct message type", { expect_error("A message", class = "rlang_error") }) -test_that("length_log: Missing lengths messages are shown", { +# length_log ---- +## Test 2: length_log: Missing lengths messages are shown ---- +test_that("messages Test 2: Missing lengths messages are shown", { # Remove empty lines in cli theme local_cli_theme() @@ -28,7 +32,8 @@ test_that("length_log: Missing lengths messages are shown", { expect_message("Problem with `var1`.*`var2`.*`var3`") }) -test_that("length_log: Missing variables messages are shown", { +## Test 3: length_log: Missing variables messages are shown ---- +test_that("messages Test 3: Missing variables messages are shown", { # Remove empty lines in cli theme local_cli_theme() @@ -40,7 +45,9 @@ test_that("length_log: Missing variables messages are shown", { expect_message("Problem with `var1`.*`var2`.*`var3`") }) -test_that("var_names_log: Renamed variables messages are shown", { +# var_names_log ---- +## Test 4: var_names_log: Renamed variables messages are shown ---- +test_that("messages Test 4: Renamed variables messages are shown", { # Remove empty lines in cli theme local_cli_theme() @@ -53,16 +60,16 @@ test_that("var_names_log: Renamed variables messages are shown", { ) tidy_names_df %>% - mutate( - renamed_n = c( - 2, - sample(c(0, 1, 2), size = NROW(.data$renamed_n) - 1, replace = TRUE) + mutate(renamed_n = c( + 2, + sample( + c(0, 1, 2), + size = NROW(.data$renamed_n) - 1, + replace = TRUE ) - ) %>% + )) %>% var_names_log("message") %>% - expect_message( - ".*[0-9]+ of [0-9]+ \\([0-9]+(\\.[0-9]+)%\\) variables were renamed.*" - ) %>% + expect_message(".*[0-9]+ of [0-9]+ \\([0-9]+(\\.[0-9]+)%\\) variables were renamed.*") %>% expect_message("Var . : '.*' was renamed to '.*'") %>% expect_message("Var . : '.*' was renamed to '.*'") %>% expect_message("Var . : '.*' was renamed to '.*'") %>% diff --git a/tests/testthat/test-metadata.R b/tests/testthat/test-metadata.R index 7a7d695d..49f49a6c 100644 --- a/tests/testthat/test-metadata.R +++ b/tests/testthat/test-metadata.R @@ -10,7 +10,9 @@ extract_var_label <- function(.x) { vapply(.x, function(.x) attr(.x, "label"), character(1), USE.NAMES = FALSE) } -test_that("xportr_label: Correctly applies label from data.frame spec", { +# xportr_label ---- +## Test 1: xportr_label: Correctly applies label from data.frame spec ---- +test_that("metadata Test 1: Correctly applies label from data.frame spec", { df <- data.frame(x = "a", y = "b") df_meta <- data.frame(dataset = "df", variable = c("x", "y"), label = c("foo", "bar")) @@ -32,7 +34,8 @@ test_that("xportr_label: Correctly applies label from data.frame spec", { ) }) -test_that("xportr_label: Correctly applies label when data is piped", { +## Test 2: xportr_label: Correctly applies label when data is piped ---- +test_that("metadata Test 2: Correctly applies label when data is piped", { df <- data.frame(x = "a", y = "b") df_meta <- data.frame(dataset = "df", variable = c("x", "y"), label = c("foo", "bar")) @@ -53,7 +56,8 @@ test_that("xportr_label: Correctly applies label when data is piped", { ) }) -test_that("xportr_label: Correctly applies label for custom domain", { +## Test 3: xportr_label: Correctly applies label for custom domain ---- +test_that("metadata Test 3: Correctly applies label for custom domain", { df <- data.frame(x = "a", y = "b") df_meta <- data.frame(dataset = rep("DOMAIN", 2), variable = c("x", "y"), label = c("foo", "bar")) @@ -74,7 +78,8 @@ test_that("xportr_label: Correctly applies label for custom domain", { ) }) -test_that("xportr_label: Correctly applies label from metacore spec", { +## Test 4: xportr_label: Correctly applies label from metacore spec ---- +test_that("metadata Test 4: Correctly applies label from metacore spec", { skip_if_not_installed("metacore") df <- data.frame(x = "a", y = "b", variable = "value") @@ -111,7 +116,8 @@ test_that("xportr_label: Correctly applies label from metacore spec", { ) }) -test_that("xportr_label: Expect error if any variable does not exist in metadata", { +## Test 5: xportr_label: Expect error if any variable does not exist in metadata ---- +test_that("metadata Test 5: Expect error if any variable does not exist in metadata", { df <- data.frame(x = "a", y = "b") df_meta <- data.frame( dataset = "df", @@ -124,7 +130,8 @@ test_that("xportr_label: Expect error if any variable does not exist in metadata expect_error() }) -test_that("xportr_label: Expect error if label exceeds 40 characters", { +## Test 6: xportr_label: Expect error if label exceeds 40 characters ---- +test_that("metadata Test 6: Expect error if label exceeds 40 characters", { df <- data.frame(x = "a", y = "b") df_meta <- data.frame( dataset = "df", @@ -136,7 +143,8 @@ test_that("xportr_label: Expect error if label exceeds 40 characters", { expect_warning("variable label must be 40 characters or less") }) -test_that("xportr_label: Expect error if domain is not a character", { +## Test 7: xportr_label: Expect error if domain is not a character ---- +test_that("metadata Test 7: Expect error if domain is not a character", { df <- data.frame(x = "a", y = "b") df_meta <- data.frame( dataset = "df", @@ -154,7 +162,9 @@ test_that("xportr_label: Expect error if domain is not a character", { ) }) -test_that("xportr_df_label: Correctly applies label from data.frame spec", { +# xportr_df_label ---- +## Test 8: xportr_df_label: Correctly applies label from data.frame spec ---- +test_that("metadata Test 8: Correctly applies label from data.frame spec", { df <- data.frame(x = "a", y = "b") df_meta <- data.frame(dataset = "df", label = "Label") @@ -173,7 +183,8 @@ test_that("xportr_df_label: Correctly applies label from data.frame spec", { ) }) -test_that("xportr_df_label: Correctly applies label when data is piped", { +## Test 9: xportr_df_label: Correctly applies label when data is piped ---- +test_that("metadata Test 9: Correctly applies label when data is piped", { df <- data.frame(x = "a", y = "b") df_meta <- data.frame(dataset = "df", label = "Label") @@ -192,7 +203,8 @@ test_that("xportr_df_label: Correctly applies label when data is piped", { ) }) -test_that("xportr_df_label: Correctly applies label for custom domain", { +## Test 10: xportr_df_label: Correctly applies label for custom domain ---- +test_that("metadata Test 10: Correctly applies label for custom domain", { df <- data.frame(x = "a", y = "b") df_meta <- data.frame(dataset = "DOMAIN", label = "Label") @@ -208,7 +220,8 @@ test_that("xportr_df_label: Correctly applies label for custom domain", { ) }) -test_that("xportr_df_label: Correctly applies label from metacore spec", { +## Test 11: xportr_df_label: Correctly applies label from metacore spec ---- +test_that("metadata Test 11: Correctly applies label from metacore spec", { skip_if_not_installed("metacore") df <- data.frame(x = "a", y = "b") @@ -236,7 +249,8 @@ test_that("xportr_df_label: Correctly applies label from metacore spec", { ) }) -test_that("xportr_df_label: Expect error if label exceeds 40 characters", { +## Test 12: xportr_df_label: Expect error if label exceeds 40 characters ---- +test_that("metadata Test 12: Expect error if label exceeds 40 characters", { df <- data.frame(x = "a", y = "b") df_meta <- data.frame( dataset = "df", @@ -249,7 +263,8 @@ test_that("xportr_df_label: Expect error if label exceeds 40 characters", { ) }) -test_that("xportr_df_label: Expect error if domain is not a character", { +## Test 13: xportr_df_label: Expect error if domain is not a character ---- +test_that("metadata Test 13: Expect error if domain is not a character", { df <- data.frame(x = "a", y = "b") df_meta <- data.frame( dataset = "df", @@ -266,7 +281,9 @@ test_that("xportr_df_label: Expect error if domain is not a character", { ) }) -test_that("xportr_format: Set formats as expected", { +# xportr_format ---- +## Test 14: xportr_format: Set formats as expected ---- +test_that("metadata Test 14: Set formats as expected", { df <- data.frame(x = 1, y = 2) df_meta <- data.frame( dataset = "df", @@ -286,7 +303,8 @@ test_that("xportr_format: Set formats as expected", { )) }) -test_that("xportr_format: Set formats as expected when data is piped", { +## Test 15: xportr_format: Set formats as expected when data is piped ---- +test_that("metadata Test 15: Set formats as expected when data is piped", { df <- data.frame(x = 1, y = 2) df_meta <- data.frame( dataset = "df", @@ -306,7 +324,8 @@ test_that("xportr_format: Set formats as expected when data is piped", { )) }) -test_that("xportr_format: Set formats as expected for metacore spec", { +## Test 16: xportr_format: Set formats as expected for metacore spec ---- +test_that("metadata Test 16: Set formats as expected for metacore spec", { skip_if_not_installed("metacore") df <- data.frame(x = 1, y = 2) metacore_meta <- suppressMessages(suppressWarnings( @@ -334,7 +353,8 @@ test_that("xportr_format: Set formats as expected for metacore spec", { )) }) -test_that("xportr_format: Set formats as expected for custom domain", { +## Test 17: xportr_format: Set formats as expected for custom domain ---- +test_that("metadata Test 17: Set formats as expected for custom domain", { df <- data.frame(x = 1, y = 2) df_meta <- data.frame( dataset = "DOMAIN", @@ -354,7 +374,8 @@ test_that("xportr_format: Set formats as expected for custom domain", { )) }) -test_that("xportr_format: Handle NA values without raising an error", { +## Test 18: xportr_format: Handle NA values without raising an error ---- +test_that("metadata Test 18: Handle NA values without raising an error", { df <- data.frame(x = 1, y = 2, z = 3, a = 4) df_meta <- data.frame( dataset = rep("df", 4), @@ -376,7 +397,8 @@ test_that("xportr_format: Handle NA values without raising an error", { )) }) -test_that("xportr_format: Expect error if domain is not a character", { +## Test 19: xportr_format: Expect error if domain is not a character ---- +test_that("metadata Test 19: Expect error if domain is not a character", { df <- data.frame(x = 1, y = 2, z = 3, a = 4) df_meta <- data.frame( dataset = "df", @@ -394,7 +416,9 @@ test_that("xportr_format: Expect error if domain is not a character", { ) }) -test_that("xportr_length: Check if width attribute is set properly", { +# xportr_length ---- +## Test 20: xportr_length: Check if width attribute is set properly ---- +test_that("metadata Test 20: Check if width attribute is set properly", { df <- data.frame(x = "a", y = "b") df_meta <- data.frame( dataset = "df", @@ -415,7 +439,8 @@ test_that("xportr_length: Check if width attribute is set properly", { )) }) -test_that("xportr_length: Check if width attribute is set properly when data is piped", { +## Test 21: xportr_length: Check if width attribute is set properly when data is piped ---- +test_that("metadata Test 21: Check if width attribute is set properly when data is piped", { df <- data.frame(x = "a", y = "b") df_meta <- data.frame( dataset = "df", @@ -436,7 +461,8 @@ test_that("xportr_length: Check if width attribute is set properly when data is )) }) -test_that("xportr_length: Check if width attribute is set properly for metacore spec", { +## Test 22: xportr_length: Check if width attribute is set properly for metacore spec ---- +test_that("metadata Test 22: Check if width attribute is set properly for metacore spec", { skip_if_not_installed("metacore") df <- data.frame(x = "a", y = "b") metacore_meta <- suppressMessages(suppressWarnings( @@ -464,7 +490,8 @@ test_that("xportr_length: Check if width attribute is set properly for metacore )) }) -test_that("xportr_length: Check if width attribute is set properly when custom domain is passed", { +## Test 23: xportr_length: Check if width attribute is set properly when custom domain is passed ---- +test_that("metadata Test 23: Check if width attribute is set properly when custom domain is passed", { df <- data.frame(x = "a", y = "b") df_meta <- data.frame( dataset = rep("DOMAIN", 2), @@ -485,7 +512,8 @@ test_that("xportr_length: Check if width attribute is set properly when custom d )) }) -test_that("xportr_length: Expect error when a variable is not present in metadata", { +## Test 24: xportr_length: Expect error when a variable is not present in metadata ---- +test_that("metadata Test 24: Expect error when a variable is not present in metadata", { df <- data.frame(x = "a", y = "b", z = "c") df_meta <- data.frame( dataset = "df", @@ -500,7 +528,8 @@ test_that("xportr_length: Expect error when a variable is not present in metadat expect_error("doesn't exist") }) -test_that("xportr_length: Check if length gets imputed when a new variable is passed", { +## Test 25: xportr_length: Check if length gets imputed when a new variable is passed ---- +test_that("metadata Test 25: Check if length gets imputed when a new variable is passed", { df <- data.frame(x = "a", y = "b", z = 3) df_meta <- data.frame( dataset = "df", @@ -525,7 +554,8 @@ test_that("xportr_length: Check if length gets imputed when a new variable is pa )) }) -test_that("xportr_length: Expect error if domain is not a character", { +## Test 26: xportr_length: Expect error if domain is not a character ---- +test_that("metadata Test 26: Expect error if domain is not a character", { df <- data.frame(x = "a", y = "b", z = 3) df_meta <- data.frame( dataset = "df", @@ -544,7 +574,9 @@ test_that("xportr_length: Expect error if domain is not a character", { ) }) -test_that("xportr_metadata: Impute character lengths based on class", { +# xportr_metadata ---- +## Test 27: xportr_metadata: Impute character lengths based on class ---- +test_that("metadata Test 27: Impute character lengths based on class", { adsl <- minimal_table(30, cols = c("x", "b")) metadata <- minimal_metadata( dataset = TRUE, length = TRUE, var_names = colnames(adsl) @@ -566,7 +598,8 @@ test_that("xportr_metadata: Impute character lengths based on class", { expect_attr_width(c(7, 199, 200, 200, 8)) }) -test_that("xportr_metadata: Throws message when variables not present in metadata", { +## Test 28: xportr_metadata: Throws message when variables not present in metadata ---- +test_that("metadata Test 28: Throws message when variables not present in metadata", { adsl <- minimal_table(30, cols = c("x", "y")) metadata <- minimal_metadata(dataset = TRUE, length = TRUE, var_names = c("x")) @@ -578,13 +611,8 @@ test_that("xportr_metadata: Throws message when variables not present in metadat expect_message(regexp = "Problem with `y`") }) -test_that("xportr_metadata: Variable ordering messaging is correct", { - skip_if_not_installed("haven") - skip_if_not_installed("readxl") - - require(haven, quietly = TRUE) - require(readxl, quietly = TRUE) - +## Test 29: xportr_metadata: Variable ordering messaging is correct ---- +test_that("metadata Test 29: Variable ordering messaging is correct", { df <- data.frame(c = 1:5, a = "a", d = 5:1, b = LETTERS[1:5]) df2 <- data.frame(a = "a", z = "z") df_meta <- data.frame( @@ -607,7 +635,9 @@ test_that("xportr_metadata: Variable ordering messaging is correct", { expect_message("All variables in dataset are ordered") }) -test_that("xportr_type: Variable types are coerced as expected and can raise messages", { +# xportr_type ---- +## Test 30: xportr_type: Variable types are coerced as expected and can raise messages ---- +test_that("metadata Test 30: Variable types are coerced as expected and can raise messages", { df <- data.frame( Subj = as.character(c(123, 456, 789, "", NA, NA_integer_)), Different = c("a", "b", "c", "", NA, NA_character_), @@ -634,14 +664,15 @@ test_that("xportr_type: Variable types are coerced as expected and can raise mes expect_message("Variable type\\(s\\) in dataframe don't match metadata") }) +# xportr_metadata ---- # many tests here are more like qualification/domain testing - this section adds # tests for `xportr_metadata()` basic functionality # start -test_that("xportr_metadata: Check metadata interaction with other functions", { +## Test 31: xportr_metadata: Check metadata interaction with other functions ---- +test_that("metadata Test 31: Check metadata interaction with other functions", { data("adsl_xportr", envir = environment()) adsl <- adsl_xportr - skip_if_not_installed("readxl") var_spec <- readxl::read_xlsx( system.file("specs", "ADaM_spec.xlsx", package = "xportr"), sheet = "Variables" @@ -711,14 +742,17 @@ test_that("xportr_metadata: Check metadata interaction with other functions", { ) }) -test_that("xportr_metadata: must throw error if both metadata and domain are null", { +## Test 32: xportr_metadata: must throw error if both metadata and domain are null ---- +test_that("metadata Test 32: must throw error if both metadata and domain are null", { expect_error( xportr_metadata(data.frame(), metadata = NULL, domain = NULL), "Must provide either `metadata` or `domain` argument" ) }) -test_that("xportr_*: Domain is kept in between calls", { +# xportr_* ---- +## Test 33: xportr_*: Domain is kept in between calls ---- +test_that("metadata Test 33: Domain is kept in between calls", { # Divert all messages to tempfile, instead of printing them # note: be aware as this should only be used in tests that don't track # messages @@ -758,52 +792,52 @@ test_that("xportr_*: Domain is kept in between calls", { }) # end -test_that("`xportr_metadata()` results match traditional results", { +# `xportr_metadata()` ---- +## Test 34: `xportr_metadata()` results match traditional results ---- +test_that("metadata Test 34: results match traditional results", { data("var_spec", "dataset_spec", "adsl_xportr", envir = environment()) adsl <- adsl_xportr - if (require(magrittr, quietly = TRUE)) { - skip_if_not_installed("withr") - trad_path <- withr::local_file("adsltrad.xpt") - metadata_path <- withr::local_file("adslmeta.xpt") - - dataset_spec_low <- setNames(dataset_spec, tolower(names(dataset_spec))) - names(dataset_spec_low)[[2]] <- "label" - - var_spec_low <- setNames(var_spec, tolower(names(var_spec))) - names(var_spec_low)[[5]] <- "type" - - metadata_df <- adsl %>% - xportr_metadata(var_spec_low, "ADSL", verbose = "none") %>% - xportr_type() %>% - xportr_length() %>% - xportr_label() %>% - xportr_order() %>% - xportr_format() %>% - xportr_df_label(dataset_spec_low) %>% - xportr_write(metadata_path) - - trad_df <- adsl %>% - xportr_type(var_spec_low, "ADSL", verbose = "none") %>% - xportr_length(var_spec_low, "ADSL", verbose = "none") %>% - xportr_label(var_spec_low, "ADSL", verbose = "none") %>% - xportr_order(var_spec_low, "ADSL", verbose = "none") %>% - xportr_format(var_spec_low, "ADSL") %>% - xportr_df_label(dataset_spec_low, "ADSL") %>% - xportr_write(trad_path) - - expect_identical( - metadata_df, - structure( - trad_df, - `_xportr.df_metadata_` = var_spec_low, - `_xportr.df_verbose_` = "none" - ) - ) + skip_if_not_installed("withr") + trad_path <- withr::local_file("adsltrad.xpt") + metadata_path <- withr::local_file("adslmeta.xpt") - expect_identical( - haven::read_xpt(metadata_path), - haven::read_xpt(trad_path) + dataset_spec_low <- setNames(dataset_spec, tolower(names(dataset_spec))) + names(dataset_spec_low)[[2]] <- "label" + + var_spec_low <- setNames(var_spec, tolower(names(var_spec))) + names(var_spec_low)[[5]] <- "type" + + metadata_df <- adsl %>% + xportr_metadata(var_spec_low, "ADSL", verbose = "none") %>% + xportr_type() %>% + xportr_length() %>% + xportr_label() %>% + xportr_order() %>% + xportr_format() %>% + xportr_df_label(dataset_spec_low) %>% + xportr_write(metadata_path) + + trad_df <- adsl %>% + xportr_type(var_spec_low, "ADSL", verbose = "none") %>% + xportr_length(var_spec_low, "ADSL", verbose = "none") %>% + xportr_label(var_spec_low, "ADSL", verbose = "none") %>% + xportr_order(var_spec_low, "ADSL", verbose = "none") %>% + xportr_format(var_spec_low, "ADSL") %>% + xportr_df_label(dataset_spec_low, "ADSL") %>% + xportr_write(trad_path) + + expect_identical( + metadata_df, + structure( + trad_df, + `_xportr.df_metadata_` = var_spec_low, + `_xportr.df_verbose_` = "none" ) - } + ) + + expect_identical( + read_xpt(metadata_path), + read_xpt(trad_path) + ) }) diff --git a/tests/testthat/test-options.R b/tests/testthat/test-options.R index 39be84b8..87eff56c 100644 --- a/tests/testthat/test-options.R +++ b/tests/testthat/test-options.R @@ -1,4 +1,6 @@ -test_that("options are originally set as expected", { +# xportr_options ---- +## Test 1: options are originally set as expected ---- +test_that("options Test 1: options are originally set as expected", { op <- options() expect_equal(op$xportr.df_domain_name, "dataset") @@ -11,8 +13,8 @@ test_that("options are originally set as expected", { expect_equal(op$xportr.format_name, "format") }) - -test_that("xportr_options: options can be fetched using the xportr_options", { +## Test 2: xportr_options: options can be fetched using the xportr_options ---- +test_that("options Test 2: xportr_options: options can be fetched using the xportr_options", { expect_equal(xportr_options(), xportr_options_list) new_domain <- "new domain name" new_label <- "new label name" @@ -25,7 +27,8 @@ test_that("xportr_options: options can be fetched using the xportr_options", { expect_equal(domain_label, list(xportr.df_domain_name = new_domain, xportr.df_label = new_label)) }) -test_that("xportr_options: options can be set using the xportr_options", { +## Test 3: xportr_options: options can be set using the xportr_options ---- +test_that("options Test 3: options can be set using the xportr_options", { op <- options() on.exit(options(op), add = TRUE, after = FALSE) old_name <- "old name" diff --git a/tests/testthat/test-order.R b/tests/testthat/test-order.R index 9f7a08f6..8aaa4dc6 100644 --- a/tests/testthat/test-order.R +++ b/tests/testthat/test-order.R @@ -1,4 +1,6 @@ -test_that("xportr_order: Variable are ordered correctly for data.frame spec", { +# xportr_order ---- +## Test 1: xportr_order: Variable are ordered correctly for data.frame spec ---- +test_that("order Test 1: Variable are ordered correctly for data.frame spec", { df <- data.frame(c = 1:5, a = "a", d = 5:1, b = LETTERS[1:5]) df_meta <- data.frame( dataset = "df", @@ -11,7 +13,8 @@ test_that("xportr_order: Variable are ordered correctly for data.frame spec", { expect_equal(names(ordered_df), df_meta$variable) }) -test_that("xportr_order: Variable are ordered correctly when data is piped", { +## Test 2: xportr_order: Variable are ordered correctly when data is piped ---- +test_that("order Test 2: Variable are ordered correctly when data is piped", { df <- data.frame(c = 1:5, a = "a", d = 5:1, b = LETTERS[1:5]) df_meta <- data.frame( dataset = "df", @@ -29,7 +32,8 @@ test_that("xportr_order: Variable are ordered correctly when data is piped", { expect_equal(names(ordered_df), df_meta$variable) }) -test_that("xportr_order: Variable are ordered correctly for custom domain", { +## Test 3: xportr_order: Variable are ordered correctly for custom domain ---- +test_that("order Test 3: Variable are ordered correctly for custom domain", { df <- data.frame(c = 1:5, a = "a", d = 5:1, b = LETTERS[1:5]) df_meta <- data.frame( dataset = "DOMAIN", @@ -44,7 +48,8 @@ test_that("xportr_order: Variable are ordered correctly for custom domain", { expect_equal(names(ordered_df), df_meta$variable) }) -test_that("xportr_order: Variable are ordered correctly for metacore spec", { +## Test 4: xportr_order: Variable are ordered correctly for metacore spec ---- +test_that("order Test 4: Variable are ordered correctly for metacore spec", { skip_if_not_installed("metacore") df <- data.frame(c = 1:5, a = "a", d = 5:1, b = LETTERS[1:5]) @@ -74,7 +79,8 @@ test_that("xportr_order: Variable are ordered correctly for metacore spec", { expect_equal(names(ordered_df), ordered_columns) }) -test_that("xportr_order: Variable are ordered when custom domain_name is passed", { +## Test 5: xportr_order: Variable are ordered when custom domain_name is passed ---- +test_that("order Test 5: Variable are ordered when custom domain_name is passed", { df <- data.frame(c = 1:5, a = "a", d = 5:1, b = LETTERS[1:5]) df_meta <- data.frame( custom_domain = "df", @@ -89,7 +95,8 @@ test_that("xportr_order: Variable are ordered when custom domain_name is passed" expect_equal(names(ordered_df), df_meta$variable) }) -test_that("xportr_order: Expect error if domain is not a character", { +## Test 6: xportr_order: Expect error if domain is not a character ---- +test_that("order Test 6: Expect error if domain is not a character", { df <- data.frame(c = 1:5, a = "a", d = 5:1, b = LETTERS[1:5]) df_meta <- data.frame( custom_domain = "df", @@ -101,7 +108,8 @@ test_that("xportr_order: Expect error if domain is not a character", { expect_error(xportr_order(df, df_meta, domain = 1, verbose = "none")) }) -test_that("xportr_order: error when metadata is not set", { +## Test 7: xportr_order: error when metadata is not set ---- +test_that("order Test 7: error when metadata is not set", { df <- data.frame(c = 1:5, a = "a", d = 5:1, b = LETTERS[1:5]) expect_error( @@ -110,7 +118,8 @@ test_that("xportr_order: error when metadata is not set", { ) }) -test_that("xportr_order: Variable ordering messaging is correct", { +## Test 8: xportr_order: Variable ordering messaging is correct ---- +test_that("order Test 8: Variable ordering messaging is correct", { skip_if_not_installed("readxl") require(haven, quietly = TRUE) @@ -126,19 +135,21 @@ test_that("xportr_order: Variable ordering messaging is correct", { # Remove empty lines in cli theme local_cli_theme() + suppressMessages( + xportr_order(df, df_meta, verbose = "message", domain = "df") %>% + expect_message("All variables in specification file are in dataset") %>% + expect_condition("4 reordered in dataset") %>% + expect_message("Variable reordered in `.df`: `a`, `b`, `c`, and `d`") + ) - xportr_order(df, df_meta, verbose = "message", domain = "df") %>% - expect_message("All variables in specification file are in dataset") %>% - expect_condition("4 reordered in dataset") %>% - expect_message("Variable reordered in `.df`: `a`, `b`, `c`, and `d`") - - xportr_order(df2, df_meta, verbose = "message", domain = "df2") %>% - expect_message("2 variables not in spec and moved to end") %>% + suppressMessages(xportr_order(df2, df_meta, verbose = "message", domain = "df2") %>% + expect_message("2 variables not in spec and moved to end") %>% # nolint expect_message("Variable moved to end in `.df`: `a` and `z`") %>% - expect_message("All variables in dataset are ordered") + expect_message("All variables in dataset are ordered")) }) -test_that("xportr_order: Metadata order columns are coersed to numeric", { +## Test 9: xportr_order: Metadata order columns are coersed to numeric ---- +test_that("order Test 9: Metadata order columns are coersed to numeric", { df <- data.frame(c = 1:5, a = "a", d = 5:1, b = LETTERS[1:5]) df_meta <- data.frame( dataset = "df", @@ -153,25 +164,27 @@ test_that("xportr_order: Metadata order columns are coersed to numeric", { expect_equal(names(ordered_df), df_meta$variable) }) -test_that("xportr_order: Gets warning when metadata has multiple rows with same variable", { +## Test 10: xportr_order: Gets warning when metadata has multiple rows with same variable ---- +test_that("order Test 10: Gets warning when metadata has multiple rows with same variable", { # This test uses the (2) functions below to reduce code duplication # All `expect_*` are being called inside the functions # # Checks that message appears when xportr.domain_name is invalid - multiple_vars_in_spec_helper(xportr_order) %>% - # expect_message() are being caught to provide clean test without output + suppressMessages(multiple_vars_in_spec_helper(xportr_order) %>% + # expect_message() are being caught to provide clean test without output #nolint expect_message("All variables in specification file are in dataset") %>% - expect_message("All variables in dataset are ordered") + expect_message("All variables in dataset are ordered")) # Checks that message doesn't appear when xportr.domain_name is valid - multiple_vars_in_spec_helper2(xportr_order) %>% - # expect_message() are being caught to provide clean test without output + suppressMessages(multiple_vars_in_spec_helper2(xportr_order) %>% + # expect_message() are being caught to provide clean test without output #nolint expect_message("All variables in specification file are in dataset") %>% - expect_message("All variables in dataset are ordered") + expect_message("All variables in dataset are ordered")) }) -test_that("xportr_order: Works as expected with only one domain in metadata", { +## Test 11: xportr_order: Works as expected with only one domain in metadata ---- +test_that("order Test 11: Works as expected with only one domain in metadata", { adsl <- data.frame( USUBJID = c(1001, 1002, 1003), BRTHDT = c(1, 1, 2) diff --git a/tests/testthat/test-pkg-load.R b/tests/testthat/test-pkg-load.R index be913992..2be6fc1e 100644 --- a/tests/testthat/test-pkg-load.R +++ b/tests/testthat/test-pkg-load.R @@ -1,4 +1,5 @@ -test_that(".onLoad: Unset options get initialised on package load with defaults", { +## Test 1: .onLoad: Unset options get initialised on package load with defaults ---- +test_that("pkg-load Test 1: .onLoad: Unset options get initialised on package load with defaults", { skip_if(getOption("testthat_interactive")) with_options( { @@ -9,7 +10,8 @@ test_that(".onLoad: Unset options get initialised on package load with defaults" ) }) -test_that(".onLoad: Initialised options are retained and not overwritten", { +## Test 2: .onLoad: Initialised options are retained and not overwritten ---- +test_that("pkg-load Test 2: .onLoad: Initialised options are retained and not overwritten", { skip_if(getOption("testthat_interactive")) with_options( { diff --git a/tests/testthat/test-support-for-tests.R b/tests/testthat/test-support-for-tests.R index 5e4136ce..918199bc 100644 --- a/tests/testthat/test-support-for-tests.R +++ b/tests/testthat/test-support-for-tests.R @@ -1,4 +1,6 @@ -test_that("minimal_table: builds minimal data frame with data", { +# minimal_table ---- +## Test 1: minimal_table: builds minimal data frame with data ---- +test_that("support-for-tests Test 1: builds minimal data frame with data", { minimal_table(31) %>% NROW() %>% expect_equal(31) @@ -8,7 +10,9 @@ test_that("minimal_table: builds minimal data frame with data", { expect_true() }) -test_that("minimal_metadata: builds minimal metadata data frame", { +# minimal_metadata ---- +## Test 2: minimal_metadata: builds minimal metadata data frame ---- +test_that("support-for-tests Test 2: builds minimal metadata data frame", { sample_metadata <- minimal_metadata( dataset = TRUE, length = TRUE, @@ -23,7 +27,8 @@ test_that("minimal_metadata: builds minimal metadata data frame", { expect_true() }) -test_that("minimal_metadata: columns in minimal_table are all in metadata", { +## Test 3: minimal_metadata: columns in minimal_table are all in metadata ---- +test_that("support-for-tests Test 3: columns in minimal_table are all in metadata", { sample_data <- minimal_table(31, cols = c("x", "y", "z", "a", "b", "c", "d")) sample_metadata <- minimal_metadata(dataset = TRUE) (colnames(sample_data) %in% sample_metadata$variable) %>% diff --git a/tests/testthat/test-type.R b/tests/testthat/test-type.R index 2bbe15de..fec148ec 100644 --- a/tests/testthat/test-type.R +++ b/tests/testthat/test-type.R @@ -12,7 +12,9 @@ df <- data.frame( Param = c("param1", "param2", "param3") ) -test_that("xportr_type: NAs are handled as expected", { +# xportr_type ---- +## Test 1: xportr_type: NAs are handled as expected ---- +test_that("type Test 1: xportr_type: NAs are handled as expected", { # Namely that "" isn't converted to NA or vice versa # Numeric columns will become NA but that is the nature of as.numeric df <- data.frame( @@ -48,7 +50,8 @@ test_that("xportr_type: NAs are handled as expected", { ) }) -test_that("xportr_type: Variable types are coerced as expected and can raise messages", { +## Test 2: xportr_type: Variable types are coerced as expected and can raise messages ---- +test_that("type Test 2: Variable types are coerced as expected and can raise messages", { # Remove empty lines in cli theme local_cli_theme() @@ -83,7 +86,8 @@ test_that("xportr_type: Variable types are coerced as expected and can raise mes )) }) -test_that("xportr_type: Variables retain column attributes, besides class", { +## Test 3: xportr_type: Variables retain column attributes, besides class ---- +test_that("type Test 3: Variables retain column attributes, besides class", { adsl <- dplyr::tibble( USUBJID = c(1001, 1002, 1003), SITEID = c(001, 002, 003), @@ -128,7 +132,8 @@ test_that("xportr_type: Variables retain column attributes, besides class", { expect_equal(df_type_label, df_label_type) }) -test_that("xportr_type: expect error when domain is not a character", { +## Test 4: xportr_type: expect error when domain is not a character ---- +test_that("type Test 4: expect error when domain is not a character", { df <- data.frame(x = 1, y = 2) df_meta <- data.frame( variable = c("x", "y"), @@ -142,7 +147,8 @@ test_that("xportr_type: expect error when domain is not a character", { expect_error(xportr_type(df, df_meta, domain = NA)) }) -test_that("xportr_type: works fine from metacore spec", { +## Test 5: xportr_type: works fine from metacore spec ---- +test_that("type Test 5: xportr_type: works fine from metacore spec", { skip_if_not_installed("metacore") df <- data.frame(x = 1, y = 2) @@ -164,14 +170,16 @@ test_that("xportr_type: works fine from metacore spec", { expect_equal(processed_df$x, "1") }) -test_that("xportr_type: error when metadata is not set", { +## Test 6: xportr_type: error when metadata is not set ---- +test_that("type Test 6: error when metadata is not set", { expect_error( xportr_type(df), regexp = "Must be of type 'data.frame', 'Metacore' or set via 'xportr_metadata\\(\\)'" ) }) -test_that("xportr_type: date variables are not converted to numeric", { +## Test 7: xportr_type: date variables are not converted to numeric ---- +test_that("type Test 7: xportr_type: date variables are not converted to numeric", { skip_if_not_installed("metacore") df <- data.frame(RFICDT = as.Date("2017-03-30"), RFICDTM = as.POSIXct("2017-03-30")) @@ -232,7 +240,8 @@ test_that("xportr_type: date variables are not converted to numeric", { expect_equal(adsl_original, adsl_xpt2) }) -test_that("xportr_type: Gets warning when metadata has multiple rows with same variable", { +## Test 8: xportr_type: Gets warning when metadata has multiple rows with same variable ---- +test_that("type Test 8: Gets warning when metadata has multiple rows with same variable", { # This test uses the (2) functions below to reduce code duplication # All `expect_*` are being called inside the functions # @@ -242,7 +251,8 @@ test_that("xportr_type: Gets warning when metadata has multiple rows with same v multiple_vars_in_spec_helper2(xportr_type) }) -test_that("xportr_type: Drops factor levels", { +## Test 9: xportr_type: Drops factor levels ---- +test_that("type Test 9: xportr_type: Drops factor levels", { metadata <- data.frame( dataset = "test", variable = c("Subj", "Param", "Val", "NotUsed"), @@ -279,7 +289,9 @@ metadata <- data.frame( format = c(NA, NA, "DATE9.", NA) ) -test_that("xportr_metadata: Var date types (--DTC) coerced as expected and raise messages", { +# xportr_metadata ---- +## Test 10: xportr_metadata: Var date types (--DTC) coerced as expected and raise messages ---- +test_that("type Test 10: Var date types (--DTC) coerced as expected and raise messages", { # Remove empty lines in cli theme local_cli_theme() @@ -296,7 +308,9 @@ test_that("xportr_metadata: Var date types (--DTC) coerced as expected and raise )) }) -test_that("xportr_type: Works as expected with only one domain in metadata", { +# xportr_type ---- +## Test 11: xportr_type: Works as expected with only one domain in metadata ---- +test_that("type Test 11: Works as expected with only one domain in metadata", { adsl <- data.frame( USUBJID = c(1001, 1002, 1003), BRTHDT = c(1, 1, 2) diff --git a/tests/testthat/test-utils-xportr.R b/tests/testthat/test-utils-xportr.R index 7ad9c9bd..fa112d99 100644 --- a/tests/testthat/test-utils-xportr.R +++ b/tests/testthat/test-utils-xportr.R @@ -1,4 +1,5 @@ -test_that("Get magrittr lhs side value", { +## Test 1: Get magrittr lhs side value ---- +test_that("utils-xportr Test 1: Get magrittr lhs side value", { x <- function(df, var) { get_pipe_call() } @@ -23,32 +24,38 @@ test_that("Get magrittr lhs side value", { ) }) - -test_that("fmt_vars: the message returns properly formatted variables", { +# fmt_vars ---- +## Test 2: fmt_vars: the message returns properly formatted variables ---- +test_that("utils-xportr Test 2: the message returns properly formatted variables", { expect_equal(fmt_vars(4), "Variable 4") expect_equal(fmt_vars(4:6), "Variables 4, 5, and 6") }) -test_that("fmt_labs: the message returns properly formatted labels", { +## Test 3: fmt_labs: the message returns properly formatted labels ---- +test_that("utils-xportr Test 3: the message returns properly formatted labels", { expect_equal(fmt_labs(4), "Label '=4'") expect_equal(fmt_labs(4:6), "Labels '=4', '=5', and '=6'") }) -test_that("xpt_validate_var_names: Get error message when the variable is over 8 characters", { +# xpt_validate_var_names ---- +## Test 4: xpt_validate_var_names: Get error message when the variable is over 8 characters ---- +test_that("utils-xportr Test 4: Get error message when the variable is over 8 characters", { expect_equal( xpt_validate_var_names(c("FOO", "BAR", "ABCDEFGHI")), "Variable `ABCDEFGHI` must be 8 characters or less." ) }) -test_that("xpt_validate_var_names: Get error message when the variable does not start with a letter", { +## Test 5: xpt_validate_var_names: Get error message when the variable does not start with a letter ---- +test_that("utils-xportr Test 5: Get error message when the variable does not start with a letter", { # nolint expect_equal( - xpt_validate_var_names(c("FOO", "2BAR")), + xpt_validate_var_names(c("FOO", "2BAR")), # nolint "Variable `2BAR` must start with a letter." ) }) -test_that("xpt_validate_var_names: Get error message when the variable contains non-ASCII characters or underscore", { +## Test 6: xpt_validate_var_names: Get error message when the variable contains non-ASCII characters or underscore ---- +test_that("utils-xportr Test 6: Get error message when the variable contains non-ASCII characters or underscore", { expect_equal( xpt_validate_var_names(c("FOO", "BAR", "FOO-BAR")), c( @@ -65,7 +72,8 @@ test_that("xpt_validate_var_names: Get error message when the variable contains ) }) -test_that("xpt_validate_var_names: Get error message when tje variable contains lowercase character", { +## Test 7: xpt_validate_var_names: Get error message when tje variable contains lowercase character ---- +test_that("utils-xportr Test 7: Get error message when the variable contains lowercase character", { xpt_validate_var_names(c("FOO", "bar")) expect_equal( xpt_validate_var_names(c("FOO", "bar")), @@ -73,7 +81,9 @@ test_that("xpt_validate_var_names: Get error message when tje variable contains ) }) -test_that("xpt_validate: Get error message when the label contains over 40 characters", { +# xpt_validate ---- +## Test 8: xpt_validate: Get error message when the label contains over 40 characters ---- +test_that("utils-xportr Test 8: Get error message when the label contains over 40 characters", { df <- data.frame(A = 1, B = 2) long_label <- paste(rep("a", 41), collapse = "") attr(df$A, "label") <- long_label @@ -83,7 +93,8 @@ test_that("xpt_validate: Get error message when the label contains over 40 chara ) }) -test_that("xpt_validate: Doesn't error out with iso8601 format", { +## Test 9: xpt_validate: Doesn't error out with iso8601 format ---- +test_that("utils-xportr Test 9: Doesn't error out with iso8601 format", { df <- data.frame(A = 1, B = 2) attr(df$A, "format.sas") <- "E8601LX." attr(df$B, "format.sas") <- "E8601DX20." @@ -93,7 +104,8 @@ test_that("xpt_validate: Doesn't error out with iso8601 format", { ) }) -test_that("xpt_validate: Get error message when the label contains non-ASCII, symbol or special characters", { +## Test 10: xpt_validate: Get error message when the label contains non-ASCII, symbol or special characters ---- +test_that("utils-xportr Test 10: Get error message when the label contains non-ASCII, symbol or special characters", { df <- data.frame(A = 1, B = 2) attr(df$A, "label") <- "fooçbar" expect_equal( @@ -102,7 +114,8 @@ test_that("xpt_validate: Get error message when the label contains non-ASCII, sy ) }) -test_that("xpt_validate: Get error message when the length of a character variable is > 200 bytes ", { +## Test 11: xpt_validate: Get error message when the length of a character variable is > 200 bytes ---- +test_that("utils-xportr Test 11: Get error message when the length of a character variable is > 200 bytes ", { df <- data.frame(A = paste(rep("A", 201), collapse = "")) expect_equal( xpt_validate(df), @@ -110,7 +123,8 @@ test_that("xpt_validate: Get error message when the length of a character variab ) }) -test_that("xpt_validate: Get error message when the length of a non-ASCII character variable is > 200 bytes", { +## Test 12: xpt_validate: Get error message when the length of a non-ASCII character variable is > 200 bytes ---- +test_that("utils-xportr Test 12: Get error message when the length of a non-ASCII character variable is > 200 bytes", { df <- data.frame(A = paste(rep("一", 67), collapse = "")) expect_equal( xpt_validate(df), @@ -118,7 +132,8 @@ test_that("xpt_validate: Get error message when the length of a non-ASCII charac ) }) -test_that("xpt_validate: Get error message when the length of a character variable is > 200 bytes and contains NAs", { +## Test 13: xpt_validate: Get error message when the length of a character variable is > 200 bytes and contains NAs ---- +test_that("utils-xportr Test 13: Get error message when the length of a character variable is > 200 bytes and contains NAs", { # nolint df <- data.frame(A = c(paste(rep("A", 201), collapse = ""), NA_character_)) expect_equal( xpt_validate(df), diff --git a/tests/testthat/test-write.R b/tests/testthat/test-write.R index bb036cf0..aa6c45f9 100644 --- a/tests/testthat/test-write.R +++ b/tests/testthat/test-write.R @@ -7,7 +7,9 @@ data_to_save <- function() { # Skip large file tests unless explicitly requested test_large_files <- Sys.getenv("XPORTR.TEST_LARGE_FILES", FALSE) -test_that("xportr_write: exported data can be saved to a file", { +# xportr_write ---- +## Test 1: xportr_write: exported data can be saved to a file ---- +test_that("write Test 1: exported data can be saved to a file", { skip_if_not_installed("withr") tmp <- withr::local_file("xyz.xpt") local_data <- data_to_save() @@ -16,7 +18,8 @@ test_that("xportr_write: exported data can be saved to a file", { expect_equal(read_xpt(tmp), local_data) }) -test_that("xportr_write: exported data can still be saved to a file with a label", { +## Test 2: xportr_write: exported data can still be saved to a file with a label ---- +test_that("write Test 2: exported data can still be saved to a file with a label", { skip_if_not_installed("withr") tmp <- withr::local_file("xyz.xpt") @@ -30,7 +33,8 @@ test_that("xportr_write: exported data can still be saved to a file with a label expect_output(str(read_xpt(tmp)), "Lorem ipsum dolor sit amet") }) -test_that("xportr_write: exported data can be saved to a file with a metadata", { +## Test 3: xportr_write: exported data can be saved to a file with a metadata ---- +test_that("write Test 3: exported data can be saved to a file with a metadata", { skip_if_not_installed("withr") tmp <- withr::local_file("xyz.xpt") @@ -46,7 +50,8 @@ test_that("xportr_write: exported data can be saved to a file with a metadata", expect_output(str(read_xpt(tmp)), "Lorem ipsum dolor sit amet") }) -test_that("xportr_write: exported data can be saved to a file with a existing metadata", { +## Test 4: xportr_write: exported data can be saved to a file with a existing metadata ---- +test_that("write Test 4: exported data can be saved to a file with a existing metadata", { skip_if_not_installed("withr") tmp <- withr::local_file("xyz.xpt") @@ -63,7 +68,8 @@ test_that("xportr_write: exported data can be saved to a file with a existing me expect_output(str(read_xpt(tmp)), "Lorem ipsum dolor sit amet") }) -test_that("xportr_write: expect error when invalid multibyte string is passed in label", { +## Test 5: xportr_write: expect error when invalid multibyte string is passed in label ---- +test_that("write Test 5: expect error when invalid multibyte string is passed in label", { skip_if_not_installed("withr") expect_error( xportr_write( @@ -77,7 +83,8 @@ test_that("xportr_write: expect error when invalid multibyte string is passed in ) }) -test_that("xportr_write: expect error when file name is over 8 characters long", { +## Test 6: xportr_write: expect error when file name is over 8 characters long ---- +test_that("write Test 6: expect error when file name is over 8 characters long", { skip_if_not_installed("withr") expect_error( xportr_write( @@ -88,7 +95,8 @@ test_that("xportr_write: expect error when file name is over 8 characters long", ) }) -test_that("xportr_write: expect error when file name contains non-ASCII symbols or special characters", { +## Test 7: xportr_write: expect error when file name contains non-ASCII symbols or special characters ---- +test_that("write Test 7: expect error when file name contains non-ASCII symbols or special characters", { skip_if_not_installed("withr") expect_error( xportr_write(data_to_save(), withr::local_file(".xpt"), strict_checks = TRUE), @@ -96,7 +104,8 @@ test_that("xportr_write: expect error when file name contains non-ASCII symbols ) }) -test_that("xportr_write: expect warning when file name contains underscore and strict_checks = FALSE", { +## Test 8: xportr_write: expect warning when file name contains underscore and strict_checks = FALSE ---- +test_that("write Test 8: expect warning when file name contains underscore and strict_checks = FALSE", { skip_if_not_installed("withr") expect_warning( xportr_write(data_to_save(), withr::local_file("test_.xpt"), strict_checks = FALSE), @@ -104,7 +113,8 @@ test_that("xportr_write: expect warning when file name contains underscore and s ) }) -test_that("xportr_write: expect error when label contains non-ASCII symbols or special characters", { +## Test 9: xportr_write: expect error when label contains non-ASCII symbols or special characters ---- +test_that("write Test 9: expect error when label contains non-ASCII symbols or special characters", { skip_if_not_installed("withr") expect_error( xportr_write( @@ -120,7 +130,8 @@ test_that("xportr_write: expect error when label contains non-ASCII symbols or s ) }) -test_that("xportr_write: expect error when label is over 40 characters", { +## Test 10: xportr_write: expect error when label is over 40 characters ---- +test_that("write Test 10: expect error when label is over 40 characters", { skip_if_not_installed("withr") expect_error( xportr_write( @@ -136,7 +147,8 @@ test_that("xportr_write: expect error when label is over 40 characters", { ) }) -test_that("xportr_write: expect error when an xpt validation fails with strict_checks set to TRUE", { +## Test 11: xportr_write: expect error when an xpt validation fails with strict_checks set to TRUE ---- +test_that("write Test 11: expect error when an xpt validation fails with strict_checks set to TRUE", { skip_if_not_installed("withr") local_data <- data_to_save() attr(local_data$X, "format.sas") <- "foo" @@ -156,7 +168,8 @@ test_that("xportr_write: expect error when an xpt validation fails with strict_c ) }) -test_that("xportr_write: expect warning when an xpt validation fails with strict_checks set to FALSE", { +## Test 12: xportr_write: expect warning when an xpt validation fails with strict_checks set to FALSE ---- +test_that("write Test 12: expect warning when an xpt validation fails with strict_checks set to FALSE", { skip_if_not_installed("withr") local_data <- data_to_save() attr(local_data$X, "format.sas") <- "foo" @@ -176,7 +189,8 @@ test_that("xportr_write: expect warning when an xpt validation fails with strict ) }) -test_that("xportr_write: Capture errors by haven and report them as such", { +## Test 13: xportr_write: Capture errors by haven and report them as such ---- +test_that("write Test 13: Capture errors by haven and report them as such", { skip_if_not_installed("withr") local_data <- data_to_save() attr(local_data$X, "format.sas") <- "E8601LXw.asdf" @@ -198,7 +212,8 @@ test_that("xportr_write: Capture errors by haven and report them as such", { ) }) -test_that("xportr_write: `split_by` attribute is used to split the data", { +## Test 14: xportr_write: `split_by` attribute is used to split the data ---- +test_that("write Test 14: `split_by` attribute is used to split the data", { tmpdir <- tempdir() tmp <- file.path(tmpdir, "xyz.xpt") @@ -241,7 +256,8 @@ test_that("xportr_write: `split_by` attribute is used to split the data", { ) }) -test_that("xportr_write: Large file sizes are reported and warned", { +## Test 15: xportr_write: Large file sizes are reported and warned ---- +test_that("write Test 15: Large file sizes are reported and warned", { skip_if_not(test_large_files) tmpdir <- tempdir() tmp <- file.path(tmpdir, "xyz.xpt") diff --git a/tests/testthat/test-xportr.R b/tests/testthat/test-xportr.R index 62d3097c..17d6cc9f 100644 --- a/tests/testthat/test-xportr.R +++ b/tests/testthat/test-xportr.R @@ -1,47 +1,46 @@ -test_that("pipeline results match `xportr()` results", { +## Test 1: pipeline results match `xportr()` results ---- +test_that("xportr Test 1: pipeline results match `xportr()` results", { data("var_spec", "dataset_spec", "adsl_xportr", envir = environment()) adsl <- adsl_xportr - if (require(magrittr, quietly = TRUE)) { - skip_if_not_installed("withr") - pipeline_path <- withr::local_file("adslpipe.xpt") - xportr_path <- withr::local_file("adslxptr.xpt") + skip_if_not_installed("withr") + pipeline_path <- withr::local_file("adslpipe.xpt") + xportr_path <- withr::local_file("adslxptr.xpt") - dataset_spec_low <- setNames(dataset_spec, tolower(names(dataset_spec))) - names(dataset_spec_low)[[2]] <- "label" + dataset_spec_low <- setNames(dataset_spec, tolower(names(dataset_spec))) + names(dataset_spec_low)[[2]] <- "label" - var_spec_low <- setNames(var_spec, tolower(names(var_spec))) - names(var_spec_low)[[5]] <- "type" + var_spec_low <- setNames(var_spec, tolower(names(var_spec))) + names(var_spec_low)[[5]] <- "type" - # Divert all messages to tempfile, instead of printing them - # note: be aware as this should only be used in tests that don't track - # messages - withr::local_message_sink(withr::local_tempfile()) - pipeline_df <- adsl %>% - xportr_metadata(var_spec_low, "ADSL", verbose = "none") %>% - xportr_type() %>% - xportr_length() %>% - xportr_label() %>% - xportr_order() %>% - xportr_format() %>% - xportr_df_label(dataset_spec_low) %>% - xportr_write(pipeline_path) + # Divert all messages to tempfile, instead of printing them + # note: be aware as this should only be used in tests that don't track + # messages + withr::local_message_sink(withr::local_tempfile()) + pipeline_df <- adsl %>% + xportr_metadata(var_spec_low, "ADSL", verbose = "none") %>% + xportr_type() %>% + xportr_length() %>% + xportr_label() %>% + xportr_order() %>% + xportr_format() %>% + xportr_df_label(dataset_spec_low) %>% + xportr_write(pipeline_path) - # `xportr()` can be used to apply a whole pipeline at once - xportr_df <- xportr( - adsl, - var_metadata = var_spec_low, - df_metadata = dataset_spec_low, - domain = "ADSL", - verbose = "none", - path = xportr_path - ) + # `xportr()` can be used to apply a whole pipeline at once + xportr_df <- xportr( + adsl, + var_metadata = var_spec_low, + df_metadata = dataset_spec_low, + domain = "ADSL", + verbose = "none", + path = xportr_path + ) - expect_identical(pipeline_df, xportr_df) + expect_identical(pipeline_df, xportr_df) - expect_identical( - haven::read_xpt(pipeline_path), - haven::read_xpt(xportr_path) - ) - } + expect_identical( + haven::read_xpt(pipeline_path), + haven::read_xpt(xportr_path) + ) })