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("group_by errors on rename, #799", {
  df <- tidytable(x = c("a", "a", "b"),
                  y = 1:3)
  expect_error(
    group_by(df, new_x = x)
  )
})

##### ungroup() -------------------------------------------

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")
})
mtfairbanks/gdt documentation built on April 12, 2024, 6:51 p.m.