From 875e2a2e6e5d95bbb657c97eb0a6d9a2b8cef100 Mon Sep 17 00:00:00 2001 From: cpauvert Date: Mon, 13 May 2024 14:54:17 +0200 Subject: [PATCH] add test for import spectra target factory inspired by examples in ropensci/jagstargets --- dev/config_fusen.yaml | 8 ++- dev/maldipickr-workflow-with-targets.Rmd | 68 +++++++++++++++---- .../test-tar_import_and_process_spectra.R | 62 ++++++++++++++++- 3 files changed, 122 insertions(+), 16 deletions(-) diff --git a/dev/config_fusen.yaml b/dev/config_fusen.yaml index def8a66..fc41c54 100644 --- a/dev/config_fusen.yaml +++ b/dev/config_fusen.yaml @@ -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 diff --git a/dev/maldipickr-workflow-with-targets.Rmd b/dev/maldipickr-workflow-with-targets.Rmd index 419e4bc..a019914 100644 --- a/dev/maldipickr-workflow-with-targets.Rmd +++ b/dev/maldipickr-workflow-with-targets.Rmd @@ -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") }) }) ``` diff --git a/tests/testthat/test-tar_import_and_process_spectra.R b/tests/testthat/test-tar_import_and_process_spectra.R index 612b7c9..b770c89 100644 --- a/tests/testthat/test-tar_import_and_process_spectra.R +++ b/tests/testthat/test-tar_import_and_process_spectra.R @@ -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") + }) })