Skip to content

Commit

Permalink
Fix bug and add test that would've caught it
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanhmorris committed Nov 19, 2024
1 parent 36ec48c commit 83a1d65
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 15 deletions.
4 changes: 4 additions & 0 deletions hewr/DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,7 @@ Imports:
urca
Remotes:
https://github.com/cdcgov/forecasttools
Suggests:
testthat (>= 3.0.0),
withr
Config/testthat/edition: 3
18 changes: 9 additions & 9 deletions hewr/R/directory_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ parse_model_run_dir_path <- function(model_run_dir_path) {
location <- fs::path_file(model_run_dir_path)

return(c(
list(location = location),
parse_model_batch_dir(batch_dir)
location = location,
parse_model_batch_dir_name(batch_dir)
))
}

Expand All @@ -76,14 +76,14 @@ parse_model_run_dir_path <- function(model_run_dir_path) {
#' that match the pattern for a forecast run for a
#' given disease and optionally a given report date.
#'
#' @param dir_of_forecast_dirs Directory in which to look for
#' subdirectories representing individual forecast date / pathogen /
#' dataset combinations.
#' @param dir_of_batch_dirs Directory in which to look for
#' "model batch" directories, each of which represents an
#' individual forecast date / pathogen / dataset combination.
#' @param diseases Names of the diseases to match, as a vector of strings,
#' or a single disease as a string.
#' @return A vector of paths to the forecast subdirectories.
get_all_forecast_dirs <- function(dir_of_forecast_dirs,
diseases) {
get_all_model_batch_dirs <- function(dir_of_batch_dirs,
diseases) {
# disease names are lowercase by convention
match_patterns <- stringr::str_c(tolower(diseases),
"_r",
Expand All @@ -92,11 +92,11 @@ get_all_forecast_dirs <- function(dir_of_forecast_dirs,

dirs <- tibble::tibble(
dir_path = fs::dir_ls(
dir_of_forecast_dirs,
dir_of_batch_dirs,
type = "directory"
)
) |>
dplyr::filter(str_starts(
dplyr::filter(stringr::str_starts(
fs::path_file(dir_path),
match_patterns
)) |>
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions hewr/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(hewr)

test_check("hewr")
64 changes: 64 additions & 0 deletions hewr/tests/testthat/test_directory_utils.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
test_that("get_all_model_batch_dirs returns expected output", {
withr::with_tempdir({
## create some directories
valid_covid <- c(
"covid-19_r_2024-02-01_f_2021-01-01_t_2024-01-31",
"covid-19_r"
)
valid_flu <- c(
"influenza_r_2022-11-12_f_2022-11-01_t_2022_11_10",
"influenza_r"
)
valid_dirs <- c(valid_flu, valid_covid)

invalid_dirs <- c(
"this_is_not_valid",
"covid19_r",
"covid-19-r",
"influenza-r",
"influnza_r",
"covid-19",
"influenza"
)

invalid_files <- c(
"covid-19_r.txt",
"influenza_r.txt"
)
fs::dir_create(c(valid_dirs, invalid_dirs))
fs::file_create(invalid_files)
expected_all_files <- c(
valid_dirs,
invalid_dirs,
invalid_files
)

result_all <- fs::dir_ls(".") |> fs::path_file()

result_valid <- get_all_model_batch_dirs(
".",
c("COVID-19", "Influenza")
)

result_valid_alt <- get_all_model_batch_dirs(
".",
c("Influenza", "COVID-19")
)

result_valid_covid <- get_all_model_batch_dirs(
".",
"COVID-19"
)

result_valid_flu <- get_all_model_batch_dirs(
".",
"Influenza"
)

expect_setequal(result_all, expected_all_files)
expect_setequal(result_valid, c(valid_flu, valid_covid))
expect_setequal(result_valid_alt, c(valid_flu, valid_covid))
expect_setequal(result_valid_covid, valid_covid)
expect_setequal(result_valid_flu, valid_flu)
})
})

0 comments on commit 83a1d65

Please sign in to comment.