Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incrementally initializing the test apps so it becomes easier to review the tests. #1132

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions tests/testthat/helper-TealAppDriver.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,18 @@ init_teal_app_driver <- function(...) {
.package = "teal"
)
}


# TODO: This will be moved to the `TealAppDriver` class.
active_module_tws_output <- function(app_driver, element = "table-table-with-settings", which = 1) {
app_driver$active_module_element(element) %>%
app_driver$get_html_rvest() %>%
rvest::html_table(fill = TRUE) %>%
{
if (identical(., list())) {
.
} else {
.[[which]]
}
}
}
77 changes: 77 additions & 0 deletions tests/testthat/test-shinytest2-tm_g_forest_tte copy.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
app_driver_tm_g_ipp <- function() {
data <- teal_data() %>%
within({
library(nestcolor)
library(dplyr)
ADSL <- tmc_ex_adsl %>%
slice(1:20) %>%
df_explicit_na()
ADLB <- tmc_ex_adlb %>%
filter(USUBJID %in% ADSL$USUBJID) %>%
df_explicit_na() %>%
filter(AVISIT != "SCREENING")
})

datanames <- c("ADSL", "ADLB")
teal.data::datanames(data) <- datanames
teal.data::join_keys(data) <- teal.data::default_cdisc_join_keys[datanames]

init_teal_app_driver(
data = data,
modules = teal::modules(
tm_g_ipp(
label = "Individual Patient Plot",
dataname = "ADLB",
arm_var = teal.transform::choices_selected(
teal.transform::value_choices(data[["ADLB"]], "ARMCD"),
"ARM A"
),
paramcd = teal.transform::choices_selected(
teal.transform::value_choices(data[["ADLB"]], "PARAMCD"),
"ALT"
),
aval_var = teal.transform::choices_selected(
teal.transform::variable_choices(data[["ADLB"]], c("AVAL", "CHG")),
"AVAL"
),
avalu_var = teal.transform::choices_selected(
teal.transform::variable_choices(data[["ADLB"]], c("AVALU")),
"AVALU",
fixed = TRUE
),
id_var = teal.transform::choices_selected(
teal.transform::variable_choices(data[["ADLB"]], c("USUBJID")),
"USUBJID",
fixed = TRUE
),
visit_var = teal.transform::choices_selected(
teal.transform::variable_choices(data[["ADLB"]], c("AVISIT")),
"AVISIT"
),
baseline_var = teal.transform::choices_selected(
teal.transform::variable_choices(data[["ADLB"]], c("BASE")),
"BASE",
fixed = TRUE
),
add_baseline_hline = FALSE,
separate_by_obs = FALSE
)
)
)
}

testthat::test_that("e2e - tm_g_ipp: Module initializes in teal without errors and produces plot output.", {
skip_if_too_deep(5)
app_driver <- app_driver_tm_g_ipp()
app_driver$expect_no_shiny_error()
app_driver$expect_no_validation_error()

testthat::expect_match(
app_driver$get_attr(
app_driver$active_module_element("myplot-plot_main > img"),
"src"
),
"data:image/png;base64,"
)
app_driver$stop()
})
69 changes: 69 additions & 0 deletions tests/testthat/test-shinytest2-tm_g_forest_tte.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
app_driver_tm_g_forest_tte <- function() {
data <- teal.data::teal_data() %>%
within({
library(nestcolor)
library(formatters)

ADSL <- tmc_ex_adsl
ADTTE <- tmc_ex_adtte
ADSL$RACE <- droplevels(ADSL$RACE) %>%
with_label("Race")
})
datanames <- c("ADSL", "ADTTE")
teal.data::datanames(data) <- datanames
teal.data::join_keys(data) <- teal.data::default_cdisc_join_keys[datanames]

arm_ref_comp <- list(
ARM = list(
ref = "B: Placebo",
comp = c("A: Drug X", "C: Combination")
),
ARMCD = list(
ref = "ARM B",
comp = c("ARM A", "ARM C")
)
)

init_teal_app_driver(
data = data,
modules = teal::modules(
tm_g_forest_tte(
label = "Forest Survival",
dataname = "ADTTE",
arm_var = teal.transform::choices_selected(
teal.transform::variable_choices(data[["ADSL"]], c("ARM", "ARMCD")),
"ARMCD"
),
arm_ref_comp = arm_ref_comp,
paramcd = teal.transform::choices_selected(
teal.transform::value_choices(data[["ADTTE"]], "PARAMCD", "PARAM"),
"OS"
),
subgroup_var = teal.transform::choices_selected(
teal.transform::variable_choices(data[["ADSL"]], names(data[["ADSL"]])),
c("BMRKR2", "SEX")
),
strata_var = teal.transform::choices_selected(
teal.transform::variable_choices(data[["ADSL"]], c("STRATA1", "STRATA2")),
"STRATA2"
)
)
)
)
}

