File: test-backend-.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,885 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("base_no_win inclues all aggregates and window funcitons", {
  # All aggregates must be included in window functions
  expect_equal(setdiff(names(base_agg), names(base_win)), character())

  # All window functions all need to be in no_in
  expect_equal(setdiff(names(base_win), names(base_no_win)), character())
})

# mathematics --------------------------------------------------------

test_that("basic arithmetic is correct", {
  expect_equal(translate_sql(1 + 2), sql("1.0 + 2.0"))
  expect_equal(translate_sql(2 * 4), sql("2.0 * 4.0"))
  expect_equal(translate_sql(5 ^ 2), sql("POWER(5.0, 2.0)"))
  expect_equal(translate_sql(100L %% 3L), sql("100 % 3"))

  expect_error(translate_sql(100L %/% 3L), "not available")
})

test_that("small numbers aren't converted to 0", {
  expect_equal(translate_sql(1e-9), sql("1e-09"))
})

test_that("unary plus works with numbers", {
  expect_equal(translate_sql(+10L), sql("10"))
  expect_equal(translate_sql(x == +10), sql('`x` = 10.0'))
  expect_equal(translate_sql(x %in% c(+1L, 0L)), sql('`x` IN (1, 0)'))
})

test_that("unary plus works for non-numeric expressions", {
  expect_equal(translate_sql(+(1L + 2L)), sql("(1 + 2)"))
  expect_equal(translate_sql(mean(x, na.rm = TRUE), window = FALSE), sql('AVG(`x`)'))
})

test_that("unary minus flips sign of number", {
  expect_equal(translate_sql(-10L), sql("-10"))
  expect_equal(translate_sql(x == -10), sql('`x` = -10.0'))
  expect_equal(translate_sql(x %in% c(-1L, 0L)), sql('`x` IN (-1, 0)'))
})

test_that("unary minus wraps non-numeric expressions", {
  expect_equal(translate_sql(-(1L + 2L)), sql("-(1 + 2)"))
  expect_equal(translate_sql(-mean(x, na.rm = TRUE), window = FALSE), sql('-AVG(`x`)'))
})

test_that("binary minus subtracts", {
  expect_equal(translate_sql(1L - 10L), sql("1 - 10"))
})

test_that("log base comes first", {
  expect_equal(translate_sql(log(x, 10)), sql('LOG(10.0, `x`)'))
})

test_that("log becomes ln", {
  expect_equal(translate_sql(log(x)), sql('LN(`x`)'))
})

test_that("can translate subsetting", {
  expect_equal(translate_sql(a$b), sql("`a`.`b`"))
  expect_equal(translate_sql(a[["b"]]), sql("`a`.`b`"))

  expect_equal(translate_sql(a[["b"]][[1]]), sql('`a`.`b`[1]'))
})


# strings -----------------------------------------------------------------

test_that("can translate case insensitive like", {
  translate_sql(str_like(x, "abc"))
  expect_snapshot(
    translate_sql(str_like(x, "abc", ignore_case = FALSE)),
    error = TRUE
  )
})



# aggregates --------------------------------------------------------------

test_that("all and any translated correctly", {
  db <- memdb_frame(g = c(1, 1, 2, 2, 3, 3), x = c(0, 0, 0, 1, 1, 1))

  sum_all_g <- db %>%
    group_by(g) %>%
    summarise(all = all(x == 1, na.rm = TRUE)) %>%
    filter(all) %>%
    pull(g)
  expect_equal(sum_all_g, 3)

  sum_any_g <- db %>%
    group_by(g) %>%
    summarise(any = any(x == 1, na.rm = TRUE)) %>%
    filter(any) %>%
    pull(g)
  expect_equal(sum_any_g, c(2, 3))

  win_all_g <- db %>%
    group_by(g) %>%
    filter(all(x == 1, na.rm = TRUE)) %>%
    pull(g)
  expect_equal(win_all_g, c(3, 3))

  win_any_g <- db %>%
    group_by(g) %>%
    filter(any(x == 1, na.rm = TRUE)) %>%
    pull(g)
  expect_equal(win_any_g, c(2, 2, 3, 3))
})

# binary/bitwise ---------------------------------------------------------------

test_that("bitwise operations", {
  expect_equal(translate_sql(bitwNot(x)),        sql("~(`x`)"))
  expect_equal(translate_sql(bitwAnd(x, 128L)),  sql("`x` & 128"))
  expect_equal(translate_sql(bitwOr(x, 128L)),   sql("`x` | 128"))
  expect_equal(translate_sql(bitwXor(x, 128L)),  sql("`x` ^ 128"))
  expect_equal(translate_sql(bitwShiftL(x, 2L)), sql("`x` << 2"))
  expect_equal(translate_sql(bitwShiftR(x, 2L)), sql("`x` >> 2"))
})

test_that("default raw escapes translated correctly", {
  mf <- lazy_frame(x = "abc", con = simulate_sqlite())

  a <- blob::as_blob("abc")
  b <- blob::as_blob(as.raw(c(0x01, 0x02)))
  L <- c(a, b)

  expect_snapshot(mf %>% filter(x == a))
  expect_snapshot(mf %>% filter(x %in% L))

  qry <- mf %>% filter(x %in% !!L)
  expect_snapshot(qry)
})

# DDL ---------------------------------------------------------------------

test_that("DDL operations generate expected SQL", {
  con <- simulate_dbi()

  expect_snapshot(sql_table_analyze(con, in_schema("schema", "tbl")))
  expect_snapshot(sql_query_explain(con, sql("SELECT * FROM foo")))

  expect_snapshot(sql_query_wrap(con, ident("table")))
  expect_snapshot(sql_query_wrap(con, in_schema("schema", "tbl")))
  expect_snapshot(sql_query_wrap(con, sql("SELECT * FROM foo")))

  expect_snapshot(sql_table_index(con, in_schema("schema", "tbl"), c("a", "b")))
  expect_snapshot(sql_table_index(con, in_schema("schema", "tbl"), "c", unique = TRUE))

  expect_snapshot(sql_query_save(con, sql("SELECT * FROM foo"), in_schema("temp", "tbl")))
})