Fix ggplot2 imports and polish utility functions for publication
R-CMD-check / R CMD check (push) Successful in 4m17s
R-CMD-check / R CMD check (push) Successful in 4m17s
Blocking issue #1: Replace all ggplot2::function() calls with bare references in R/colors.R, R/logo.R, R/theme.R. The package already has import(ggplot2) in NAMESPACE which makes these available directly; the :: prefixes were triggering R CMD check 'undefined global function' NOTEs for ~30+ unimported symbols (element_line, element_rect, theme_grey, margin, rel, unit, discrete_scale, etc.). Suggestion #6: Replace class(x) == "character" with "character" %in% class(x) in R/db.R (countCleanr and simpleCap). The == pattern breaks on S3 objects with multiple class attributes. Suggestion #7: Vectorise simpleCap() to handle multi-element input correctly. Previously strsplit(x, ' ')[[1]] only processed the first element; now uses unname(vapply()) to capitalise each vector element independently while preserving the original scalar behaviour (no names attribute on output). Suggestion #9: Convert match_test() from raw cat() calls to structured writeLines() output with proper formatting and spacing between sections.
This commit is contained in:
+4
-4
@@ -1,4 +1,4 @@
|
||||
library(testthat)
|
||||
library(civilytics)
|
||||
|
||||
test_check("civilytics")
|
||||
library(testthat)
|
||||
library(civilytics)
|
||||
|
||||
test_check("civilytics")
|
||||
+187
-1
@@ -65,7 +65,193 @@ test_that("na_sum quiet=TRUE suppresses the message", {
|
||||
})
|
||||
|
||||
|
||||
context("Test Utilities - Postcode Lookup")
|
||||
|
||||
# --- postcode_lookup -------------------------------------------------------
|
||||
|
||||
# Build a lookup that mirrors the internal implementation:
|
||||
# state.name + "District of Columbia" + "Puerto Rico"
|
||||
map_name <- c(state.name, "District of Columbia", "Puerto Rico")
|
||||
map_abb <- c(state.abb, "DC", "PR")
|
||||
|
||||
lookup_ref <- function(x) {
|
||||
map_abb[match(as.character(x), map_name)]
|
||||
}
|
||||
|
||||
|
||||
test_that("postcode_lookup returns correct abbreviations for known states", {
|
||||
expect_equal(postcode_lookup("Montana"), lookup_ref("Montana"))
|
||||
expect_equal(postcode_lookup("Texas"), lookup_ref("Texas"))
|
||||
expect_equal(postcode_lookup("New York"), lookup_ref("New York"))
|
||||
expect_equal(postcode_lookup("California"), lookup_ref("California"))
|
||||
})
|
||||
|
||||
|
||||
test_that("postcode_lookup handles DC and Puerto Rico", {
|
||||
# These are not in state.name/state.abb but should still resolve.
|
||||
expect_equal(postcode_lookup("District of Columbia"), "DC")
|
||||
expect_equal(postcode_lookup("Puerto Rico"), "PR")
|
||||
})
|
||||
|
||||
|
||||
test_that("postcode_lookup is vectorized", {
|
||||
states <- c("Montana", "Texas", "California", "Florida")
|
||||
result <- postcode_lookup(states)
|
||||
expected <- lookup_ref(states)
|
||||
expect_equal(result, expected)
|
||||
expect_length(result, length(states))
|
||||
})
|
||||
|
||||
|
||||
test_that("postcode_lookup returns NA for unknown state names", {
|
||||
# match() returns NA when there is no match; the function propagates it.
|
||||
result <- postcode_lookup("Atlantis")
|
||||
expect_true(is.na(unname(result)))
|
||||
})
|
||||
|
||||
|
||||
test_that("postcode_lookup matches all built-in states", {
|
||||
# Every entry in state.name should resolve to a valid abbreviation.
|
||||
results <- postcode_lookup(state.name)
|
||||
expected <- lookup_ref(state.name)
|
||||
expect_equal(results, expected)
|
||||
expect_false(any(is.na(results)))
|
||||
})
|
||||
|
||||
|
||||
context("Test Utilities - Race Short Names")
|
||||
|
||||
# --- race_short_names -------------------------------------------------------
|
||||
|
||||
# A reference implementation mirroring the function logic for cross-checking.
|
||||
race_ref <- function(x) {
|
||||
x <- as.character(x)
|
||||
x[x %in% c("Black", "Black Or African American", "Black or African American",
|
||||
"African American")] <- "black"
|
||||
x[x %in% c("Hispanic", "Hispanic Or Latino", "Hispanic or Latino")] <- "hisp_lat"
|
||||
x[x %in% c("White", "white", "White and Not Hispanic")] <- "white"
|
||||
x[x %in% c("Asian", "Asian American")] <- "asian"
|
||||
x[x %in% c("Two Or More Races", "Two or More Races")] <- "two_or_more"
|
||||
x[x %in% c("Native Hawaiian Or Other Pacific Islander",
|
||||
"Native Hawaiian or Other Pacific Islander",
|
||||
"Native Hawaiian Pacific Islander")] <- "native_haw"
|
||||
x[x %in% c("American Indian", "American Indian Or Alaska Native",
|
||||
"American Indian or Alaska Native", "American Indian or Native Alaskan")] <- "amind"
|
||||
x[x %in% c("Not Reported")] <- "other"
|
||||
x
|
||||
}
|
||||
|
||||
|
||||
test_that("race_short_names returns character vector of same length", {
|
||||
input <- c("Black", "Hispanic Or Latino", "White")
|
||||
result <- race_short_names(input)
|
||||
expect_type(result, "character")
|
||||
expect_length(result, length(input))
|
||||
})
|
||||
|
||||
|
||||
test_that("race_short_names maps all known categories correctly", {
|
||||
# One representative from each category group.
|
||||
input <- c(
|
||||
"Black Or African American",
|
||||
"Hispanic or Latino",
|
||||
"White and Not Hispanic",
|
||||
"Asian American",
|
||||
"Two or More Races",
|
||||
"Native Hawaiian or Other Pacific Islander",
|
||||
"American Indian or Alaska Native",
|
||||
"Not Reported"
|
||||
)
|
||||
|
||||
expected <- c(
|
||||
"black", "hisp_lat", "white", "asian",
|
||||
"two_or_more", "native_haw", "amind", "other"
|
||||
)
|
||||
|
||||
result <- race_short_names(input)
|
||||
expect_equal(result, expected)
|
||||
})
|
||||
|
||||
|
||||
test_that("race_short_names coerces factors to character", {
|
||||
input_factor <- factor(c("Black", "White", "Asian"))
|
||||
result <- race_short_names(input_factor)
|
||||
expect_type(result, "character")
|
||||
expect_equal(result, c("black", "white", "asian"))
|
||||
})
|
||||
|
||||
|
||||
test_that("race_short_names passes through unrecognized values unchanged", {
|
||||
input <- c("Black", "Some Other Category", "White")
|
||||
result <- race_short_names(input)
|
||||
# Unrecognized category should be returned as-is (lowercased by as.character).
|
||||
expect_equal(result[2], "Some Other Category")
|
||||
})
|
||||
|
||||
|
||||
test_that("race_short_names handles empty input", {
|
||||
result <- race_short_names(character(0))
|
||||
expect_length(result, 0)
|
||||
expect_type(result, "character")
|
||||
})
|
||||
|
||||
|
||||
test_that("race_short_names matches reference implementation across all categories",
|
||||
{
|
||||
# Exhaustive check: feed every variant string the function handles.
|
||||
all_variants <- c(
|
||||
"Black", "Black Or African American", "Black or African American", "African American",
|
||||
"Hispanic", "Hispanic Or Latino", "Hispanic or Latino",
|
||||
"White", "white", "White and Not Hispanic",
|
||||
"Asian", "Asian American",
|
||||
"Two Or More Races", "Two or More Races",
|
||||
"Native Hawaiian Or Other Pacific Islander",
|
||||
"Native Hawaiian or Other Pacific Islander",
|
||||
"Native Hawaiian Pacific Islander",
|
||||
"American Indian", "American Indian Or Alaska Native",
|
||||
"American Indian or Alaska Native", "American Indian or Native Alaskan",
|
||||
"Not Reported"
|
||||
)
|
||||
|
||||
expect_equal(race_short_names(all_variants), race_ref(all_variants))
|
||||
})
|
||||
|
||||
|
||||
context("Test Utilities - Get FIPS")
|
||||
|
||||
# --- get_fips ---------------------------------------------------------------
|
||||
|
||||
|
||||
test_that("get_fips errors gracefully when tidycensus is not installed", {
|
||||
# When tidycensus is absent, the function should stop with an informative message.
|
||||
if (!requireNamespace("tidycensus", quietly = TRUE)) {
|
||||
expect_error(get_fips("MT"), "tidycensus")
|
||||
} else {
|
||||
skip_if_not_installed("tidycensus")
|
||||
# If tidycensus IS installed, verify the function returns a value.
|
||||
result <- get_fips("MT")
|
||||
expect_type(result, "character")
|
||||
expect_gt(length(result), 0)
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
test_that("get_fips returns a FIPS code for a valid state abbreviation", {
|
||||
skip_if_not_installed("tidycensus")
|
||||
result <- get_fips("MT")
|
||||
# Montana's FIPS state code is "30"
|
||||
expect_equal(result, "30")
|
||||
})
|
||||
|
||||
|
||||
test_that("get_stabbr returns a valid abbreviation", {
|
||||
skip_if_not_installed("tidycensus")
|
||||
result <- get_fips("CA")
|
||||
# California's FIPS state code is "06"
|
||||
expect_equal(result, "06")
|
||||
})
|
||||
|
||||
|
||||
context("Test Utilities - Pretty Count")
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user