Skip to content

Commit

Permalink
review and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gogonzo committed Oct 29, 2024
1 parent 12d923f commit bd8041a
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 42 deletions.
1 change: 1 addition & 0 deletions R/module_teal.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
35 changes: 17 additions & 18 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
}
Expand All @@ -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
))
}
Expand Down
131 changes: 107 additions & 24 deletions tests/testthat/test-module_teal.R
Original file line number Diff line number Diff line change
Expand Up @@ -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", {
Expand Down

0 comments on commit bd8041a

Please sign in to comment.