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

Wave 1 - tm_g_barchart_simple shinytest2 #1134

Merged
Merged
Changes from 18 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
54a82bb
feat: initial support with default values
averissimo Apr 24, 2024
e560edf
fix: adds support for all the data extract inputs
averissimo Apr 24, 2024
10984ac
fix: adds prefix for teal.transform functions
averissimo Apr 24, 2024
438655f
fix: typo and generates test title with correct id
averissimo Apr 24, 2024
853685e
fix: remove duplicate test
averissimo Apr 24, 2024
147ab9a
feat: split tests into 3
averissimo Apr 24, 2024
c34a7ef
chore: remove comment
averissimo Apr 24, 2024
821e52b
feat: adds test for each of the plot settings
averissimo Apr 24, 2024
9fd4a22
fix: remove expression
averissimo Apr 24, 2024
2f871ec
fix: linter problems
averissimo Apr 24, 2024
b1f6d73
fix: break line
averissimo Apr 24, 2024
b572245
fix: manual test as it requires other options
averissimo Apr 24, 2024
1014eab
fix: minor bugs
averissimo Apr 24, 2024
bd89f1a
fix: setup for rotate_bar_labels needed changing
averissimo Apr 24, 2024
56d41cd
feat: one last simplifcation of plot_settings
averissimo Apr 24, 2024
f58a473
fix: remove extra code
averissimo Apr 24, 2024
48c09b0
Update tests/testthat/test-shinytest2-tm_g_barchart_simple.R
averissimo Apr 25, 2024
dd23353
Merge branch 'shinytest2@main' into 1108_shinytest2_tm_g_barchart_sim…
averissimo Apr 25, 2024
840264a
fix: rename parameter from id to input_id
averissimo Apr 25, 2024
ee1e7df
Apply suggestions from @kartikeyakirar
averissimo Apr 25, 2024
4321501
fix: rename id to input_id and add new expectation
averissimo Apr 25, 2024
a184d41
fix: rename id to input_id
averissimo Apr 25, 2024
0ef09b4
fix: shinytest2 doesn't have to wait for a UI change
averissimo Apr 25, 2024
cf654ee
fix: linter error
averissimo Apr 25, 2024
f4f4c40
feat: adds testing depth to check shinytest2
averissimo Apr 25, 2024
5297ea9
fix: add rvest to dependencies
averissimo Apr 25, 2024
107713f
fix: typo
averissimo Apr 25, 2024
81e06e6
Merge branch 'shinytest2@main' into 1108_shinytest2_tm_g_barchart_sim…
averissimo Apr 25, 2024
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
346 changes: 346 additions & 0 deletions tests/testthat/test-shinytest2-tm_g_barchart_simple.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,346 @@
app_driver_tm_g_barchart_simple <- function() { # nolint: object_length.
data <- within(teal.data::teal_data(), {
ADSL <- dplyr::mutate(
teal.modules.clinical::tmc_ex_adsl,
ITTFL = with_label(factor("Y"), "Intent-To-Treat Population Flag")
)

ADAE <- dplyr::filter(
teal.modules.clinical::tmc_ex_adae,
!((AETOXGR == 1) & (AESEV == "MILD") & (ARM == "A: Drug X"))
)
})

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 = tm_g_barchart_simple(
label = "ADAE Analysis (e2e)",
x = teal.transform::data_extract_spec(
dataname = "ADSL",
select = teal.transform::select_spec(
choices = teal.transform::variable_choices(
"ADSL", c("ARM", "ACTARM", "SEX", "RACE", "ITTFL", "SAFFL", "STRATA2")
),
selected = "ACTARM",
multiple = FALSE
)
),
fill = list(
teal.transform::data_extract_spec(
dataname = "ADSL",
select = teal.transform::select_spec(
choices = teal.transform::variable_choices(
"ADSL", c("ARM", "ACTARM", "SEX", "RACE", "ITTFL", "SAFFL", "STRATA2")
),
selected = "SEX",
multiple = FALSE
)
),
teal.transform::data_extract_spec(
dataname = "ADAE",
select = teal.transform::select_spec(
choices = teal.transform::variable_choices("ADAE", c("AETOXGR", "AESEV", "AESER")),
selected = NULL,
multiple = FALSE
)
)
),
x_facet = list(
teal.transform::data_extract_spec(
dataname = "ADAE",
select = teal.transform::select_spec(
choices = teal.transform::variable_choices("ADAE", c("AETOXGR", "AESEV", "AESER")),
selected = "AETOXGR",
multiple = FALSE
)
),
teal.transform::data_extract_spec(
dataname = "ADSL",
select = teal.transform::select_spec(
choices = teal.transform::variable_choices(
"ADSL",
c("ARM", "ACTARM", "SEX", "RACE", "ITTFL", "SAFFL", "STRATA2")
),
selected = NULL,
multiple = FALSE
)
)
),
y_facet = list(
data_extract_spec(
dataname = "ADAE",
select = teal.transform::select_spec(
choices = teal.transform::variable_choices(
"ADAE",
c("AETOXGR", "AESEV", "AESER")
),
selected = "AESEV",
multiple = FALSE
)
),
data_extract_spec(
dataname = "ADSL",
select = teal.transform::select_spec(
choices = teal.transform::variable_choices(
"ADSL",
c("ARM", "ACTARM", "SEX", "RACE", "ITTFL", "SAFFL", "STRATA2")
),
selected = NULL,
multiple = FALSE
)
)
),
plot_options = list(
stacked = TRUE,
label_bars = FALSE,
rotate_bar_labels = TRUE,
rotate_x_label = TRUE,
rotate_y_label = TRUE,
flip_axis = TRUE,
show_n = FALSE
)
averissimo marked this conversation as resolved.
Show resolved Hide resolved
)
)
}

