Skip to content

Commit

Permalink
Fixing the code style
Browse files Browse the repository at this point in the history
  • Loading branch information
sadchla-codes committed Mar 6, 2024
1 parent b247ba3 commit 274c88e
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 122 deletions.
58 changes: 32 additions & 26 deletions tests/testthat/test-deprecation.R
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
## 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")
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))
})
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.")
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))
})
expect_error(xportr_format(df, metacore = df_meta))
})

## Test 3: xportr_label: using the deprecated metacore argument gives an error ----
test_that(
Expand All @@ -28,9 +28,11 @@ test_that(

df <- data.frame(x = "a", y = "b")
df_meta <-
data.frame(dataset = "df",
variable = "x",
label = "foo")
data.frame(
dataset = "df",
variable = "x",
label = "foo"
)

expect_error(xportr_label(df, metacore = df_meta))
}
Expand Down Expand Up @@ -59,13 +61,17 @@ test_that(
{
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)
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"))
}
Expand Down
26 changes: 14 additions & 12 deletions tests/testthat/test-label.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ test_that("label Test 1: xportr_label: error when metadata is not set", {
)

expect_error(xportr_label(df),
regexp = "Must be of type 'data.frame', 'Metacore' or set via 'xportr_metadata\\(\\)'")
regexp = "Must be of type 'data.frame', 'Metacore' or set via 'xportr_metadata\\(\\)'"
)
})

## Test 2: xportr_label: Gets warning when metadata has multiple rows with same variable ----
Expand All @@ -27,16 +28,17 @@ test_that(


## 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))
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)
)

metadata <- data.frame(
dataset = c("adsl", "adsl"),
variable = c("USUBJID", "BRTHDT"),
label = c("Hello", "Hello2")
)
metadata <- data.frame(
dataset = c("adsl", "adsl"),
variable = c("USUBJID", "BRTHDT"),
label = c("Hello", "Hello2")
)

expect_silent(xportr_label(adsl, metadata))
})
expect_silent(xportr_label(adsl, metadata))
})
88 changes: 43 additions & 45 deletions tests/testthat/test-metadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -608,11 +608,11 @@ test_that("metadata Test 28: xportr_metadata: Throws message when variables not

## Test 29: xportr_metadata: Variable ordering messaging is correct ----
test_that("metadata Test 29: xportr_metadata: Variable ordering messaging is correct", {
#skip_if_not_installed("haven")
#skip_if_not_installed("readxl")
# skip_if_not_installed("haven")
# skip_if_not_installed("readxl")

#require(haven, quietly = TRUE)
#require(readxl, quietly = TRUE)
# require(haven, quietly = TRUE)
# require(readxl, quietly = TRUE)

df <- data.frame(c = 1:5, a = "a", d = 5:1, b = LETTERS[1:5])
df2 <- data.frame(a = "a", z = "z")
Expand Down Expand Up @@ -793,51 +793,49 @@ test_that("metadata Test 33: xportr_*: Domain is kept in between calls", {

## Test 34: `xportr_metadata()` results match traditional results ----
test_that("metadata Test 34: `xportr_metadata()` results match traditional results", {

data("var_spec", "dataset_spec", "adsl_xportr", envir = environment())
adsl <- adsl_xportr

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(
haven::read_xpt(metadata_path),
haven::read_xpt(trad_path)
)
})
11 changes: 6 additions & 5 deletions tests/testthat/test-order.R
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,11 @@ test_that("order Test 8: 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`")
)

suppressMessages(xportr_order(df2, df_meta, verbose = "message", domain = "df2") %>%
expect_message("2 variables not in spec and moved to end") %>%
Expand Down Expand Up @@ -174,7 +175,7 @@ test_that("order Test 10: xportr_order: Gets warning when metadata has multiple
expect_message("All variables in dataset are ordered"))

# Checks that message doesn't appear when xportr.domain_name is valid
suppressMessages( multiple_vars_in_spec_helper2(xportr_order) %>%
suppressMessages(multiple_vars_in_spec_helper2(xportr_order) %>%
# expect_message() are being caught to provide clean test without output
expect_message("All variables in specification file are in dataset") %>%
expect_message("All variables in dataset are ordered"))
Expand Down
68 changes: 34 additions & 34 deletions tests/testthat/test-xportr.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,44 @@ test_that("xportr Test 1: pipeline results match `xportr()` results", {
data("var_spec", "dataset_spec", "adsl_xportr", envir = environment())
adsl <- adsl_xportr

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)
)
})

0 comments on commit 274c88e

Please sign in to comment.