Skip to content

Commit

Permalink
split tests into smaller pieces
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Jun 22, 2024
1 parent 6f73045 commit f7105c0
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions tests/testthat/test-data_tabulate.R
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ test_that("data_tabulate drop levels", {


# select helpers ------------------------------

test_that("data_tabulate regex", {
data(mtcars)
expect_identical(
Expand Down Expand Up @@ -296,6 +297,7 @@ test_that("data_tabulate exclude/include missing values", {


# cross tables ------------------------------

test_that("data_tabulate, cross tables", {
data(efc, package = "datawizard")
set.seed(123)
Expand Down Expand Up @@ -380,6 +382,7 @@ test_that("data_tabulate, cross tables, markdown", {
expect_snapshot(print_md(data_tabulate(efc$c172code, by = efc$e16sex, proportions = "full", include_na = FALSE, weights = efc$weights))) # nolint
})


# validate against table -------------------------

test_that("data_tabulate, validate against table", {
Expand Down Expand Up @@ -409,7 +412,7 @@ test_that("data_tabulate, correct 0% for proportions", {

# coercing to data frame -------------------------

test_that("data_tabulate, as.data.frame", {
test_that("data_tabulate, as.data.frame, frequency tables", {
data(mtcars)
# frequency table
x <- data_tabulate(mtcars$cyl)
Expand All @@ -426,6 +429,11 @@ test_that("data_tabulate, as.data.frame", {
expect_named(out$table[[1]], c("Variable", "Value", "N", "Raw %", "Valid %", "Cumulative %"))
expect_identical(out$table[[1]]$Variable, c("cyl", "cyl", "cyl", "cyl"))
expect_false(any(vapply(out$table[[1]][2:ncol(out$table[[1]])], is.character, logical(1))))
})


test_that("data_tabulate, as.data.frame, cross tables", {
data(mtcars)
# cross table
x <- data_tabulate(mtcars, "cyl", by = "am")
out <- as.data.frame(x)
Expand All @@ -434,6 +442,18 @@ test_that("data_tabulate, as.data.frame", {
expect_length(out$table, 1L)
expect_named(out$table[[1]], c("cyl", "0", "1", "NA"))
expect_identical(nrow(out$table[[1]]), 4L)
# cross tables
x <- data_tabulate(mtcars, c("cyl", "vs"), by = "am")
out <- as.data.frame(x)
expect_named(out, c("var", "table"))
expect_equal(vapply(out, class, character(1)), c("character", "AsIs"), ignore_attr = TRUE)
expect_length(out$table, 2L)
expect_named(out$table[[1]], c("cyl", "0", "1", "NA"))
expect_identical(nrow(out$table[[1]]), 4L)
})


test_that("data_tabulate, as.data.frame, cross tables with total N", {
# cross table, with total
x <- data_tabulate(mtcars, "cyl", by = "am")
out <- as.data.frame(x, add_total = TRUE)
Expand All @@ -443,14 +463,6 @@ test_that("data_tabulate, as.data.frame", {
expect_named(out$table[[1]], c("cyl", "0", "1", "<NA>", "Total"))
expect_identical(nrow(out$table[[1]]), 5L)
expect_identical(out$table[[1]]$cyl, c("4", "6", "8", NA, "Total"))
# cross tables
x <- data_tabulate(mtcars, c("cyl", "vs"), by = "am")
out <- as.data.frame(x)
expect_named(out, c("var", "table"))
expect_equal(vapply(out, class, character(1)), c("character", "AsIs"), ignore_attr = TRUE)
expect_length(out$table, 2L)
expect_named(out$table[[1]], c("cyl", "0", "1", "NA"))
expect_identical(nrow(out$table[[1]]), 4L)
# cross tables, with total
x <- data_tabulate(mtcars, c("cyl", "vs"), by = "am")
out <- as.data.frame(x, add_total = TRUE)
Expand Down

0 comments on commit f7105c0

Please sign in to comment.