]>
Commit | Line | Data |
---|---|---|
1d09f67e TL |
1 | # Licensed to the Apache Software Foundation (ASF) under one |
2 | # or more contributor license agreements. See the NOTICE file | |
3 | # distributed with this work for additional information | |
4 | # regarding copyright ownership. The ASF licenses this file | |
5 | # to you under the Apache License, Version 2.0 (the | |
6 | # "License"); you may not use this file except in compliance | |
7 | # with the License. You may obtain a copy of the License at | |
8 | # | |
9 | # http://www.apache.org/licenses/LICENSE-2.0 | |
10 | # | |
11 | # Unless required by applicable law or agreed to in writing, | |
12 | # software distributed under the License is distributed on an | |
13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
14 | # KIND, either express or implied. See the License for the | |
15 | # specific language governing permissions and limitations | |
16 | # under the License. | |
17 | ||
18 | skip_if_not_available("dataset") | |
19 | ||
20 | library(dplyr, warn.conflicts = FALSE) | |
21 | library(stringr) | |
22 | ||
23 | tbl <- example_data | |
24 | ||
25 | test_that("Empty select returns no columns", { | |
26 | compare_dplyr_binding( | |
27 | .input %>% select() %>% collect(), | |
28 | tbl, | |
29 | skip_table = "Table with 0 cols doesn't know how many rows it should have" | |
30 | ) | |
31 | }) | |
32 | test_that("Empty select still includes the group_by columns", { | |
33 | expect_message( | |
34 | compare_dplyr_binding( | |
35 | .input %>% group_by(chr) %>% select() %>% collect(), | |
36 | tbl | |
37 | ), | |
38 | "Adding missing grouping variables" | |
39 | ) | |
40 | }) | |
41 | ||
42 | test_that("select/rename", { | |
43 | compare_dplyr_binding( | |
44 | .input %>% | |
45 | select(string = chr, int) %>% | |
46 | collect(), | |
47 | tbl | |
48 | ) | |
49 | compare_dplyr_binding( | |
50 | .input %>% | |
51 | rename(string = chr) %>% | |
52 | collect(), | |
53 | tbl | |
54 | ) | |
55 | compare_dplyr_binding( | |
56 | .input %>% | |
57 | rename(strng = chr) %>% | |
58 | rename(other = strng) %>% | |
59 | collect(), | |
60 | tbl | |
61 | ) | |
62 | }) | |
63 | ||
64 | test_that("select/rename with selection helpers", { | |
65 | ||
66 | # TODO: add some passing tests here | |
67 | ||
68 | expect_error( | |
69 | compare_dplyr_binding( | |
70 | .input %>% | |
71 | select(where(is.numeric)) %>% | |
72 | collect(), | |
73 | tbl | |
74 | ), | |
75 | "Unsupported selection helper" | |
76 | ) | |
77 | }) | |
78 | ||
79 | test_that("filtering with rename", { | |
80 | compare_dplyr_binding( | |
81 | .input %>% | |
82 | filter(chr == "b") %>% | |
83 | select(string = chr, int) %>% | |
84 | collect(), | |
85 | tbl | |
86 | ) | |
87 | compare_dplyr_binding( | |
88 | .input %>% | |
89 | select(string = chr, int) %>% | |
90 | filter(string == "b") %>% | |
91 | collect(), | |
92 | tbl | |
93 | ) | |
94 | }) | |
95 | ||
96 | test_that("relocate", { | |
97 | df <- tibble(a = 1, b = 1, c = 1, d = "a", e = "a", f = "a") | |
98 | compare_dplyr_binding( | |
99 | .input %>% relocate(f) %>% collect(), | |
100 | df, | |
101 | ) | |
102 | compare_dplyr_binding( | |
103 | .input %>% relocate(a, .after = c) %>% collect(), | |
104 | df, | |
105 | ) | |
106 | compare_dplyr_binding( | |
107 | .input %>% relocate(f, .before = b) %>% collect(), | |
108 | df, | |
109 | ) | |
110 | compare_dplyr_binding( | |
111 | .input %>% relocate(a, .after = last_col()) %>% collect(), | |
112 | df, | |
113 | ) | |
114 | compare_dplyr_binding( | |
115 | .input %>% relocate(ff = f) %>% collect(), | |
116 | df, | |
117 | ) | |
118 | }) | |
119 | ||
120 | test_that("relocate with selection helpers", { | |
121 | df <- tibble(a = 1, b = 1, c = 1, d = "a", e = "a", f = "a") | |
122 | compare_dplyr_binding( | |
123 | .input %>% relocate(any_of(c("a", "e", "i", "o", "u"))) %>% collect(), | |
124 | df | |
125 | ) | |
126 | compare_dplyr_binding( | |
127 | .input %>% relocate(where(is.character)) %>% collect(), | |
128 | df | |
129 | ) | |
130 | compare_dplyr_binding( | |
131 | .input %>% relocate(a, b, c, .after = where(is.character)) %>% collect(), | |
132 | df | |
133 | ) | |
134 | compare_dplyr_binding( | |
135 | .input %>% relocate(d, e, f, .before = where(is.numeric)) %>% collect(), | |
136 | df | |
137 | ) | |
138 | # works after other dplyr verbs | |
139 | compare_dplyr_binding( | |
140 | .input %>% | |
141 | mutate(c = as.character(c)) %>% | |
142 | relocate(d, e, f, .after = where(is.numeric)) %>% | |
143 | collect(), | |
144 | df | |
145 | ) | |
146 | }) |