Skip to content

Commit

Permalink
updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Konrad1991 committed Dec 16, 2024
1 parent 2414b32 commit e1b3b3e
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 22 deletions.
4 changes: 4 additions & 0 deletions bs/R/assumption.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ assServer <- function(id, data, listResults) {
}
res <- do.call(rbind, res)
check_rls(listResults$all_data, res)
res
},
warning = function(warn) {
print_warn(warn$message)
Expand Down Expand Up @@ -120,6 +121,7 @@ assServer <- function(id, data, listResults) {
res <- broom::tidy(shapiro.test(r))
res$`Residuals normal distributed` <- res$p.value > 0.05
check_rls(listResults$all_data, res)
res
},
warning = function(warn) {
print_warn(warn$message)
Expand Down Expand Up @@ -160,6 +162,7 @@ assServer <- function(id, data, listResults) {
fit <- broom::tidy(car::leveneTest(formula, data = df, center = input$center))
fit$`Variance homogenity` <- fit$p.value > 0.05
check_rls(listResults$all_data, fit)
fit
},
warning = function(warn) {
print_warn(warn$message)
Expand Down Expand Up @@ -199,6 +202,7 @@ assServer <- function(id, data, listResults) {
{
p <- diagnosticPlots(df, formula)
check_rls(listResults$all_data, p)
p
},
warning = function(warn) {
print_warn(warn$message)
Expand Down
4 changes: 3 additions & 1 deletion bs/R/statisticalTests.R
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ testsServer <- function(id, data, listResults) {
data = df, conf.level = input$confLevel,
alternative = input$altHyp, var.equal = eq
))
check_rls(listResults$all_data)
check_rls(listResults$all_data, fit)
fit
},
warning = function(warn) {
print_warn(warn$message)
Expand Down Expand Up @@ -225,6 +226,7 @@ testsServer <- function(id, data, listResults) {
}
)
check_rls(listResults$all_data, fit)
fit
},
warning = function(warn) {
print_warn(warn$message)
Expand Down
16 changes: 15 additions & 1 deletion bs/R/utils.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Upload data into R
readData <- function(path) {
stopifnot(is.character(path))
max_file_size <- 50 * 1024^2 # 50 MB in bytes
file_size <- file.info(path)$size
if (is.na(file_size) || file_size > max_file_size) {
stop("File size exceeds the 50 MB limit. Please upload a smaller file.")
}
df <- NULL
df <- try(as.data.frame(readxl::read_excel(
path,
Expand Down Expand Up @@ -44,6 +49,15 @@ readData <- function(path) {
df <- Map(conv, check, df)
df <- data.frame(df)
}
# Check data frame dimensions
max_rows <- 1e6
max_cols <- 1000
if (nrow(df) > max_rows || ncol(df) > max_cols) {
stop(sprintf(
"Data exceeds the limit of %d rows or %d columns. Please upload a smaller dataset.",
max_rows, max_cols
))
}
return(df)
}

Expand Down Expand Up @@ -426,7 +440,7 @@ check_axis_limits <- function(col, min, max) {

# check that result is only of allowed type
check_type_res <- function(res) {
allowed <- c("numeric", "integer", "logical", "character", "data.frame")
allowed <- c("numeric", "factor", "integer", "logical", "character", "data.frame")
if (!(class(res) %in% allowed)) {
stop(paste0("Found result with unallowed type: ", class(res)))
}
Expand Down
3 changes: 2 additions & 1 deletion bs/inst/tinytest/Assumptions.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ app$set_inputs(conditionedPanels = "Assumption")
app$wait_for_idle()
app$set_window_size(width = 2259, height = 1326)
app$wait_for_idle()
app$click("ASS-open_formula_editor")
app$click("open_formula_editor")
app$wait_for_idle()
app$set_inputs(`FO-colnames-dropdown_0` = "uptake")
app$wait_for_idle()
Expand All @@ -38,6 +38,7 @@ expected <- rbind(
)
expected$variable <- c("nonchilled", "chilled")
expected$`Normal distributed` <- expected$p.value > 0.05
res
tinytest::expect_equal(res[[1]], expected)

# Update output value
Expand Down
2 changes: 1 addition & 1 deletion bs/inst/tinytest/Correlation.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ app$set_window_size(width = 2259, height = 1326)
app$wait_for_idle()
app$set_inputs(conditionedPanels = "Correlation")
app$wait_for_idle()
app$click("CORR-open_formula_editor")
app$click("open_formula_editor")
app$wait_for_idle()
app$set_inputs(`FO-colnames-dropdown_0` = "uptake")
app$wait_for_idle()
Expand Down
70 changes: 56 additions & 14 deletions bs/inst/tinytest/DataWrangling.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# TODO: Add tests for the following: DataFrame & seq
library(shinytest2)
library(tinytest)
library(bs)
Expand All @@ -22,6 +21,49 @@ app$wait_for_idle()
app$set_inputs(`conditionedPanels` = "DataWrangling")
app$wait_for_idle()

# Seq tests
app$click("OP-seq")
app$wait_for_idle()
content <- app$get_values()$input[["OP-editable_code"]]
app$set_inputs(
`OP-editable_code` =
paste0(
content, "1, 100, 1"
)
)
app$click("OP-bracket_close")
app$wait_for_idle()
app$set_inputs(`OP-iv` = "Seq")
app$wait_for_idle()
app$click("OP-run_op_intermediate")
app$wait_for_idle()
content <- app$get_values()$input[["OP-editable_code"]]
expect_equal(content, " Seq(1, 100, 1 )")
iv_list <- app$get_values()$export[["OP-iv_list"]]
expect_equal(iv_list$Seq, seq(1, 100, 1))

# dataframe tests
app$set_inputs(`OP-editable_code` = "")
app$click("OP-df")
app$wait_for_idle()
app$click("OP-colnames_conc_0")
app$click("OP-comma")
app$click("OP-colnames_conc_0")
app$click("OP-bracket_close")
content <- app$get_values()$input[["OP-editable_code"]]
app$wait_for_idle()
app$set_inputs(`OP-iv` = "df_new")
app$wait_for_idle()
app$click("OP-run_op_intermediate")
app$wait_for_idle()
content <- app$get_values()$input[["OP-editable_code"]]
expect_equal(content, " DataFrame( conc , conc )")
iv_list <- app$get_values()$export[["OP-iv_list"]]
df_new <- data.frame(CO2$conc, CO2$conc)
names(df_new) <- c("conc", "conc")
expect_equal(iv_list$df_new, df_new)
app$set_inputs(`OP-editable_code` = "")

# random tests
random_funcs <- c(
"dnorm", "pnorm", "qnorm",
Expand Down Expand Up @@ -57,7 +99,7 @@ for (i in seq_along(random_funcs)) {
app$wait_for_idle()
iv_list <- app$get_values()$export[["OP-iv_list"]]
output <- iv_list[[make.names(func)]]
expect_equal(output, res)
expect_equal(output, res) |> print()
}

# NOTE: this is necessary as no seed can be set
Expand All @@ -77,10 +119,10 @@ validate_distribution <- function(values, dist, ...) {
prob_success <- 0.5
expect_true(abs(mean(values) - (n_trials * prob_success)) < 0.5,
info = "Binomial mean should be close to n * p"
)
) |> print()
expect_true(abs(var(values) - (n_trials * prob_success * (1 - prob_success))) < 0.5,
info = "Binomial variance should be close to n * p * (1 - p)"
)
) |> print()
}
}
random_funcs <- c(
Expand Down Expand Up @@ -351,12 +393,12 @@ Num <- -1.5
expect_equal(iv_list$MATH4, eval(parse(text = content)))

# Test comparison operations
app$set_inputs(`OP-editable_code` = "c(1, 2, 2, 2)")
app$set_inputs(`OP-editable_code` = "C(1, 2, 2, 2)")
app$set_inputs(`OP-iv` = "Vec1")
app$wait_for_idle()
app$click("OP-run_op_intermediate")
app$wait_for_idle()
app$set_inputs(`OP-editable_code` = "c(1, 2, 3, 4)")
app$set_inputs(`OP-editable_code` = "C(1, 2, 3, 4)")
app$set_inputs(`OP-iv` = "Vec2")
app$wait_for_idle()
app$click("OP-run_op_intermediate")
Expand All @@ -383,7 +425,7 @@ for (i in operations) {
app$wait_for_idle()
iv_list <- app$get_values()$export[["OP-iv_list"]]
# print(iv_list[[make.names(i)]])
expect_equal(iv_list[[make.names(i)]], eval(parse(text = content)))
expect_equal(iv_list[[make.names(i)]], eval(parse(text = content))) |> print()
}

# Test statistical operations and utilities
Expand All @@ -410,7 +452,7 @@ for (i in operations) {
app$wait_for_idle()
iv_list <- app$get_values()$export[["OP-iv_list"]]
# print(iv_list[[make.names(i)]])
expect_equal(iv_list[[make.names(i)]], eval(parse(text = content)))
expect_equal(iv_list[[make.names(i)]], eval(parse(text = content))) |> print()
}

app$set_inputs(`OP-editable_code` = "")
Expand Down Expand Up @@ -455,13 +497,13 @@ expect_equal(iv_list[["get_cols"]], CO2[, c("conc", "conc", "uptake")])


# Test string functions
app$set_inputs(`OP-editable_code` = 'c("A", "B", "C")')
app$set_inputs(`OP-editable_code` = 'C("A", "B", "C")')
app$wait_for_idle()
app$set_inputs(`OP-iv` = "S1")
app$wait_for_idle()
app$click("OP-run_op_intermediate")
app$wait_for_idle()
app$set_inputs(`OP-editable_code` = 'c("d", "e", "f")')
app$set_inputs(`OP-editable_code` = 'C("d", "e", "f")')
app$wait_for_idle()
app$set_inputs(`OP-iv` = "S2")
app$wait_for_idle()
Expand Down Expand Up @@ -493,7 +535,7 @@ for (i in operations) {
app$wait_for_idle()
iv_list <- app$get_values()$export[["OP-iv_list"]]
# print(iv_list[[make.names(i)]])
expect_equal(iv_list[[make.names(i)]], eval(parse(text = content)))
expect_equal(iv_list[[make.names(i)]], eval(parse(text = content))) |> print()
}
operations <- c(
"OP-tolower", "OP-toupper"
Expand All @@ -515,11 +557,11 @@ for (i in operations) {
app$wait_for_idle()
iv_list <- app$get_values()$export[["OP-iv_list"]]
# print(iv_list[[make.names(i)]])
expect_equal(iv_list[[make.names(i)]], eval(parse(text = content)))
expect_equal(iv_list[[make.names(i)]], eval(parse(text = content))) |> print()
}

# Test casts
app$set_inputs(`OP-editable_code` = 'c("10.5", "1.4", "1.3", 3.14)')
app$set_inputs(`OP-editable_code` = 'C("10.5", "1.4", "1.3", 3.14)')
app$wait_for_idle()
app$set_inputs(`OP-iv` = "S1")
app$wait_for_idle()
Expand Down Expand Up @@ -549,6 +591,6 @@ for (i in operations) {
app$click("OP-run_op_intermediate")
app$wait_for_idle()
iv_list <- app$get_values()$export[["OP-iv_list"]]
expect_equal(iv_list[[i]], eval(parse(text = content)))
expect_equal(iv_list[[make.names(i)]], eval(parse(text = content))) |> print()
}
app$stop()
2 changes: 1 addition & 1 deletion bs/inst/tinytest/DoseResponse.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ app$set_inputs(conditionedPanels = "Dose Response analysis")
app$wait_for_idle()

# Define formula
app$click("DOSERESPONSE-open_formula_editor")
app$click("open_formula_editor")
app$wait_for_idle()
app$set_inputs(`FO-colnames-dropdown_0` = "abs")
app$wait_for_idle()
Expand Down
4 changes: 2 additions & 2 deletions bs/inst/tinytest/StatisticalTests.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ app$set_inputs(conditionedPanels = "Tests")
app$wait_for_idle()

# Define formula
app$click("TESTS-open_formula_editor")
app$click("open_formula_editor")
app$wait_for_idle()
app$set_inputs(`FO-colnames-dropdown_0` = "uptake")
app$wait_for_idle()
Expand All @@ -40,7 +40,7 @@ names(expected)[ncol(expected)] <- paste0("conc * Treatment + Type", collapse =
tinytest::expect_equal(res[[1]], expected)

# Kruskal-Wallis
app$click("TESTS-open_formula_editor")
app$click("open_formula_editor")
app$wait_for_idle()
app$set_inputs(`FO-colnames-dropdown_0` = "uptake")
app$wait_for_idle()
Expand Down
2 changes: 1 addition & 1 deletion bs/inst/tinytest/TTest.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ app$set_window_size(width = 2259, height = 1326)
app$wait_for_idle()
app$set_inputs(conditionedPanels = "Tests")
app$wait_for_idle()
app$click("TESTS-open_formula_editor")
app$click("open_formula_editor")
app$wait_for_idle()
app$set_inputs(`FO-colnames-dropdown_0` = "uptake")
app$wait_for_idle()
Expand Down
77 changes: 77 additions & 0 deletions bs/inst/tinytest/UtilsTests.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
library(tinytest)
library(bs)
library(readxl)

# Test 1: Valid input with an Excel file
test_file <- tempfile(fileext = ".xlsx")
write.csv(data.frame(a = 1:5, b = letters[1:5]), test_file, row.names = FALSE)
readxl::write_xlsx(read.csv(test_file), test_file)
result <- readData(test_file)
expect_equal(class(result), "data.frame")
expect_equal(nrow(result), 5)
expect_equal(ncol(result), 2)

# Test 2: Valid input with a CSV file (comma-separated)
test_that("readData reads a valid CSV file (comma-separated)", {
test_file <- tempfile(fileext = ".csv")
write.csv(data.frame(a = 1:5, b = letters[1:5]), test_file, row.names = FALSE)
result <- readData(test_file)
expect_equal(class(result), "data.frame")
expect_equal(nrow(result), 5)
expect_equal(ncol(result), 2)
})

# Test 3: File exceeds size limit
test_that("readData throws an error for file exceeding size limit", {
test_file <- tempfile()
write.csv(data.frame(a = 1:(50 * 1024^2 / 2)), test_file, row.names = FALSE)
expect_error(readData(test_file), "File size exceeds the 50 MB limit.")
})

# Test 4: File with unknown separator
test_that("readData returns error for unknown separator", {
test_file <- tempfile()
writeLines("a|b|c\n1|2|3", test_file)
result <- readData(test_file)
expect_equal(result, "error")
})

# Test 5: File with semicolon separator
test_that("readData reads a file with semicolon separator", {
test_file <- tempfile()
writeLines("a;b;c\n1;2;3", test_file)
result <- readData(test_file)
expect_equal(class(result), "data.frame")
expect_equal(nrow(result), 1)
expect_equal(ncol(result), 3)
})

# Test 6: File with tab separator
test_that("readData reads a file with tab separator", {
test_file <- tempfile()
writeLines("a\tb\tc\n1\t2\t3", test_file)
result <- readData(test_file)
expect_equal(class(result), "data.frame")
expect_equal(nrow(result), 1)
expect_equal(ncol(result), 3)
})

# Test 7: File with invalid path
test_that("readData throws an error for invalid path", {
expect_error(readData("nonexistent_file.csv"), "cannot open the connection")
})

# Test 8: Data exceeds row or column limits
test_that("readData throws an error for data exceeding row or column limits", {
test_file <- tempfile(fileext = ".csv")
write.csv(data.frame(matrix(1, nrow = 1e6 + 1, ncol = 2)), test_file, row.names = FALSE)
expect_error(readData(test_file), "Data exceeds the limit of")

write.csv(data.frame(matrix(1, nrow = 10, ncol = 1001)), test_file, row.names = FALSE)
expect_error(readData(test_file), "Data exceeds the limit of")
})

# Test 9: Non-character input for path
test_that("readData throws an error for non-character path", {
expect_error(readData(123), "is.character")
})

0 comments on commit e1b3b3e

Please sign in to comment.