Compare commits
7
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e067a5930f
|
||
|
|
342debaefa
|
||
|
|
b59b79b2d5 | ||
|
|
5668d6b102
|
||
|
|
0a6d878a36 | ||
|
|
da726a61f6
|
||
|
|
03c313b46d |
@@ -11,6 +11,21 @@ jobs:
|
||||
steps:
|
||||
- name: Install system libraries and Node.js (required by actions/checkout)
|
||||
run: |
|
||||
# Switch apt to HTTPS mirrors. Measured from this runner on
|
||||
# 2026-08-04: the SAME index file takes 20.1s over http:// and 3.1s
|
||||
# over https://. apt fetches many indexes serially, so http:// does
|
||||
# not read as "slow" -- it reads as a hang (zero bytes in
|
||||
# /var/cache/apt/archives after 3+ minutes, apt's http workers parked
|
||||
# in S state). rocker/r-ver:4.4 already ships ca-certificates and
|
||||
# apt 2.8.3 has the https method built in, so nothing needs to be
|
||||
# installed over http first to bootstrap this.
|
||||
# `|| true` because the step runs under `sh -e`: on an image whose
|
||||
# sources live in the other location, the missing-file sed must not
|
||||
# kill the job.
|
||||
sed -i -E 's#http://(archive|security)\.ubuntu\.com#https://\1.ubuntu.com#g' \
|
||||
/etc/apt/sources.list.d/ubuntu.sources 2>/dev/null || true
|
||||
sed -i -E 's#http://(archive|security)\.ubuntu\.com#https://\1.ubuntu.com#g' \
|
||||
/etc/apt/sources.list 2>/dev/null || true
|
||||
apt-get update -qq
|
||||
apt-get install -y --no-install-recommends \
|
||||
nodejs git \
|
||||
|
||||
+13
-6
@@ -5,12 +5,19 @@
|
||||
#' Returns the category taxonomy exposed by the corpus's
|
||||
#' `summary_categories` view, grouped to one row per
|
||||
#' `(category, subtype)` pair. Use this to discover valid `category`
|
||||
#' values for [cog_spending()] / [cog_revenue()] /
|
||||
#' values for [cog_spending()] / [cog_revenue()] / [cog_balances()] /
|
||||
#' [cog_geographic_rollup()] and to audit which Census item codes feed
|
||||
#' each category.
|
||||
#'
|
||||
#' @param type Either `NULL` (default, return both spending and revenue
|
||||
#' rows), `"spending"`, or `"revenue"`.
|
||||
#' `subtype` COALESCEs the crosswalk's three subtype columns, so it carries
|
||||
#' `spend_subtype` on expenditure rows, `revenue_subtype` on revenue rows and
|
||||
#' `balance_subtype` on balance rows. Note that [cog_balances()] itself takes
|
||||
#' no `subtype` argument — for holdings, `category` is a strict coarsening of
|
||||
#' `balance_subtype` — but the value is surfaced here because it is the
|
||||
#' discovery surface downstream consumers build their vocabulary from.
|
||||
#'
|
||||
#' @param type Either `NULL` (default, every row: expenditure, revenue and
|
||||
#' balance), `"spending"`, `"revenue"`, or `"balance"`.
|
||||
#' @param pattern Optional regex matched case-insensitively against the
|
||||
#' `category` column (e.g. `"Police"` or `"Tax"`).
|
||||
#' @return Tibble with columns `category`, `category_type`, `subtype`,
|
||||
@@ -20,8 +27,8 @@
|
||||
cog_categories <- function(type = NULL, pattern = NULL) {
|
||||
if (!is.null(type)) {
|
||||
if (!is.character(type) || length(type) != 1L ||
|
||||
!type %in% c("spending", "revenue")) {
|
||||
cli::cli_abort('`type` must be NULL, "spending", or "revenue".')
|
||||
!type %in% c("spending", "revenue", "balance")) {
|
||||
cli::cli_abort('`type` must be NULL, "spending", "revenue", or "balance".')
|
||||
}
|
||||
}
|
||||
if (!is.null(pattern) &&
|
||||
@@ -48,7 +55,7 @@ cog_categories <- function(type = NULL, pattern = NULL) {
|
||||
|
||||
sql <- paste(
|
||||
"SELECT category, category_type,
|
||||
COALESCE(spend_subtype, revenue_subtype) AS subtype,
|
||||
COALESCE(spend_subtype, revenue_subtype, balance_subtype) AS subtype,
|
||||
COUNT(DISTINCT item_code) AS n_codes,
|
||||
string_agg(DISTINCT item_code, ',' ORDER BY item_code) AS item_codes
|
||||
FROM summary_categories",
|
||||
|
||||
+1
-1
@@ -138,7 +138,7 @@
|
||||
#' year, matching canonical_fips_xwalk) rather than as-of-year; as-of-year
|
||||
#' moved to the *_asof columns. This package's own geography always came from
|
||||
#' the xwalk (already present-based), so behaviour is unchanged.
|
||||
.validate_schema <- function(manifest, supported = c(4L, 5L, 6L)) {
|
||||
.validate_schema <- function(manifest, supported = c(4L, 5L, 6L, 7L)) {
|
||||
if (!manifest$schema_version %in% supported) {
|
||||
cli::cli_abort(c(
|
||||
"Corpus schema version mismatch.",
|
||||
|
||||
+1
-1
@@ -12,7 +12,7 @@ cog_open <- function(url = .resolve_url(),
|
||||
DBI::dbExecute(con, "INSTALL httpfs; LOAD httpfs;")
|
||||
|
||||
manifest <- .fetch_or_cache_manifest(url, cache_dir)
|
||||
.validate_schema(manifest, supported = c(4L, 5L, 6L))
|
||||
.validate_schema(manifest, supported = c(4L, 5L, 6L, 7L))
|
||||
.validate_scope(manifest)
|
||||
|
||||
.register_views(con, url, manifest)
|
||||
|
||||
+11
-3
@@ -7,8 +7,8 @@
|
||||
cog_categories(type = NULL, pattern = NULL)
|
||||
}
|
||||
\arguments{
|
||||
\item{type}{Either `NULL` (default, return both spending and revenue
|
||||
rows), `"spending"`, or `"revenue"`.}
|
||||
\item{type}{Either `NULL` (default, every row: expenditure, revenue and
|
||||
balance), `"spending"`, `"revenue"`, or `"balance"`.}
|
||||
|
||||
\item{pattern}{Optional regex matched case-insensitively against the
|
||||
`category` column (e.g. `"Police"` or `"Tax"`).}
|
||||
@@ -22,7 +22,15 @@ Tibble with columns `category`, `category_type`, `subtype`,
|
||||
Returns the category taxonomy exposed by the corpus's
|
||||
`summary_categories` view, grouped to one row per
|
||||
`(category, subtype)` pair. Use this to discover valid `category`
|
||||
values for [cog_spending()] / [cog_revenue()] /
|
||||
values for [cog_spending()] / [cog_revenue()] / [cog_balances()] /
|
||||
[cog_geographic_rollup()] and to audit which Census item codes feed
|
||||
each category.
|
||||
}
|
||||
\details{
|
||||
`subtype` COALESCEs the crosswalk's three subtype columns, so it carries
|
||||
`spend_subtype` on expenditure rows, `revenue_subtype` on revenue rows and
|
||||
`balance_subtype` on balance rows. Note that [cog_balances()] itself takes
|
||||
no `subtype` argument — for holdings, `category` is a strict coarsening of
|
||||
`balance_subtype` — but the value is surfaced here because it is the
|
||||
discovery surface downstream consumers build their vocabulary from.
|
||||
}
|
||||
|
||||
@@ -93,3 +93,40 @@ test_that("cog_categories sorted by category_type, category, subtype", {
|
||||
test_that("cog_categories rejects invalid type", {
|
||||
expect_error(cog_categories(type = "both"), "type")
|
||||
})
|
||||
|
||||
test_that("cog_categories() surfaces balance subtypes", {
|
||||
skip_if_no_corpus()
|
||||
with_fixture_corpus({
|
||||
cc <- cog_categories()
|
||||
b <- cc[cc$category_type == "balance", ]
|
||||
expect_true(nrow(b) > 0L)
|
||||
|
||||
# Every balance row must carry its subtype. Before the COALESCE included
|
||||
# balance_subtype these were all NA, which silently made the balance
|
||||
# taxonomy undiscoverable -- cog-api derives its subtype vocabulary from
|
||||
# this function, so an NA here becomes an unusable API parameter.
|
||||
expect_false(any(is.na(b$subtype)))
|
||||
|
||||
# The exact set, read independently from the crosswalk rather than from
|
||||
# the function under test.
|
||||
con2 <- DBI::dbConnect(duckdb::duckdb())
|
||||
on.exit(DBI::dbDisconnect(con2, shutdown = TRUE), add = TRUE)
|
||||
p <- file.path(fixture_corpus_path(), "data", "summary_categories.parquet")
|
||||
want <- DBI::dbGetQuery(con2, sprintf(
|
||||
"SELECT DISTINCT balance_subtype FROM read_parquet(%s)
|
||||
WHERE category_type = 'balance' AND balance_subtype IS NOT NULL
|
||||
ORDER BY 1", uscogdata:::.sql_lit_chr(p)))$balance_subtype
|
||||
expect_true(length(want) > 1L)
|
||||
expect_identical(sort(unique(b$subtype)), sort(want))
|
||||
})
|
||||
})
|
||||
|
||||
test_that('cog_categories(type = "balance") filters to holdings', {
|
||||
skip_if_no_corpus()
|
||||
with_fixture_corpus({
|
||||
b <- cog_categories(type = "balance")
|
||||
expect_true(nrow(b) > 0L)
|
||||
expect_identical(unique(b$category_type), "balance")
|
||||
expect_false(any(is.na(b$subtype)))
|
||||
})
|
||||
})
|
||||
|
||||
@@ -111,7 +111,7 @@ test_that("cog_manifest returns the active session's parsed manifest", {
|
||||
})
|
||||
})
|
||||
|
||||
test_that(".validate_schema accepts schema_version 4, 5 and 6, rejects others", {
|
||||
test_that(".validate_schema accepts schema_version 4 through 7, rejects others", {
|
||||
expect_silent(uscogdata:::.validate_schema(list(schema_version = 4L)))
|
||||
expect_silent(uscogdata:::.validate_schema(list(schema_version = 5L)))
|
||||
# v6 = FIPS geography harmonization (2026-07-22): _code -> _asof rename +
|
||||
@@ -119,12 +119,22 @@ test_that(".validate_schema accepts schema_version 4, 5 and 6, rejects others",
|
||||
# renamed columns and its geography comes from the xwalk, so v6 is accepted
|
||||
# without behavioural change -- see .validate_schema()'s note.
|
||||
expect_silent(uscogdata:::.validate_schema(list(schema_version = 6L)))
|
||||
# v7 = `data_year` APPENDED as column 29 (cog_pipeline #80, 2026-08-03), the
|
||||
# most recent fiscal year contributing to a collapsed key. Appended, never
|
||||
# inserted: canonical_govid stays at position 26, so nothing this package
|
||||
# reads shifts. Verified against the real v7 corpus before widening the
|
||||
# allow-list -- cog_spending()/cog_balances() return correctly for FY2024 AND
|
||||
# for FY2012, so the new column is inert here.
|
||||
expect_silent(uscogdata:::.validate_schema(list(schema_version = 7L)))
|
||||
expect_error(
|
||||
uscogdata:::.validate_schema(list(schema_version = 3L)),
|
||||
"schema_version"
|
||||
)
|
||||
# The upper bound still has to be ENFORCED, not just moved. Without this the
|
||||
# test would no longer prove that an unknown future schema is refused, and a
|
||||
# v8 corpus with a genuinely breaking change would sail through.
|
||||
expect_error(
|
||||
uscogdata:::.validate_schema(list(schema_version = 7L)),
|
||||
uscogdata:::.validate_schema(list(schema_version = 8L)),
|
||||
"schema_version"
|
||||
)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user