Skip to content

Commit

Permalink
add check_metadata conditional to assist with debugging input values
Browse files Browse the repository at this point in the history
  • Loading branch information
mghoff committed Nov 13, 2023
1 parent ff430b3 commit 1807cfa
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 26 deletions.
4 changes: 3 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# eia 0.4.1

* Added dynamic error handling to `eia_data()` via metadata layer.
* Added dynamic error handling to `eia_data()` via metadata layer conditioned on
new function argument `check_metadata`.


# eia 0.4.0

Expand Down
20 changes: 14 additions & 6 deletions R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#' @param length numeric or `NULL`, number of rows to return.
#' @param offset numeric or `NULL`, number of rows to skip before return.
#' @param tidy logical or `NULL`, return a tidier result. See details.
#' @param check_metadata logical, if `TRUE` makes preemptive call to data metadata endpoint
#' to validate all input values against.
#' @param cache logical, cache result for duration of R session using memoization.
#' See details.
#' @param key API key: character if set explicitly; not needed if key is set
Expand All @@ -48,18 +50,19 @@
eia_data <- function(dir, data = NULL, facets = NULL,
freq = NULL, start = NULL, end = NULL,
sort = NULL, length = NULL, offset = NULL,
tidy = TRUE, cache = TRUE, key = eia_get_key()){
tidy = TRUE, check_metadata = FALSE, cache = TRUE,
key = eia_get_key()){
.key_check(key)
if(cache){
if (check_metadata)
.eia_metadata_check(dir, data, facets, freq, start, end, sort, length, offset, key)
if (cache){
.eia_data_memoized(dir, data, facets, freq, start, end, sort, length, offset, tidy, key)
} else {
}else {
.eia_data(dir, data, facets, freq, start, end, sort, length, offset, tidy, key)
}
}

.eia_data <- function(dir, data, facets, freq, start, end, sort, length, offset, tidy, key){
md <- eia_metadata(dir, TRUE, TRUE, key)
.eia_data_check(md, dir, data, facets, freq, start, end, sort, length, offset)
r <- .eia_get(.eia_data_url(dir, data, facets, freq, start, end, sort, length, offset, key))
if(is.na(tidy)) return(r)
r <- jsonlite::fromJSON(r)
Expand Down Expand Up @@ -93,7 +96,12 @@ eia_data <- function(dir, data = NULL, facets = NULL,
)
}

.eia_data_check <- function(md, dir, data, facets, freq, start, end, sort, length, offset){
.eia_metadata_check <- function(dir, data, facets, freq, start, end, sort, length, offset, key){
md <- eia_metadata(dir, TRUE, TRUE, key)
.eia_check_call(md, dir, data, facets, freq, start, end, sort, length, offset)
}

.eia_check_call <- function(md, dir, data, facets, freq, start, end, sort, length, offset){
.data_check(data, md$Data$id)
.facet_check(facets, md$Facets$id)
.freq_check(freq, md$Frequency$id)
Expand Down
4 changes: 4 additions & 0 deletions man/eia_data.Rd

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

55 changes: 36 additions & 19 deletions tests/testthat/test-data.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,45 +55,59 @@ test_that("data queries return as expected", {

# Test "data" input value
err <- "'data' must be some combination of: revenue, sales, price, customers"
expect_error(eia_data("electricity/retail-sales", data = "prce"), err)
expect_error(eia_data("electricity/retail-sales", data = "prce", check_metadata = TRUE), err)

# Test "facet" input value
err <- "names of the 'facets' list input must be some combination of: stateid, sectorid"
expect_error(eia_data("electricity/retail-sales", facets = list(statid = "OH")), err)
expect_error(eia_data("electricity/retail-sales", facets = list(statid = "OH"), check_metadata = TRUE), err)

# Test "freq" input value
err <- "'freq' must be one of: monthly, quarterly, annual"
expect_error(eia_data("electricity/retail-sales", freq = "anual"), err)
expect_error(eia_data("electricity/retail-sales", freq = "anual", check_metadata = TRUE), err)
err <- "'freq' must be a character value of length 1."
expect_error(eia_data("electricity/retail-sales", freq = c("annual", "monthly")), err)
expect_error(eia_data("electricity/retail-sales", freq = c("annual", "monthly"), check_metadata = TRUE), err)

# Test "start" input value
err <- "'start' must be a character string of format: YYYY"
expect_error(eia_data("electricity/retail-sales", freq="annual", start=2010), err)
expect_error(eia_data("electricity/retail-sales", freq="annual", start=2010, check_metadata = TRUE), err)
err <- "'start' is beyond the end of available data."
expect_error(eia_data("electricity/retail-sales", freq="annual", start="2099"), err)
expect_error(eia_data("electricity/retail-sales", freq="annual", start="2099", check_metadata = TRUE), err)
err <- "'start' must be a character string of format: YYYY"
expect_error(eia_data("electricity/retail-sales", freq="annual", start="2020-06"), err)
expect_error(eia_data("electricity/retail-sales", freq="annual", start="2020-06", check_metadata = TRUE), err)
err <- "'start' must be a character string of format: YYYY-MM"
expect_error(eia_data("electricity/retail-sales", freq="monthly", start="2020"), err)
expect_error(eia_data("electricity/retail-sales", freq="monthly", start="2020", check_metadata = TRUE), err)
wrn <- "'start' is beyond available history. Earliest available: 2001-01"
expect_warning(
eia_data("electricity/retail-sales", facets=list(stateid="OH"), freq="annual", start="1980", end="2020"),
eia_data(
"electricity/retail-sales",
facets=list(stateid="OH"),
freq="annual",
start="1980",
end="2020",
check_metadata = TRUE
),
wrn
)

# Test "end" input value
err <- "'end' must be a character string of format: YYYY"
expect_error(eia_data("electricity/retail-sales", freq="annual", end=2010), err)
expect_error(eia_data("electricity/retail-sales", freq="annual", end=2010, check_metadata = TRUE), err)
err <- "'end' is before the start of available data."
expect_error(eia_data("electricity/retail-sales", freq="annual", end="1980"), err)
expect_error(eia_data("electricity/retail-sales", freq="annual", end="1980", check_metadata = TRUE), err)
err <- "'end' must be a character string of format: YYYY"
expect_error(eia_data("electricity/retail-sales", freq="annual", end="2020-06"), err)
expect_error(eia_data("electricity/retail-sales", freq="annual", end="2020-06", check_metadata = TRUE), err)
err <- "'end' must be a character string of format: YYYY-MM"
expect_error(eia_data("electricity/retail-sales", freq="monthly", end="2020"), err)
expect_error(eia_data("electricity/retail-sales", freq="monthly", end="2020", check_metadata = TRUE), err)
wrn <- "'end' is beyond available history. Latest available: 2023-08"
expect_warning(
eia_data("electricity/retail-sales", facets=list(stateid="OH"), freq="annual", start="2020", end="2099"),
eia_data(
"electricity/retail-sales",
facets=list(stateid="OH"),
freq="annual",
start="2020",
end="2099",
check_metadata = TRUE
),
wrn
)

Expand All @@ -103,15 +117,15 @@ test_that("data queries return as expected", {
)
expect_equal(nrow(x), 10)
err <- "'length' must be a single numeric value between 0 and 5000."
expect_error(eia_data("electricity/retail-sales", length = "10"), err)
expect_error(eia_data("electricity/retail-sales", length = "10", check_metadata = TRUE), err)

# Test "offset" input value
expect_warning(
x <- eia_data("electricity/retail-sales", length = 10, offset = 10)
)
expect_equal(nrow(x), 10)
err <- "'offset' must be a single numeric value greater than 0."
expect_error(eia_data("electricity/retail-sales", offset = "10"), err)
expect_error(eia_data("electricity/retail-sales", offset = "10", check_metadata = TRUE), err)

# Test misspelling of sort objects
err <- paste0(
Expand All @@ -124,7 +138,8 @@ test_that("data queries return as expected", {
data = "price",
facets = list(stateid = "OH", sectorid = "RES"),
freq = "annual", start = "2011", end = "2020",
sort = list(cols = "period", ordr = "asc")
sort = list(cols = "period", ordr = "asc"),
check_metadata = TRUE
),
err
)
Expand All @@ -137,7 +152,8 @@ test_that("data queries return as expected", {
data = "price",
facets = list(stateid = "OH", sectorid = c("RES", "COM")),
freq = "annual", start = "2011", end = "2020",
sort = list(cols = c("sectorid", "period"), order = c("asc", "desc"))
sort = list(cols = c("sectorid", "period"), order = c("asc", "desc")),
check_metadata = TRUE
),
err
)
Expand All @@ -150,7 +166,8 @@ test_that("data queries return as expected", {
data = "price",
facets = list(stateid = "OH", sectorid = "RES"),
freq = "annual", start = "2011", end = "2020",
sort = list(cols = "period", order = "ascc")
sort = list(cols = "period", order = "ascc"),
check_metadata = TRUE
),
err
)
Expand Down

0 comments on commit 1807cfa

Please sign in to comment.