Skip to content

Commit

Permalink
add unit tests for tidy_erp()
Browse files Browse the repository at this point in the history
  • Loading branch information
MattCowgill committed Jan 14, 2025
1 parent 85bef67 commit d31c667
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
22 changes: 18 additions & 4 deletions R/read_erp.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ read_erp <- function(age_range = 0:100,
}

# Restrict the 'sex' argument to the valid choices
sex <- match.arg(sex)
sex <- match.arg(sex, several.ok = TRUE)

# Restrict the states argument to valid choices but include abbreviations
# Always return the full name if an abbreviation has been used.
Expand Down Expand Up @@ -100,8 +100,24 @@ read_erp <- function(age_range = 0:100,
path = path
)

x <- tidy_erp(erp_raw,
age_range,
sex)

x <- erp_raw %>%
x
}

#' @keywords internal
#' @noRd
#' Tidy a table of ERP data downloaded with read_abs(cat_no = "3101.0")
#' @examples
#' x <- read_abs(cat_no = "3101.0")
#' tidy_erp(x)
#'
tidy_erp <- function(erp_raw,
age_range,
sex) {
erp_raw %>%
dplyr::mutate(
age = gsub("[^0-9]", "", .data$series),
age = as.integer(.data$age),
Expand All @@ -117,8 +133,6 @@ read_erp <- function(age_range = 0:100,
dplyr::arrange(.data$state, .data$series_sex, .data$age, .data$date) %>%
dplyr::select("date", "state", sex = "series_sex",
"erp", "age")

x
}

#' @keywords internal
Expand Down
41 changes: 41 additions & 0 deletions tests/testthat/test-read_erp.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
test_that("read_erp() works", {
x <- read_erp()

expect_is(x, "tbl_df")
expect_length(x, 5)
expect_gt(nrow(x), 40000)
expect_is(x$date, "Date")
expect_is(x$state, "character")
expect_is(x$sex, "character")
expect_is(x$erp, "numeric")
expect_is(x$age, "integer")
expect_equal(length(unique(x$state)), 9)
expect_equal(unique(x$sex), "Persons")
expect_equal(unique(x$age), 0:100)
})

test_that("tidy_erp() works with all permutations of parameters", {
raw_erp <- read_abs("3101.0")

test_tidy_erp <- function(age_range,
sex,
erp_raw = raw_erp) {
x <- tidy_erp(erp_raw,
age_range,
sex)

expect_is(x, "tbl_df")
expect_length(x, 5)
expect_equal(sort(unique(x$age)), sort(age_range))
expect_equal(sort(unique(x$sex)), sort(sex))
}

test_tidy_erp(1:10, "Male")
test_tidy_erp(15, "Female")
test_tidy_erp(c(10, 50, 90), "Persons")
test_tidy_erp(0:100,
c("Male", "Female", "Persons"))
test_tidy_erp(1,
c("Female", "Male"))

})

0 comments on commit d31c667

Please sign in to comment.