Files
uscogdata/tests/testthat/test-revenue.R
T
jared a778d790d8 fix: govid input ergonomics + clearer missing-govid message
Two UX fixes surfaced by first real-user use:

1. cog_spending / cog_revenue / cog_geographic_rollup now accept either
   a character vector OR a data.frame with a canonical_govid column
   (e.g. output of cog_gov_search() or cog_find_peers()). Shared
   .coerce_govid_input() helper in session.R. This lets the natural
   pipe work:

     cog_gov_search('MIAMI', state='FL', type='city') |>
       cog_spending(years=2022, category='Police')

   cog_peer_compare already accepted a data.frame for the peer arg;
   behavior there is unchanged.

2. .check_govids_in_scope() message reworded. The old text led with
   'v0.1 covers gov_types 0-3' which falsely implied the missing govids
   were scope-excluded types when the more common real cause is a typo
   or a guessed value. New message leads with typo + pre-2017 PID,
   mentions scope exclusion as one possibility, and points at
   cog_gov_search() as the recovery path.

Tests: 165 pass / 0 fail. check 0E/0W/0N.
2026-04-24 17:49:08 -04:00

39 lines
1.4 KiB
R

test_that("cog_revenue returns expected shape for Broward Property Tax 2020", {
skip_if_no_corpus()
r <- cog_revenue("101006006", years = 2020L, category = "Property Tax")
expect_s3_class(r, "tbl_df")
expected_cols <- c("year", "canonical_govid", "gov_name", "revenue_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)
})
test_that("cog_revenue with no category filter returns multiple categories", {
skip_if_no_corpus()
r <- cog_revenue("101006006", years = 2020L)
expect_gt(length(unique(r$category)), 1L)
})
test_that("cog_revenue with per_capita + adjust_to_year adds all columns", {
skip_if_no_corpus()
r <- cog_revenue("101006006", 2020L,
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_revenue result has provenance attribute", {
skip_if_no_corpus()
r <- cog_revenue("101006006", 2020L)
prov <- attr(r, "provenance")
expect_equal(prov$verb, "cog_revenue")
expect_true(grepl("revenue_annotated", prov$sql_query))
})
test_that("cog_revenue rejects invalid inputs", {
expect_error(cog_revenue(list(), 2020L), "character|data frame")
})