testthat::test_that("e2e - tm_g_forest_tte: Module initializes in teal without errors and produces plot output.", {
skip_if_too_deep(5)
app_driver <- app_driver_tm_g_forest_tte()
app_driver$expect_no_shiny_error()
app_driver$expect_no_validation_error()

testthat::expect_match(
app_driver$get_attr(
app_driver$active_module_element("myplot-plot_main > img"),
"src"
),
"data:image/png;base64,"
)
app_driver$stop()
})
65 changes: 65 additions & 0 deletions tests/testthat/test-shinytest2-tm_g_km.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
app_driver_tm_g_km <- function() {
data <- teal.data::teal_data() %>%
within({
ADSL <- tmc_ex_adsl
ADTTE <- tmc_ex_adtte
})

datanames <- c("ADSL", "ADTTE")
teal.data::datanames(data) <- datanames
teal.data::join_keys(data) <- teal.data::default_cdisc_join_keys[datanames]

arm_ref_comp <- list(
ACTARMCD = list(
ref = "ARM B",
comp = c("ARM A", "ARM C")
),
ARM = list(
ref = "B: Placebo",
comp = c("A: Drug X", "C: Combination")
)
)

init_teal_app_driver(
data = data,
modules = teal::modules(
tm_g_km(
label = "Kaplan-Meier Plot",
dataname = "ADTTE",
arm_var = teal.transform::choices_selected(
teal.transform::variable_choices(data[["ADSL"]], c("ARM", "ARMCD", "ACTARMCD")),
"ARM"
),
paramcd = teal.transform::choices_selected(
teal.transform::value_choices(data[["ADTTE"]], "PARAMCD", "PARAM"),
"OS"
),
arm_ref_comp = arm_ref_comp,
strata_var = teal.transform::choices_selected(
teal.transform::variable_choices(data[["ADSL"]], c("SEX", "BMRKR2")),
"SEX"
),
facet_var = teal.transform::choices_selected(
teal.transform::variable_choices(data[["ADSL"]], c("SEX", "BMRKR2")),
NULL
)
)
)
)
}

testthat::test_that("e2e - tm_g_km: Module initializes in teal without errors and produces plot output.", {
skip_if_too_deep(5)
app_driver <- app_driver_tm_g_km()
app_driver$expect_no_shiny_error()
app_driver$expect_no_validation_error()

testthat::expect_match(
app_driver$get_attr(
app_driver$active_module_element("myplot-plot_main > img"),
"src"
),
"data:image/png;base64,"
)
app_driver$stop()
})
53 changes: 53 additions & 0 deletions tests/testthat/test-shinytest2-tm_g_lineplot.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
app_driver_tm_g_lineplot <- function() {
data <- teal.data::teal_data() %>%
within({
library(dplyr)
library(forcats)

ADSL <- tmc_ex_adsl
ADLB <- tmc_ex_adlb %>%
mutate(AVISIT == fct_reorder(AVISIT, AVISITN, min))
})

datanames <- c("ADSL", "ADLB")
teal.data::datanames(data) <- datanames
teal.data::join_keys(data) <- teal.data::default_cdisc_join_keys[datanames]

init_teal_app_driver(
data = data,
modules = teal::modules(
tm_g_lineplot(
label = "Line Plot",
dataname = "ADLB",
strata = teal.transform::choices_selected(
teal.transform::variable_choices(data[["ADSL"]], c("ARM", "ARMCD", "ACTARMCD")),
"ARM"
),
y = teal.transform::choices_selected(
teal.transform::variable_choices(data[["ADLB"]], c("AVAL", "BASE", "CHG", "PCHG")),
"AVAL"
),
param = teal.transform::choices_selected(
teal.transform::value_choices(data[["ADLB"]], "PARAMCD", "PARAM"),
"ALT"
)
)
)
)
}

