From f74b30dad546ea7ee6c5948b2b9d2cff4cb9f1cd Mon Sep 17 00:00:00 2001 From: Joe Zhu Date: Wed, 13 Nov 2024 12:59:13 +0000 Subject: [PATCH] addin tests --- tests/testthat/_snaps/t_dd_slide.md | 8 ++ tests/testthat/_snaps/t_dm_slide.md | 177 +++++++++++++++++++++++++++ tests/testthat/_snaps/t_dor_slide.md | 30 +++++ tests/testthat/setup-options.R | 11 ++ tests/testthat/setup.R | 147 ++++++++++++++++++++++ tests/testthat/test-data_snapshot.R | 2 +- tests/testthat/test-save-output.R | 10 -- tests/testthat/test-save_output.R | 12 ++ 8 files changed, 386 insertions(+), 11 deletions(-) create mode 100644 tests/testthat/_snaps/t_dd_slide.md create mode 100644 tests/testthat/_snaps/t_dm_slide.md create mode 100644 tests/testthat/_snaps/t_dor_slide.md create mode 100644 tests/testthat/setup-options.R create mode 100644 tests/testthat/setup.R delete mode 100755 tests/testthat/test-save-output.R diff --git a/tests/testthat/_snaps/t_dd_slide.md b/tests/testthat/_snaps/t_dd_slide.md new file mode 100644 index 00000000..fd9589de --- /dev/null +++ b/tests/testthat/_snaps/t_dd_slide.md @@ -0,0 +1,8 @@ +# Test t_dd_slide table with expected Error + + Code + t_dd_slide(adsl, "TRT") + Condition + Error: + ! object 'adsl' not found + diff --git a/tests/testthat/_snaps/t_dm_slide.md b/tests/testthat/_snaps/t_dm_slide.md new file mode 100644 index 00000000..5e48f9b3 --- /dev/null +++ b/tests/testthat/_snaps/t_dm_slide.md @@ -0,0 +1,177 @@ +# Test demographic table creation of t_dm_slide + + Code + t_dm_slide(adsl, "TRT01P", c("SEX", "AGE", "RACE", "ETHNIC", "COUNTRY")) + Output + Demographic slide + + —————————————————————————————————————————————————————————————————————————————————————————————————————————————————————— + A: Drug X B: Placebo C: Combination All Patients All Patients + —————————————————————————————————————————————————————————————————————————————————————————————————————————————————————— + Sex + F 79 (59%) 82 (61.2%) 70 (53%) 231 (57.8%) 231 (57.8%) + M 55 (41%) 52 (38.8%) 62 (47%) 169 (42.2%) 169 (42.2%) + Age + Median 33.00 35.00 35.00 34.00 34.00 + Min - Max 21.0 - 50.0 21.0 - 62.0 20.0 - 69.0 20.0 - 69.0 20.0 - 69.0 + Race + ASIAN 68 (50.7%) 67 (50%) 73 (55.3%) 208 (52%) 208 (52%) + BLACK OR AFRICAN AMERICAN 31 (23.1%) 28 (20.9%) 32 (24.2%) 91 (22.8%) 91 (22.8%) + WHITE 27 (20.1%) 26 (19.4%) 21 (15.9%) 74 (18.5%) 74 (18.5%) + AMERICAN INDIAN OR ALASKA NATIVE 8 (6%) 11 (8.2%) 6 (4.5%) 25 (6.2%) 25 (6.2%) + MULTIPLE 0 1 (0.7%) 0 1 (0.2%) 1 (0.2%) + NATIVE HAWAIIAN OR OTHER PACIFIC ISLANDER 0 1 (0.7%) 0 1 (0.2%) 1 (0.2%) + OTHER 0 0 0 0 0 + UNKNOWN 0 0 0 0 0 + Ethnicity + NOT REPORTED 6 (4.5%) 10 (7.5%) 11 (8.3%) 27 (6.8%) 27 (6.8%) + HISPANIC OR LATINO 15 (11.2%) 18 (13.4%) 15 (11.4%) 48 (12%) 48 (12%) + NOT HISPANIC OR LATINO 104 (77.6%) 103 (76.9%) 101 (76.5%) 308 (77%) 308 (77%) + UNKNOWN 9 (6.7%) 3 (2.2%) 5 (3.8%) 17 (4.2%) 17 (4.2%) + Country + CHN 74 (55.2%) 81 (60.4%) 64 (48.5%) 219 (54.8%) 219 (54.8%) + USA 10 (7.5%) 13 (9.7%) 17 (12.9%) 40 (10%) 40 (10%) + BRA 13 (9.7%) 7 (5.2%) 10 (7.6%) 30 (7.5%) 30 (7.5%) + PAK 12 (9%) 9 (6.7%) 10 (7.6%) 31 (7.8%) 31 (7.8%) + NGA 8 (6%) 7 (5.2%) 11 (8.3%) 26 (6.5%) 26 (6.5%) + RUS 5 (3.7%) 8 (6%) 6 (4.5%) 19 (4.8%) 19 (4.8%) + JPN 5 (3.7%) 4 (3%) 9 (6.8%) 18 (4.5%) 18 (4.5%) + GBR 4 (3%) 3 (2.2%) 2 (1.5%) 9 (2.2%) 9 (2.2%) + CAN 3 (2.2%) 2 (1.5%) 3 (2.3%) 8 (2%) 8 (2%) + CHE 0 0 0 0 0 + +--- + + Code + t_dm_slide(adsl, "TRT01P", c("SEX", "AGE", "RACE", "ETHNIC", "COUNTRY"), + side_by_side = 2) + Output + Demographic slide + + ——————————————————————————————————————————————————————————————————————————————————————————————————————— + A: Drug X B: Placebo C: Combination All Patients + ——————————————————————————————————————————————————————————————————————————————————————————————————————— + Sex + F 79 (59%) 82 (61.2%) 70 (53%) 231 (57.8%) + M 55 (41%) 52 (38.8%) 62 (47%) 169 (42.2%) + Age + Median 33.00 35.00 35.00 34.00 + Min - Max 21.0 - 50.0 21.0 - 62.0 20.0 - 69.0 20.0 - 69.0 + Race + ASIAN 68 (50.7%) 67 (50%) 73 (55.3%) 208 (52%) + BLACK OR AFRICAN AMERICAN 31 (23.1%) 28 (20.9%) 32 (24.2%) 91 (22.8%) + WHITE 27 (20.1%) 26 (19.4%) 21 (15.9%) 74 (18.5%) + AMERICAN INDIAN OR ALASKA NATIVE 8 (6%) 11 (8.2%) 6 (4.5%) 25 (6.2%) + MULTIPLE 0 1 (0.7%) 0 1 (0.2%) + NATIVE HAWAIIAN OR OTHER PACIFIC ISLANDER 0 1 (0.7%) 0 1 (0.2%) + OTHER 0 0 0 0 + UNKNOWN 0 0 0 0 + Ethnicity + NOT REPORTED 6 (4.5%) 10 (7.5%) 11 (8.3%) 27 (6.8%) + HISPANIC OR LATINO 15 (11.2%) 18 (13.4%) 15 (11.4%) 48 (12%) + NOT HISPANIC OR LATINO 104 (77.6%) 103 (76.9%) 101 (76.5%) 308 (77%) + UNKNOWN 9 (6.7%) 3 (2.2%) 5 (3.8%) 17 (4.2%) + Country + CHN 74 (55.2%) 81 (60.4%) 64 (48.5%) 219 (54.8%) + USA 10 (7.5%) 13 (9.7%) 17 (12.9%) 40 (10%) + BRA 13 (9.7%) 7 (5.2%) 10 (7.6%) 30 (7.5%) + PAK 12 (9%) 9 (6.7%) 10 (7.6%) 31 (7.8%) + NGA 8 (6%) 7 (5.2%) 11 (8.3%) 26 (6.5%) + RUS 5 (3.7%) 8 (6%) 6 (4.5%) 19 (4.8%) + JPN 5 (3.7%) 4 (3%) 9 (6.8%) 18 (4.5%) + GBR 4 (3%) 3 (2.2%) 2 (1.5%) 9 (2.2%) + CAN 3 (2.2%) 2 (1.5%) 3 (2.3%) 8 (2%) + CHE 0 0 0 0 + +--- + + Code + t_dm_slide(adsl, "TRT01P", c("SEX", "AGE", "RACE", "ETHNIC", "COUNTRY"), + split_by_study = TRUE, side_by_side = 2) + Condition + Warning in `build_table_header()`: + split_by_study argument will be ignored + Output + Demographic slide + + ——————————————————————————————————————————————————————————————————————————————————————————————————————— + A: Drug X B: Placebo C: Combination All Patients + ——————————————————————————————————————————————————————————————————————————————————————————————————————— + Sex + F 79 (59%) 82 (61.2%) 70 (53%) 231 (57.8%) + M 55 (41%) 52 (38.8%) 62 (47%) 169 (42.2%) + Age + Median 33.00 35.00 35.00 34.00 + Min - Max 21.0 - 50.0 21.0 - 62.0 20.0 - 69.0 20.0 - 69.0 + Race + ASIAN 68 (50.7%) 67 (50%) 73 (55.3%) 208 (52%) + BLACK OR AFRICAN AMERICAN 31 (23.1%) 28 (20.9%) 32 (24.2%) 91 (22.8%) + WHITE 27 (20.1%) 26 (19.4%) 21 (15.9%) 74 (18.5%) + AMERICAN INDIAN OR ALASKA NATIVE 8 (6%) 11 (8.2%) 6 (4.5%) 25 (6.2%) + MULTIPLE 0 1 (0.7%) 0 1 (0.2%) + NATIVE HAWAIIAN OR OTHER PACIFIC ISLANDER 0 1 (0.7%) 0 1 (0.2%) + OTHER 0 0 0 0 + UNKNOWN 0 0 0 0 + Ethnicity + NOT REPORTED 6 (4.5%) 10 (7.5%) 11 (8.3%) 27 (6.8%) + HISPANIC OR LATINO 15 (11.2%) 18 (13.4%) 15 (11.4%) 48 (12%) + NOT HISPANIC OR LATINO 104 (77.6%) 103 (76.9%) 101 (76.5%) 308 (77%) + UNKNOWN 9 (6.7%) 3 (2.2%) 5 (3.8%) 17 (4.2%) + Country + CHN 74 (55.2%) 81 (60.4%) 64 (48.5%) 219 (54.8%) + USA 10 (7.5%) 13 (9.7%) 17 (12.9%) 40 (10%) + BRA 13 (9.7%) 7 (5.2%) 10 (7.6%) 30 (7.5%) + PAK 12 (9%) 9 (6.7%) 10 (7.6%) 31 (7.8%) + NGA 8 (6%) 7 (5.2%) 11 (8.3%) 26 (6.5%) + RUS 5 (3.7%) 8 (6%) 6 (4.5%) 19 (4.8%) + JPN 5 (3.7%) 4 (3%) 9 (6.8%) 18 (4.5%) + GBR 4 (3%) 3 (2.2%) 2 (1.5%) 9 (2.2%) + CAN 3 (2.2%) 2 (1.5%) 3 (2.3%) 8 (2%) + CHE 0 0 0 0 + +--- + + Code + t_dm_slide(adsl, "TRT01P", c("SEX", "AGE", "RACE", "ETHNIC", "COUNTRY"), + split_by_study = TRUE, side_by_side = 2) + Condition + Warning in `build_table_header()`: + split_by_study argument will be ignored + Output + Demographic slide + + ——————————————————————————————————————————————————————————————————————————————————————————————————————— + A: Drug X B: Placebo C: Combination All Patients + ——————————————————————————————————————————————————————————————————————————————————————————————————————— + Sex + F 79 (59%) 82 (61.2%) 70 (53%) 231 (57.8%) + M 55 (41%) 52 (38.8%) 62 (47%) 169 (42.2%) + Age + Median 33.00 35.00 35.00 34.00 + Min - Max 21.0 - 50.0 21.0 - 62.0 20.0 - 69.0 20.0 - 69.0 + Race + ASIAN 68 (50.7%) 67 (50%) 73 (55.3%) 208 (52%) + BLACK OR AFRICAN AMERICAN 31 (23.1%) 28 (20.9%) 32 (24.2%) 91 (22.8%) + WHITE 27 (20.1%) 26 (19.4%) 21 (15.9%) 74 (18.5%) + AMERICAN INDIAN OR ALASKA NATIVE 8 (6%) 11 (8.2%) 6 (4.5%) 25 (6.2%) + MULTIPLE 0 1 (0.7%) 0 1 (0.2%) + NATIVE HAWAIIAN OR OTHER PACIFIC ISLANDER 0 1 (0.7%) 0 1 (0.2%) + OTHER 0 0 0 0 + UNKNOWN 0 0 0 0 + Ethnicity + NOT REPORTED 6 (4.5%) 10 (7.5%) 11 (8.3%) 27 (6.8%) + HISPANIC OR LATINO 15 (11.2%) 18 (13.4%) 15 (11.4%) 48 (12%) + NOT HISPANIC OR LATINO 104 (77.6%) 103 (76.9%) 101 (76.5%) 308 (77%) + UNKNOWN 9 (6.7%) 3 (2.2%) 5 (3.8%) 17 (4.2%) + Country + CHN 74 (55.2%) 81 (60.4%) 64 (48.5%) 219 (54.8%) + USA 10 (7.5%) 13 (9.7%) 17 (12.9%) 40 (10%) + BRA 13 (9.7%) 7 (5.2%) 10 (7.6%) 30 (7.5%) + PAK 12 (9%) 9 (6.7%) 10 (7.6%) 31 (7.8%) + NGA 8 (6%) 7 (5.2%) 11 (8.3%) 26 (6.5%) + RUS 5 (3.7%) 8 (6%) 6 (4.5%) 19 (4.8%) + JPN 5 (3.7%) 4 (3%) 9 (6.8%) 18 (4.5%) + GBR 4 (3%) 3 (2.2%) 2 (1.5%) 9 (2.2%) + CAN 3 (2.2%) 2 (1.5%) 3 (2.3%) 8 (2%) + CHE 0 0 0 0 + diff --git a/tests/testthat/_snaps/t_dor_slide.md b/tests/testthat/_snaps/t_dor_slide.md new file mode 100644 index 00000000..3139aa15 --- /dev/null +++ b/tests/testthat/_snaps/t_dor_slide.md @@ -0,0 +1,30 @@ +# Test DOR table when time unit is 'DAYS' + + Code + t_dor_slide(adsl, adtte) + Output + DOR slide + + —————————————————————————————————————————————————————————————————————————— + A: Drug X B: Placebo C: Combination + (N=134) (N=134) (N=132) + —————————————————————————————————————————————————————————————————————————— + Responders 134 134 132 + With subsequent event (%) 58 (43.3%) 58 (43.3%) 69 (52.3%) + Median (Months, 95% CI) NA (9.3, NA) NA (9.4, NA) 9.4 (7.6, NA) + +# Test DOR table when time unit is 'YEARS' + + Code + t_dor_slide(adsl, adtte) + Output + DOR slide + + ————————————————————————————————————————————————————————————————————————————————————— + A: Drug X B: Placebo C: Combination + (N=134) (N=134) (N=132) + ————————————————————————————————————————————————————————————————————————————————————— + Responders 134 134 132 + With subsequent event (%) 58 (43.3%) 58 (43.3%) 69 (52.3%) + Median (Months, 95% CI) NA (3397.7, NA) NA (3447.1, NA) 3439.6 (2784.8, NA) + diff --git a/tests/testthat/setup-options.R b/tests/testthat/setup-options.R new file mode 100644 index 00000000..a1244da3 --- /dev/null +++ b/tests/testthat/setup-options.R @@ -0,0 +1,11 @@ +opts_partial_match_old <- list( + warnPartialMatchDollar = getOption("warnPartialMatchDollar"), + warnPartialMatchArgs = getOption("warnPartialMatchArgs"), + warnPartialMatchAttr = getOption("warnPartialMatchAttr") +) + +opts_partial_match_new <- list( + warnPartialMatchDollar = TRUE, + warnPartialMatchArgs = TRUE, + warnPartialMatchAttr = TRUE +) diff --git a/tests/testthat/setup.R b/tests/testthat/setup.R new file mode 100644 index 00000000..49a917f4 --- /dev/null +++ b/tests/testthat/setup.R @@ -0,0 +1,147 @@ +# Extra libraries (suggested) for tests +library(dplyr) +library(assertthat) +library(rtables) +library(tern) +library(purrr) +require("ggplot2") +set.seed(12893) +# skip_if_too_deep +skip_if_too_deep <- function(depth) { # nolintr + checkmate::assert_number(depth, lower = 0, upper = 5) + + testing_depth <- getOption("TESTING_DEPTH") + if (is.null(testing_depth)) testing_depth <- Sys.getenv("TESTING_DEPTH") + + testing_depth <- tryCatch( + as.numeric(testing_depth), + error = function(error) 3, + warning = function(warning) 3 + ) + + if (length(testing_depth) != 1 || is.na(testing_depth)) testing_depth <- 3 + + if (testing_depth < depth) { + testthat::skip(paste("testing depth", testing_depth, "is below current testing specification", depth)) + } +} + + +expect_snapshot_ggplot <- function(title, fig, width = NA, height = NA) { + testthat::skip_on_ci() + testthat::skip_if_not_installed("svglite") + + name <- paste0(title, ".svg") + path <- tempdir() + withr::with_options( + opts_partial_match_old, + suppressMessages(ggplot2::ggsave(name, fig, path = path, width = width, height = height)) + ) + path <- file.path(path, name) + + testthat::announce_snapshot_file(name = name) + testthat::expect_snapshot_file(path, name) +} + +adsl <- eg_adsl %>% + mutate(TRT01A = factor(TRT01A, levels = c("A: Drug X", "B: Placebo", "C: Combination"))) %>% + preprocess_t_dd() %>% + mutate(DISTRTFL = sample(c("Y", "N"), size = length(TRT01A), replace = TRUE, prob = c(.1, .9))) %>% + # preprocess_t_ds() %>% + mutate( + DTRFL = if_else(EOTSTT == "DISCONTINUED", "Y", "N"), + TRTSDT = as.Date(TRTSDTM) + ) +adsl$FASFL <- adsl$SAFFL +adsl_two_arm <- adsl %>% + dplyr::filter(TRT01A %in% c("A: Drug X", "B: Placebo")) %>% + mutate(TRT01A = factor(TRT01A, levels = c("A: Drug X", "B: Placebo"))) + +adae <- eg_adae %>% + mutate(TRT01A = factor(TRT01A, levels = c("A: Drug X", "B: Placebo", "C: Combination"))) +adae <- adae %>% mutate( + dis_flags = ifelse(AEACN == "DRUG WITHDRAWN", TRUE, FALSE), + red_flags = ifelse(AEACN == "DRUG REDUCED", TRUE, FALSE), + int_flags = ifelse(AEACN == "DRUG INTERRUPTED", TRUE, FALSE), + dis_flags = ifelse(AEACN == "DRUG WITHDRAWN", TRUE, FALSE), + red_flags = ifelse(AEACN == "DRUG REDUCED", TRUE, FALSE), + int_flags = ifelse(AEACN == "DRUG INTERRUPTED", TRUE, FALSE), + ATOXGR = AETOXGR +) + +# ADAE for AESEV grading +adae_aesev <- adae %>% + mutate(AESEV = as.factor(case_when( + AETOXGR %in% c("1", "2") ~ "MILD", + AETOXGR == "3" ~ "MODERATE", + AETOXGR %in% c("4", "5") ~ "SEVERE" + ))) + +# ADAE with custom grouping +adae_custom <- adae %>% + mutate(AEGRP = as.factor(case_when( + AETOXGR %in% c("1", "2") ~ "Grade 1-2", + AETOXGR %in% c("3", "4", "5") ~ "Grade 3-5" + ))) + +# ADAE for ATOXGR grading +adae_atoxgr <- adae %>% + mutate(ATOXGR = AETOXGR) + +adae_two_arm <- adae %>% + dplyr::filter(TRT01A %in% c("A: Drug X", "B: Placebo")) %>% + mutate(TRT01A = factor(TRT01A, levels = c("A: Drug X", "B: Placebo"))) + +advs <- eg_advs %>% + mutate(TRT01A = factor(TRT01A, levels = c("A: Drug X", "B: Placebo", "C: Combination"))) + +adrs <- eg_adrs %>% + mutate(TRT01A = factor(TRT01A, levels = c("A: Drug X", "B: Placebo", "C: Combination"))) %>% + dplyr::filter(PARAMCD == "INVET") + +adtte <- eg_adtte %>% + mutate(TRT01A = factor(TRT01A, levels = c("A: Drug X", "B: Placebo", "C: Combination"))) + +adlb <- eg_adlb %>% + mutate(TRT01A = factor(TRT01A, levels = c("A: Drug X", "B: Placebo", "C: Combination"))) + +adeg <- eg_adeg %>% + mutate(TRT01A = factor(TRT01A, levels = c("A: Drug X", "B: Placebo", "C: Combination"))) + +adex <- eg_adex +adex_tdosint <- adex %>% + filter(PARAM == "Total dose administered") %>% + mutate( + AVAL = ASEQ / max(ASEQ) * 100, + PARAMCD = "TDOSINT", + PARAM = "Fake dose intensity" + ) +adex_tdurd <- adex %>% + filter(PARAM == "Total dose administered") %>% + mutate( + AVAL = ASEQ, + PARAMCD = "TDURD", + PARAM = "Fake treatment duration" + ) +adex <- data.frame(Reduce("rbind", list(adex, adex_tdosint, adex_tdurd))) + +testdata <- list( + "adsl" = adsl, + "adae" = adae, + "adae_aesev" = adae_aesev, + "adae_atoxgr" = adae_atoxgr, + "adae_custom" = adae_custom, + "adtte" = adtte, + "adrs" = adrs, + "advs" = advs, + "adlb" = adlb, + "adeg" = adeg, + "adex" = adex +) + +testdata_two_arm <- list( + "adsl" = adsl_two_arm, + "adae" = adae_two_arm +) + + diff --git a/tests/testthat/test-data_snapshot.R b/tests/testthat/test-data_snapshot.R index 94375737..190d8308 100755 --- a/tests/testthat/test-data_snapshot.R +++ b/tests/testthat/test-data_snapshot.R @@ -1,5 +1,5 @@ test_that("Test data snapshot", { - eg_dataname <- data(package = "autoslider.core")$results[, "Item"] %>% .[grep("eg_", .)] + eg_dataname <- data(package = "autoslider.core")$results[, "Item"] %>% .[grep("eg_", .)] %>% sort for (datai in eg_dataname) { expect_snapshot(dim(get(datai))) expect_snapshot(head(get(datai))) diff --git a/tests/testthat/test-save-output.R b/tests/testthat/test-save-output.R deleted file mode 100755 index 85752e3d..00000000 --- a/tests/testthat/test-save-output.R +++ /dev/null @@ -1,10 +0,0 @@ -test_that("save an output", { - output <- t_ds_slide(adsl = testdata$adsl) %>% decorate(title = "test title", footnote = "") - testthat::expect_no_error( - save_output( - output, - file_name = "t_ds_output", - save_rds = TRUE - ) - ) -}) diff --git a/tests/testthat/test-save_output.R b/tests/testthat/test-save_output.R index 74c52c19..300e3a74 100755 --- a/tests/testthat/test-save_output.R +++ b/tests/testthat/test-save_output.R @@ -1,3 +1,15 @@ +test_that("save an output", { + output <- t_ds_slide(adsl = testdata$adsl) %>% decorate(title = "test title", footnote = "") + testthat::expect_no_error( + save_output( + output, + file_name = "t_ds_output", + save_rds = TRUE + ) + ) +}) + + test_that("Test save_output (Save an Output)", { library(dplyr) adsl <- eg_adsl %>%