File: test-verb-pull.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 (53 lines) | stat: -rw-r--r-- 1,344 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
test_that("default extracts last var from data frame", {
  df <- memdb_frame(x = 1:3, z = 4:6)
  expect_equal(pull(df), 4:6)
})

test_that("can extract by name, or positive/negative position", {
  x <- 1:3
  df <- memdb_frame(x = x, y = 4:6)

  expect_equal(pull(df, x), x)
  expect_equal(pull(df, 1L), x)
  expect_equal(pull(df, 1), x)
  expect_equal(pull(df, -2), x)
  expect_equal(pull(df, -2L), x)
})

test_that("extracts correct column from grouped tbl", {
  mf <- memdb_frame(id = "a", value = 42)
  gf <- mf %>% group_by(id)

  expect_equal(pull(mf, value), 42)
})

test_that("doesn't unnecessarily select", {
  mf <- memdb_frame(x = c(3, 1, 2))
  # no warning about select after arrange
  expect_warning(out <- mf %>% arrange(x) %>% pull(), NA)
  expect_equal(out, 1:3)
})

test_that("can extract named vectors", {
  x <- 1:3
  y <- letters[x]
  df <- memdb_frame(x = x, y = y)
  xn <- set_names(x, y)

  expect_equal(pull(df, x), x)
  expect_equal(pull(df, x, y), xn)
  expect_equal(pull(df, 1, 2), xn)
  expect_equal(names(pull(df, x, y)), y)
})


test_that("ungroup() produces nice error messages", {
  expect_snapshot(error = TRUE, {
    memdb_frame(x = 1) %>% pull(non_existent)
    memdb_frame(x = 1) %>% pull("non_existent")
    memdb_frame(x = 1) %>% pull(1000)

    memdb_frame(x = 1) %>% pull(x, "name_non_existent")
  })
})