feat(api): export cog_suggestions() (#75)
cog_suggestions(govid, years, category, flow, concept, years_present) returns the list that cog_spending() and cog_revenue() attach as attr(x, "provenance")$suggestions, given the years the caller's rows cover. A caller can run a verb once with no category, filter the rows itself, and build the suggestions for a category only when it needs them, without reaching into .build_suggestions() and six other internal names. It validates every argument before opening a session, with cli errors whose classes match the verbs' where one exists (uscogdata_invalid_expenditure_concept, uscogdata_invalid_revenue_concept, uscogdata_all_categories_not_combinable) and a new uscogdata_invalid_flow. It resolves the verbs' default basis the way .verb_spendrev() does, so it returns an empty list on a schema_version < 5 corpus, and it does not print the suggestion message. Tests check that it returns exactly the verbs' list for empty_year and suppressed_component fires, a multi-government cohort, a data-frame govid, both revenue concepts, "All Categories" under each concept, and expenditure_concept = "total" given Direct-leg years. Closes #75 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Qbe6u2h83wgZdTgJgE4Xh5
This commit is contained in:
6 files changed
+575
-5
No files matched your search
@@ -1,4 +1,9 @@
|
||||
# tests/testthat/test-suggestions.R
|
||||
#
|
||||
# cog_suggestions() (uscogdata#75): the exported form of .build_suggestions().
|
||||
# Each equivalence test runs a verb with a category named, takes the years its
|
||||
# rows cover, and checks that cog_suggestions() returns exactly the list the
|
||||
# verb attached as provenance$suggestions.
|
||||
|
||||
verb_suggestions <- function(r) attr(r, "provenance")$suggestions
|
||||
|
||||
@@ -6,9 +11,151 @@ suggestion_ids <- function(sugg) {
|
||||
vapply(sugg, function(s) s$recipe_id, character(1))
|
||||
}
|
||||
|
||||
test_that("cog_suggestions() matches cog_spending() on an empty_year fire", {
|
||||
skip_if_no_corpus()
|
||||
r <- suppressMessages(
|
||||
cog_spending("061037123085", years = 2011L, category = "Corrections"))
|
||||
expect_length(verb_suggestions(r), 3L)
|
||||
|
||||
s <- cog_suggestions("061037123085", years = 2011L, category = "Corrections",
|
||||
flow = "spending", years_present = unique(r$year))
|
||||
expect_identical(s, verb_suggestions(r))
|
||||
expect_identical(
|
||||
suggestion_ids(s),
|
||||
c("corrections_capital_combined", "corrections_combined",
|
||||
"corrections_other_capital_combined"))
|
||||
})
|
||||
|
||||
test_that("cog_suggestions() matches cog_spending() on a suppressed_component fire", {
|
||||
skip_if_no_corpus()
|
||||
r <- suppressMessages(
|
||||
cog_spending("061037123085", years = 2011L, category = "Public Welfare"))
|
||||
expect_gt(nrow(r), 0L)
|
||||
expect_length(verb_suggestions(r), 2L)
|
||||
|
||||
s <- cog_suggestions("061037123085", years = 2011L,
|
||||
category = "Public Welfare", flow = "spending",
|
||||
years_present = unique(r$year))
|
||||
expect_identical(s, verb_suggestions(r))
|
||||
})
|
||||
|
||||
test_that("cog_suggestions() matches cog_spending() for a multi-government cohort", {
|
||||
skip_if_no_corpus()
|
||||
govids <- c("061037123085", "121011212191", "010000226085")
|
||||
r <- suppressMessages(
|
||||
cog_spending(govids, years = c(2011L, 2012L), category = "Corrections"))
|
||||
expect_gt(length(verb_suggestions(r)), 0L)
|
||||
|
||||
s <- cog_suggestions(govids, years = c(2011L, 2012L),
|
||||
category = "Corrections", flow = "spending",
|
||||
years_present = unique(r$year))
|
||||
expect_identical(s, verb_suggestions(r))
|
||||
})
|
||||
|
||||
test_that("cog_suggestions() accepts a data frame of governments", {
|
||||
skip_if_no_corpus()
|
||||
r <- suppressMessages(
|
||||
cog_spending("061037123085", years = 2011L, category = "Corrections"))
|
||||
s <- cog_suggestions(data.frame(canonical_govid = "061037123085"),
|
||||
years = 2011L, category = "Corrections",
|
||||
years_present = unique(r$year))
|
||||
expect_identical(s, verb_suggestions(r))
|
||||
})
|
||||
|
||||
test_that("cog_suggestions() matches cog_revenue()", {
|
||||
skip_if_no_corpus()
|
||||
r <- suppressMessages(
|
||||
cog_revenue("020000227749", years = 2011L,
|
||||
category = "Miscellaneous Revenue"))
|
||||
expect_length(verb_suggestions(r), 1L)
|
||||
|
||||
s <- cog_suggestions("020000227749", years = 2011L,
|
||||
category = "Miscellaneous Revenue", flow = "revenue",
|
||||
years_present = unique(r$year))
|
||||
expect_identical(s, verb_suggestions(r))
|
||||
})
|
||||
|
||||
test_that("cog_suggestions() matches cog_revenue() under revenue_concept = 'total'", {
|
||||
skip_if_no_corpus()
|
||||
r <- suppressMessages(
|
||||
cog_revenue("020000227749", years = 2011L,
|
||||
category = "Miscellaneous Revenue", revenue_concept = "total"))
|
||||
s <- cog_suggestions("020000227749", years = 2011L,
|
||||
category = "Miscellaneous Revenue", flow = "revenue",
|
||||
concept = "total", years_present = unique(r$year))
|
||||
expect_identical(s, verb_suggestions(r))
|
||||
})
|
||||
|
||||
test_that("cog_suggestions() matches the verbs for 'All Categories' under each concept", {
|
||||
skip_if_no_corpus()
|
||||
gov <- "010000226085"
|
||||
for (concept in c("primary", "direct")) {
|
||||
r <- suppressMessages(
|
||||
cog_spending(gov, 2011L, category = "All Categories",
|
||||
expenditure_concept = concept))
|
||||
expect_gt(length(verb_suggestions(r)), 0L)
|
||||
s <- cog_suggestions(gov, 2011L, category = "All Categories",
|
||||
concept = concept, years_present = unique(r$year))
|
||||
expect_identical(s, verb_suggestions(r), info = concept)
|
||||
}
|
||||
|
||||
r <- suppressMessages(
|
||||
cog_revenue(gov, 2011L, category = "All Categories",
|
||||
revenue_concept = "total"))
|
||||
s <- cog_suggestions(gov, 2011L, category = "All Categories",
|
||||
flow = "revenue", concept = "total",
|
||||
years_present = unique(r$year))
|
||||
expect_identical(s, verb_suggestions(r))
|
||||
})
|
||||
|
||||
test_that("cog_suggestions() matches expenditure_concept = 'total' given Direct-leg years", {
|
||||
# cog_spending() measures gaps on the Direct leg only (C1(a) in
|
||||
# R/suggestions.R), so the caller passes the years with a
|
||||
# non-intergovernmental row, as the years_present documentation says.
|
||||
skip_if_no_corpus()
|
||||
r <- suppressMessages(
|
||||
cog_spending("061037123085", years = 2011L, category = "Corrections",
|
||||
expenditure_concept = "total"))
|
||||
expect_gt(length(verb_suggestions(r)), 0L)
|
||||
|
||||
direct <- r[r$spend_subtype != "intergovernmental", ]
|
||||
s <- cog_suggestions("061037123085", years = 2011L,
|
||||
category = "Corrections", concept = "total",
|
||||
years_present = unique(direct$year))
|
||||
expect_identical(s, verb_suggestions(r))
|
||||
})
|
||||
|
||||
test_that("cog_suggestions() returns an empty list when nothing qualifies", {
|
||||
skip_if_no_corpus()
|
||||
r <- cog_spending("121011212191", years = 2019:2020, category = "Corrections")
|
||||
expect_length(verb_suggestions(r), 0L)
|
||||
s <- cog_suggestions("121011212191", years = 2019:2020,
|
||||
category = "Corrections",
|
||||
years_present = unique(r$year))
|
||||
expect_identical(s, list())
|
||||
})
|
||||
|
||||
test_that("cog_suggestions() follows the verbs to basis = 'raw' on a schema_version 4 corpus", {
|
||||
skip_if_no_corpus()
|
||||
with_doctored_schema_version(4L, {
|
||||
expect_identical(
|
||||
cog_suggestions("061037123085", years = 2011L,
|
||||
category = "Corrections", years_present = integer(0)),
|
||||
list())
|
||||
})
|
||||
})
|
||||
|
||||
test_that("cog_suggestions() prints nothing", {
|
||||
skip_if_no_corpus()
|
||||
uscogdata:::.ensure_session()
|
||||
expect_silent(
|
||||
cog_suggestions("061037123085", years = 2011L, category = "Corrections",
|
||||
years_present = integer(0)))
|
||||
})
|
||||
|
||||
# --- stable order ------------------------------------------------------------
|
||||
|
||||
test_that("cog_spending() returns suggestions ordered by recipe_id", {
|
||||
test_that("suggestions come back ordered by recipe_id, in the verbs and here", {
|
||||
# On the fixture, the unsorted candidate query lists
|
||||
# corrections_other_capital_combined before corrections_combined, so this
|
||||
# fails without the ORDER BY in .query_candidate_recipes().
|
||||
@@ -18,6 +165,22 @@ test_that("cog_spending() returns suggestions ordered by recipe_id", {
|
||||
ids <- suggestion_ids(verb_suggestions(r))
|
||||
expect_gt(length(ids), 1L)
|
||||
expect_identical(ids, sort(ids, method = "radix"))
|
||||
|
||||
s <- cog_suggestions("010000226085", 2011L, category = "All Categories",
|
||||
years_present = unique(r$year))
|
||||
expect_identical(suggestion_ids(s), ids)
|
||||
})
|
||||
|
||||
test_that("repeated cog_suggestions() calls return the same list in the same order", {
|
||||
skip_if_no_corpus()
|
||||
calls <- lapply(seq_len(10L), function(i) {
|
||||
cog_suggestions("010000226085", 2011L, category = "All Categories",
|
||||
years_present = 2011L)
|
||||
})
|
||||
expect_gt(length(calls[[1]]), 1L)
|
||||
for (i in seq_along(calls)[-1L]) {
|
||||
expect_identical(calls[[i]], calls[[1]], info = paste("call", i))
|
||||
}
|
||||
})
|
||||
|
||||
test_that(".query_candidate_recipes() returns recipe ids in sorted order", {
|
||||
@@ -29,3 +192,90 @@ test_that(".query_candidate_recipes() returns recipe ids in sorted order", {
|
||||
expect_gt(length(ids), 1L)
|
||||
expect_identical(ids, sort(ids, method = "radix"))
|
||||
})
|
||||
|
||||
# --- input validation --------------------------------------------------------
|
||||
|
||||
test_that("cog_suggestions() requires every argument without a default", {
|
||||
expect_error(
|
||||
cog_suggestions(years = 2011L, category = "Police", years_present = 2011L),
|
||||
"govid")
|
||||
expect_error(
|
||||
cog_suggestions("061037123085", category = "Police", years_present = 2011L),
|
||||
"years")
|
||||
expect_error(
|
||||
cog_suggestions("061037123085", years = 2011L, years_present = 2011L),
|
||||
"category")
|
||||
expect_error(
|
||||
cog_suggestions("061037123085", years = 2011L, category = "Police"),
|
||||
"years_present")
|
||||
})
|
||||
|
||||
test_that("cog_suggestions() validates govid, years and category", {
|
||||
expect_error(
|
||||
cog_suggestions(character(0), 2011L, "Police", years_present = 2011L),
|
||||
"govid")
|
||||
expect_error(
|
||||
cog_suggestions(NA_character_, 2011L, "Police", years_present = 2011L),
|
||||
"govid")
|
||||
expect_error(
|
||||
cog_suggestions(list("061037123085"), 2011L, "Police",
|
||||
years_present = 2011L),
|
||||
"govid")
|
||||
expect_error(
|
||||
cog_suggestions("061037123085", integer(0), "Police",
|
||||
years_present = 2011L),
|
||||
"years")
|
||||
expect_error(
|
||||
cog_suggestions("061037123085", "2011", "Police", years_present = 2011L),
|
||||
"years")
|
||||
expect_error(
|
||||
cog_suggestions("061037123085", 2011L, NULL, years_present = 2011L),
|
||||
"category")
|
||||
expect_error(
|
||||
cog_suggestions("061037123085", 2011L, 1L, years_present = 2011L),
|
||||
"category")
|
||||
expect_error(
|
||||
cog_suggestions("061037123085", 2011L, c("Police", NA),
|
||||
years_present = 2011L),
|
||||
"category")
|
||||
expect_error(
|
||||
cog_suggestions("061037123085", 2011L, c("All Categories", "Police"),
|
||||
years_present = 2011L),
|
||||
class = "uscogdata_all_categories_not_combinable")
|
||||
})
|
||||
|
||||
test_that("cog_suggestions() validates flow and concept against each other", {
|
||||
expect_error(
|
||||
cog_suggestions("061037123085", 2011L, "Police", flow = "balances",
|
||||
years_present = 2011L),
|
||||
class = "uscogdata_invalid_flow")
|
||||
expect_error(
|
||||
cog_suggestions("061037123085", 2011L, "Police", flow = "spending",
|
||||
concept = "general", years_present = 2011L),
|
||||
class = "uscogdata_invalid_expenditure_concept")
|
||||
expect_error(
|
||||
cog_suggestions("061037123085", 2011L, "Taxes", flow = "revenue",
|
||||
concept = "primary", years_present = 2011L),
|
||||
class = "uscogdata_invalid_revenue_concept")
|
||||
expect_error(
|
||||
cog_suggestions("061037123085", 2011L, "Police",
|
||||
concept = c("primary", "direct"), years_present = 2011L),
|
||||
class = "uscogdata_invalid_expenditure_concept")
|
||||
})
|
||||
|
||||
test_that("cog_suggestions() validates years_present", {
|
||||
expect_error(
|
||||
cog_suggestions("061037123085", 2011L, "Police", years_present = NULL),
|
||||
"integer\\(0\\)")
|
||||
expect_error(
|
||||
cog_suggestions("061037123085", 2011L, "Police", years_present = "2011"),
|
||||
"years_present")
|
||||
expect_error(
|
||||
cog_suggestions("061037123085", 2011L, "Police",
|
||||
years_present = NA_integer_),
|
||||
"years_present")
|
||||
})
|
||||
|
||||
test_that("cog_suggestions() is exported", {
|
||||
expect_true("cog_suggestions" %in% getNamespaceExports("uscogdata"))
|
||||
})
|
||||
Reference in new issue
Block a user