Skip to content

Commit

Permalink
More coverage tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mdblocker committed Dec 15, 2023
1 parent b4a7da1 commit 296e55c
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
22 changes: 22 additions & 0 deletions tests/testthat/test-census_helper.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Note: must provide a valid U.S. Census API key for test cases that use U.S. Census statistics
# > usethis::edit_r_profile
# Sys.setenv("CENSUS_API_KEY" = "yourkey")
# For testing package coverage use: Sys.setenv("NOT_CRAN" = "TRUE")
options("piggyback.verbose" = FALSE)
options("wru_data_wd" = TRUE)

skip_if_not(nzchar(Sys.getenv("CENSUS_API_KEY")))
test_that("census_helper old still returns predictions", {
data(voters)
census <- readRDS(test_path("data/census_test_nj_block_2020.rds"))
x <- census_helper(
voter.file = voters,
states = "NJ",
year = "2020",
census.data = census
)
expect_named(x, c('VoterID', 'surname', 'state', 'CD', 'county',
'tract', 'block', 'precinct', 'age', 'sex', 'party',
'PID', 'place', 'last', 'first', 'r_whi', 'r_bla', 'r_his',
'r_asi', 'r_oth'))
})
9 changes: 9 additions & 0 deletions tests/testthat/test-format_legacy_data.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
options("piggyback.verbose" = FALSE)
options("wru_data_wd" = TRUE)

test_that("legacy data returns expected geo groups",{
skip_on_cran()
de <- format_legacy_data(PL94171::pl_url('DE', 2020), state = "DE")

expect_named(de, c("county", "tract", "blockGroup", "block"))
})
32 changes: 32 additions & 0 deletions tests/testthat/test-race_prediction_funs.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Note: must provide a valid U.S. Census API key for test cases that use U.S. Census statistics
# > usethis::edit_r_profile
# Sys.setenv("CENSUS_API_KEY" = "yourkey")
# For testing package coverage use: Sys.setenv("NOT_CRAN" = "TRUE")
options("piggyback.verbose" = FALSE)
options("wru_data_wd" = TRUE)

skip_if_not(nzchar(Sys.getenv("CENSUS_API_KEY")))
test_that("old predict_race fxn returns sensible predictions for test names", {
data("voters")
x <- .predict_race_old(
voter.file = voters,
census.surname = TRUE,
surname.only = TRUE,
surname.year = 2010,
census.geo = "tract",
census.key = Sys.getenv("CENSUS_API_KEY"),
age = FALSE,
sex = FALSE,
year = "2010",
retry = 3,
impute.missing = TRUE,
use.counties = FALSE
)
expect_equal(x[x$surname == "Lopez", "pred.whi"], 0.0486000, tolerance = .000001)
expect_equal(x[x$surname == "Khanna", "pred.whi"], 0.0676000, tolerance = .000001)
expect_equal(x[x$surname == "Lopez", "pred.bla"], 0.00570000, tolerance = .000001)
expect_equal(x[x$surname == "Khanna", "pred.bla"], 0.00430000, tolerance = .000001)
expect_equal(x[x$surname == "Lopez", "pred.his"], 0.92920000, tolerance = .000001) #assumed to be high Hispanic score
expect_equal(x[x$surname == "Zhou", "pred.asi"], 0.98200000, tolerance = .000001) #assumed to be high Asian score

})

0 comments on commit 296e55c

Please sign in to comment.