tests/testthat/test-group_by.R

test_that("group_by works", {
  df <- tidytable(x = c("a", "a", "b"),
                  y = c("a", "a", "b"),
                  z = 1:3)
  res <- group_by(df, x, y)
  expect_equal(group_vars(res), c("x", "y"))
  expect_true(is_grouped_df(res))
})

test_that("group_by works with .add", {
  df <- tidytable(x = c("a", "a", "b"),
                  y = c("a", "a", "b"),
                  z = 1:3)
  res <- df %>%
    group_by(x) %>%
    group_by(y, .add = TRUE)
  expect_equal(group_vars(res), c("x", "y"))
  expect_true(is_grouped_df(res))
})

test_that("works with dot", {
  df <- tidytable(x = c("a", "a", "b"),
                  y = c("a", "a", "b"),
                  z = 1:3)
  res <- group_by.(df, x, y) %>%
    suppressWarnings()
  expect_equal(suppressWarnings(group_vars.(res)), c("x", "y"))
  expect_true(suppressWarnings(is_grouped_df.(res)))
})

test_that("works on rowwise_tt", {
  df <- rowwise(tidytable(x = 1:3, y = 1:3))
  res <- ungroup(df)
  expect_false(inherits(res, "rowwise_tt"))
})

test_that("ungroup works", {
  df <- tidytable(x = 1:3, y = 1:3) %>%
    group_by(x, y)
  res <- ungroup(df)
  expect_false(is_grouped_df(res))
  expect_equal(group_vars(res), NULL)
  res <- ungroup(df, y)
  expect_equal(group_vars(res), "x")
})

Try the tidytable package in your browser

Any scripts or data that you put into this service are public.

tidytable documentation built on Oct. 5, 2023, 5:07 p.m.