Skip to content

Commit

Permalink
Correct failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
EeethB committed Jan 9, 2024
1 parent 21accf0 commit 9afeeb5
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 18 deletions.
1 change: 1 addition & 0 deletions R/xportr-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
#'
#'
#' @keywords internal
#' @aliases xportr-package
#'
#' @import rlang haven
#' @importFrom dplyr left_join bind_cols filter select rename rename_with n
Expand Down
10 changes: 5 additions & 5 deletions R/xportr.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
#'
#' @examples
#'
#' has_pkgs <- require(admiral, quietly = TRUE) &&
#' require(dplyr, quietly = TRUE) &&
#' require(readxl, quietly = TRUE) &&
#' require(rlang, quietly = TRUE)
#' has_pkgs <- requireNamespace("admiral", quietly = TRUE) &&
#' requireNamespace("dplyr", quietly = TRUE) &&
#' requireNamespace("readxl", quietly = TRUE) &&
#' requireNamespace("rlang", quietly = TRUE)
#'
#' if (has_pkgs) {
#' adsl <- admiral::admiral_adsl
Expand Down Expand Up @@ -64,7 +64,7 @@ xportr <- function(.df,
path,
strict_checks = FALSE) {
.df %>%
xportr_metadata(var_metadata, domain, verbose) %>%
xportr_metadata(var_metadata, domain = domain, verbose = verbose) %>%
xportr_type() %>%
xportr_length() %>%
xportr_label() %>%
Expand Down
4 changes: 2 additions & 2 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@ adsl %>%

The `xportr_metadata()` function can reduce duplication by setting the variable specification and domain explicitly at the top of a pipeline. If you would like to use the `verbose` argument, you will need to set in each function call.

```{r, message=FALSE, eval=FALSE}
```{r, warning=FALSE, message=FALSE, eval=FALSE}
adsl %>%
xportr_metadata(var_spec, "ADSL", verbose = "warn") %>%
xportr_metadata(var_spec, domain = "ADSL") %>%
xportr_type() %>%
xportr_length() %>%
xportr_label() %>%
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ each function call.

``` r
adsl %>%
xportr_metadata(var_spec, "ADSL") %>%
xportr_metadata(var_spec, domain = "ADSL") %>%
xportr_type() %>%
xportr_length() %>%
xportr_label() %>%
Expand Down
2 changes: 1 addition & 1 deletion man/xportr-package.Rd

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

23 changes: 14 additions & 9 deletions tests/testthat/test-metadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -594,13 +594,13 @@ test_that("xportr_metadata: Variable ordering messaging is correct", {
)

# Metadata versions
xportr_metadata(df, df_meta, verbose = "message") %>%
xportr_metadata(df, df_meta, domain = "df", verbose = "message") %>%
xportr_order() %>%
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_metadata(df2, df_meta, verbose = "message") %>%
xportr_metadata(df2, df_meta, domain = "df2", verbose = "message") %>%
xportr_order() %>%
expect_message("2 variables not in spec and moved to end") %>%
expect_message("Variable moved to end in `.df`: `a` and `z`") %>%
Expand All @@ -623,13 +623,13 @@ test_that("xportr_type: Variable types are coerced as expected and can raise mes

# Metadata version of the last statement
df %>%
xportr_metadata(meta_example, verbose = "warn") %>%
xportr_metadata(meta_example, domain = "df", verbose = "warn") %>%
xportr_type() %>%
expect_warning()

# Metadata version
df %>%
xportr_metadata(meta_example, verbose = "message") %>%
xportr_metadata(meta_example, domain = "df", verbose = "message") %>%
xportr_type() %>%
expect_message("Variable type\\(s\\) in dataframe don't match metadata")
})
Expand All @@ -654,7 +654,8 @@ test_that("xportr_metadata: Check metadata interaction with other functions", {
`_xportr.df_metadata_` = var_spec,
`_xportr.df_verbose_` = "none"),
suppressMessages(
xportr_metadata(adsl, var_spec, domain = "adsl") %>% xportr_type()
xportr_metadata(adsl, var_spec, domain = "adsl", verbose = "none") %>%
xportr_type()
)
)

Expand All @@ -663,7 +664,8 @@ test_that("xportr_metadata: Check metadata interaction with other functions", {
`_xportr.df_metadata_` = var_spec,
`_xportr.df_verbose_` = "none"),
suppressMessages(
xportr_metadata(adsl, var_spec, domain = "adsl") %>% xportr_length()
xportr_metadata(adsl, var_spec, domain = "adsl", verbose = "none") %>%
xportr_length()
)
)

Expand All @@ -672,7 +674,8 @@ test_that("xportr_metadata: Check metadata interaction with other functions", {
`_xportr.df_metadata_` = var_spec,
`_xportr.df_verbose_` = "none"),
suppressMessages(
xportr_metadata(adsl, var_spec, domain = "adsl") %>% xportr_label()
xportr_metadata(adsl, var_spec, domain = "adsl", verbose = "none") %>%
xportr_label()
)
)

Expand All @@ -681,7 +684,8 @@ test_that("xportr_metadata: Check metadata interaction with other functions", {
`_xportr.df_metadata_` = var_spec,
`_xportr.df_verbose_` = "none"),
suppressMessages(
xportr_metadata(adsl, var_spec, domain = "adsl") %>% xportr_order()
xportr_metadata(adsl, var_spec, domain = "adsl", verbose = "none") %>%
xportr_order()
)
)

Expand All @@ -690,7 +694,8 @@ test_that("xportr_metadata: Check metadata interaction with other functions", {
`_xportr.df_metadata_` = var_spec,
`_xportr.df_verbose_` = "none"),
suppressMessages(
xportr_metadata(adsl, var_spec, domain = "adsl") %>% xportr_format()
xportr_metadata(adsl, var_spec, domain = "adsl", verbose = "none") %>%
xportr_format()
)
)
})
Expand Down
52 changes: 52 additions & 0 deletions tests/testthat/test-xportr.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
test_that("pipeline results match `xportr()` results", {

has_pkgs <- requireNamespace("admiral", quietly = TRUE) &&
requireNamespace("dplyr", quietly = TRUE) &&
requireNamespace("readxl", quietly = TRUE) &&
requireNamespace("rlang", quietly = TRUE)

if (has_pkgs) {
adsl <- admiral::admiral_adsl

spec_path <- system.file(paste0("specs/", "ADaM_admiral_spec.xlsx"),
package = "xportr")

var_spec <- readxl::read_xlsx(spec_path, sheet = "Variables") %>%
dplyr::rename(type = "Data Type") %>%
rlang::set_names(tolower)
dataset_spec <- readxl::read_xlsx(spec_path, sheet = "Datasets") %>%
dplyr::rename(label = "Description") %>%
rlang::set_names(tolower)

test_dir <- tempdir()

pipeline_path <- file.path(test_dir, "adslpipe.xpt")
xportr_path <- file.path(test_dir, "adslxptr.xpt")

pipeline_df <- adsl %>%
xportr_metadata(var_spec, "ADSL", verbose = "none") %>%
xportr_type() %>%
xportr_length() %>%
xportr_label() %>%
xportr_order() %>%
xportr_format() %>%
xportr_df_label(dataset_spec) %>%
xportr_write(pipeline_path)

# `xportr()` can be used to apply a whole pipeline at once
xportr_df <- xportr(adsl,
var_metadata = var_spec,
df_metadata = dataset_spec,
domain = "ADSL",
verbose = "none",
path = xportr_path
)

expect_identical(pipeline_df, xportr_df)

expect_identical(
haven::read_xpt(pipeline_path),
haven::read_xpt(xportr_path)
)
}
})

0 comments on commit 9afeeb5

Please sign in to comment.