generated from CDCgov/template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test coverage of GI/delay reader
- Loading branch information
Showing
3 changed files
with
269 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
write_sample_parameters_file <- function(value, | ||
path, | ||
state, | ||
param, | ||
disease, | ||
parameter, | ||
start_date, | ||
end_date) { | ||
df <- data.frame( | ||
start_date = as.Date(start_date), | ||
state = state, | ||
disease = disease, | ||
parameter = parameter, | ||
end_date = end_date, | ||
disease = disease, | ||
value = I(list(value)) | ||
) | ||
|
||
con <- DBI::dbConnect(duckdb::duckdb()) | ||
|
||
duckdb::duckdb_register(con, "test_table", df) | ||
# This is bad practice but `dbBind()` doesn't allow us to parameterize COPY | ||
# ... TO. The danger of doing it this way seems quite low risk because it's | ||
# an ephemeral from a temporary in-memory DB. There's no actual database to | ||
# guard against a SQL injection attack. | ||
query <- paste0("COPY (SELECT * FROM test_table) TO '", path, "'") | ||
DBI::dbExecute(con, query) | ||
DBI::dbDisconnect(con) | ||
|
||
invisible(path) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,208 @@ | ||
test_that("Can read on happy path", { | ||
expected <- c(0.8, 0.2) | ||
path <- "test.parquet" | ||
parameter <- "delay" | ||
start_date <- as.Date("2023-01-01") | ||
|
||
# COVID-19 | ||
disease <- "COVID-19" | ||
withr::with_tempdir({ | ||
write_sample_parameters_file( | ||
value = expected, | ||
path = path, | ||
state = "test", | ||
disease = disease, | ||
parameter = parameter, | ||
param = parameter, | ||
start_date = start_date, | ||
end_date = NA | ||
) | ||
actual <- read_interval_pmf( | ||
path = path, | ||
disease = disease, | ||
as_of_date = start_date + 1, | ||
parameter = parameter | ||
) | ||
}) | ||
expect_equal(actual, expected) | ||
|
||
|
||
# Influenza | ||
disease <- "Influenza" | ||
withr::with_tempdir({ | ||
write_sample_parameters_file( | ||
value = expected, | ||
path = path, | ||
state = "test", | ||
disease = disease, | ||
parameter = parameter, | ||
param = parameter, | ||
start_date = start_date, | ||
end_date = NA | ||
) | ||
actual <- read_interval_pmf( | ||
path = path, | ||
disease = disease, | ||
as_of_date = start_date + 1, | ||
parameter = parameter | ||
) | ||
}) | ||
expect_equal(actual, expected) | ||
}) | ||
|
||
|
||
test_that("Not a PMF errors", { | ||
expected <- "hello" | ||
path <- "test.parquet" | ||
parameter <- "delay" | ||
start_date <- as.Date("2023-01-01") | ||
|
||
# COVID-19 | ||
disease <- "COVID-19" | ||
withr::with_tempdir({ | ||
write_sample_parameters_file( | ||
value = expected, | ||
path = path, | ||
state = "test", | ||
disease = disease, | ||
parameter = parameter, | ||
param = parameter, | ||
start_date = start_date, | ||
end_date = NA | ||
) | ||
expect_error(read_interval_pmf( | ||
path = path, | ||
disease = disease, | ||
as_of_date = start_date + 1, | ||
parameter = parameter | ||
)) | ||
}) | ||
}) | ||
|
||
test_that("Invalid disease errors", { | ||
expected <- c(0.8, 0.2) | ||
path <- "test.parquet" | ||
parameter <- "delay" | ||
start_date <- as.Date("2023-01-01") | ||
disease <- "not_a_valid_disease" | ||
|
||
withr::with_tempdir({ | ||
write_sample_parameters_file( | ||
value = expected, | ||
path = path, | ||
state = "test", | ||
disease = disease, | ||
parameter = parameter, | ||
param = parameter, | ||
start_date = start_date, | ||
end_date = NA | ||
) | ||
|
||
expect_error(read_interval_pmf( | ||
path = path, | ||
disease = disease, | ||
as_of_date = start_date + 1, | ||
parameter = parameter | ||
)) | ||
}) | ||
}) | ||
|
||
test_that("Invalid parameter errors", { | ||
expected <- c(0.8, 0.2) | ||
path <- "test.parquet" | ||
parameter <- "not_a_valid_parameter" | ||
start_date <- as.Date("2023-01-01") | ||
disease <- "COVID-19" | ||
|
||
withr::with_tempdir({ | ||
write_sample_parameters_file( | ||
value = expected, | ||
path = path, | ||
state = "test", | ||
disease = disease, | ||
parameter = parameter, | ||
param = parameter, | ||
start_date = start_date, | ||
end_date = NA | ||
) | ||
|
||
expect_error(read_interval_pmf( | ||
path = path, | ||
disease = disease, | ||
as_of_date = start_date + 1, | ||
parameter = parameter | ||
)) | ||
}) | ||
}) | ||
|
||
test_that("Return isn't exactly one errors", { | ||
expected <- c(0.8, 0.2) | ||
path <- "test.parquet" | ||
parameter <- "delay" | ||
start_date <- as.Date("2023-01-01") | ||
disease <- "COVID-19" | ||
|
||
withr::with_tempdir({ | ||
write_sample_parameters_file( | ||
value = expected, | ||
path = path, | ||
state = "test", | ||
disease = disease, | ||
parameter = parameter, | ||
param = parameter, | ||
start_date = start_date, | ||
end_date = NA | ||
) | ||
|
||
# Date too early | ||
expect_error(read_interval_pmf( | ||
path = path, | ||
disease = disease, | ||
as_of_date = start_date - 1, | ||
parameter = parameter | ||
)) | ||
}) | ||
}) | ||
|
||
test_that("No file exists errors", { | ||
expected <- c(0.8, 0.2) | ||
path <- "test.parquet" | ||
parameter <- "delay" | ||
start_date <- as.Date("2023-01-01") | ||
disease <- "COVID-19" | ||
|
||
expect_error(read_interval_pmf( | ||
path = "not_a_real_file", | ||
disease = disease, | ||
as_of_date = start_date - 1, | ||
parameter = parameter | ||
)) | ||
}) | ||
|
||
test_that("Invalid query throws wrapped error", { | ||
expected <- c(0.8, 0.2) | ||
path <- "test.parquet" | ||
parameter <- "delay" | ||
start_date <- as.Date("2023-01-01") | ||
disease <- "COVID-19" | ||
|
||
withr::with_tempdir({ | ||
write_sample_parameters_file( | ||
value = expected, | ||
path = path, | ||
state = "test", | ||
disease = disease, | ||
parameter = parameter, | ||
param = parameter, | ||
start_date = start_date, | ||
end_date = NA | ||
) | ||
|
||
expect_error(read_interval_pmf( | ||
path = path, | ||
disease = disease, | ||
as_of_date = "abc123", | ||
parameter = parameter | ||
)) | ||
}) | ||
}) |