I am working with an internal package that has a series of functions utilizing the output from previous functions. Think of a group of mathematical models that are used to forecast multiple time periods into the future. There are many interdependencies between the functions and the submodels.
Below is an oversimplified example of those interdependencies:
current_quarter = as.Date("2021-04-01")
my_data = tibble(balance = c(1,2,3,4,5)) # This would normally be generated by earlier function calls
scaling_factors = c(2,4,0.5,0.75,1) # This would normally be generated by earlier function calls
first_function = function(.data, current_quarter) {
.data = .data %>% mutate(date = current_quarter)
}
second_function = function(.data, scaling_factors){
.data = .data %>% mutate(new_bal = balance * scaling_factors)
}
my_data2 = first_function(my_data, current_quarter)
output_data = second_function(my_data2, scaling factors)
I would like to test the output from the second function so I wrote a test as such:
context("Scaling")
test_that("Scaling of Balances", {
local_edition(3)
expect_snapshot(second_function())
})
One way to insert the correct arguments is to simply run the code in debug mode and the save off the data frames/arguments before the function I would like to test is run. Then load the data within the actual test like this:
test_that("Scaling of Balances", {
local_edition(3)
expect_snapshot(second_function(.data = readRDS(my_saved_data))
})
Is there a better way to do this so that I do not have to keep track of a bunch of different data objects? In other words, if I know that the code before the function to be tested produces the correct data frame, is there a way to run my test without updating the arguments within the actual test? Or am I just doing this all wrong?
Note: I have many more 2 functions and hundreds of thousands of observations within my data frame. The functions are very complicated (although I have done my best to "functionalize" the model) and so it is impossible or impractical to use anything other than expect_snapshot as a test... But I also need a way to easily check to see if the output has changed when I change the code, and how.
Thanks