From 3566a9f12d753b6bb102a0986baf0f051cce4880 Mon Sep 17 00:00:00 2001 From: Matthew Hoff Date: Thu, 16 Nov 2023 15:09:28 -0500 Subject: [PATCH] separate testing into multiple parts based on type/class/methods of the inputs being tested --- tests/testthat/test-data.R | 207 +++++++++++++++++-------------------- 1 file changed, 97 insertions(+), 110 deletions(-) diff --git a/tests/testthat/test-data.R b/tests/testthat/test-data.R index cdab329..833a7db 100644 --- a/tests/testthat/test-data.R +++ b/tests/testthat/test-data.R @@ -1,6 +1,7 @@ +options(eia_antidos = 2) suppressWarnings(key <- eia_get_key()) -test_that("data queries return as expected", { +test_that("data queries return data as expected", { if(is.null(key)) skip("API key not available.") # Test character object returned given `tidy = NA` @@ -13,19 +14,14 @@ test_that("data queries return as expected", { expect_equal(length(x), 4) expect_equal(names(x), c("response", "request", "apiVersion", "ExcelAddInVersion")) - options(eia_antidos = 3) - # Test tidy-returned data x <- eia_data( dir = "electricity/retail-sales", data = "price", facets = list(stateid = "OH", sectorid = "RES"), - freq = "annual", start = "2011", end = "2020", + freq = "annual", start = 2011, end = 2020, sort = list(cols = "period", order = "asc") ) - - options(eia_antidos = 0) - expect_s3_class(x, "tbl_df") expect_equal(ncol(x), 7) expect_equal(nrow(x), 10) @@ -34,7 +30,7 @@ test_that("data queries return as expected", { expect_true("price" %in% names(x)) # Test warning that API limit has been reached - expect_warning(eia_data("electricity/retail-sales")) + expect_warning(eia_data("electricity/retail-sales", data = "price")) # Test warning that more data is available even if API limit not reached wrn <- paste("Rows returned: 8\nRows available: 10") @@ -43,7 +39,7 @@ test_that("data queries return as expected", { dir = "electricity/retail-sales", data = "price", facets = list(stateid = "OH", sectorid = "RES"), - freq = "annual", start = "2011", end = "2020", + freq = "annual", start = 2011, end = 2020, sort = list(cols = "period", order = "asc"), length = 8 ), @@ -53,143 +49,134 @@ test_that("data queries return as expected", { # Test no data expect_error(suppressMessages(eia_data("electricity/zzz")), "Page not found") +}) + + +test_that("'data', 'facets', and 'freq' error/warning messages return as expected", { + if(is.null(key)) skip("API key not available.") + # Test "data" input value err <- "Invalid data 'prce' provided. The only valid data are 'revenue', 'sales', 'price', and 'customers'." expect_error(eia_data("electricity/retail-sales", data = "prce"), err) - err <- "'data' must be some combination of: revenue, sales, price, customers" + err <- "Invalid 'data' provided. Options are: 'revenue', 'sales', 'price', 'customers'" 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"), check_metadata = TRUE), err) + err <- "Invalid facet 'state' provided. The only valid facets are 'stateid', and 'sectorid'." + expect_error(eia_data("electricity/retail-sales", facets = list(state = "OH")), err) + err <- "Invalid 'facets' provided. Options are: 'stateid', 'sectorid'" + expect_error(eia_data("electricity/retail-sales", facets = list(state = "OH"), check_metadata = TRUE), err) # Test "freq" input value - err <- "'freq' must be one of: monthly, quarterly, annual" + err <- "Invalid frequency 'anual' provided. The only valid frequencies are 'monthly', 'quarterly', and 'annual'." + expect_error(eia_data("electricity/retail-sales", freq = "anual"), err) + err <- "Invalid 'freq' provided. Must be one of: 'monthly', 'quarterly', 'annual'" 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"), check_metadata = TRUE), err) + expect_warning(eia_data("electricity/retail-sales", freq = c("annual", "monthly"))) + expect_error(eia_data("electricity/retail-sales", freq = c("monthly", "annual"), check_metadata = TRUE), err) + +}) + + +test_that("'start' and 'end' error/warning messages return as expected", { + if(is.null(key)) skip("API key not available.") # 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, check_metadata = TRUE), err) + err <- "'start' requires 'freq' be non-NULL." + expect_error(eia_data("electricity/retail-sales", start = 2020), err) + expect_no_error(eia_data("electricity/retail-sales", freq = "annual", start = 2020)) + err <- "No data available - check inputs." + expect_error(eia_data("electricity/retail-sales", freq = "annual", start = 2099), err) err <- "'start' is beyond the end of available data." - 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", 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", 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", - check_metadata = TRUE - ), - wrn - ) + expect_error(eia_data("electricity/retail-sales", freq = "annual", start = 2099, check_metadata = TRUE), err) + eia_clear_data() + expect_error(eia_data("electricity/retail-sales", freq = "annual", start = "2099", check_metadata = TRUE), err) + expect_no_error(eia_data("electricity/retail-sales", freq = "annual", start = "2020-06")) + err <- "'start' must be a string of format: YYYY" + expect_error(eia_data("electricity/retail-sales", freq = "annual", start = "2020-06", check_metadata = TRUE), err) + err <- "'start' must be a string of format: YYYY-MM" + expect_error(eia_data("electricity/retail-sales", freq = "monthly", start = 2020, check_metadata = TRUE), err) + eia_clear_data() + expect_error(eia_data("electricity/retail-sales", freq = "monthly", start = "2020", check_metadata = TRUE), err) + expect_warning(eia_data("electricity/retail-sales", freq = "annual", start = 1980)) # 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, check_metadata = TRUE), err) + err <- "'end' requires 'freq' be non-NULL." + expect_error(eia_data("electricity/retail-sales", end = 2001), err) + expect_no_error(eia_data("electricity/retail-sales", freq = "annual", end = 2001)) + err <- "No data available - check inputs." + expect_error(eia_data("electricity/retail-sales", freq = "annual", end = 1980), err) err <- "'end' is before the start of available data." - 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", 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", 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", - check_metadata = TRUE - ), - wrn - ) + expect_error(eia_data("electricity/retail-sales", freq = "annual", end = 1980, check_metadata = TRUE), err) + eia_clear_cache() + expect_error(eia_data("electricity/retail-sales", freq = "annual", end = "1980", check_metadata = TRUE), err) + err <- "'end' must be a string of format: YYYY" + expect_error(eia_data("electricity/retail-sales", freq = "annual", end = "2020-06", check_metadata = TRUE), err) + err <- "'end' must be a string of format: YYYY-MM" + expect_error(eia_data("electricity/retail-sales", freq = "monthly", end = 2020, check_metadata = TRUE), err) + eia_clear_cache() + expect_error(eia_data("electricity/retail-sales", freq = "monthly", end = "2020", check_metadata = TRUE), err) + expect_warning(eia_data("electricity/retail-sales", freq = "annual", end = 2099)) - # Test "length" input value - expect_warning( - x <- eia_data("electricity/retail-sales", length = 10) - ) - 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", 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", check_metadata = TRUE), err) - # Test misspelling of sort objects +test_that("'sort' error/warning messages return as expected", { + if(is.null(key)) skip("API key not available.") + + # Test "sort" input values err <- paste0( "'sort' must be a named list of length 2 containing the following:\n", "'cols' and 'order' of arbitrary length and of length 1, respectively." ) expect_error( - eia_data( - dir = "electricity/retail-sales", - data = "price", - facets = list(stateid = "OH", sectorid = "RES"), - freq = "annual", start = "2011", end = "2020", - sort = list(cols = "period", ordr = "asc"), - check_metadata = TRUE - ), + eia_data("electricity/retail-sales", "price", sort = list(cols = "period", o = "asc")), err ) - - # Test sort input where length of "order" > 1 - err <- "must provide a single value for 'order': 'asc' or 'desc'." + err <- paste( + "Invalid sort direction 'as' provided for column 'period'.", + "The only valid sort directions are 'asc' and 'desc'." + ) expect_error( - eia_data( - dir = "electricity/retail-sales", - 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")), - check_metadata = TRUE - ), + eia_data("electricity/retail-sales", "price", sort = list(cols = "period", order = "as")), err ) - - # Test misspelling of sort objects - err <- "'order' must be one of 'asc' or 'desc'." + err <- paste( + "Invalid sort 'perid' provided.", + "The only valid sort parameters are 'revenue', 'sales', 'price', 'customers', 'period', 'stateid', and 'sectorid' in either 'asc' or 'desc' order.", + "If direction is not provided or is incorrect, 'asc' is the default" + ) expect_error( - eia_data( - dir = "electricity/retail-sales", - data = "price", - facets = list(stateid = "OH", sectorid = "RES"), - freq = "annual", start = "2011", end = "2020", - sort = list(cols = "period", order = "ascc"), - check_metadata = TRUE - ), + eia_data("electricity/retail-sales", "price", sort = list(cols = "perid", order = "asc")), err ) }) -test_that("metadata helper returns as expected", { - if(is.null(key)) skip("API key not available.") - x <- eia_metadata("electricity/retail-sales", cache = FALSE) - expect_type(x, "list") +test_that("'length' and 'offset' error/warning messages return as expected", { + if(is.null(key)) skip("API key not available.") - x <- eia_metadata("electricity/retail-sales", tidy = FALSE, cache = FALSE) - expect_type(x, "list") - expect_length(x, 4) - expect_equal(names(x), c("response", "request", "apiVersion", "ExcelAddInVersion")) + # Test "length" input value + expect_warning(x <- eia_data("electricity/retail-sales", length = 10)) + expect_equal(ncol(x), 5) + expect_equal(nrow(x), 10) + expect_warning(x <- eia_data("electricity/retail-sales", length = "10")) + expect_equal(ncol(x), 5) + expect_equal(nrow(x), 10) + err <- "'length' must be a single value between 0 and 5000." + expect_error(eia_data("electricity/retail-sales", length = 5001)) + expect_error(eia_data("electricity/retail-sales", length = "5001")) - x <- eia_metadata("electricity/retail-sales", tidy = NA, cache = FALSE) - expect_type(x, "character") + # Test "offset" input value + expect_warning(x <- eia_data("electricity/retail-sales", length = 10, offset = 10)) + expect_equal(ncol(x), 5) + expect_equal(nrow(x), 10) + expect_warning(x <- eia_data("electricity/retail-sales", length = 10, offset = "10")) + expect_equal(ncol(x), 5) + expect_equal(nrow(x), 10) + err <- "'offset' must be a single value greater than 0." + expect_error(eia_data("electricity/retail-sales", offset = -1), err) + expect_error(eia_data("electricity/retail-sales", offset = "-1"), err) }) - -options(eia_antidos = 1)