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.
101 lines
3.6 KiB
R
101 lines
3.6 KiB
R
test_that("cog_geographic_rollup aggregates state + county + city layers", {
|
|
skip_if_no_corpus()
|
|
r <- cog_geographic_rollup(
|
|
govids = list(
|
|
state = "100000000", # Florida state govt
|
|
county = "101006006", # Broward County
|
|
city = "102006004" # Fort Lauderdale City
|
|
),
|
|
category = "Police",
|
|
years = 2019:2020
|
|
)
|
|
expect_s3_class(r, "tbl_df")
|
|
expected_cols <- c("year", "layer", "canonical_govid", "gov_name",
|
|
"spend_subtype", "category", "amt_nominal",
|
|
"codes_included", "aggregate_fallback",
|
|
"scope_note", "notes")
|
|
expect_true(all(expected_cols %in% names(r)))
|
|
expect_setequal(unique(r$layer), c("state", "county", "city"))
|
|
expect_true(all(r$category == "Police"))
|
|
expect_true(all(r$year %in% 2019:2020))
|
|
})
|
|
|
|
test_that("cog_geographic_rollup respects per_capita + adjust_to_year", {
|
|
skip_if_no_corpus()
|
|
r <- cog_geographic_rollup(
|
|
govids = list(county = "101006006", city = "102006004"),
|
|
category = "Police",
|
|
years = 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)))
|
|
# Each layer's per-capita uses its own population: city pop < county pop,
|
|
# so per_capita_nominal for city rows should differ meaningfully from county.
|
|
city_pc <- r$amt_per_capita_nominal[r$layer == "city"]
|
|
cty_pc <- r$amt_per_capita_nominal[r$layer == "county"]
|
|
expect_true(length(city_pc) > 0L)
|
|
expect_true(length(cty_pc) > 0L)
|
|
})
|
|
|
|
test_that("cog_geographic_rollup scope_notes describe each layer", {
|
|
skip_if_no_corpus()
|
|
r <- cog_geographic_rollup(
|
|
govids = list(state = "100000000", county = "101006006",
|
|
city = "102006004"),
|
|
category = "Police", years = 2020L
|
|
)
|
|
state_notes <- unique(r$scope_note[r$layer == "state"])
|
|
expect_true(any(grepl("state total", state_notes)))
|
|
county_notes <- unique(r$scope_note[r$layer == "county"])
|
|
expect_true(any(grepl("county", county_notes)))
|
|
city_notes <- unique(r$scope_note[r$layer == "city"])
|
|
expect_true(any(grepl("city proper", city_notes)))
|
|
})
|
|
|
|
test_that("cog_geographic_rollup single-layer call works", {
|
|
skip_if_no_corpus()
|
|
r <- cog_geographic_rollup(
|
|
govids = list(county = c("101006006")),
|
|
category = "Corrections",
|
|
years = 2020L
|
|
)
|
|
expect_true(all(r$layer == "county"))
|
|
expect_gt(nrow(r), 0L)
|
|
})
|
|
|
|
test_that("cog_geographic_rollup provenance reports the outer verb", {
|
|
skip_if_no_corpus()
|
|
r <- cog_geographic_rollup(
|
|
govids = list(state = "100000000", county = "101006006"),
|
|
category = "Police", years = 2020L
|
|
)
|
|
prov <- attr(r, "provenance")
|
|
expect_equal(prov$verb, "cog_geographic_rollup")
|
|
expect_setequal(prov$layers, c("state", "county"))
|
|
expect_true(grepl("cog_geographic_rollup", prov$call))
|
|
})
|
|
|
|
test_that("cog_geographic_rollup accepts data.frames per layer", {
|
|
skip_if_no_corpus()
|
|
fl_state <- cog_gov_search("^FLORIDA STATE GOVT$", type = "state")
|
|
broward <- cog_gov_search("^BROWARD COUNTY$", state = "FL", type = "county")
|
|
r <- cog_geographic_rollup(
|
|
govids = list(state = fl_state, county = broward),
|
|
category = "Police", years = 2020L
|
|
)
|
|
expect_setequal(unique(r$layer), c("state", "county"))
|
|
expect_gt(nrow(r), 0L)
|
|
})
|
|
|
|
test_that("cog_geographic_rollup rejects invalid inputs", {
|
|
expect_error(cog_geographic_rollup(list(), "Police", 2020L), "length")
|
|
expect_error(cog_geographic_rollup(c("101006006"), "Police", 2020L), "list")
|
|
expect_error(
|
|
cog_geographic_rollup(list(planet = "100000000"), "Police", 2020L),
|
|
"state|county|city"
|
|
)
|
|
})
|