1- library(fs )
2- library(dplyr )
3- library(testthat )
4- library(hubhelpr )
5- library(forecasttools )
6-
7- run_update_hub_target_data_test <- function (
1+ update_hub_target_data_test <- function (
82 disease ,
93 nhsn_col ,
104 nssp_col ,
115 nhsn_target ,
126 nssp_target
137) {
148 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" )
9+ fs :: dir_create(base_hub_path )
10+ output_file <- fs :: path(base_hub_path , " target-data/time-series.parquet" )
1711 fs :: dir_create(fs :: path(base_hub_path , " target-data" ))
1812
1913 mock_target_ts <- tibble :: tibble(
20- date = as.Date (character ()),
14+ date = lubridate :: as_date (character ()),
2115 observation = numeric (),
2216 location = character (),
23- as_of = as.Date (character ()),
17+ as_of = lubridate :: as_date (character ()),
2418 target = character ()
2519 )
2620 forecasttools :: write_tabular_file(mock_target_ts , output_file )
2721
2822 mock_nhsn <- tibble :: tibble(
29- weekendingdate = as.Date (c(
23+ weekendingdate = lubridate :: as_date (c(
3024 " 2024-11-09" ,
3125 " 2024-11-16" ,
3226 " 2024-11-09" ,
@@ -37,7 +31,7 @@ run_update_hub_target_data_test <- function(
3731 totalconfrsvnewadm = c(3 , 4 , 3 , 2 )
3832 )
3933 mock_nssp <- tibble :: tibble(
40- week_end = as.Date (c(" 2024-11-09" , " 2024-11-09" )),
34+ week_end = lubridate :: as_date (c(" 2024-11-09" , " 2024-11-09" )),
4135 county = c(" All" , " All" ),
4236 geography = c(" Alabama" , " Alaska" ),
4337 percent_visits_covid = c(1.0 , 0.2 ),
@@ -52,17 +46,16 @@ run_update_hub_target_data_test <- function(
5246 } else if (dataset == " nssp_prop_ed_visits" ) {
5347 mock_nssp | > dplyr :: select(week_end , geography , all_of(columns ))
5448 } else {
55- stop(" Unknown dataset " )
49+ stop(" Dataset type not supported by the Hubs " )
5650 }
5751 },
5852 ns = " forecasttools"
5953 )
6054
6155 expect_silent(
62- update_hub_target_data(
56+ hubhelpr :: update_hub_target_data(
6357 base_hub_path = base_hub_path ,
64- disease = disease ,
65- first_full_weekending_date = as.Date(" 2024-11-09" )
58+ disease = disease
6659 )
6760 )
6861
@@ -78,23 +71,30 @@ run_update_hub_target_data_test <- function(
7871 dplyr :: select(date , observation , location , target ) | >
7972 dplyr :: arrange(date , location ),
8073 tibble :: tibble(
81- date = as.Date(c(mock_nhsn $ weekendingdate , mock_nssp $ week_end )),
74+ date = lubridate :: as_date(c(
75+ mock_nhsn $ weekendingdate ,
76+ mock_nssp $ week_end
77+ )),
8278 observation = c(
8379 as.numeric(mock_nhsn [[nhsn_col ]]),
8480 as.numeric(mock_nssp [[nssp_col ]]) / 100
8581 ),
8682 location = c(
87- us_location_recode(mock_nhsn $ jurisdiction , " abbr" , " code" ),
88- us_location_recode(mock_nssp $ geography , " name" , " code" )
83+ forecasttools :: us_location_recode(
84+ mock_nhsn $ jurisdiction ,
85+ " abbr" ,
86+ " code"
87+ ),
88+ forecasttools :: us_location_recode(mock_nssp $ geography , " name" , " code" )
8989 ),
9090 target = c(rep(nhsn_target , 4 ), rep(nssp_target , 2 ))
9191 ) | >
9292 dplyr :: arrange(date , location )
9393 )
9494}
9595
96- test_that(" update_hub_target_data returns exactly the mocked NHSN data for covid" , {
97- run_update_hub_target_data_test (
96+ test_that(" update_hub_target_data returns expected data for covid" , {
97+ update_hub_target_data_test (
9898 disease = " covid" ,
9999 nhsn_col = " totalconfc19newadm" ,
100100 nssp_col = " percent_visits_covid" ,
@@ -103,12 +103,22 @@ test_that("update_hub_target_data returns exactly the mocked NHSN data for covid
103103 )
104104})
105105
106- test_that(" update_hub_target_data returns exactly the mocked NHSN data for rsv" , {
107- run_update_hub_target_data_test (
106+ test_that(" update_hub_target_data returns expected data for rsv" , {
107+ update_hub_target_data_test (
108108 disease = " rsv" ,
109109 nhsn_col = " totalconfrsvnewadm" ,
110110 nssp_col = " percent_visits_rsv" ,
111111 nhsn_target = " wk inc rsv hosp" ,
112112 nssp_target = " wk inc rsv prop ed visits"
113113 )
114114})
115+
116+ test_that(" update_hub_target_data errors for unsupported disease" , {
117+ expect_error(
118+ update_hub_target_data(
119+ base_hub_path = tempdir(),
120+ disease = " flu"
121+ ),
122+ " 'disease' must be either 'covid' or 'rsv'"
123+ )
124+ })
0 commit comments