Skip to content

Commit e6ebad5

Browse files
test: create basic tests
1 parent 9dc36a5 commit e6ebad5

4 files changed

Lines changed: 152 additions & 0 deletions

File tree

tests/test-setup.R

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env Rscript
2+
3+
cat("Testing basic test setup...\n")
4+
5+
cwd <- getwd()
6+
cat("Current directory:", cwd, "\n")
7+
8+
repo_root_script <- normalizePath(dirname(cwd))
9+
cat("Calculated repo_root:", repo_root_script, "\n")
10+
11+
fixture_file <- file.path(
12+
repo_root_script,
13+
"..",
14+
"code",
15+
"MOSuite",
16+
"tests",
17+
"testthat",
18+
"data",
19+
"moo.rds"
20+
)
21+
cat("\nLooking for fixture data at:", fixture_file, "\n")
22+
cat("Fixture data exists:", file.exists(fixture_file), "\n")
23+
24+
code_main <- file.path(repo_root_script, "..", "code", "main.R")
25+
code_run <- file.path(repo_root_script, "..", "code", "run")
26+
cat("code/main.R exists:", file.exists(code_main), "\n")
27+
cat("code/run exists:", file.exists(code_run), "\n")
28+
29+
if (file.exists(fixture_file)) {
30+
cat("\nTrying to load fixture MOO object...\n")
31+
library(readr)
32+
moo <- readr::read_rds(fixture_file)
33+
cat("Fixture MOO loaded successfully!\n")
34+
cat("MOO class:", class(moo), "\n")
35+
}

tests/testthat.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
library(testthat)
2+
test_dir(here::here('tests/testthat/'))

tests/testthat/helper-cli.R

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
setup_cli_workspace <- function(prefix = "mosuite_plot_volcano_summary_test_") {
2+
workspace <- tempfile(prefix)
3+
dir.create(workspace)
4+
5+
code_dir <- file.path(workspace, "code")
6+
data_dir <- file.path(workspace, "data")
7+
results_dir <- file.path(workspace, "results")
8+
dir.create(code_dir, recursive = TRUE)
9+
dir.create(data_dir, recursive = TRUE)
10+
dir.create(file.path(results_dir, "figures"), recursive = TRUE)
11+
dir.create(file.path(results_dir, "moo"), recursive = TRUE)
12+
13+
repo_root <- normalizePath(
14+
file.path(testthat::test_path(), "..", ".."),
15+
mustWork = TRUE
16+
)
17+
18+
test_data_file <- file.path(
19+
repo_root,
20+
"code",
21+
"MOSuite",
22+
"tests",
23+
"testthat",
24+
"data",
25+
"moo.rds"
26+
)
27+
28+
expect_true(
29+
file.exists(test_data_file),
30+
info = paste("Test data file should exist at", test_data_file)
31+
)
32+
file.copy(test_data_file, file.path(data_dir, "moo.rds"), overwrite = TRUE)
33+
34+
file.copy(
35+
file.path(repo_root, "code", "main.R"),
36+
file.path(code_dir, "main.R"),
37+
overwrite = TRUE
38+
)
39+
40+
# Keep main.R behavior the same while pointing to this checkout's MOSuite package.
41+
main_copy <- file.path(code_dir, "main.R")
42+
main_lines <- readLines(main_copy)
43+
main_lines <- gsub(
44+
"devtools::load_all('/code/MOSuite')",
45+
sprintf(
46+
"devtools::load_all('%s')",
47+
file.path(repo_root, "code", "MOSuite")
48+
),
49+
main_lines,
50+
fixed = TRUE
51+
)
52+
writeLines(main_lines, main_copy)
53+
54+
list(
55+
workspace = workspace,
56+
code_dir = code_dir,
57+
results_dir = results_dir,
58+
repo_root = repo_root
59+
)
60+
}
61+
62+
expect_outputs_created <- function(results_dir) {
63+
plot_path <- file.path(results_dir, "figures", "diff", "volcano_summary.png")
64+
csv_path <- file.path(results_dir, "moo", "volcano_summary_data.csv")
65+
66+
expect_true(file.exists(plot_path), info = "Volcano summary plot should be created")
67+
expect_true(
68+
file.info(plot_path)$size > 0,
69+
info = "Volcano summary plot should be non-empty"
70+
)
71+
72+
expect_true(file.exists(csv_path), info = "Volcano summary CSV should be created")
73+
expect_true(
74+
file.info(csv_path)$size > 0,
75+
info = "Volcano summary CSV should be non-empty"
76+
)
77+
}
78+
79+
common_cli_args <- c(
80+
"--num_features_to_label=10",
81+
"--plot_filename=volcano_summary.png"
82+
)

tests/testthat/test-main.R

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
test_that("main.R CLI creates volcano summary outputs", {
2+
setup <- setup_cli_workspace("mosuite_plot_volcano_summary_test_")
3+
on.exit(unlink(setup$workspace, recursive = TRUE), add = TRUE)
4+
5+
old_wd <- getwd()
6+
setwd(setup$code_dir)
7+
on.exit(setwd(old_wd), add = TRUE)
8+
9+
exit_code <- system2("Rscript", args = c("main.R", common_cli_args))
10+
expect_equal(exit_code, 0, info = "main.R should execute without error")
11+
12+
expect_outputs_created(setup$results_dir)
13+
})
14+
15+
test_that("run wrapper executes and creates volcano summary outputs", {
16+
setup <- setup_cli_workspace("mosuite_plot_volcano_summary_run_test_")
17+
on.exit(unlink(setup$workspace, recursive = TRUE), add = TRUE)
18+
19+
file.copy(
20+
file.path(setup$repo_root, "code", "run"),
21+
file.path(setup$code_dir, "run"),
22+
overwrite = TRUE
23+
)
24+
25+
old_wd <- getwd()
26+
setwd(setup$code_dir)
27+
on.exit(setwd(old_wd), add = TRUE)
28+
29+
exit_code <- system2("bash", args = c("run", common_cli_args))
30+
expect_equal(exit_code, 0, info = "run script should execute without error")
31+
32+
expect_outputs_created(setup$results_dir)
33+
})

0 commit comments

Comments
 (0)