Skip to content

Commit

Permalink
add test for import spectra target factory
Browse files Browse the repository at this point in the history
inspired by examples in ropensci/jagstargets
  • Loading branch information
cpauvert committed May 13, 2024
1 parent 8afef9e commit 875e2a2
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 16 deletions.
8 changes: 6 additions & 2 deletions dev/config_fusen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,12 @@ keep:
maldipickr-workflow-with-targets.Rmd:
path: dev/maldipickr-workflow-with-targets.Rmd
state: active
R: R/tar_import_and_process_spectra.R
tests: tests/testthat/test-tar_import_and_process_spectra.R
R:
- R/tar_import_and_process_spectra.R
- R/tar_pick_with_similarity.R
tests:
- tests/testthat/test-tar_import_and_process_spectra.R
- tests/testthat/test-tar_pick_with_similarity.R
vignettes: vignettes/maldipickr-workflow-with-targets.Rmd
inflate:
flat_file: dev/maldipickr-workflow-with-targets.Rmd
Expand Down
68 changes: 55 additions & 13 deletions dev/maldipickr-workflow-with-targets.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -159,25 +159,67 @@ if (Sys.getenv("TAR_LONG_EXAMPLES") == "true") {
```

```{r tests-tar_import_and_process_spectra}
# Tests for targets factory borrowed from
# src: https://github.com/ropensci/jagstargets/blob/2bad8b2958d0252a5993d341d38f8d9d02aeb41a/tests/testthat/test-tar_jags_rep_dic.R
test_that("tar_import_and_process_spectra works", {
skip_if_not_installed("targets")
skip_if_not_installed("tarchetypes")
skip_on_cran()
targets::tar_dir({ # tar_dir() runs code from a temporary directory.
targets::tar_script({
library(maldipickr)
list(
tar_import_and_process_spectra(
name = "anaerobe",
raw_spectra_directories = system.file(
"toy-species-spectra",
package = "maldipickr"),
tolerance = 1
)
)},ask = FALSE)
# Enough targets are created.
out <- targets::tar_manifest(callr_function = NULL)
expect_equal(nrow(out), 7L)
library(maldipickr)
list(
tar_import_and_process_spectra(
name = "anaerobe",
raw_spectra_directories = system.file(
"toy-species-spectra",
package = "maldipickr"),
tolerance = 1
)
)},ask = FALSE)
# Enough targets are created.
out <- targets::tar_manifest(callr_function = NULL)
expect_equal(nrow(out), 7L)
# Nodes in the graph are connected properly.
out <- targets::tar_network(callr_function = NULL, targets_only = TRUE)$edges
out <- dplyr::arrange(out, from, to)
rownames(out) <- NULL
exp <- tibble::tribble(
~from, ~to,
"anaerobe_checks", "anaerobe_spectra_stats",
"anaerobe_checks", "anaerobe_valid_spectra",
"anaerobe_plates", "anaerobe_spectra_raw",
"anaerobe_plates", "anaerobe_spectra_stats",
"anaerobe_plates_files", "anaerobe_plates",
"anaerobe_spectra_raw", "anaerobe_checks",
"anaerobe_spectra_raw", "anaerobe_valid_spectra",
"anaerobe_valid_spectra", "anaerobe_processed"
)
exp <- dplyr::arrange(exp, from, to)
rownames(exp) <- NULL
expect_equal(out, exp)
# The pipeline produces correctly formatted output.
capture.output(suppressWarnings(targets::tar_make(callr_function = NULL)))
expect_equal( nrow(
targets::tar_read(anaerobe_processed)[[1]][["metadata"]]
), 6L )
})
})
test_that("tar_import_and_process_spectra fails on wrong input", {
skip_if_not_installed("targets")
skip_if_not_installed("tarchetypes")
skip_on_cran()
targets::tar_dir({ # tar_dir() runs code from a temporary directory.
targets::tar_script({
library(maldipickr)
list(
tar_import_and_process_spectra(
name = "anaerobe",
raw_spectra_directories = "data_directory_that_should_not_exist",
tolerance = 1
)
)},ask = FALSE)
expect_error(tar_make(), class = "tar_condition_validate")
})
})
```
Expand Down
62 changes: 61 additions & 1 deletion tests/testthat/test-tar_import_and_process_spectra.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,65 @@
# WARNING - Generated by {fusen} from dev/maldipickr-workflow-with-targets.Rmd: do not edit by hand

# Tests for targets factory borrowed from
# src: https://github.com/ropensci/jagstargets/blob/2bad8b2958d0252a5993d341d38f8d9d02aeb41a/tests/testthat/test-tar_jags_rep_dic.R
test_that("tar_import_and_process_spectra works", {
expect_true(inherits(tar_import_and_process_spectra, "function"))
skip_if_not_installed("targets")
skip_if_not_installed("tarchetypes")
skip_on_cran()
targets::tar_dir({ # tar_dir() runs code from a temporary directory.
targets::tar_script({
library(maldipickr)
list(
tar_import_and_process_spectra(
name = "anaerobe",
raw_spectra_directories = system.file(
"toy-species-spectra",
package = "maldipickr"),
tolerance = 1
)
)},ask = FALSE)
# Enough targets are created.
out <- targets::tar_manifest(callr_function = NULL)
expect_equal(nrow(out), 7L)
# Nodes in the graph are connected properly.
out <- targets::tar_network(callr_function = NULL, targets_only = TRUE)$edges
out <- dplyr::arrange(out, from, to)
rownames(out) <- NULL
exp <- tibble::tribble(
~from, ~to,
"anaerobe_checks", "anaerobe_spectra_stats",
"anaerobe_checks", "anaerobe_valid_spectra",
"anaerobe_plates", "anaerobe_spectra_raw",
"anaerobe_plates", "anaerobe_spectra_stats",
"anaerobe_plates_files", "anaerobe_plates",
"anaerobe_spectra_raw", "anaerobe_checks",
"anaerobe_spectra_raw", "anaerobe_valid_spectra",
"anaerobe_valid_spectra", "anaerobe_processed"
)
exp <- dplyr::arrange(exp, from, to)
rownames(exp) <- NULL
expect_equal(out, exp)
# The pipeline produces correctly formatted output.
capture.output(suppressWarnings(targets::tar_make(callr_function = NULL)))
expect_equal( nrow(
targets::tar_read(anaerobe_processed)[[1]][["metadata"]]
), 6L )
})
})
test_that("tar_import_and_process_spectra fails on wrong input", {
skip_if_not_installed("targets")
skip_if_not_installed("tarchetypes")
skip_on_cran()
targets::tar_dir({ # tar_dir() runs code from a temporary directory.
targets::tar_script({
library(maldipickr)
list(
tar_import_and_process_spectra(
name = "anaerobe",
raw_spectra_directories = "data_directory_that_should_not_exist",
tolerance = 1
)
)},ask = FALSE)
expect_error(tar_make(), class = "tar_condition_validate")
})
})

0 comments on commit 875e2a2

Please sign in to comment.