File: test-verb-arrange.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 (137 lines) | stat: -rw-r--r-- 3,707 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
134
135
136
137
test_that("two arranges equivalent to one", {
  skip("not clear whether test makes sense")
  mf <- memdb_frame(x = c(2, 2, 1), y = c(1, -1, 1))

  mf1 <- mf %>% arrange(x, y)
  mf2 <- mf %>% arrange(y) %>% arrange(x)

  expect_warning(compare_tbl(mf1, mf2))
})

# sql_render --------------------------------------------------------------

test_that("quoting for rendering ordered grouped table", {
  db <- copy_to_test("sqlite", tibble(x = 1, y = 2), name = "test-verb-arrange")
  out <- db %>% group_by(x) %>% arrange(y) %>% ungroup()
  expect_snapshot(sql_render(out))
  expect_equal(collect(out), tibble(x = 1, y = 2))
})

test_that("arrange renders correctly (#373)", {
  expect_snapshot({
    "# arrange renders correctly"
    lf <- lazy_frame(a = 1:3, b = 3:1)

    "basic"
    lf %>% arrange(a)

    "double arrange"
    lf %>% arrange(a) %>% arrange(b)

    "remove ordered by"
    lf %>% arrange(a) %>% select(-a)
    lf %>% arrange(a) %>% select(-a) %>% arrange(b)

    "un-arrange"
    lf %>% arrange(a) %>% arrange()
    lf %>% arrange(a) %>% select(-a) %>% arrange()

    "use order"
    lf %>% arrange(a) %>% select(-a) %>% mutate(c = lag(b))
  })
})

test_that("arrange renders correctly for single-table verbs (#373)", {
  expect_snapshot({
    lf <- lazy_frame(a = 1:3, b = 3:1)

    "head"
    lf %>% head(1) %>% arrange(a)
    lf %>% arrange(a) %>% head(1)
    lf %>% arrange(a) %>% head(1) %>% arrange(b)

    "mutate"
    lf %>% mutate(a = b) %>% arrange(a)

    "complex mutate"
    lf %>% arrange(a) %>% mutate(a = b) %>% arrange(a)
    lf %>% arrange(a) %>% mutate(a = 1) %>% arrange(b)
    lf %>% mutate(a = -a) %>% arrange(a) %>% mutate(a = -a)
  })
})

test_that("can combine arrange with dual table verbs", {
  expect_snapshot({
    lf <- lazy_frame(a = 1:3, b = 3:1)
    rf <- lazy_frame(a = 1:3, c = 4:6)

    "warn if arrange before join"
    lf %>% arrange(a) %>% left_join(rf)
    lf %>% arrange(a) %>% semi_join(rf)
    lf %>% arrange(a) %>% union(rf)

    "can arrange after join"
    lf %>% left_join(rf) %>% arrange(a)
    lf %>% semi_join(rf) %>% arrange(a)
    lf %>% union(rf) %>% arrange(a)
  })
})

test_that("only add step if necessary", {
  lf <- lazy_frame(x = 1:3, y = 1:3)
  expect_equal(lf %>% arrange(), lf)
})

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

test_that("arrange generates order_by", {
  out <- lazy_frame(x = 1, y = 1) %>%
    arrange(x) %>%
    sql_build()

  expect_equal(out$order_by, sql('`x`'))
})

test_that("arrange converts desc", {
  out <- lazy_frame(x = 1, y = 1) %>%
    arrange(desc(x)) %>%
    sql_build()

  expect_equal(out$order_by, sql('`x` DESC'))
})

test_that("grouped arrange doesn't order by groups", {
  out <- lazy_frame(x = 1, y = 1) %>%
    group_by(x) %>%
    arrange(y) %>%
    sql_build()

  expect_equal(out$order_by, sql('`y`'))
})

test_that("grouped arrange order by groups when .by_group  is set to TRUE", {
  lf <- lazy_frame(x = 1, y = 1, con = simulate_dbi())
  out <- lf %>%
    group_by(x) %>%
    arrange(y, .by_group = TRUE) %>%
    sql_build()
  expect_equal(out$order_by, sql(c('`x`','`y`')))
})

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

test_that("arranges captures DESC", {
  out <- lazy_frame(x = 1:3, y = 3:1) %>% arrange(desc(x))

  sort <- lapply(op_sort(out), get_expr)
  expect_equal(sort, list(quote(desc(x))))
})

test_that("multiple arranges combine", {
  skip("not clear whether test makes sense")
  out <- lazy_frame(x = 1:3, y = 3:1) %>% arrange(x) %>% arrange(y)
  out <- arrange(arrange(lazy_frame(x = 1:3, y = 3:1), x), y)

  sort <- lapply(op_sort(out), get_expr)
  expect_equal(sort, list(quote(x), quote(y)))
})