File: test-verb-group_by.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 (151 lines) | stat: -rw-r--r-- 4,141 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
test_that("group_by with .add = TRUE adds groups", {
  mf <- memdb_frame(x = 1:3, y = 1:3)
  gf1 <- mf %>% group_by(x, y)
  gf2 <- mf %>% group_by(x) %>% group_by(y, .add = TRUE)

  expect_equal(group_vars(gf1), c("x", "y"))
  expect_equal(group_vars(gf2), c("x", "y"))
})

test_that("warns about add argument ", {
  mf <- memdb_frame(x = 1:3, y = 1:3)
  expect_warning(
    gf <- mf %>% group_by(x) %>% group_by(y, add = TRUE),
    "deprecated"
  )
  expect_equal(group_vars(gf), c("x", "y"))
})

test_that("errors for .drop = FALSE", {
  expect_snapshot(
    error = TRUE,
    lazy_frame(x = 1:3, y = 1:3) %>% group_by(y, .drop = FALSE)
  )
})

test_that("informative errors for missing variables", {
  expect_snapshot({
    (expect_error(lazy_frame(x = 1:3) %>% group_by(y)))
  })
})

test_that("group_by() produces nice error messages", {
  lf <- lazy_frame(x = 1)

  expect_snapshot(error = TRUE, {
    lf %>% group_by(z = non_existent + 1)
    lf %>% group_by(across(non_existent))
  })
})

test_that("collect, collapse and compute preserve grouping", {
  g <- memdb_frame(x = 1:3, y = 1:3) %>% group_by(x, y)

  expect_equal(group_vars(compute(g)), c("x", "y"))
  expect_equal(group_vars(collapse(g)), c("x", "y"))
  expect_equal(group_vars(collect(g)), c("x", "y"))
})

test_that("joins preserve grouping", {
  g <- memdb_frame(x = 1:3, y = 1:3) %>% group_by(x)

  expect_equal(group_vars(inner_join(g, g, by = c("x", "y"))), "x")
  expect_equal(group_vars(left_join(g, g, by = c("x", "y"))), "x")
  expect_equal(group_vars(semi_join(g, g, by = c("x", "y"))), "x")
  expect_equal(group_vars(anti_join(g, g, by = c("x", "y"))), "x")
})

test_that("group_by can perform mutate", {
  mf <- memdb_frame(x = 3:1, y = 1:3)
  out <- mf %>%
    group_by(z = x + y) %>%
    summarise(n = n()) %>%
    collect()

  expect_equal(out, tibble(z = 4L, n = 3L))
})

test_that("group_by handles empty dots", {
  lf <- lazy_frame(x = 1) %>% group_by(x)

  expect_equal(lf %>% group_by() %>% group_vars(), character())
  expect_equal(lf %>% group_by(!!!list()) %>% group_vars(), character())
  expect_equal(lf %>% group_by(.add = TRUE) %>% group_vars(), c("x"))
})

test_that("only adds step if necessary", {
  lf <- lazy_frame(x = 1, y = 1)
  expect_equal(lf %>% group_by(), lf)

  expect_equal(lf %>% ungroup(), lf)
  expect_equal(lf %>% ungroup(x), lf)

  lf_grouped <- lf %>% group_by(x)
  lf_grouped2 <- lf_grouped %>% group_by(x)
  expect_equal(lf_grouped, lf_grouped2)
  expect_equal(lf_grouped %>% ungroup(y), lf_grouped)

  out <- lf_grouped %>% mutate(y = y - mean(y)) %>% group_by()
  expect_equal(group_vars(out), character())
})

test_that("group_by mutate is not optimised away", {
  lf <- lazy_frame(x = 1) %>% group_by(x)

  expect_equal(
    lf %>% group_by(x = x + 1) %>% remote_query(),
    sql("SELECT `x` + 1.0 AS `x`\nFROM `df`")
  )
})


# sql_build ---------------------------------------------------------------

test_that("ungroup drops PARTITION BY", {
  out <- lazy_frame(x = 1) %>%
    group_by(x) %>%
    ungroup() %>%
    mutate(x = sum(x)) %>%
    sql_build()
  expect_equal(out$select, sql(x = 'SUM(`x`) OVER ()'))
})

# ops ---------------------------------------------------------------------

test_that("group_by overrides existing groups", {
  df <- tibble(g1 = 1, g2 = 2, x = 3) %>% tbl_lazy()

  out1 <- df %>% group_by(g1)
  expect_equal(op_grps(out1), "g1")

  out2 <- out1 %>% group_by(g2)
  expect_equal(op_grps(out2), "g2")
})

test_that("group_by increases grouping if add = TRUE", {
  df <- tibble(g1 = 1, g2 = 2, x = 3) %>% tbl_lazy()

  out <- df %>% group_by(g1) %>% group_by(g2, .add = TRUE)
  expect_equal(op_grps(out), c("g1", "g2"))
})


test_that("ungroup drops all groups", {
  out1 <- lazy_frame(g1 = 1, g2 = 2) %>%
    group_by(g1, g2) %>%
    ungroup()

  out2 <- lazy_frame(g1 = 1, g2 = 2) %>%
    group_by(g1, g2) %>%
    ungroup() %>%
    rename(g3 = g1)

  expect_equal(op_grps(out1), character())
  expect_equal(op_grps(out2), character())
})

test_that("ungroup() produces nice error messages", {
  expect_snapshot(error = TRUE, {
    lazy_frame(x = 1) %>% ungroup(non_existent)
  })
})