feat: cog_categories

Discovery verb over the summary_categories view, grouped one row per
(category, subtype). Parallels cog_gov_search: analysts use it to
find the valid `category` values to pass into cog_spending(),
cog_revenue(), cog_geographic_rollup().

Columns: category, category_type, subtype, n_codes, item_codes
(comma-separated, alphabetical). Optional filters:

  type    = NULL | "spending" | "revenue"
  pattern = regex matched case-insensitively on category

The user-facing "spending" alias is translated internally to the
corpus-native "expenditure" so callers don't have to learn Census
vocabulary, while the returned category_type column preserves the
native value for auditability.

Also: fix @noRd placement in session.R so devtools::document() stops
warning.

Tests: +16 new / 181 total pass. check 0E/0W/0N.
This commit is contained in:
2026-04-24 18:15:26 -04:00
parent a778d790d8
commit 488d03d74b
5 changed files with 161 additions and 10 deletions
+62
View File
@@ -0,0 +1,62 @@
test_that("cog_categories returns all categories grouped by subtype", {
skip_if_no_corpus()
r <- cog_categories()
expect_s3_class(r, "tbl_df")
expected <- c("category", "category_type", "subtype",
"n_codes", "item_codes")
expect_true(all(expected %in% names(r)))
expect_gt(nrow(r), 10L)
# corpus preserves Census-native "expenditure" vocabulary; the API takes
# "spending" as a friendlier alias.
expect_setequal(unique(r$category_type), c("expenditure", "revenue"))
})
test_that("cog_categories(type = 'spending') returns only expenditure rows", {
skip_if_no_corpus()
r <- cog_categories(type = "spending")
expect_true(all(r$category_type == "expenditure"))
expect_true(all(r$subtype %in% c("operations", "capital")))
})
test_that("cog_categories(type = 'revenue') returns only revenue rows", {
skip_if_no_corpus()
r <- cog_categories(type = "revenue")
expect_true(all(r$category_type == "revenue"))
expect_true(all(r$subtype %in%
c("own_source", "federal", "state", "local_aid")))
})
test_that("cog_categories(pattern = ...) filters case-insensitively", {
skip_if_no_corpus()
r <- cog_categories(pattern = "police")
expect_gt(nrow(r), 0L)
expect_true(all(grepl("Police", r$category, ignore.case = TRUE)))
})
test_that("cog_categories has one row per (category, subtype)", {
skip_if_no_corpus()
r <- cog_categories()
key <- paste(r$category, r$subtype, sep = "|")
expect_equal(length(key), length(unique(key)))
})
test_that("cog_categories item_codes is non-empty comma-separated string", {
skip_if_no_corpus()
r <- cog_categories()
expect_true(all(nzchar(r$item_codes)))
expect_true(all(r$n_codes >= 1L))
# n_codes should equal count of commas + 1
expect_equal(r$n_codes,
vapply(strsplit(r$item_codes, ","), length, integer(1)))
})
test_that("cog_categories sorted by category_type, category, subtype", {
skip_if_no_corpus()
r <- cog_categories()
sorted <- r[order(r$category_type, r$category, r$subtype), ]
expect_identical(r, sorted)
})
test_that("cog_categories rejects invalid type", {
expect_error(cog_categories(type = "both"), "type")
})