Drake: Incorporate unit testing with drake pipelines

Created on 1 May 2020  路  3Comments  路  Source: ropensci/drake

Prework

  • [x] Read and abide by drake's code of conduct.
  • [x] Search for duplicates among the existing issues, both open and closed.
  • [x] If you think your question has a quick and definite answer, consider posting to Stack Overflow under the drake-r-package tag. (If you anticipate extended follow-up and discussion, you are already in the right place!)

Question

I'm writing some production pipelines for which drake has made my life so much better (huge thank you).

I have a ton of functions which I use within my drake pipeline. Apart from sending these functions into a separate package, is there a workflow / hack with which I can incorporate unit-testing for all the functions called by drake, and have the drake pipeline fail if any of the unit-tests fail, before running the drake pipeline?

question

All 3 comments

As you said, workflows structured as R packages (https://books.ropensci.org/drake/projects.html#workflows-as-r-packages; example: drakepkg) have the organizational layout to accommodate both a pipeline and a set of unit tests.

Other than that, the challenge is that in most situations, one's functions take a long time to run in drake pipelines, but unit tests need to be fast. So there is sometimes need to toggle a function to run in test mode (fast, returns partial results) vs production mode (slow, returns all results). downsize, the first package I ever submitted to CRAN, was an early attempt to make this easier. You could also achieve the same mechanism using your own system of environment variables, or use the mockr package.

drake itself tries not to get involved in this process. One option is to write a master R script that runs the unit tests just prior to make()/r_make().

Things get trickier if you only want to run certain parts of the pipeline: in other words, run pieces of validation at the target level and combine with keep_going = TRUE (so targets with validated functions run and targets with problematic functions get skipped). This style of function writing might help:

```{r}
original_function <- function(...) {
# stuff
}

original_function_validate <- function(...) {
# run some unit tests
}

original_function_deploy <- function(...) {
original_function_validate()
original_function()
}

plan <- drake_plan(
x = original_function_deploy(),
y = ...
)

make(plan, keep_going = TRUE)


Alternatively, custom triggers might help if you want failed validation to trigger a skip rather than an error.

```{r}
plan <- drake_plan(
  x = target(
    original_function(...),
    trigger = trigger(condition = original_function_validate(), mode = "blacklist")
  )
)

Thanks @wlandau

Was this page helpful?
0 / 5 - 0 ratings

Related issues

wlandau picture wlandau  路  4Comments

AlexAxthelm picture AlexAxthelm  路  8Comments

wlandau picture wlandau  路  8Comments

bart1 picture bart1  路  7Comments

rsangole picture rsangole  路  7Comments