From bd8041aee7d8a942a695fe91ac1391e584bb8f61 Mon Sep 17 00:00:00 2001 From: go_gonzo Date: Tue, 29 Oct 2024 06:00:11 +0100 Subject: [PATCH] review and tests --- R/module_teal.R | 1 + R/utils.R | 35 ++++---- tests/testthat/test-module_teal.R | 131 ++++++++++++++++++++++++------ 3 files changed, 125 insertions(+), 42 deletions(-) diff --git a/R/module_teal.R b/R/module_teal.R index 8624636dd..3e913c234 100644 --- a/R/module_teal.R +++ b/R/module_teal.R @@ -210,6 +210,7 @@ srv_teal <- function(id, data, modules, filter = teal_slices()) { data_load_status <- reactive({ if (inherits(data_pulled(), "teal_data")) { "ok" + # todo: should we hide warnings on top for a data? } else if (inherits(data, "teal_data_module")) { "teal_data_module failed" } else { diff --git a/R/utils.R b/R/utils.R index 21c83e77b..f3054cb4c 100644 --- a/R/utils.R +++ b/R/utils.R @@ -169,30 +169,30 @@ check_modules_datanames_html <- function(modules, function(mod) { tagList( tags$span( - tags$span(`if`(length(mod$missing_datanames) > 1, "Datasets", "Dataset")), + tags$span(if (length(mod$missing_datanames) == 1) "Dataset" else "Datasets"), to_html_code_list(mod$missing_datanames), tags$span( paste0( - `if`(length(mod$missing_datanames) > 1, "are missing", "is missing"), - `if`(show_module_info, sprintf(" for tab '%s'.", mod$label), ".") + if (length(mod$missing_datanames) > 1) "are missing" else "is missing", + if (show_module_info) sprintf(" for tab '%s'.", mod$label) else "." ) - ), - if (length(mod$datanames) >= 1) { + ) + ), + if (length(datanames) >= 1) { + tagList( + tags$span(if (length(datanames) == 1) "Dataset" else "Datasets"), + tags$span("available in data:"), tagList( - tags$span(`if`(length(mod$datanames) > 1, "Datasets", "Dataset")), - tags$span("available in data:"), - tagList( - tags$span( - to_html_code_list(mod$datanames), - tags$span(".", .noWS = "outside"), - .noWS = c("outside") - ) + tags$span( + to_html_code_list(datanames), + tags$span(".", .noWS = "outside"), + .noWS = c("outside") ) ) - } else { - tags$span("No datasets are available in data.") - } - ), + ) + } else { + tags$span("No datasets are available in data.") + }, tags$br(.noWS = "before") ) } @@ -215,7 +215,6 @@ check_modules_datanames_recursive <- function(modules, datanames) { # nolint: ob if (length(missing_datanames)) { list(list( label = modules$label, - dataname = modules$datanames, missing_datanames = missing_datanames )) } diff --git a/tests/testthat/test-module_teal.R b/tests/testthat/test-module_teal.R index ed01caaef..8e91f7f2c 100644 --- a/tests/testthat/test-module_teal.R +++ b/tests/testthat/test-module_teal.R @@ -551,32 +551,115 @@ testthat::describe("srv_teal teal_modules", { ) }) - testthat::it("throws warning when dataname is not available", { - testthat::skip_if_not_installed("rvest") - shiny::testServer( - app = srv_teal, - args = list( - id = "test", - data = teal_data(mtcars = mtcars), - modules = modules( - module("module_1", server = function(id, data) data, datanames = c("iris")) - ) - ), - expr = { - session$setInputs(`teal_modules-active_tab` = "module_1") + testthat::describe("warnings on missing datanames", { + testthat::it("warns when dataname is not available", { + testthat::skip_if_not_installed("rvest") + shiny::testServer( + app = srv_teal, + args = list( + id = "test", + data = teal_data(iris = iris), + modules = modules( + module("module_1", server = function(id, data) data, datanames = c("iris", "missing")) + ) + ), + expr = { + session$setInputs(`teal_modules-active_tab` = "module_1") + testthat::expect_equal( + trimws( + rvest::html_text2( + rvest::read_html( + output[["teal_modules-module_1-validate_datanames-shiny_warnings-message"]]$html + ) + ) + ), + "Dataset missing is missing. Dataset available in data: iris." + ) + } + ) + }) - testthat::expect_equal( - trimws( - rvest::html_text2( - rvest::read_html( - output[["teal_modules-module_1-validate_datanames-shiny_warnings-message"]]$html + testthat::it("warns when datanames are not available", { + testthat::skip_if_not_installed("rvest") + shiny::testServer( + app = srv_teal, + args = list( + id = "test", + data = teal_data(mtcars = mtcars, iris = iris), + modules = modules( + module("module_1", datanames = c("mtcars", "iris", "missing1", "missing2")) + ) + ), + expr = { + session$setInputs(`teal_modules-active_tab` = "module_1") + + testthat::expect_equal( + trimws( + rvest::html_text2( + rvest::read_html( + output[["teal_modules-module_1-validate_datanames-shiny_warnings-message"]]$html + ) ) - ) - ), - "Dataset iris is missing. No datasets are available in data." - ) - } - ) + ), + "Datasets missing1 and missing2 are missing. Datasets available in data: iris and mtcars." + ) + } + ) + }) + + testthat::it("warns about empty data when none of module$datanames is available (even if data is not empty)", { + testthat::skip_if_not_installed("rvest") + shiny::testServer( + app = srv_teal, + args = list( + id = "test", + data = teal_data(mtcars = mtcars), + modules = modules( + module("module_1", datanames = c("missing1", "missing2")) + ) + ), + expr = { + session$setInputs(`teal_modules-active_tab` = "module_1") + testthat::expect_equal( + trimws( + rvest::html_text2( + rvest::read_html( + output[["teal_modules-module_1-validate_datanames-shiny_warnings-message"]]$html + ) + ) + ), + "Datasets missing1 and missing2 are missing. No datasets are available in data." + ) + } + ) + }) + + testthat::it("warns about empty data when none of module$datanames is available", { + testthat::skip_if_not_installed("rvest") + shiny::testServer( + app = srv_teal, + args = list( + id = "test", + data = reactive(teal_data(mtcars = mtcars)), + modules = modules( + module("module_1", datanames = c("missing1", "missing2")) + ) + ), + expr = { + session$setInputs(`teal_modules-active_tab` = "module_1") + testthat::expect_equal( + trimws( + rvest::html_text2( + rvest::read_html( + output[["validate-shiny_warnings-message"]]$html + ) + ) + ), + "Datasets missing1 and missing2 are missing for tab 'module_1'. Dataset available in data: mtcars." + ) + } + ) + }) }) testthat::it("is called and receives data even if datanames in `teal_data` are not sufficient", {