File: test-verb-count.R

package info (click to toggle)
r-cran-dbplyr 2.3.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,376 kB
  • sloc: sh: 13; makefile: 2
file content (47 lines) | stat: -rw-r--r-- 1,571 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
test_that("generates expected SQL for common situations", {
  db <- lazy_frame(g = 1, x = 2)

  expect_snapshot(db %>% count(g))
  expect_snapshot(db %>% count(g, wt = x))
  expect_snapshot(db %>% count(g, sort = TRUE))

  expect_snapshot(db %>% add_count(g, sort = TRUE))
  expect_snapshot(db %>% group_by(g) %>% add_count())
})

test_that("preserves group of input", {
  db <- lazy_frame(g = 1, x = 2)
  expect_equal(db %>% count(g) %>% group_vars(), character())
  expect_equal(db %>% group_by(g) %>% count() %>% group_vars(), "g")
  expect_equal(db %>% group_by(g, x) %>% count() %>% group_vars(), c("g", "x"))

  expect_equal(db %>% group_by(g, x) %>% add_count(g) %>% group_vars(), c("g", "x"))
  expect_equal(db %>% add_count(g) %>% group_vars(), character())
})

test_that("informs if n column already present, unless overridden", {
  df1 <- lazy_frame(n = c(1, 1, 2, 2, 2))
  expect_message(out <- count(df1, n), "already present")
  expect_named(out, c("n", "nn"))

  # not a good idea, but supported
  expect_message(out <- count(df1, n, name = "n"), NA)
  expect_named(out, "n")

  expect_message(out <- count(df1, n, name = "nn"), NA)
  expect_named(out, c("n", "nn"))

  df2 <- lazy_frame(n = c(1, 1, 2, 2, 2), nn = 1:5)
  expect_message(out <- count(df2, n), "already present")
  expect_named(out, c("n", "nn"))

  expect_message(out <- count(df2, n, nn), "already present")
  expect_named(out, c("n", "nn", "nnn"))
})

test_that(".drop is not supported", {
  expect_snapshot(error = TRUE, {
    lazy_frame(g = 1) %>%
      add_count(.drop = TRUE)
  })
})