Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
averissimo committed Nov 18, 2024
1 parent 585a3ba commit 33b308f
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 70 deletions.
2 changes: 2 additions & 0 deletions R/dummy_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ example_module <- function(label = "example teal module",
verbatim_content = reactive(teal.code::get_code(req(table_data_decorated()))),
title = "Example Code"
)

table_data_decorated
})
},
ui = function(id, decorators) {
Expand Down
4 changes: 2 additions & 2 deletions R/module_data_summary.R
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,10 @@ get_filter_overview_wrapper <- function(teal_data) {

current_data_objs <- sapply(
datanames,
function(name) teal.code::get_var(teal_data(), name),
function(name) teal_data()[[name]],
simplify = FALSE
)
initial_data_objs <- teal.code::get_var(teal_data(), ".raw_data")
initial_data_objs <- teal_data()[[".raw_data"]]

out <- lapply(
datanames,
Expand Down
2 changes: 1 addition & 1 deletion R/module_transform_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ srv_teal_transform_data <- function(id, data, transformators, modules = NULL, is
moduleServer(name, function(input, output, session) {
logger::log_debug("srv_teal_transform_data initializing for { name }.")
is_transform_failed[[name]] <- FALSE
data_out <- transformators[[name]]$server(name, data = data_previous)
data_out <- transformators[[name]]$server("transform", data = data_previous)
data_handled <- reactive(tryCatch(data_out(), error = function(e) e))
observeEvent(data_handled(), {
if (inherits(data_handled(), "teal_data")) {
Expand Down
5 changes: 5 additions & 0 deletions R/teal_transform_module.R
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,11 @@ teal_transform_module <- function(ui = NULL,
#'
#' @export
make_teal_transform_server <- function(expr) {
if (is.call(expr)) {
expr <- as.expression(expr)
}
checkmate::assert_multi_class(expr, c("call", "expression"))

function(id, data) {
moduleServer(id, function(input, output, session) {
reactive({
Expand Down
122 changes: 58 additions & 64 deletions tests/testthat/test-module_teal.R
Original file line number Diff line number Diff line change
Expand Up @@ -1948,27 +1948,24 @@ testthat::describe("srv_teal teal_module(s) transformator", {
})

testthat::it("changes module output for a module with a static decorator", {
label <- "output_decorator"
output_decorator <- teal_transform_module(
label = label,
server = make_teal_transform_server(
expression(
data1 <- rev(data1)
)
)
label = "output_decorator",
server = make_teal_transform_server(expression(object <- rev(object)))
)

shiny::testServer(
app = srv_teal_transform_data,
app = srv_teal,
args = list(
id = "test",
data = reactive(teal.data::teal_data(data1 = iris, data2 = mtcars)),
transformators = output_decorator
data = teal.data::teal_data(object = iris),
modules = modules(example_module("mod1", decorators = output_decorator))
),
expr = {
data_out <- transformators[[label]]$server(label, data = data)
session$setInputs(`teal_modules-active_tab` = "mod1")
session$setInputs(`teal_modules-mod1-module-dataname` = "object")
session$flushReact()
testthat::expect_identical(
data_out()[["data1"]],
modules_output$mod1()()[["object"]],
rev(iris)
)
}
Expand All @@ -1977,25 +1974,16 @@ testthat::describe("srv_teal teal_module(s) transformator", {


testthat::it("changes module output for a module with a decorator that is a function of an object name", {
label <- "output_decorator_name"
output_decorator_name <- function(output_name, label) {
decorator_name <- function(output_name, label) {
teal_transform_module(
label = label,
ui = function(id) {
ns <- NS(id)
div(
textInput(ns("append_text"), "Append text", value = "random text")
)
},
server = function(id, data) {
moduleServer(id, function(input, output, session) {
reactive({
req(data())
within(data(),
{
output_name <- paste0(output_name, append_text)
},
append_text = input$append_text,
within(
data(),
output_name <- paste0(output_name, " lorem ipsum"),
text = input$text,
output_name = as.name(output_name)
)
})
Expand All @@ -2005,60 +1993,66 @@ testthat::describe("srv_teal teal_module(s) transformator", {
}

shiny::testServer(
app = srv_teal_transform_data,
app = srv_teal,
args = list(
id = "test",
data = reactive(teal.data::teal_data(x1 = "ABC")),
transformators = output_decorator_name(output_name = "x1", label = label)
data = teal.data::teal_data(x1 = "ABC"),
modules = modules(
example_module(
"mod1",
decorators = decorator_name(output_name = "object", label = "decorator_name")
)
)
),
expr = {
data_out <- transformators[[label]]$server(label, data = data)
testthat::expect_identical(
data_out()[["x1"]],
paste0("ABC", "random text") # "random text" is not appended
)
session$setInputs(`teal_modules-active_tab` = "mod1")
session$setInputs(`teal_modules-mod1-module-dataname` = "x1")
session$flushReact()

testthat::expect_identical(modules_output$mod1()()[["object"]], "ABC lorem ipsum")
}
)
})

testthat::it("changes module output for a module with an interactive decorator", {
label <- "output_decorator_int"
output_decorator_int <- teal_transform_module(
label = label,
ui = function(id) {
ns <- NS(id)
div(
textInput(ns("append_text"), "Append text", value = "random text")
)
},
server = function(id, data) {
moduleServer(id, function(input, output, session) {
reactive({
req(data())
within(data(),
{
x1 <- paste0(x1, append_text)
},
append_text = input$append_text
)
decorator_name <- function(output_name, label) {
teal_transform_module(
label = label,
server = function(id, data) {
moduleServer(id, function(input, output, session) {
reactive({
req(data(), input$text)
within(
data(),
output_name <- paste0(output_name, " ", text),
text = input$text,
output_name = as.name(output_name)
)
})
})
})
}
)
}
)
}

shiny::testServer(
app = srv_teal_transform_data,
app = srv_teal,
args = list(
id = "test",
data = reactive(teal.data::teal_data(x1 = "ABC")),
transformators = output_decorator_int
data = teal.data::teal_data(x1 = "ABC"),
modules = modules(
example_module(
"mod1",
decorators = decorator_name(output_name = "object", label = "decorator_name")
)
)
),
expr = {
data_out <- transformators[[label]]$server(label, data = data)
testthat::expect_identical(
data_out()[["x1"]],
paste0("ABC", "random text") # "random text" is not appended
)
session$setInputs(`teal_modules-active_tab` = "mod1")
session$setInputs(`teal_modules-mod1-module-dataname` = "x1")
session$setInputs(`teal_modules-mod1-module-decorate-decorator_name-transform-text` = "lorem ipsum dolor")
session$flushReact()

testthat::expect_identical(modules_output$mod1()()[["object"]], "ABC lorem ipsum dolor")
}
)
})
Expand Down
32 changes: 29 additions & 3 deletions tests/testthat/test-modules.R
Original file line number Diff line number Diff line change
Expand Up @@ -509,9 +509,35 @@ testthat::test_that("format.teal_modules returns proper structure", {

appended_mods <- append_module(mods, mod3)

testthat::expect_equal(
gsub("\033\\[[0-9;]*m", "", format(appended_mods)),
"TEAL ROOT\n |- a\n | |- Datasets : all\n | |- Properties:\n | | |- Bookmarkable : FALSE\n | | L- Reportable : FALSE\n | |- UI Arguments : \n | |- Server Arguments : \n | L- transformators : \n |- c\n | |- Datasets : all\n | |- Properties:\n | | |- Bookmarkable : FALSE\n | | L- Reportable : FALSE\n | |- UI Arguments : \n | |- Server Arguments : \n | L- transformators : \n L- c\n |- Datasets : all\n |- Properties:\n | |- Bookmarkable : FALSE\n | L- Reportable : FALSE\n |- UI Arguments : \n |- Server Arguments : \n L- transformators : \n" # nolint: line_length
testthat::expect_setequal(
strsplit(gsub("\033\\[[0-9;]*m", "", format(appended_mods)), "\n")[[1]],
c(
"TEAL ROOT",
" |- a",
" | |- Datasets : all",
" | |- Properties:",
" | | |- Bookmarkable : FALSE",
" | | L- Reportable : FALSE",
" | |- UI Arguments : ",
" | |- Server Arguments : ",
" | L- Transformators : ",
" |- c",
" | |- Datasets : all",
" | |- Properties:",
" | | |- Bookmarkable : FALSE",
" | | L- Reportable : FALSE",
" | |- UI Arguments : ",
" | |- Server Arguments : ",
" | L- Transformators : ",
" L- c",
" |- Datasets : all",
" |- Properties:",
" | |- Bookmarkable : FALSE",
" | L- Reportable : FALSE",
" |- UI Arguments : ",
" |- Server Arguments : ",
" L- Transformators : "
)
)
})

Expand Down
47 changes: 47 additions & 0 deletions tests/testthat/test-teal_transform_module.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
testthat::describe("make_teal_transform_server produces a valid teal_transform_module", {
testthat::it("expression", {
label <- "output_decorator"
output_decorator <- teal_transform_module(
label = label,
server = make_teal_transform_server(
expression(data1 <- rev(data1))
)
)

shiny::testServer(
app = srv_teal_transform_data,
args = list(
id = "test",
data = reactive(teal.data::teal_data(data1 = iris, data2 = mtcars)),
transformators = output_decorator
),
expr = {
data_out <- transformators[[label]]$server(label, data = data)
testthat::expect_identical(data_out()[["data1"]], rev(iris))
}
)
})

testthat::it("quote", {
label <- "output_decorator"
output_decorator <- teal_transform_module(
label = label,
server = make_teal_transform_server(
quote(data1 <- rev(data1))
)
)

shiny::testServer(
app = srv_teal_transform_data,
args = list(
id = "test",
data = reactive(teal.data::teal_data(data1 = iris, data2 = mtcars)),
transformators = output_decorator
),
expr = {
data_out <- transformators[[label]]$server(label, data = data)
testthat::expect_identical(data_out()[["data1"]], rev(iris))
}
)
})
})

0 comments on commit 33b308f

Please sign in to comment.