Skip to content

Commit

Permalink
Make testing setup compatible with CI, remove namespace qualification…
Browse files Browse the repository at this point in the history
… in test scripts
  • Loading branch information
dylanhmorris committed Dec 13, 2024
1 parent db7bf04 commit 772bc8c
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 31 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Suggests:
tidyr,
roxygen2,
usethis,
testthat,
testthat (>= 3.0.0),
rcmdcheck,
httptest
VignetteBuilder:
Expand All @@ -63,3 +63,4 @@ LazyData: true
Remotes:
hubverse-org/hubData
BugReports: https://github.com/CDCgov/forecasttools/issues
Config/testthat/edition: 3
12 changes: 12 additions & 0 deletions tests/testthat.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# This file is part of the standard setup for testthat.
# It is recommended that you do not modify it.
#
# Where should you do additional test configuration?
# Learn more about the roles of various files in:
# * https://r-pkgs.org/testing-design.html#sec-tests-files-overview
# * https://testthat.r-lib.org/articles/special-files.html

library(testthat)
library(forecasttools)

test_check("forecasttools")
18 changes: 9 additions & 9 deletions tests/testthat/test_epiweek_to_date.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ schema <- tidyr::crossing(
day_of_week = 1:7
)

testthat::test_that(paste0(
test_that(paste0(
"epiweek_to_date() function's internal ",
"validation passes for USA epiweeks for ",
"every epiweek in 1:52 for every epiyear ",
"in 1800:2200, for every day of the epiweek"
), {
testthat::expect_no_error(
expect_no_error(
with_usa_dates <- schema |>
dplyr::mutate(
date = forecasttools::epiweek_to_date(
Expand All @@ -24,13 +24,13 @@ testthat::test_that(paste0(
)
})

testthat::test_that(paste0(
test_that(paste0(
"epiweek_to_date() function's internal ",
"validation passes for ISO (epi)weeks for ",
"every epiweek in 1:52 for every epiyear ",
"in 1800:2200, for every day of the epiweek"
), {
testthat::expect_no_error(
expect_no_error(
with_iso_dates <- schema |>
dplyr::mutate(
date = forecasttools::epiweek_to_date(
Expand All @@ -45,14 +45,14 @@ testthat::test_that(paste0(
})


testthat::test_that(paste0(
test_that(paste0(
"epiweek_to_date() function's internal ",
"validation fails if you try to get the ",
"start of an epiweek that doesn't exist ",
"but passes if you get a valid epiweek"
), {
## 2020 had an epiweek 53
testthat::expect_no_error(
expect_no_error(
forecasttools::epiweek_to_date(
53,
2020,
Expand All @@ -62,7 +62,7 @@ testthat::test_that(paste0(
)

## 2021 did not
testthat::expect_error(
expect_error(
forecasttools::epiweek_to_date(
53,
2021,
Expand All @@ -72,7 +72,7 @@ testthat::test_that(paste0(
)

## a single failure should raise an error
testthat::expect_error(
expect_error(
forecasttools::epiweek_to_date(
53,
c(2020, 2021),
Expand All @@ -83,7 +83,7 @@ testthat::test_that(paste0(

## but the validation should be vectorized
## and succeed accordingly
testthat::expect_no_error(
expect_no_error(
forecasttools::epiweek_to_date(
c(53, 52),
c(2020, 2021),
Expand Down
14 changes: 7 additions & 7 deletions tests/testthat/test_inferencedata_dataframe_to_tidydraws.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
testthat::test_that("inferencedata_to_tidy_draws converts data correctly", {
test_that("inferencedata_to_tidy_draws converts data correctly", {
data("ex_inferencedata_dataframe")
result <- inferencedata_to_tidy_draws(ex_inferencedata_dataframe)

testthat::expect_setequal(colnames(result), c("group", "data"))
testthat::expect_setequal(result$group, c("posterior", "predictions"))
expect_setequal(colnames(result), c("group", "data"))
expect_setequal(result$group, c("posterior", "predictions"))

testthat::expect_equal(
expect_equal(
colnames(result$data[[1]]),
c(
".chain", ".iteration", ".draw", "a", "b[troll_shore]", "b[drawn]",
Expand All @@ -17,18 +17,18 @@ testthat::test_that("inferencedata_to_tidy_draws converts data correctly", {
)
)

testthat::expect_equal(
expect_equal(
colnames(result$data[[2]]),
c(
".chain", ".iteration", ".draw", "obs[woken]", "obs[awash]",
"obs[inter_ought]"
)
)

testthat::expect_no_error(
expect_no_error(
tidybayes::spread_draws(result$data[[1]], a, b[x], c[y, z])
)
testthat::expect_no_error(
expect_no_error(
tidybayes::spread_draws(result$data[[2]], obs[a])
)
})
20 changes: 10 additions & 10 deletions tests/testthat/test_simple_bottom_up.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ test_u_mat <- matrix(c(0.1, 0.3, 0.7), nrow = 1)
location_names <- c("A", "B", "C")
colnames(test_u_mat) <- location_names

testthat::test_that("`validate_base_forecasts` throws an error", {
testthat::expect_error(validate_base_forecasts(bad_test_base_forecasts,
test_that("`validate_base_forecasts` throws an error", {
expect_error(validate_base_forecasts(bad_test_base_forecasts,
test_cp,
value_to_aggregate_col = "hosps",
rank_quantity_col = "rank_quantity",
Expand All @@ -42,7 +42,7 @@ testthat::test_that("`validate_base_forecasts` throws an error", {
))
})

testthat::test_that("`count_trajectories` returns correct number of trajs", {
test_that("`count_trajectories` returns correct number of trajs", {
result <- count_trajectories(test_base_forecasts,
location_col = "location",
date_col = "date"
Expand All @@ -51,10 +51,10 @@ testthat::test_that("`count_trajectories` returns correct number of trajs", {
location = c("A", "B", "C"),
n_sample_trajs = c(2, 2, 2)
)
testthat::expect_equal(result, expected_output)
expect_equal(result, expected_output)
})

testthat::test_that("`copula2tbl` returns the expected output", {
test_that("`copula2tbl` returns the expected output", {
i <- 1
location_col <- "location"

Expand All @@ -65,10 +65,10 @@ testthat::test_that("`copula2tbl` returns the expected output", {
location = location_names
)

testthat::expect_equal(result, expected_output)
expect_equal(result, expected_output)
})

testthat::test_that("`rank_sampled_trajectories` returns correct rankings", {
test_that("`rank_sampled_trajectories` returns correct rankings", {
ranked_base_forecasts <-
rank_sampled_trajectories(test_base_forecasts,
location_col = "location",
Expand All @@ -83,10 +83,10 @@ testthat::test_that("`rank_sampled_trajectories` returns correct rankings", {
rank = c(1, 2, 1, 2, 1, 2)
)

testthat::expect_equal(ranked_base_forecasts, expected_rankings)
expect_equal(ranked_base_forecasts, expected_rankings)
})

testthat::test_that("sample_aggregated_trajectories", {
test_that("sample_aggregated_trajectories", {
i <- 1
location_col <- "location"
test_sample <- copula2tbl(
Expand Down Expand Up @@ -123,5 +123,5 @@ testthat::test_that("sample_aggregated_trajectories", {
forecast = c(123, 200)
)

testthat::expect_equal(result, expected_output)
expect_equal(result, expected_output)
})
8 changes: 4 additions & 4 deletions tests/testthat/test_to_hubverse.R
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
testthat::test_that(
test_that(
paste0(
"get_hubverse_table errors if reference date ",
"is the wrong day of the week"
),
{
testthat::expect_error(
expect_error(
forecasttools::get_hubverse_table(
tibble::tibble(),
"2025-01-01"
),
"which is day number 4"
)
testthat::expect_error(
expect_error(
forecasttools::get_hubverse_table(
tibble::tibble(),
"2025-01-01",
week_start = 3
),
"which is day number 1"
)
testthat::expect_error(
expect_error(
forecasttools::get_hubverse_table(
tibble::tibble(),
"2025-01-01",
Expand Down

0 comments on commit 772bc8c

Please sign in to comment.