Skip to content

Commit

Permalink
addin tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shajoezhu committed Nov 13, 2024
1 parent 6201959 commit f74b30d
Show file tree
Hide file tree
Showing 8 changed files with 386 additions and 11 deletions.
8 changes: 8 additions & 0 deletions tests/testthat/_snaps/t_dd_slide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Test t_dd_slide table with expected Error

Code
t_dd_slide(adsl, "TRT")
Condition
Error:
! object 'adsl' not found

177 changes: 177 additions & 0 deletions tests/testthat/_snaps/t_dm_slide.md

Large diffs are not rendered by default.

30 changes: 30 additions & 0 deletions tests/testthat/_snaps/t_dor_slide.md
Original file line number Diff line number Diff line change
@@ -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)

11 changes: 11 additions & 0 deletions tests/testthat/setup-options.R
Original file line number Diff line number Diff line change
@@ -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
)
147 changes: 147 additions & 0 deletions tests/testthat/setup.R
Original file line number Diff line number Diff line change
@@ -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
)

Check warning on line 146 in tests/testthat/setup.R

View workflow job for this annotation

GitHub Actions / SuperLinter 🦸‍♀️ / Lint R code 🧶

file=tests/testthat/setup.R,line=146,col=1,[trailing_blank_lines_linter] Trailing blank lines are superfluous.

Check warning on line 147 in tests/testthat/setup.R

View workflow job for this annotation

GitHub Actions / SuperLinter 🦸‍♀️ / Lint R code 🧶

file=tests/testthat/setup.R,line=147,col=1,[trailing_blank_lines_linter] Trailing blank lines are superfluous.
2 changes: 1 addition & 1 deletion tests/testthat/test-data_snapshot.R
Original file line number Diff line number Diff line change
@@ -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)))
Expand Down
10 changes: 0 additions & 10 deletions tests/testthat/test-save-output.R

This file was deleted.

12 changes: 12 additions & 0 deletions tests/testthat/test-save_output.R
Original file line number Diff line number Diff line change
@@ -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 %>%
Expand Down

0 comments on commit f74b30d

Please sign in to comment.