ns_dataset <- function(prefix, suffix, dataset, extract = "singleextract") {
kartikeyakirar marked this conversation as resolved.
Show resolved Hide resolved
sprintf("%s-dataset_%s_%s-%s", prefix, dataset, extract, suffix)
}

# Initialization --------------------------------------------------------------

testthat::test_that("e2e - tm_g_barchart_simple: Module initializes in teal without errors and produces output.", {
skip_if_too_deep(5)

app_driver <- app_driver_tm_g_barchart_simple()
app_driver$expect_no_shiny_error()
app_driver$expect_no_validation_error()
testthat::expect_true(
app_driver$is_visible(app_driver$active_module_element("myplot-plot_out_main"))
)

testthat::expect_true(app_driver$is_visible(app_driver$active_module_element("table")))

app_driver$stop()
})

testthat::test_that(
paste0(
"e2e - tm_g_barchart_simple: Starts with specified ",
"label, id_var, arm_var, visit_var, paramcd, cov_var, conf_level and conf_struct."
),
{
skip_if_too_deep(5)
app_driver <- app_driver_tm_g_barchart_simple()

testthat::expect_equal(
trimws(app_driver$get_text("#teal-main_ui-root-active_tab > li.active")),
"ADAE Analysis (e2e)"
)

testthat::expect_equal(
app_driver$get_active_module_input(ns_dataset("x", "select", "ADSL")),
"ACTARM"
)

testthat::expect_equal(app_driver$get_active_module_input("fill-dataset"), "ADSL")

testthat::expect_equal(
app_driver$get_active_module_input(ns_dataset("fill", "select", "ADSL")),
"SEX"
)

testthat::expect_equal(
app_driver$get_active_module_input("x_facet-dataset"),
"ADAE"
)

testthat::expect_equal(
app_driver$get_active_module_input(ns_dataset("x_facet", "select", "ADAE")),
"AETOXGR"
)

testthat::expect_equal(
app_driver$get_active_module_input("y_facet-dataset"),
"ADAE"
)

testthat::expect_equal(
app_driver$get_active_module_input(ns_dataset("y_facet", "select", "ADAE")),
"AESEV"
)

# Plot settings -----------------------------------------------------------
# only tests the options that are customizable

testthat::expect_equal(app_driver$get_active_module_input("barlayout"), "stacked")
testthat::expect_equal(app_driver$get_active_module_input("label_bars"), FALSE)
testthat::expect_equal(app_driver$get_active_module_input("rotate_bar_labels"), TRUE)
testthat::expect_equal(app_driver$get_active_module_input("rotate_x_label"), TRUE)
testthat::expect_equal(app_driver$get_active_module_input("rotate_y_label"), TRUE)
testthat::expect_equal(app_driver$get_active_module_input("flip_axis"), TRUE)
testthat::expect_equal(app_driver$get_active_module_input("show_n"), FALSE)
averissimo marked this conversation as resolved.
Show resolved Hide resolved

app_driver$stop()
}
)

# X-variable ------------------------------------------------------------------

testthat::test_that(
"e2e - tm_g_barchart_simple: Selection of 'x' changes the element and does not throw validation errors",
{
skip_if_too_deep(5)
app_driver <- app_driver_tm_g_barchart_simple()
plot_before <- app_driver$get_active_module_pws_output("myplot")
app_driver$set_active_module_input(ns_dataset("x", "select", "ADSL"), "RACE")
testthat::expect_false(identical(plot_before, app_driver$get_active_module_pws_output("myplot")))
app_driver$expect_no_validation_error()
app_driver$stop()
}
)

