Files
uscogdata/tests/testthat/test-spending.R
T
jared 377eed1240 feat: cog_gov_search + cog_mirror + scope-aware verb behavior
Three pieces:

1. cog_gov_search: name/state/type search over canonical_fips_xwalk
   for resolving human-readable place names into canonical_govids.
   Accepts USPS abbrev ('FL') or FIPS int (12) for state; integer
   0-3 or name ('state','county','city','township') for type. Types
   4/5 emit an explanatory cli message and return an empty tibble
   (v0.1 corpus excludes them). USPS<->FIPS table hardcoded with
   50 states + DC + territories; FIPS 66 = GU (not GA).

2. cog_mirror: downloads manifest-listed files to a local directory
   with SHA-256 idempotency (files with matching hash return status
   'cached'). Supports HTTP and local-path fixture URLs. Round-trip
   test: mirror + re-open against the mirror + query Broward 2020
   returns identical results.

3. Scope-aware verbs: .check_govids_in_scope() helper in session.R
   queries canonical_fips_xwalk for the requested govids, emits a
   cli_inform listing any missing ones, and records the found/missing
   sets under provenance$scope. Wired into cog_spending (and
   transitively into cog_revenue, cog_geographic_rollup,
   cog_peer_compare via their cog_spending calls).

Also: dropped dbplyr from Imports (unused).

Tests: +29 (22 search + 12 mirror - 5 refactored) / 159 total pass.
devtools::check() now clean: 0E / 0W / 0N.
2026-04-24 11:38:32 -04:00

98 lines
3.7 KiB
R

test_that("cog_spending returns expected shape for Broward Corrections 2020", {
skip_if_no_corpus()
r <- cog_spending("101006006", years = 2020L, category = "Corrections")
expect_s3_class(r, "tbl_df")
expected_cols <- c("year", "canonical_govid", "gov_name", "spend_subtype",
"category", "amt_nominal", "codes_included",
"aggregate_fallback", "notes")
expect_true(all(expected_cols %in% names(r)))
expect_equal(unique(r$canonical_govid), "101006006")
expect_equal(unique(r$year), 2020L)
expect_equal(unique(r$category), "Corrections")
expect_true(all(r$spend_subtype %in% c("operations", "capital")))
expect_true(all(r$amt_nominal > 0))
})
test_that("cog_spending vectorised years + categories", {
skip_if_no_corpus()
r <- cog_spending("101006006", 2019:2020,
category = c("Corrections", "Police"))
expect_true(all(r$year %in% 2019:2020))
expect_true(all(r$category %in% c("Corrections", "Police")))
expect_gte(nrow(r), 4L)
})
test_that("cog_spending with per_capita adds per-capita nominal column", {
skip_if_no_corpus()
r <- cog_spending("101006006", 2020L, "Corrections", per_capita = TRUE)
expect_true("amt_per_capita_nominal" %in% names(r))
expect_false("amt_real" %in% names(r))
expect_false("amt_per_capita_real" %in% names(r))
expect_true(all(is.finite(r$amt_per_capita_nominal)))
expect_true(all(r$amt_per_capita_nominal < r$amt_nominal))
})
test_that("cog_spending with adjust_to_year adds real column", {
skip_if_no_corpus()
r <- cog_spending("101006006", 2015:2020, "Corrections",
adjust_to_year = 2022L)
expect_true("amt_real" %in% names(r))
r2015 <- dplyr::filter(r, year == 2015L)
expect_true(any(r2015$amt_nominal != r2015$amt_real))
})
test_that("cog_spending with per_capita + adjust_to_year adds all columns", {
skip_if_no_corpus()
r <- cog_spending("101006006", 2020L, "Corrections",
per_capita = TRUE, adjust_to_year = 2022L)
expect_true(all(c("amt_nominal", "amt_real",
"amt_per_capita_nominal", "amt_per_capita_real") %in%
names(r)))
})
test_that("cog_spending for unknown govid returns empty tibble + informs", {
skip_if_no_corpus()
expect_message(
r <- cog_spending("XXXINVALID", 2020L, "Corrections"),
"not found|v0.1"
)
expect_s3_class(r, "tbl_df")
expect_equal(nrow(r), 0L)
expect_true("notes" %in% names(r))
prov <- attr(r, "provenance")
expect_false(is.null(prov))
expect_equal(prov$scope$govids_missing, "XXXINVALID")
expect_equal(length(prov$scope$govids_found), 0L)
})
test_that("cog_spending records found + missing govids in provenance", {
skip_if_no_corpus()
suppressMessages(
r <- cog_spending(c("101006006", "XXXINVALID"), 2020L, "Corrections")
)
prov <- attr(r, "provenance")
expect_equal(sort(prov$scope$govids_found), "101006006")
expect_equal(sort(prov$scope$govids_missing), "XXXINVALID")
})
test_that("cog_spending result has provenance attribute matching schema", {
skip_if_no_corpus()
r <- cog_spending("101006006", 2020L, "Corrections")
prov <- attr(r, "provenance")
expect_type(prov, "list")
expect_equal(prov$verb, "cog_spending")
required <- c("verb", "target", "years", "scope", "manifest", "sql_query")
expect_true(all(required %in% names(prov)))
expect_equal(prov$years, 2020L)
expect_equal(prov$category, "Corrections")
expect_type(prov$sql_query, "character")
expect_true(grepl("spending_annotated", prov$sql_query))
expect_type(prov$codes_summed$observed, "character")
expect_true(all(c("E04") %in% prov$codes_summed$observed))
})
test_that("cog_spending rejects invalid inputs", {
expect_error(cog_spending(123, 2020L), "character")
expect_error(cog_spending("101006006", "2020"), "years")
})