From d21214fb4b3c7186a326b1d684ba24ea457bfa92 Mon Sep 17 00:00:00 2001 From: "joe.zhu" Date: Thu, 5 Dec 2024 16:31:08 +0800 Subject: [PATCH] addin gnoew snapshot --- tests/testthat/_snaps/filter_spec-verbose.md | 6 +- tests/testthat/_snaps/t_ds_slide.md | 96 ++++++++++++++++ tests/testthat/setup.R | 2 +- tests/testthat/test-filter_spec-verbose.R | 5 +- tests/testthat/test-g_mean_slides.R | 115 +++++++++++++++++++ tests/testthat/test-t_ds_slide.R | 16 +++ 6 files changed, 234 insertions(+), 6 deletions(-) create mode 100644 tests/testthat/_snaps/t_ds_slide.md create mode 100644 tests/testthat/test-g_mean_slides.R create mode 100644 tests/testthat/test-t_ds_slide.R diff --git a/tests/testthat/_snaps/filter_spec-verbose.md b/tests/testthat/_snaps/filter_spec-verbose.md index 95710d10..6801fa80 100644 --- a/tests/testthat/_snaps/filter_spec-verbose.md +++ b/tests/testthat/_snaps/filter_spec-verbose.md @@ -1,10 +1,10 @@ # Listing print correctly Code - full_spec %>% filter_spec(., program %in% c("t_ds_slide", "t_ds_trt_slide"), - verbose = TRUE) + full_spec %>% filter_spec(., program %in% c("t_ds_slide", "t_ds_trt_slide", + "i_am_wrong"), verbose = TRUE) Output - v 2/47 outputs matched the filter condition `program %in% c("t_ds_slide", "t_ds_trt_slide")`. + v 2/47 outputs matched the filter condition `program %in% c("t_ds_slide", "t_ds_trt_slide", "i_am_wrong")`. $t_ds_slide_FAS $t_ds_slide_FAS$program [1] "t_ds_slide" diff --git a/tests/testthat/_snaps/t_ds_slide.md b/tests/testthat/_snaps/t_ds_slide.md new file mode 100644 index 00000000..d6477a15 --- /dev/null +++ b/tests/testthat/_snaps/t_ds_slide.md @@ -0,0 +1,96 @@ +# Test table creation of t_ds_slide + + Code + t_ds_slide(adsl = testdata$adsl) + Output + Discontinue table + + ———————————————————————————————————————————————————————————————————————————————————————————————— + A: Drug X B: Placebo C: Combination All Patients + ———————————————————————————————————————————————————————————————————————————————————————————————— + Received Treatment 134 (100.00%) 134 (100.00%) 132 (100.00%) 400 (100.00%) + On-study Status 3 (2.2%) 2 (1.5%) 4 (3.0%) 9 (2.2%) + On Treatment 0 0 0 0 + In Follow-up 3 (2.2%) 2 (1.5%) 4 (3%) 9 (2.2%) + Discontinued the study 42 (31.3%) 40 (29.9%) 38 (28.8%) 120 (30.0%) + Adverse Event 3 (2.2%) 6 (4.5%) 5 (3.8%) 14 (3.5%) + Death 25 (18.7%) 23 (17.2%) 22 (16.7%) 70 (17.5%) + Lack Of Efficacy 2 (1.5%) 2 (1.5%) 3 (2.3%) 7 (1.8%) + Physician Decision 2 (1.5%) 3 (2.2%) 2 (1.5%) 7 (1.8%) + Protocol Violation 5 (3.7%) 3 (2.2%) 4 (3%) 12 (3%) + Withdrawal By Parent/Guardian 4 (3%) 2 (1.5%) 1 (0.8%) 7 (1.8%) + Withdrawal By Subject 1 (0.7%) 1 (0.7%) 1 (0.8%) 3 (0.8%) + +--- + + Code + t_ds_slide(adsl = testdata$adsl, side_by_side = TRUE) + Output + Discontinue table + + ———————————————————————————————————————————————————————————————————————————————————————————————— + A: Drug X B: Placebo C: Combination All Patients + ———————————————————————————————————————————————————————————————————————————————————————————————— + Received Treatment 134 (100.00%) 134 (100.00%) 132 (100.00%) 400 (100.00%) + On-study Status 3 (2.2%) 2 (1.5%) 4 (3.0%) 9 (2.2%) + On Treatment 0 0 0 0 + In Follow-up 3 (2.2%) 2 (1.5%) 4 (3%) 9 (2.2%) + Discontinued the study 42 (31.3%) 40 (29.9%) 38 (28.8%) 120 (30.0%) + Adverse Event 3 (2.2%) 6 (4.5%) 5 (3.8%) 14 (3.5%) + Death 25 (18.7%) 23 (17.2%) 22 (16.7%) 70 (17.5%) + Lack Of Efficacy 2 (1.5%) 2 (1.5%) 3 (2.3%) 7 (1.8%) + Physician Decision 2 (1.5%) 3 (2.2%) 2 (1.5%) 7 (1.8%) + Protocol Violation 5 (3.7%) 3 (2.2%) 4 (3%) 12 (3%) + Withdrawal By Parent/Guardian 4 (3%) 2 (1.5%) 1 (0.8%) 7 (1.8%) + Withdrawal By Subject 1 (0.7%) 1 (0.7%) 1 (0.8%) 3 (0.8%) + +--- + + Code + t_ds_slide(adsl = testdata$adsl, split_by_study = TRUE) + Output + Discontinue table + + ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————— + AB12345-1 AB12345-2 + A: Drug X B: Placebo C: Combination A: Drug X B: Placebo C: Combination + ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————— + Received Treatment 62 (100.00%) 74 (100.00%) 64 (100.00%) 72 (100.00%) 60 (100.00%) 68 (100.00%) + On-study Status 2 (3.2%) 0 (0.0%) 1 (1.6%) 1 (1.4%) 2 (3.3%) 3 (4.4%) + On Treatment 0 0 0 0 0 0 + In Follow-up 2 (3.2%) 0 1 (1.6%) 1 (1.4%) 2 (3.3%) 3 (4.4%) + Discontinued the study 19 (30.6%) 24 (32.4%) 15 (23.4%) 23 (31.9%) 16 (26.7%) 23 (33.8%) + Adverse Event 2 (3.2%) 4 (5.4%) 4 (6.2%) 1 (1.4%) 2 (3.3%) 1 (1.5%) + Death 12 (19.4%) 13 (17.6%) 7 (10.9%) 13 (18.1%) 10 (16.7%) 15 (22.1%) + Lack Of Efficacy 1 (1.6%) 2 (2.7%) 0 1 (1.4%) 0 3 (4.4%) + Physician Decision 2 (3.2%) 2 (2.7%) 0 0 1 (1.7%) 2 (2.9%) + Protocol Violation 1 (1.6%) 2 (2.7%) 2 (3.1%) 4 (5.6%) 1 (1.7%) 2 (2.9%) + Withdrawal By Parent/Guardian 1 (1.6%) 1 (1.4%) 1 (1.6%) 3 (4.2%) 1 (1.7%) 0 + Withdrawal By Subject 0 0 1 (1.6%) 1 (1.4%) 1 (1.7%) 0 + +--- + + Code + t_ds_slide(adsl = testdata$adsl, split_by_study = TRUE, side_by_side = TRUE) + Condition + Warning in `build_table_header()`: + split_by_study argument will be ignored + Output + Discontinue table + + ———————————————————————————————————————————————————————————————————————————————————————————————— + A: Drug X B: Placebo C: Combination All Patients + ———————————————————————————————————————————————————————————————————————————————————————————————— + Received Treatment 134 (100.00%) 134 (100.00%) 132 (100.00%) 400 (100.00%) + On-study Status 3 (2.2%) 2 (1.5%) 4 (3.0%) 9 (2.2%) + On Treatment 0 0 0 0 + In Follow-up 3 (2.2%) 2 (1.5%) 4 (3%) 9 (2.2%) + Discontinued the study 42 (31.3%) 40 (29.9%) 38 (28.8%) 120 (30.0%) + Adverse Event 3 (2.2%) 6 (4.5%) 5 (3.8%) 14 (3.5%) + Death 25 (18.7%) 23 (17.2%) 22 (16.7%) 70 (17.5%) + Lack Of Efficacy 2 (1.5%) 2 (1.5%) 3 (2.3%) 7 (1.8%) + Physician Decision 2 (1.5%) 3 (2.2%) 2 (1.5%) 7 (1.8%) + Protocol Violation 5 (3.7%) 3 (2.2%) 4 (3%) 12 (3%) + Withdrawal By Parent/Guardian 4 (3%) 2 (1.5%) 1 (0.8%) 7 (1.8%) + Withdrawal By Subject 1 (0.7%) 1 (0.7%) 1 (0.8%) 3 (0.8%) + diff --git a/tests/testthat/setup.R b/tests/testthat/setup.R index f38b5f48..3175426a 100755 --- a/tests/testthat/setup.R +++ b/tests/testthat/setup.R @@ -47,7 +47,7 @@ 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() %>% + preprocess_t_ds() %>% mutate( DTRFL = if_else(EOTSTT == "DISCONTINUED", "Y", "N"), TRTSDT = as.Date(TRTSDTM) diff --git a/tests/testthat/test-filter_spec-verbose.R b/tests/testthat/test-filter_spec-verbose.R index 22268248..2c15fadc 100644 --- a/tests/testthat/test-filter_spec-verbose.R +++ b/tests/testthat/test-filter_spec-verbose.R @@ -9,9 +9,10 @@ test_that("Listing print correctly", { full_spec <- spec_file %>% read_spec() - expect_snapshot(full_spec %>% + testthat::expect_snapshot(full_spec %>% filter_spec(., program %in% c( "t_ds_slide", - "t_ds_trt_slide" + "t_ds_trt_slide", + "i_am_wrong" ), verbose = TRUE)) }) diff --git a/tests/testthat/test-g_mean_slides.R b/tests/testthat/test-g_mean_slides.R new file mode 100644 index 00000000..9010393b --- /dev/null +++ b/tests/testthat/test-g_mean_slides.R @@ -0,0 +1,115 @@ +# data setup +library(dplyr) +library(ggplot2) +library(svglite) + +adeg <- testdata$adeg %>% filter(PARAMCD == "QT") +advs <- testdata$advs %>% filter(PARAMCD == "WEIGHT") +adlb <- testdata$adlb %>% filter(PARAMCD == "IGA") + +# tests +path_to_test <- "_snaps" + +## adeg +## Test 1: Test graph creation of `g_eg_slide` (eg mean plot) ---- +test_that("g_mean_slides Test 1: Test graph creation of `g_eg_slide` (eg mean plot)", { + testthat::expect_silent(geg <- withr::with_options( + opts_partial_match_old, + g_eg_slide(testdata$adsl, adeg, arm = "TRT01P", paramcd = "PARAMCD") + )) + + expect_snapshot_ggplot(title = "g_eg_test", geg, width = 10, height = 8) +}) + + +## Test 2: Test error of `g_eg_slide` ---- +test_that("g_mean_slides Test 2: Test error of `g_eg_slide`", { + testthat::expect_error(g_eg_slide(testdata$adsl, adeg, arm = TRT01P)) +}) + +## adlb +## Test 3: Test graph creation of `g_lb_slide` (lab mean plot) ---- +test_that("g_mean_slides Test 3: Test graph creation of `g_lb_slide` (lab mean plot)", { + testthat::expect_silent(glb <- withr::with_options( + opts_partial_match_old, + g_lb_slide(testdata$adsl, adlb, arm = "TRT01P", paramcd = "PARAMCD") + )) + + expect_snapshot_ggplot(title = "g_lb_test", glb, width = 10, height = 8) +}) + + +## Test 4: Test graph creation of `g_lb_slide` (lab change from baseline plot) ---- +test_that("g_mean_slides Test 4: Test graph creation of `g_lb_slide` (lab change from baseline plot)", { + testthat::expect_silent(plot_lb_chg <- withr::with_options( + opts_partial_match_old, + g_lb_slide( + testdata$adsl, adlb, + arm = "TRT01P", + paramcd = "PARAMCD", + y = "CHG", + subtitle = "Plot of change from baseline and 95% Confidence Limit by Visit." + ) + )) + + expect_snapshot_ggplot(title = "g_lb_chg_test", plot_lb_chg, width = 10, height = 8) +}) + + + +## Test 5: Test error of `g_lb_slide` ---- +test_that("g_mean_slides Test 5: Test error of `g_lb_slide`", { + expect_error(g_lb_slide(testdata$adsl, adlb, arm = TRT01P)) +}) + +## advs +## Test 6: Test graph creation of `g_vs_slide` (vital sign mean plot) ---- +test_that("g_mean_slides Test 6: Test graph creation of `g_vs_slide` (vital sign mean plot)", { + testthat::expect_silent(gvs <- withr::with_options( + opts_partial_match_old, + g_vs_slide(testdata$adsl, advs, arm = "TRT01P", paramcd = "PARAMCD") + )) + + expect_snapshot_ggplot(title = "g_vs_test", gvs, width = 10, height = 8) +}) + +## Test 7: Test error of `g_vs_slide` ---- +test_that("g_mean_slides Test 7: Test error of `g_vs_slide`", { + expect_error(g_vs_slide(testdata$adsl, advs, arm = TRT01P)) +}) + +## general +## Test 8: Test graph creation of `g_mean_slide` (vital sign mean plot) ---- +test_that("g_mean_slides Test 8: Test graph creation of `g_mean_slide` (vital sign mean plot)", { + testthat::expect_silent(gm1 <- withr::with_options( + opts_partial_match_old, + g_mean_general(testdata$adsl, + adeg, + variables = tern::control_lineplot_vars( + group_var = "TRT01P", + paramcd = "PARAMCD" + ) + ) + )) + + expect_snapshot_ggplot(title = "g_mean_general_test", gm1, width = 10, height = 8) + + testthat::expect_silent(gm2 <- withr::with_options( + opts_partial_match_old, + g_mean_general(testdata$adsl, + adeg, + variables = tern::control_lineplot_vars( + group_var = "TRT01P", + paramcd = "PARAMCD" + ), + subtitle_add_unit = FALSE + ) + )) + + expect_snapshot_ggplot(title = "g_mean_nounit_test", gm2, width = 10, height = 8) +}) + +## Test 9: Test error of general function `g_mean_slide` ---- +test_that("g_mean_slides Test 9: Test error of general function `g_mean_slide`", { + expect_error(g_eg_slide(testdata$adsl, adeg, arm = TRT01P)) +}) diff --git a/tests/testthat/test-t_ds_slide.R b/tests/testthat/test-t_ds_slide.R new file mode 100644 index 00000000..e4e0279e --- /dev/null +++ b/tests/testthat/test-t_ds_slide.R @@ -0,0 +1,16 @@ +test_that("Test table creation of t_ds_slide", { + expect_snapshot(t_ds_slide(adsl = testdata$adsl)) +}) + +test_that("Test table creation of t_ds_slide", { + expect_snapshot(t_ds_slide(adsl = testdata$adsl, side_by_side = TRUE)) +}) + +test_that("Test table creation of t_ds_slide", { + expect_snapshot(t_ds_slide(adsl = testdata$adsl, split_by_study = TRUE)) +}) + +test_that("Test table creation of t_ds_slide", { + expect_snapshot(t_ds_slide(adsl = testdata$adsl, split_by_study = TRUE, side_by_side = TRUE)) + expect_warning(t_ds_slide(adsl = testdata$adsl, split_by_study = TRUE, side_by_side = TRUE)) +})