testthat::test_that("e2e - tm_g_barchart_simple: Deselection of 'x' throws validation error.", {
skip_if_too_deep(5)
app_driver <- app_driver_tm_g_barchart_simple()
app_driver$set_active_module_input(ns_dataset("x", "select", "ADSL"), character(0L))
app_driver$expect_validation_error()
testthat::expect_match(
app_driver$active_module_element_text(
sprintf(
"%s .shiny-validation-message",
ns_dataset("x", "select_input", "ADSL")
)
),
"^Please select an x-variable$"
)
app_driver$stop()
})

# Test pairs of dataset selection ---------------------------------------------

test_dataset_selection <- function(id, new_dataset, new_value) {
testthat::test_that(
sprintf(
"e2e - tm_g_barchart_simple: Selection of '%s' dataset changes the element and does not throw validation errors",
id
),
{
skip_if_too_deep(5)
app_driver <- app_driver_tm_g_barchart_simple()
plot_before <- app_driver$get_active_module_pws_output("myplot")
app_driver$set_active_module_input(sprintf("%s-dataset", id), new_dataset)
testthat::expect_false(identical(plot_before, app_driver$get_active_module_pws_output("myplot")))
testthat::expect_null(app_driver$get_active_module_input(ns_dataset(id, "select", new_dataset)))
app_driver$set_active_module_input(ns_dataset(id, "select", new_dataset), new_value)
testthat::expect_identical(app_driver$get_active_module_input(ns_dataset(id, "select", new_dataset)), new_value)
app_driver$expect_no_validation_error()
app_driver$stop()
}
)

testthat::test_that(
sprintf(
"%s: De-selection of '%s' dataset changes the element and does not throw validation errors",
"e2e - tm_g_barchart_simple",
id
),
{
skip_if_too_deep(5)
app_driver <- app_driver_tm_g_barchart_simple()
plot_before <- app_driver$get_active_module_pws_output("myplot")
app_driver$set_active_module_input(sprintf("%s-dataset", id), character(0L))
testthat::expect_null(app_driver$get_active_module_input(id))

app_driver$expect_no_validation_error()
app_driver$stop()
}
)
}
kartikeyakirar marked this conversation as resolved.
Show resolved Hide resolved

test_dataset_selection("fill", "ADAE", "AESER")
test_dataset_selection("x_facet", "ADSL", "RACE")
test_dataset_selection("y_facet", "ADSL", "ARM")

# Duplicate variables cannot be selected --------------------------------------

for (id in c("fill", "x_facet", "y_facet")) {
testthat::test_that(
sprintf(
"e2e - tm_g_barchart_simple: Duplicate between 'x' and '%s' selection throws validation error",
id
),
{
skip_if_too_deep(5)
app_driver <- app_driver_tm_g_barchart_simple()

app_driver$set_active_module_input(ns_dataset("x", "select", "ADSL"), "ACTARM")
averissimo marked this conversation as resolved.
Show resolved Hide resolved
app_driver$set_active_module_input(sprintf("%s-dataset", id), "ADSL")
app_driver$set_active_module_input(ns_dataset(id, "select", "ADSL"), "ACTARM")

app_driver$expect_validation_error()

testthat::expect_match(
app_driver$active_module_element_text(
sprintf(
"%s .shiny-validation-message",
ns_dataset("x", "select_input", "ADSL")
)
),
"^Duplicated value: ACTARM$"
)

testthat::expect_match(
app_driver$active_module_element_text(
sprintf(
"%s .shiny-validation-message",
ns_dataset(id, "select_input", "ADSL")
)
),
"^Duplicated value: ACTARM$"
)
}
)
}

# Plot settings ---------------------------------------------------------------

test_that_plot_settings <- function(id, new_value, setup_fun = function(app_driver) NULL) {
testthat::test_that(
sprintf(
"e2e - tm_g_barchart_simple: Changing '%s' changes the plot and does not throw validation errors.",
id
),
{
skip_if_too_deep(5)
app_driver <- app_driver_tm_g_barchart_simple()
setup_fun(app_driver)
plot_before <- app_driver$get_active_module_pws_output("myplot")
app_driver$set_active_module_input(id, new_value)
testthat::expect_false(identical(plot_before, app_driver$get_active_module_pws_output("myplot")))
app_driver$expect_no_validation_error()
app_driver$stop()
}
)
}

test_that_plot_settings("barlayout", "side_by_side")
test_that_plot_settings("expand_y_range", 0.9)
test_that_plot_settings("facet_scale_x", FALSE)
test_that_plot_settings("facet_scale_y", FALSE)
test_that_plot_settings("label_bars", TRUE)
test_that_plot_settings("rotate_x_label", FALSE)
test_that_plot_settings("rotate_y_label", FALSE)
test_that_plot_settings("flip_axis", FALSE)
test_that_plot_settings("show_n", TRUE)

# needs extra setup
test_that_plot_settings(
"rotate_bar_labels",
FALSE,
setup_fun = function(app_driver) app_driver$set_active_module_input("label_bars", TRUE)
)
Loading