File: test-verb-expand.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 (133 lines) | stat: -rw-r--r-- 3,406 bytes parent folder | download | duplicates (2)
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
test_that("expand completes all values", {
  expect_equal(
    memdb_frame(x = 1:2, y = 1:2) %>% tidyr::expand(x, y) %>% collect(),
    tibble(x = c(1, 1, 2, 2), y = c(1, 2, 1, 2))
  )

  expect_snapshot(lazy_frame(x = 1, y = 1) %>% tidyr::expand(x, y))
})

test_that("nesting doesn't expand values", {
  df <- tibble(x = 1:2, y = 1:2)
  expect_equal(
    tidyr::expand(memdb_frame(!!!df), nesting(x, y)) %>%
      collect(),
    df
  )

  df_lazy <- lazy_frame(!!!df)
  expect_snapshot(df_lazy %>% tidyr::expand(nesting(x, y)))
})

test_that("expand accepts expressions", {
  df <- lazy_frame(x = 1)
  expect_snapshot(tidyr::expand(df, round(x / 2)))
  expect_snapshot(tidyr::expand(df, nesting(x_half = round(x / 2), x1 = x + 1)))
})

test_that("works with tidyr::nesting", {
  df_lazy <- lazy_frame(x = 1:2, y = 1:2)
  expect_snapshot(df_lazy %>% tidyr::expand(tidyr::nesting(x, y)))
})

test_that("expand respects groups", {
  df <- tibble(
    a = c(1L, 1L, 2L),
    b = c(1L, 2L, 1L),
    c = c("b", "a", "a")
  )
  expect_equal(
    memdb_frame(!!!df) %>% group_by(a) %>% tidyr::expand(b, c) %>% collect(),
    tibble(
      a = c(1, 1, 1, 1, 2),
      b = c(1, 1, 2, 2, 1),
      c = c("b", "a", "b", "a", "a")
    ) %>%
      group_by(a)
  )

  df_lazy <- lazy_frame(!!!df)
  expect_snapshot(df_lazy %>% group_by(a) %>% tidyr::expand(b, c))
})

test_that("NULL inputs", {
  expect_snapshot(tidyr::expand(lazy_frame(x = 1), x, y = NULL))
})

test_that("expand() errors when expected", {
  expect_snapshot(error = TRUE, tidyr::expand(memdb_frame(x = 1)))
  expect_snapshot(error = TRUE, tidyr::expand(memdb_frame(x = 1), x = NULL))
})

test_that("nesting() respects .name_repair", {
  expect_snapshot(error = TRUE, 
    tidyr::expand(
      memdb_frame(x = 1, y = 1),
      nesting(x, x = x + 1)
    )
  )

  vars <- suppressMessages(
    tidyr::expand(
      memdb_frame(x = 1, y = 1),
      nesting(x, x = x + 1, .name_repair = "unique")
    ) %>%
      op_vars()
  )

  expect_equal(vars, c("x...1", "x...2"))
})

test_that("expand respect .name_repair", {
  vars <- suppressMessages(
    memdb_frame(x = integer(), z = integer()) %>%
      tidyr::expand(x, z = x, nesting(x), nesting(z), .name_repair = "unique") %>%
      op_vars()
  )

  expect_equal(vars, c("x...1", "z...2", "x...3", "z...4"))
})

# replace_na --------------------------------------------------------------

test_that("replace_na replaces missing values", {
  expect_equal(
    memdb_frame(x = c(1, NA), y = c(NA, "b")) %>%
      tidyr::replace_na(list(x = 0, y = "unknown")) %>%
      collect(),
    tibble(
      x = c(1, 0),
      y = c("unknown", "b")
    )
  )

  expect_snapshot(
    lazy_frame(x = 1, y = "a") %>% tidyr::replace_na(list(x = 0, y = "unknown"))
  )
})

test_that("replace_na ignores missing columns", {
  expect_snapshot(lazy_frame(x = 1) %>% tidyr::replace_na(list(not_there = 0)))
})

# complete ----------------------------------------------------------------

test_that("complete completes missing combinations", {
  df <- tibble(
    x = 1:2,
    y = 1:2,
    z = c("a", "b")
  )

  expect_equal(
    memdb_frame(!!!df) %>% tidyr::complete(x, y, fill = list(z = "c")) %>% collect(),
    tibble(
      x = c(1, 1, 2, 2),
      y = c(1, 2, 1, 2),
      z = c("a", "c", "c", "b")
    )
  )

  df_lazy <- lazy_frame(!!!df)
  expect_snapshot(df_lazy %>% tidyr::complete(x, y, fill = list(z = "c")))
})