testthat::test_that("e2e - tm_g_lineplot: Module initializes in teal without errors and produces plot output.", {
skip_if_too_deep(5)
app_driver <- app_driver_tm_g_lineplot()
app_driver$expect_no_shiny_error()
app_driver$expect_no_validation_error()

testthat::expect_match(
app_driver$get_attr(
app_driver$active_module_element("myplot-plot_main > img"),
"src"
),
"data:image/png;base64,"
)
app_driver$stop()
})
70 changes: 70 additions & 0 deletions tests/testthat/test-shinytest2-tm_g_pp_adverse_events.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
app_driver_tm_g_pp_adverse_events <- function() { # nolint: object_length
data <- teal.data::teal_data() %>%
within({
library(dplyr)
ADAE <- tmc_ex_adae
ADSL <- tmc_ex_adsl %>%
filter(USUBJID %in% ADAE$USUBJID)
})

datanames <- c("ADSL", "ADAE")
teal.data::datanames(data) <- datanames
teal.data::join_keys(data) <- teal.data::default_cdisc_join_keys[datanames]

init_teal_app_driver(
data = data,
modules = teal::modules(
tm_g_pp_adverse_events(
label = "Adverse Events",
dataname = "ADAE",
parentname = "ADSL",
patient_col = "USUBJID",
plot_height = c(600L, 200L, 2000L),
aeterm = teal.transform::choices_selected(
choices = teal.transform::variable_choices(data[["ADAE"]], "AETERM"),
selected = "AETERM"
),
tox_grade = teal.transform::choices_selected(
choices = teal.transform::variable_choices(data[["ADAE"]], "AETOXGR"),
selected = "AETOXGR"
),
causality = teal.transform::choices_selected(
choices = teal.transform::variable_choices(data[["ADAE"]], "AEREL"),
selected = "AEREL"
),
outcome = teal.transform::choices_selected(
choices = teal.transform::variable_choices(data[["ADAE"]], "AEOUT"),
selected = "AEOUT"
),
action = teal.transform::choices_selected(
choices = teal.transform::variable_choices(data[["ADAE"]], "AEACN"),
selected = "AEACN"
),
time = teal.transform::choices_selected(
choices = teal.transform::variable_choices(data[["ADAE"]], "ASTDY"),
selected = "ASTDY"
),
decod = NULL
)
)
)
}

testthat::test_that(
"e2e - tm_g_pp_adverse_events: Module initializes in teal without errors and produces plot output.",
{
skip_if_too_deep(5)
app_driver <- app_driver_tm_g_pp_adverse_events()
app_driver$expect_no_shiny_error()
app_driver$expect_no_validation_error()

testthat::expect_match(
app_driver$get_attr(
app_driver$active_module_element("myplot-plot_main > img"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is stupid but ui_g_adverse_events uses teal.widgets::plot_with_settings_ui(id = ns("chart"))

Suggested change
app_driver$active_module_element("myplot-plot_main > img"),
app_driver$active_module_element("chart-plot_main > img"),

Checkout the discussion about discrepancy in plot namespace name in here : ) insightsengineering/teal#1208 (comment)

"src"
),
"data:image/png;base64,"
)
app_driver$stop()
}
)
19 changes: 19 additions & 0 deletions tests/testthat/test-shinytest2-tm_g_pp_patient_timeline.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
app_driver_tm_g_km <- function() {

}

testthat::test_that("e2e - tm_g_km: Module initializes in teal without errors and produces plot output.", {
skip_if_too_deep(5)
app_driver <- app_driver_tm_g_km()
app_driver$expect_no_shiny_error()
app_driver$expect_no_validation_error()

testthat::expect_match(
app_driver$get_attr(
app_driver$active_module_element("myplot-plot_main > img"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same reasoning as here https://github.com/insightsengineering/teal.modules.clinical/pull/1132/files#r1576318046

IMHO it would be better if we can go module by module, so we can focus on one module and catch such things.

Suggested change
app_driver$active_module_element("myplot-plot_main > img"),
app_driver$active_module_element("patient_timeline_plot-plot_main > img"),

"src"
),
"data:image/png;base64,"
)
app_driver$stop()
})
Loading
Loading