Skip to content

Commit 7a020d2

Browse files
committed
add a test
1 parent f2008b1 commit 7a020d2

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed

tests/testthat.R

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# This file is part of the standard setup for testthat.
2+
# It is recommended that you do not modify it.
3+
#
4+
# Where should you do additional test configuration?
5+
# Learn more about the roles of various files in:
6+
# * https://r-pkgs.org/testing-design.html#sec-tests-files-overview
7+
# * https://testthat.r-lib.org/articles/special-files.html
8+
9+
library(testthat)
10+
library(hubhelpr)
11+
12+
test_check("hubhelpr")
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
library(fs)
2+
library(dplyr)
3+
library(testthat)
4+
library(hubhelpr)
5+
library(forecasttools)
6+
7+
run_update_hub_target_data_test <- function(
8+
disease,
9+
nhsn_col,
10+
nssp_col,
11+
nhsn_target,
12+
nssp_target
13+
) {
14+
base_hub_path <- tempfile(paste0("base_hub_", disease, "_"))
15+
dir_create(base_hub_path)
16+
output_file <- path(base_hub_path, "target-data/time-series.parquet")
17+
fs::dir_create(fs::path(base_hub_path, "target-data"))
18+
19+
mock_target_ts <- tibble::tibble(
20+
date = as.Date(character()),
21+
observation = numeric(),
22+
location = character(),
23+
as_of = as.Date(character()),
24+
target = character()
25+
)
26+
forecasttools::write_tabular_file(mock_target_ts, output_file)
27+
28+
mock_nhsn <- tibble::tibble(
29+
weekendingdate = as.Date(c(
30+
"2024-11-09",
31+
"2024-11-16",
32+
"2024-11-09",
33+
"2024-11-16"
34+
)),
35+
jurisdiction = c("MI", "MI", "CA", "CA"),
36+
totalconfc19newadm = c(1, 2, 1, 0),
37+
totalconfrsvnewadm = c(3, 4, 3, 2)
38+
)
39+
mock_nssp <- tibble::tibble(
40+
week_end = as.Date(c("2024-11-09", "2024-11-09")),
41+
county = c("All", "All"),
42+
geography = c("Alabama", "Alaska"),
43+
percent_visits_covid = c(1.0, 0.2),
44+
percent_visits_rsv = c(0.5, 0.15)
45+
)
46+
assignInNamespace(
47+
"pull_data_cdc_gov_dataset",
48+
function(dataset, columns, start_date = NULL, locations = NULL) {
49+
if (dataset == "nhsn_hrd_prelim") {
50+
mock_nhsn |>
51+
dplyr::select(weekendingdate, jurisdiction, all_of(columns))
52+
} else if (dataset == "nssp_prop_ed_visits") {
53+
mock_nssp |> dplyr::select(week_end, geography, all_of(columns))
54+
} else {
55+
stop("Unknown dataset")
56+
}
57+
},
58+
ns = "forecasttools"
59+
)
60+
61+
expect_silent(
62+
update_hub_target_data(
63+
base_hub_path = base_hub_path,
64+
disease = disease,
65+
first_full_weekending_date = as.Date("2024-11-09")
66+
)
67+
)
68+
69+
target_ts <- forecasttools::read_tabular_file(output_file)
70+
71+
expect_true(all(
72+
c("date", "observation", "location", "as_of", "target") %in%
73+
names(target_ts)
74+
))
75+
76+
expect_equal(
77+
target_ts |>
78+
dplyr::select(date, observation, location, target) |>
79+
dplyr::arrange(date, location),
80+
tibble::tibble(
81+
date = as.Date(c(mock_nhsn$weekendingdate, mock_nssp$week_end)),
82+
observation = c(
83+
as.numeric(mock_nhsn[[nhsn_col]]),
84+
as.numeric(mock_nssp[[nssp_col]]) / 100
85+
),
86+
location = c(
87+
us_location_recode(mock_nhsn$jurisdiction, "abbr", "code"),
88+
us_location_recode(mock_nssp$geography, "name", "code")
89+
),
90+
target = c(rep(nhsn_target, 4), rep(nssp_target, 2))
91+
) |>
92+
dplyr::arrange(date, location)
93+
)
94+
}
95+
96+
test_that("update_hub_target_data returns exactly the mocked NHSN data for covid", {
97+
run_update_hub_target_data_test(
98+
disease = "covid",
99+
nhsn_col = "totalconfc19newadm",
100+
nssp_col = "percent_visits_covid",
101+
nhsn_target = "wk inc covid hosp",
102+
nssp_target = "wk inc covid prop ed visits"
103+
)
104+
})
105+
106+
test_that("update_hub_target_data returns exactly the mocked NHSN data for rsv", {
107+
run_update_hub_target_data_test(
108+
disease = "rsv",
109+
nhsn_col = "totalconfrsvnewadm",
110+
nssp_col = "percent_visits_rsv",
111+
nhsn_target = "wk inc rsv hosp",
112+
nssp_target = "wk inc rsv prop ed visits"
113+
)
114+
})

0 commit comments

Comments
 (0)