From 21b3d66c0edc2684da89e634b83ac9bc76539c75 Mon Sep 17 00:00:00 2001 From: Jared Knowles Date: Wed, 29 Apr 2026 16:20:41 -0400 Subject: [PATCH] test(spending): per-year denominator expectation (failing) Adds a RED test asserting that cog_spending(per_capita = TRUE) divides by the per-year F-33 population (Broward 2019: 1,935,878; 2020: 1,952,778) rather than the static ACS value (1,940,907). Uses absolute-tolerance expect_true(abs(...) < 1) instead of expect_equal(tolerance=1) because testthat 3 treats the tolerance argument as relative. --- tests/testthat/test-spending.R | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/testthat/test-spending.R b/tests/testthat/test-spending.R index 3d39f19..d1a279c 100644 --- a/tests/testthat/test-spending.R +++ b/tests/testthat/test-spending.R @@ -128,3 +128,20 @@ test_that("cog_spending accepts a basket-mode cog_gov_search result", { expect_s3_class(spending, "tbl_df") expect_setequal(unique(spending$canonical_govid), basket$canonical_govid) }) + +test_that("per_capita denominator is the per-year F-33 population", { + skip_if_no_corpus() + with_fixture_corpus({ + r <- cog_spending("101006006", years = 2019:2020, + category = "Police", per_capita = TRUE) + r_ops <- r[r$spend_subtype == "operations", ] + # Implied denominator from amt_nominal / amt_per_capita_nominal + implied_pop <- r_ops$amt_nominal / r_ops$amt_per_capita_nominal + names(implied_pop) <- r_ops$year + # Use absolute tolerance: within 1 person of per-year F-33 values + expect_true(abs(implied_pop[["2019"]] - 1935878) < 1) + expect_true(abs(implied_pop[["2020"]] - 1952778) < 1) + # And the implied denominator does NOT equal the static ACS value + expect_false(all(abs(implied_pop - 1940907) < 1)) + }) +})