Files
uscogdata/tests/testthat/test-search.R
T
jared 392e818f52 feat(search): add substring fallback and no_match handling
.resolve_basket_row() now falls back to case-insensitive substring
match when no exact match is found, and short-circuits empty/whitespace
input to no_match. Disambiguation stub raises pending Task 5.
2026-04-28 11:06:30 -04:00

207 lines
6.3 KiB
R

test_that("cog_gov_search by name pattern returns matches", {
skip_if_no_corpus()
r <- cog_gov_search("^BROWARD")
expect_s3_class(r, "tbl_df")
expect_true(all(grepl("^BROWARD", r$gov_name)))
expect_true("canonical_govid" %in% names(r))
})
test_that("cog_gov_search case-insensitive", {
skip_if_no_corpus()
r <- cog_gov_search("broward")
expect_gt(nrow(r), 0L)
})
test_that("cog_gov_search by state abbreviation", {
skip_if_no_corpus()
r <- cog_gov_search(state = "FL", type = 1L)
expect_true(all(r$fips_state == "12"))
expect_true(all(r$govs_type == 1L))
})
test_that("cog_gov_search accepts FIPS int for state", {
skip_if_no_corpus()
r <- cog_gov_search(state = 12, type = "county")
expect_true(all(r$fips_state == "12"))
expect_true(all(r$govs_type == 1L))
})
test_that("cog_gov_search accepts type as name string", {
skip_if_no_corpus()
for (pair in list(c("state", 0), c("county", 1), c("city", 2), c("township", 3))) {
r <- cog_gov_search(type = pair[1])
expect_true(all(r$govs_type == as.integer(pair[2])))
}
})
test_that("cog_gov_search for excluded types emits message and returns empty", {
skip_if_no_corpus()
expect_message(
r <- cog_gov_search(type = 4L),
"v0.1|excluded"
)
expect_equal(nrow(r), 0L)
expect_true("canonical_govid" %in% names(r))
expect_message(
r2 <- cog_gov_search(type = "special_district"),
"v0.1|excluded"
)
expect_equal(nrow(r2), 0L)
})
test_that("cog_gov_search combining filters", {
skip_if_no_corpus()
r <- cog_gov_search("BROWARD", state = "FL")
expect_true(all(grepl("BROWARD", r$gov_name)))
expect_true(all(r$fips_state == "12"))
})
test_that("cog_gov_search sorts by population desc", {
skip_if_no_corpus()
r <- cog_gov_search(state = "FL", type = 2L)
nonNA <- r$population_acs[!is.na(r$population_acs)]
expect_true(all(diff(nonNA) <= 0))
})
test_that("cog_gov_search rejects unknown type string", {
expect_error(cog_gov_search(type = "galaxy"), "Unknown type")
})
test_that("cog_gov_search with no filters returns full registry", {
skip_if_no_corpus()
r <- cog_gov_search()
expect_gt(nrow(r), 1000L)
})
# ---- basket mode internal helpers ----
test_that(".validate_basket_args recycles state from length 1", {
out <- uscogdata:::.validate_basket_args(
name = c("Broward", "San Diego", "Austin"),
state = "FL",
type = NULL
)
expect_equal(out$name, c("Broward", "San Diego", "Austin"))
expect_equal(out$state, c("FL", "FL", "FL"))
expect_equal(out$type, c(NA_character_, NA_character_, NA_character_))
})
test_that(".validate_basket_args recycles type from length 1", {
out <- uscogdata:::.validate_basket_args(
name = c("San Diego", "Oakland"),
state = "CA",
type = "city"
)
expect_equal(out$type, c("city", "city"))
})
test_that(".validate_basket_args accepts per-row state and type", {
out <- uscogdata:::.validate_basket_args(
name = c("Broward", "San Diego"),
state = c("FL", "CA"),
type = c(NA, "city")
)
expect_equal(out$state, c("FL", "CA"))
expect_equal(out$type, c(NA_character_, "city"))
})
test_that(".validate_basket_args rejects length-mismatched state", {
expect_error(
uscogdata:::.validate_basket_args(
name = c("Broward", "San Diego", "Austin"),
state = c("FL", "CA"),
type = NULL
),
regexp = "must be length 1 or 3"
)
})
test_that(".validate_basket_args rejects length-mismatched type", {
expect_error(
uscogdata:::.validate_basket_args(
name = c("Broward", "San Diego"),
state = "FL",
type = c("county", "city", "city")
),
regexp = "must be length 1 or 2"
)
})
test_that(".validate_basket_args allows NULL state and type", {
out <- uscogdata:::.validate_basket_args(
name = c("Broward", "San Diego"),
state = NULL,
type = NULL
)
expect_equal(out$state, c(NA_character_, NA_character_))
expect_equal(out$type, c(NA_character_, NA_character_))
})
test_that(".resolve_basket_row exact match returns one row", {
con <- uscogdata:::.ensure_session()
out <- uscogdata:::.resolve_basket_row(
name = "BROWARD COUNTY", state = "FL", type = NA_character_, con = con
)
expect_equal(out$status, "resolved")
expect_equal(out$match_method, "exact")
expect_equal(out$n_candidates, 1L)
expect_equal(nrow(out$row), 1L)
expect_equal(out$row$canonical_govid, "101006006")
expect_equal(out$row$gov_name, "BROWARD COUNTY")
})
test_that(".resolve_basket_row exact match is case-insensitive", {
con <- uscogdata:::.ensure_session()
out <- uscogdata:::.resolve_basket_row(
name = "broward county", state = "FL", type = NA_character_, con = con
)
expect_equal(out$status, "resolved")
expect_equal(out$match_method, "exact")
expect_equal(out$row$canonical_govid, "101006006")
})
test_that(".resolve_basket_row exact match honors per-row type", {
con <- uscogdata:::.ensure_session()
out <- uscogdata:::.resolve_basket_row(
name = "SAN DIEGO CITY", state = "CA", type = "city", con = con
)
expect_equal(out$status, "resolved")
expect_equal(out$row$canonical_govid, "052037010")
})
test_that(".resolve_basket_row substring fallback resolves single match", {
con <- uscogdata:::.ensure_session()
out <- uscogdata:::.resolve_basket_row(
name = "Broward", state = "FL", type = NA_character_, con = con
)
expect_equal(out$status, "resolved")
expect_equal(out$match_method, "substring")
expect_equal(out$n_candidates, 1L)
expect_equal(out$row$canonical_govid, "101006006")
})
test_that(".resolve_basket_row no_match returns 0-row tibble", {
con <- uscogdata:::.ensure_session()
out <- uscogdata:::.resolve_basket_row(
name = "Notarealplace", state = "NY", type = NA_character_, con = con
)
expect_equal(out$status, "no_match")
expect_true(is.na(out$match_method))
expect_equal(out$n_candidates, 0L)
expect_equal(nrow(out$row), 0L)
expect_equal(nrow(out$candidates), 0L)
})
test_that(".resolve_basket_row treats empty/whitespace name as no_match", {
con <- uscogdata:::.ensure_session()
out_empty <- uscogdata:::.resolve_basket_row(
name = "", state = "FL", type = NA_character_, con = con
)
expect_equal(out_empty$status, "no_match")
out_ws <- uscogdata:::.resolve_basket_row(
name = " ", state = "FL", type = NA_character_, con = con
)
expect_equal(out_ws$status, "no_match")
})