-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest-miscellaneous.R
More file actions
338 lines (280 loc) · 8.13 KB
/
test-miscellaneous.R
File metadata and controls
338 lines (280 loc) · 8.13 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
test_that("sort order is as expected (sorted by keys)", {
dsin1 <- data.frame(
G1 = rep(c(1, 10, 2, 101), 3),
G2 = as.Date(c(
"2015-10-12", "2015-10-12", "2015-10-12", "2015-10-12",
"2015-11-12", "2015-11-12", "2015-11-12", "2015-11-12",
"2015-12-12", "2015-12-12", "2015-12-12", "2015-12-12"
)),
var = c(9, 2, 5, 2, 3, 10, 12, 42, 1, 2, 8, 54)
)
dsin2 <- dsin1
dsin2$var[c(1, 2, 3)] <- c(97, 98, 99)
x <- diffdf(dsin1, dsin2, keys = c("G1", "G2"), suppress_warnings = TRUE)
x2 <- diffdf_issuerows(dsin2, x)
expect_true(all(x$VarDiff_var$G1 == c(1, 2, 10)))
expect_true(all(x$VarDiff_var$COMPARE == c(97, 99, 98)))
expect_true(all(x2$G1 == c(1, 2, 10)))
expect_true(all(x2$var == c(97, 99, 98)))
})
test_that("as_ascii_table() can handle white space and newlines", {
x1 <- data.frame(
x = c(
"no-quotes",
"yes have\x0D\nquotes",
"yes it should have quotes but its long"
)
)
gold <- c(
" =====================================",
" x ",
" -------------------------------------",
" no-quotes ",
' "yes have<cr><nl>quotes" ',
' "yes it should have quotes but ..." ',
" -------------------------------------"
)
expect_equal(
(as_ascii_table(x1) |> strsplit("\n"))[[1]],
gold
)
#
# Show that converting to factor makes no difference
#
x2 <- x1
x2$x <- factor(x2$x)
expect_equal(
(as_ascii_table(x2) |> strsplit("\n"))[[1]],
gold
)
})
test_that("can handle null datasets", {
expect_warning(
{res <- diffdf(data.frame(), iris)},
regexp = "rows in COMPARE that are not in BASE.*columns in COMPARE that are not in BASE"
)
expect_snapshot(res)
expect_warning(
{res <- diffdf(iris, data.frame())},
regexp = "rows in BASE that are not in COMPARE.*columns in BASE that are not in COMPARE"
)
expect_snapshot(res)
expect_output(
print(diffdf(data.frame(), data.frame())),
"No issues were found!"
)
expect_output(
print(diffdf(iris, iris)),
"No issues were found!"
)
x1 <- tibble(
x = c(1, 2, 3),
y = c(4, 5, 6)
)
expect_warning(
{res <- diffdf(x1, x1[FALSE, "x"])},
regexp = "rows in BASE that are not in COMPARE.*columns in BASE that are not in COMPARE"
)
expect_snapshot(res)
expect_warning(
{res <- diffdf(x1[FALSE, "x"], x1)},
regexp = "rows in COMPARE that are not in BASE.*columns in COMPARE that are not in BASE"
)
expect_snapshot(res)
})
test_that("can handle non-overlapping keys", {
expect_warning(
{
x1 <- data.frame(ID = c("A", "C"), val = c(1, 2))
x2 <- data.frame(ID = c("B", "C"), val = c(3, 2))
res <- diffdf(x1, x2, "ID")
},
regexp = "rows in BASE that are not in COMPARE.*rows in COMPARE that are not in BASE"
)
expect_snapshot(res)
})
test_that("Can handle missing keys", {
d1 <- tibble(
v1 = 1,
v2 = 1,
)
d2 <- tibble(
v1 = 1,
v2 = 1,
v3 = 1,
v4 = 1,
v5 = 1
)
expect_error(
diffdf(d1, d2, c("v1", "v2", "v3", "v4")),
regexp = "not available in BASE.* v3.* v4"
)
expect_error(
diffdf(d2, d1, c("v1", "v2", "v3", "v4")),
regexp = "not available in COMPARE.* v3.* v4"
)
})
test_that("Format Char works on standard data types", {
expect_equal(
as_fmt_char(c(1.12345678, 2, 3, 4)),
c("1.123457", "2.000000", "3.000000", "4.000000")
)
expect_equal(
as_fmt_char(c("A", "awdaiowjdidddddoawj awijdwwwwwoiawjd", "B", "C")),
c("A", "\"awdaiowjdidddddoawj awijdwwwww...\"", "B", "C")
)
expect_equal(
as_fmt_char(factor(c("A", "B", "C", "adiawi iawjdoi aiwjdioawj iawjdoa"))),
c("A", "B", "C", "\"adiawi iawjdoi aiwjdioawj iaw...\"")
)
expect_equal(
as_fmt_char(c(TRUE, NA, FALSE)),
c("TRUE", "<NA>", "FALSE")
)
expect_equal(
as_fmt_char(lubridate::ymd("2020-01-01", "1888-01-29 UTC")),
c("2020-01-01", "1888-01-29")
)
expect_equal(
as_fmt_char(lubridate::ymd_hms(
c(
"2023-01-01 14:12:12",
"2023-07-05 15:45:30"
),
tz = "Europe/London"
)),
c("2023-01-01 14:12:12 GMT", "2023-07-05 15:45:30 BST")
)
x <- c(1, 2, 3, 4)
class(x) <- "some random class"
expect_equal(
as_fmt_char(x),
c("1", "2", "3", "4")
)
# Test that as_fmt_char doesn't enter inf loop if
# as.character does't return a character
x <- 1
class(x) <- c("myclass", "myclass2")
testthat::with_mocked_bindings(
expect_error(
as_fmt_char(x),
regexp = "`'myclass', 'myclass2'`"
),
as_character = function(x) x,
)
})
test_that("ascii_table can handle all standard datatypes", {
expect_snapshot(as_ascii_table(TDAT) |> cat())
})
test_that("datetimes compare as expected", {
# Same character values but different underlying numerics
d1 <- tibble(
id = c(1, 2),
dt1 = lubridate::ymd_hms(
"2024-01-10 01-02-03",
"2024-01-24 14-12-49",
tz = "EST"
)
)
d2 <- tibble(
id = c(1, 2),
dt1 = lubridate::ymd_hms(
"2024-01-10 01-02-03",
"2024-01-24 14-12-49",
tz = "CET"
)
)
expect_warning(
res <- diffdf(d1, d2, "id"),
regexp = "differing attributes.*Not all Values Compared Equal"
)
expect_snapshot(
print(res)
)
# Same underlying numerics but different character values
d1 <- tibble(
id = c(1, 2),
dt1 = lubridate::ymd_hms(
"2024-01-10 01-02-03",
"2024-01-24 14-12-49",
tz = "EST"
)
)
d2 <- d1
d2$dt1 <- lubridate::with_tz(d2$dt1, tzone = "CET")
expect_warning(
res <- diffdf(d1, d2, "id"),
regexp = "differing attributes[ !]*$"
)
expect_snapshot(
print(res)
)
})
test_that("`as_ascii_table() can handle missing datetimes (#132)", {
d1 <- tibble(
id = c(1, 2, 3),
dt1 = lubridate::ymd_hms(
"2024-01-10 01-02-03",
"2024-01-24 14-12-49",
"1821-02-01 01-01-01"
)
)
d2 <- tibble(
id = c(1, NA, 3),
dt1 = lubridate::ymd_hms(
"2024-01-10 01-02-03",
NA,
"1821-02-01 01-01-01"
)
)
expect_snapshot(diffdf(d1, d2, suppress_warnings = TRUE))
d3 <- tibble(
id = c(1, 2, 3),
dt1 = lubridate::ymd_hms(NA, NA, NA)
)
expect_snapshot(diffdf(d1, d3, suppress_warnings = TRUE))
set.seed(2211)
offset <- 2000000000
n <- 50
d5 <- tibble(
id = seq_len(n),
dt1 = lubridate::ymd_hms("2000-01-01 01-01-01") + round(runif(n, -offset, offset))
)
d6 <- d5
d6[seq(1, n, by = 2), "dt1"] <- NA
expect_snapshot(diffdf(d5, d6, suppress_warnings = TRUE))
})
test_that("`as_ascii_table() can handle missing dates (#132)", {
d1 <- tibble(
id = c(1, 2, 3),
dt1 = lubridate::ymd(
"2024-01-10",
"2024-01-24",
"1821-02-01"
)
)
d2 <- tibble(
id = c(1, NA, 3),
dt1 = lubridate::ymd(
"2024-01-10",
NA,
"1821-02-01"
)
)
expect_snapshot(diffdf(d1, d2, suppress_warnings = TRUE))
d3 <- tibble(
id = c(1, 2, 3),
dt1 = lubridate::ymd(NA, NA, NA)
)
expect_snapshot(diffdf(d1, d3, suppress_warnings = TRUE))
set.seed(2211)
offset <- 200000
n <- 50
d5 <- tibble(
id = seq_len(n),
dt1 = lubridate::ymd("2000-01-01") + round(runif(n, -offset, offset))
)
d6 <- d5
d6[seq(1, n, by = 2), "dt1"] <- NA
expect_snapshot(diffdf(d5, d6, suppress_warnings = TRUE))
})