Skip to content

Commit

Permalink
Add tests for scripts that generate example-complex-forecast data
Browse files Browse the repository at this point in the history
While the tests can't run during R checks, they were useful for
local development (in this case, ensuring that switching the data source
from local disk to S3 didn't impact the resulting .rda files)
  • Loading branch information
bsweger committed Aug 27, 2024
1 parent dae9d18 commit 2bdb407
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 14 deletions.
27 changes: 15 additions & 12 deletions .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# Contributing to hubExamples

This outlines how to propose a change to `hubExamples`.
This outlines how to propose a change to `hubExamples`.
For more general info about contributing to this, and other hubverse packages, please see the
[**hubverse contributing guide**](https://hubverse.io/en/latest/overview/contribute.html).

You can fix typos, spelling mistakes, or grammatical errors in the documentation directly using the GitHub web interface, as long as the changes are made in the _source_ file.
This generally means you'll need to edit [roxygen2 comments](https://roxygen2.r-lib.org/articles/roxygen2.html) in an `.R`, not a `.Rd` file.
You can fix typos, spelling mistakes, or grammatical errors in the documentation directly using the GitHub web interface, as long as the changes are made in the _source_ file.
This generally means you'll need to edit [roxygen2 comments](https://roxygen2.r-lib.org/articles/roxygen2.html) in an `.R`, not a `.Rd` file.
You can find the `.R` file that generates the `.Rd` by reading the comment in the first line.

## Bigger changes

If you want to make a bigger change, it's a good idea to first file an issue and make sure someone from the team agrees that it’s needed.
If you’ve found a bug, please file an issue that illustrates the bug with a minimal
If you want to make a bigger change, it's a good idea to first file an issue and make sure someone from the team agrees that it’s needed.
If you’ve found a bug, please file an issue that illustrates the bug with a minimal
[reprex](https://www.tidyverse.org/help/#reprex) (this will also help you write a unit test, if needed).

Our procedures for contributing bigger changes, code in particular, generally follow those advised by the tidyverse dev team, including following the tidyverse style guide for code and recording user facing changes in `NEWS.md`.
Expand All @@ -20,8 +20,8 @@ Our procedures for contributing bigger changes, code in particular, generally fo

* Fork the package and clone onto your computer. If you haven't done this before, we recommend using `usethis::create_from_github("hubverse-org/hubExamples", fork = TRUE)`.

* Install all development dependencies with `devtools::install_dev_deps()`, and then make sure the package passes R CMD check by running `devtools::check()`.
If R CMD check doesn't pass cleanly, it's a good idea to ask for help before continuing.
* Install all development dependencies with `devtools::install_dev_deps()`, and then make sure the package passes R CMD check by running `devtools::check()`.
If R CMD check doesn't pass cleanly, it's a good idea to ask for help before continuing.
* Create a Git branch for your pull request (PR). We recommend using `usethis::pr_init("brief-description-of-change")`.

* Make your changes, commit to git, and then create a PR by running `usethis::pr_push()`, and following the prompts in your browser.
Expand All @@ -30,15 +30,18 @@ Our procedures for contributing bigger changes, code in particular, generally fo

* For user-facing changes, add a bullet to the top of `NEWS.md` (i.e. just below the first header). Follow the style described in <https://style.tidyverse.org/news.html>.

* If you're changing the scripts in `data-raw/`, you can run tests locally: `devtools::test()` (because the
contents of `data-raw` are not included in the package, these tests are skipped during R checks).d

### Code style

* New code should follow the tidyverse [style guide](https://style.tidyverse.org).
You can use the [styler](https://CRAN.R-project.org/package=styler) package to apply these styles, but please don't restyle code that has nothing to do with your PR.
* New code should follow the tidyverse [style guide](https://style.tidyverse.org).
You can use the [styler](https://CRAN.R-project.org/package=styler) package to apply these styles, but please don't restyle code that has nothing to do with your PR.

* We use [roxygen2](https://cran.r-project.org/package=roxygen2), with [Markdown syntax](https://cran.r-project.org/web/packages/roxygen2/vignettes/rd-formatting.html), for documentation.
* We use [roxygen2](https://cran.r-project.org/package=roxygen2), with [Markdown syntax](https://cran.r-project.org/web/packages/roxygen2/vignettes/rd-formatting.html), for documentation.

* We use [testthat](https://cran.r-project.org/package=testthat) for unit tests.
Contributions with test cases included are easier to accept.
* We use [testthat](https://cran.r-project.org/package=testthat) for unit tests.
Contributions with test cases included are easier to accept.

## Code of Conduct

Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Description: This package provides example data for forecasting and scenario
License: MIT + file LICENSE
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
Suggests:
dplyr,
ggplot2,
Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/helper-path.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
code_path <- dirname(dirname(getwd()))
forecast_data_path <- file.path(code_path, "data-raw")
64 changes: 63 additions & 1 deletion tests/testthat/test-generate_example_forecast_data.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,66 @@
library(dplyr)

# These tests are designed to help devs check their work when updating the logic that generates hubExample's
# data files. Because these scripts (i.e., the contents of data-raw/) are not included in the hubExample
# package itself, the tests will not run during R checks.
skip_if(is_checking())

# Source the script that generates the example forecast data so we can test the functions
source(file.path(forecast_data_path, "generate_example_forecast_data.R"))


test_that("forecast_outputs dataset is generated correctly", {
cols <- c("output_type", "reference_date", "horizon", "target_end_date", "location", "model_id", "target", "output_type_id", "value")

load(test_path("testdata", "forecast_outputs.rda"))
expect_equal("1", "1")
expected_forecast_outputs <- forecast_outputs %>%
arrange_at(cols) %>%
select(all_of(cols))

actual_forecast_outputs <- create_forecast_outputs() %>%
arrange_at(cols) %>%
select(all_of(cols))

expect_equal(
as.data.frame(expected_forecast_outputs),
as.data.frame(actual_forecast_outputs)
)
})


test_that("forecast_target_observations dataset is generated correctly", {
cols <- c("location", "target_end_date", "target", "output_type", "output_type_id", "observation")

load(test_path("testdata", "forecast_target_observations.rda"))
expected_forecast_target_observations <- forecast_target_observations %>%
arrange_at(cols) %>%
select(all_of(cols))

actual_forecast_target_observations <- create_forecast_target_observations() %>%
arrange_at(cols) %>%
select(all_of(cols))

expect_equal(
as.data.frame(expected_forecast_target_observations),
as.data.frame(actual_forecast_target_observations)
)
})


test_that("forecast_target_ts dataset is generated correctly", {
cols <- c("date", "location", "observation")

load(test_path("testdata", "forecast_target_ts.rda"))
expected_forecast_target_ts <- forecast_target_ts %>%
arrange_at(cols) %>%
select(all_of(cols))

actual_forecast_target_ts <- create_forecast_target_ts() %>%
arrange_at(cols) %>%
select(all_of(cols))

expect_equal(
as.data.frame(expected_forecast_target_ts),
as.data.frame(actual_forecast_target_ts)
)
})

0 comments on commit 2bdb407

Please sign in to comment.