Files
uscogdata/tests/testthat/test-peers.R
T
jared 919548685b
R-CMD-check / check (push) Failing after 1m30s
polish(per-year-pop): expand popyear in cog_explain + propagate pop_range
Final-review followups:

1. cog_explain rendered the popyear range as raw 2-digit values
   ("popyear range: 19-20"), which a user could read as the years 19-20.
   Added .expand_popyear() helper to format as 4-digit calendar years
   (2019-2020). Pivot at 70 to handle pre-2000 vintages if the corpus
   ever extends backward.

2. cog_peer_compare provenance was missing pop_range and is_ratio,
   omitted from the spec-required reproducibility metadata.
   cog_find_peers now stamps both as tibble attributes; cog_peer_compare
   reads them through to provenance$pop_range and provenance$is_ratio.

Test coverage extended: explain test asserts the 4-digit format and
rejects the old 2-digit form; peer-compare test asserts pop_range +
is_ratio propagate end-to-end.

326 PASS / 0 FAIL.
2026-04-29 19:16:48 -04:00

139 lines
4.9 KiB
R

test_that("cog_find_peers returns same-type peers in the default pop band", {
skip_if_no_corpus()
peers <- cog_find_peers("101006006") # Broward County
expect_s3_class(peers, "tbl_df")
expected_cols <- c("canonical_govid", "gov_name", "fips_state",
"population", "pop_ratio", "rank")
expect_true(all(expected_cols %in% names(peers)))
expect_true(all(peers$pop_ratio >= 0.7 & peers$pop_ratio <= 1.3))
expect_false("101006006" %in% peers$canonical_govid)
expect_equal(peers$rank, seq_len(nrow(peers)))
})
test_that("cog_find_peers respects same_state restriction", {
skip_if_no_corpus()
peers <- cog_find_peers("101006006", same_state = TRUE,
pop_range = c(0.1, 10))
expect_true(all(peers$fips_state == "12"))
})
test_that("cog_find_peers absolute pop range works", {
skip_if_no_corpus()
peers <- cog_find_peers("101006006",
pop_range = c(1.5e6, 2.5e6),
is_ratio = FALSE, max_peers = 20L)
expect_true(all(peers$population >= 1.5e6 &
peers$population <= 2.5e6))
})
test_that("cog_find_peers errors cleanly on unknown govid", {
skip_if_no_corpus()
expect_error(cog_find_peers("XXXINVALID"), "not found")
})
test_that("cog_peer_compare accepts a cog_find_peers result directly", {
skip_if_no_corpus()
peers <- cog_find_peers("101006006", max_peers = 4L)
r <- cog_peer_compare("101006006", peers, "Police", years = 2020L)
expect_s3_class(r, "tbl_df")
expect_true("role" %in% names(r))
expect_setequal(
unique(r$role),
c("target", "peer", "summary_p25", "summary_p50", "summary_p75")
)
expect_equal(sum(r$role == "target" & r$spend_subtype == "operations"), 1L)
})
test_that("cog_peer_compare accepts a character vector of govids", {
skip_if_no_corpus()
r <- cog_peer_compare(
"101006006",
peers = c("441015015", "441220220"), # Bexar, Tarrant
category = "Police", years = 2020L
)
expect_true("peer" %in% r$role)
expect_equal(sum(r$role == "peer" & r$spend_subtype == "operations"), 2L)
})
test_that("cog_peer_compare summary rows use real per-capita when requested", {
skip_if_no_corpus()
r <- cog_peer_compare(
"101006006",
peers = c("441015015", "441220220", "231082082"),
category = "Police", years = 2019:2020,
per_capita = TRUE, adjust_to_year = 2022L
)
summaries <- dplyr::filter(r, grepl("^summary_", role))
expect_true(all(is.finite(summaries$amt_per_capita_real)))
# summary rows have NA canonical_govid and named gov_name
expect_true(all(is.na(summaries$canonical_govid)))
expect_true(all(grepl("Peer", summaries$gov_name)))
})
test_that("cog_peer_compare provenance reports the outer verb + peer count", {
skip_if_no_corpus()
r <- cog_peer_compare("101006006",
peers = c("441015015", "441220220"),
category = "Police", years = 2020L)
prov <- attr(r, "provenance")
expect_equal(prov$verb, "cog_peer_compare")
expect_equal(prov$peer_count, 2L)
})
test_that("cog_peer_compare handles zero peers gracefully", {
skip_if_no_corpus()
r <- cog_peer_compare("101006006",
peers = character(0),
category = "Police", years = 2020L)
expect_true(all(r$role == "target"))
expect_equal(sum(grepl("^summary_", r$role)), 0L)
})
test_that("cog_find_peers defaults `year` to most recent observed year for target", {
skip_if_no_corpus()
peers <- cog_find_peers("101006006")
expect_equal(attr(peers, "cohort_year"), 2020L)
# Returned column is now `population`, not `population_acs`
expect_true("population" %in% names(peers))
expect_false("population_acs" %in% names(peers))
})
test_that("cog_find_peers honors an explicit `year`", {
skip_if_no_corpus()
peers <- cog_find_peers("101006006", year = 2019L)
expect_equal(attr(peers, "cohort_year"), 2019L)
})
test_that("cog_find_peers errors when target has no observed pop in `year`", {
skip_if_no_corpus()
expect_error(
cog_find_peers("101006006", year = 1999L),
"no observed population"
)
})
test_that("cog_peer_compare stamps cohort_year from peers attribute", {
skip_if_no_corpus()
peers <- cog_find_peers("101006006", year = 2019L, max_peers = 4L,
pop_range = c(0.5, 1.5))
r <- cog_peer_compare("101006006", peers, "Police", years = 2020L)
expect_true("cohort_year" %in% names(r))
expect_true(all(r$cohort_year == 2019L))
prov <- attr(r, "provenance")
expect_equal(prov$cohort_year, 2019L)
expect_equal(length(prov$cohort_govids), nrow(peers))
# pop_range and is_ratio propagate from cog_find_peers attrs
expect_equal(prov$pop_range, c(0.5, 1.5))
expect_true(prov$is_ratio)
})
test_that("cog_peer_compare cohort_year is NA for bare character peers", {
skip_if_no_corpus()
r <- cog_peer_compare(
"101006006",
peers = c("441015015", "441220220"),
category = "Police", years = 2020L
)
expect_true(all(is.na(r$cohort_year)))
})