File: test-table-name.R

package info (click to toggle)
r-cran-dbplyr 2.5.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,644 kB
  • sloc: sh: 13; makefile: 2
file content (114 lines) | stat: -rw-r--r-- 3,096 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
test_that("table_path possess key methods", {
  expect_snapshot({
    name <- table_path(c("x", "y", "z"))
    name
  })

  expect_equal(name[c(1, 3)], table_path(c("x", "z")))
  expect_equal(name[[2]], table_path("y"))
  expect_equal(c(name[[1]], name[[2]]), table_path(c("x", "y")))
})

test_that("can check for table name", {
  foo <- function(y) check_table_path(y)
  expect_snapshot(foo(1), error = TRUE)
})

# as_table_path -----------------------------------------------------------

test_that("can coerce all user facing inputs", {
  con <- simulate_dbi()

  x_esc <- table_path("`x`")
  x_raw <- table_path("x")

  id <- table_path("x")
  expect_true(is_table_id(id))
  expect_equal(as_table_path(id, con), x_raw)

  id <- "x"
  expect_true(is_table_id(id))
  expect_equal(as_table_path(id, con), x_esc)

  id <- I("x")
  expect_true(is_table_id(id))
  expect_equal(as_table_path(id, con), x_raw)

  id <- ident("x")
  expect_true(is_table_id(id))
  expect_equal(as_table_path(id, con), x_esc)

  id <- ident_q("x")
  expect_true(is_table_id(id))
  expect_equal(as_table_path(id, con), x_raw)

  id <- DBI::Id(schema = "foo", table = "bar")
  expect_true(is_table_id(id))
  expect_equal(as_table_path(id, con), table_path("`foo`.`bar`"))

  # strip names, simulating DBI 1.2.0
  names(id@name) <- NULL
  expect_equal(as_table_path(id, con), table_path("`foo`.`bar`"))

  id <- in_schema("foo", "bar")
  expect_true(is_table_id(id))
  expect_equal(as_table_path(id, con), table_path("`foo`.`bar`"))

  id <- in_catalog("foo", "bar", "baz")
  expect_true(is_table_id(id))
  expect_equal(as_table_path(id, con), table_path("`foo`.`bar`.`baz`"))

  id <- in_catalog("foo", sql("bar"), ident_q("baz"))
  expect_true(is_table_id(id))
  expect_equal(as_table_path(id, con), table_path("`foo`.bar.baz"))
})

test_that("vectorised table_path is not a table_id", {
  expect_false(is_table_id(table_path(c("x", "y"))))
})

test_that("strips names", {
  con <- simulate_dbi()
  expect_equal(as_table_path(c(x = "x"), con), table_path("`x`"))

  id <- in_schema(c(x = "a"), "b")
  expect_equal(as_table_path(id, con), table_path("`a`.`b`"))

  id <- in_catalog("a", "b", c(x = "c"))
  expect_equal(as_table_path(id, con), table_path("`a`.`b`.`c`"))
})

test_that("as_table_path validates its inputs", {
  con <- simulate_dbi()
  expect_snapshot(error = TRUE, {
    as_table_path("x")
    as_table_path(c("x", "y"), con)
    as_table_path(1, con)
    as_table_path(I(1), con)
  })
})

test_that("as_table_path warns when using sql", {
  con <- simulate_dbi()
  expect_snapshot(as_table_path(sql("x"), con))
})

# components --------------------------------------------------------------

test_that("can parse components from path", {
  con <- simulate_dbi()

  expect_equal(
    table_path_components(table_path(c("x.y", "`x.y`.z", "`x.y`.`y.z`")), con),
    list(c("x", "y"), c("x.y", "z"), c("x.y", "y.z"))
  )
})

test_that("can extract names from path", {
  con <- simulate_dbi()

  expect_equal(
    table_path_name(table_path(c("x.y", "`x.y`.z", "x.`y.z`")), con),
    c("y", "z", "y.z")
  )
})