Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
markfairbanks committed Dec 21, 2023
1 parent f694f9a commit c10e163
Showing 1 changed file with 115 additions and 0 deletions.
115 changes: 115 additions & 0 deletions tests/testthat/test-pivot_wider.R
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,118 @@ test_that("correctly labels columns when `names_glue` is used, #579", {
expect_named(result2, c("v1_b", "v1_a", "v1_c", "v2_b", "v2_a", "v2_c"))
expect_equal(unname(unlist(result2)), c("b", "a", "c", "b", "a", "c"))
})

# unused -------------------------------------------------------------------

test_that("`unused_fn` can summarize unused columns (#990)", {
df <- tidytable(
id = c(1, 1, 2, 2),
unused1 = c(1, 2, 4, 3),
unused2 = c(1, 2, 4, 3),
name = c("a", "b", "a", "b"),
value = c(1, 2, 3, 4)
)

# # By name
# res <- pivot_wider(df, id_cols = id, unused_fn = list(unused1 = max))
# expect_named(res, c("id", "a", "b", "unused1"))
# expect_identical(res$unused1, c(2, 4))

# Globally
res <- pivot_wider(df, id_cols = id, unused_fn = list)
expect_named(res, c("id", "a", "b", "unused1", "unused2"))
expect_identical(res$unused1, list(c(1, 2), c(4, 3)))
expect_identical(res$unused2, list(c(1, 2), c(4, 3)))

# https://stackoverflow.com/a/73554147
df <- data.frame(A = c(1, 1, 1, 2 , 2, 2),
B = c(3, 3, 3, 6, 6, 6),
C = c(2, 3, 9, 12, 2, 6),
D = c("a1", "a2", "a3", "a1", "a2", "a3"))

res <- df %>%
pivot_wider(id_cols = A, names_from = D, values_from = C, unused_fn = mean)
expect_named(res, c("A", "a1", "a2", "a3", "B"))
expect_equal(res$B, c(3, 6))

# Works with anonymous functions
res <- df %>%
pivot_wider(id_cols = A, names_from = D, values_from = C, unused_fn = ~ mean(.x))
expect_named(res, c("A", "a1", "a2", "a3", "B"))
expect_equal(res$B, c(3, 6))
})

test_that("`unused_fn` works with anonymous functions", {
df <- tidytable(
id = c(1, 1, 2, 2),
unused = c(1, NA, 4, 3),
name = c("a", "b", "a", "b"),
value = c(1, 2, 3, 4)
)

res <- pivot_wider(df, id_cols = id, unused_fn = ~ mean(.x, na.rm = TRUE))
expect_identical(res$unused, c(1, 3.5))
})

# test_that("`unused_fn` must result in single summary values", {
# df <- tidytable(
# id = c(1, 1, 2, 2),
# unused = c(1, 2, 4, 3),
# name = c("a", "b", "a", "b"),
# value = c(1, 2, 3, 4)
# )
#
# expect_snapshot(
# (expect_error(pivot_wider(df, id_cols = id, unused_fn = identity)))
# )
# })

# test_that("`unused_fn` works with expanded key from `id_expand`", {
# df <- tidytable(
# id = factor(c(1, 1, 2, 2), levels = 1:3),
# unused = c(1, 2, 4, 3),
# name = c("a", "b", "a", "b"),
# value = c(1, 2, 3, 4)
# )
#
# res <- pivot_wider(df, id_cols = id, id_expand = TRUE, unused_fn = max)
# expect_identical(res$id, factor(1:3))
# expect_identical(res$unused, c(2, 4, NA))
#
# res <- pivot_wider(df, id_cols = id, id_expand = TRUE, unused_fn = ~ sum(is.na(.x)))
# expect_identical(res$unused, c(0L, 0L, 1L))
# })

# test_that("can't fill implicit missings in unused column with `values_fill`", {
# # (in theory this would need `unused_fill`, but it would only be used when
# # `id_expand = TRUE`, which doesn't feel that useful)
#
# df <- tidytable(
# id = factor(c(1, 1, 2, 2), levels = 1:3),
# unused = c(1, 2, 4, 3),
# name = c("a", "b", "a", "b"),
# value = c(1, 2, 3, 4)
# )
#
# res <- pivot_wider(
# data = df,
# id_cols = id,
# id_expand = TRUE,
# unused_fn = list,
# values_fill = 0
# )
#
# expect_identical(res$a, c(1, 3, 0))
# expect_identical(res$b, c(2, 4, 0))
# expect_identical(res$unused, list(c(1, 2), c(4, 3), NA_real_))
#
# res <- pivot_wider(
# data = df,
# id_cols = id,
# id_expand = TRUE,
# unused_fn = list,
# values_fill = list(unused = 0)
# )
#
# expect_identical(res$unused, list(c(1, 2), c(4, 3), NA_real_))
# })

0 comments on commit c10e163

Please sign in to comment.