Nothing
# tests/testthat/test-catalog.R
# Tests for list_elections(), list_counties(), and catalog validation.
# These tests require election_catalog and county_catalog to be present
# (run data-raw/create_catalogs.R first).
test_that("list_elections() returns a data.frame with expected columns", {
result <- list_elections()
expect_s3_class(result, "data.frame")
expect_named(result, c("code", "office", "type", "level", "counties"))
expect_gt(nrow(result), 0L)
})
test_that("list_elections() filter by type returns only that type", {
for (t in c("single_member", "multi_member", "referendum")) {
result <- list_elections(type = t)
if (nrow(result) > 0L) expect_true(all(result$type == t))
}
})
test_that("list_elections() filter by level returns only that level", {
result <- list_elections(level = "federal")
if (nrow(result) > 0L) expect_true(all(result$level == "federal"))
})
test_that("list_elections() combined filters work", {
result <- list_elections(type = "referendum", level = "state")
if (nrow(result) > 0L) {
expect_true(all(result$type == "referendum"))
expect_true(all(result$level == "state"))
}
})
test_that("list_counties() returns a data.frame with expected columns", {
result <- list_counties()
expect_s3_class(result, "data.frame")
expect_named(result, c("county", "n_elections"))
expect_gt(nrow(result), 0L)
expect_true(all(result$n_elections > 0L))
})
test_that("Lee county is present in county_catalog", {
result <- list_counties()
expect_true("Lee" %in% result$county)
})
test_that("election_catalog entries have required fields", {
required <- c("type", "office", "level", "counties")
for (code in names(election_catalog)) {
entry <- election_catalog[[code]]
missing <- setdiff(required, names(entry))
expect_equal(
length(missing), 0L,
label = sprintf("election_catalog[[\"%s\"]] missing: %s",
code, paste(missing, collapse = ", "))
)
}
})
test_that("multi_member entries have vote_for and candidates fields", {
mm <- Filter(function(e) e$type == "multi_member", election_catalog)
for (code in names(mm)) {
expect_true("vote_for" %in% names(mm[[code]]),
label = sprintf("%s missing vote_for", code))
expect_true("candidates" %in% names(mm[[code]]),
label = sprintf("%s missing candidates", code))
}
})
test_that("all counties in election_catalog exist in county_catalog", {
all_ref <- unique(unlist(lapply(election_catalog, function(e) {
if (identical(e$counties, "all")) character(0) else e$counties
})))
missing <- setdiff(all_ref, names(county_catalog))
expect_equal(
length(missing), 0L,
label = paste("Counties missing from county_catalog:",
paste(missing, collapse = ", "))
)
})
test_that(".validate_elections() errors on unknown race codes", {
expect_error(
eiballots:::.validate_elections("ZZZNOPE"),
regexp = "not found in catalog"
)
})
test_that(".counties_for_elections() warns when no county is shared", {
expect_warning(
get_election_data(c("SRH91", "MOS1")), # ajusta a tu catálogo
regexp = "No county participates"
)
})
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.