Skip to content

Commit f25f1ef

Browse files
krlmlraviator-bot
authored andcommitted
write_table_test_roundtrip() and append_table_test_roundtrip()
1 parent c927e61 commit f25f1ef

File tree

2 files changed

+93
-71
lines changed

2 files changed

+93
-71
lines changed

R/spec-sql-append-table.R

Lines changed: 58 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ spec_sql_append_table <- list(
8888
select = "unique", from = "join", where = "order",
8989
stringsAsFactors = FALSE
9090
)
91-
test_table_roundtrip(use_append = TRUE, con, tbl_in, name = "exists")
91+
append_table_test_roundtrip(con, tbl_in, name = "exists")
9292
},
9393

9494
append_roundtrip_quotes = function(ctx, con, table_name) {
@@ -104,7 +104,7 @@ spec_sql_append_table <- list(
104104
)
105105

106106
names(tbl_in) <- letters[seq_along(tbl_in)]
107-
test_table_roundtrip(con, tbl_in, use_append = TRUE)
107+
append_table_test_roundtrip(con, tbl_in)
108108
},
109109

110110
append_roundtrip_quotes_table_names = function(ctx, con) {
@@ -125,7 +125,7 @@ spec_sql_append_table <- list(
125125
tbl_in <- trivial_df()
126126

127127
for (table_name in table_names) {
128-
test_table_roundtrip_one(con, tbl_in, use_append = TRUE, .add_na = FALSE)
128+
append_table_test_roundtrip_one(con, tbl_in, .add_na = FALSE)
129129
}
130130
},
131131

@@ -145,7 +145,7 @@ spec_sql_append_table <- list(
145145

146146
tbl_in <- trivial_df(length(column_names), column_names)
147147

148-
test_table_roundtrip_one(con, tbl_in, use_append = TRUE, .add_na = FALSE)
148+
append_table_test_roundtrip_one(con, tbl_in, .add_na = FALSE)
149149
},
150150

151151
#'
@@ -154,13 +154,13 @@ spec_sql_append_table <- list(
154154
#' and be read identically with [dbReadTable()]:
155155
#' - integer
156156
tbl_in <- data.frame(a = c(1:5))
157-
test_table_roundtrip(use_append = TRUE, con, tbl_in)
157+
append_table_test_roundtrip(con, tbl_in)
158158
},
159159

160160
append_roundtrip_numeric = function(con) {
161161
#' - numeric
162162
tbl_in <- data.frame(a = c(seq(1, 3, by = 0.5)))
163-
test_table_roundtrip(use_append = TRUE, con, tbl_in)
163+
append_table_test_roundtrip(con, tbl_in)
164164
#' (the behavior for `Inf` and `NaN` is not specified)
165165
},
166166

@@ -169,14 +169,13 @@ spec_sql_append_table <- list(
169169
tbl_in <- data.frame(a = c(TRUE, FALSE, NA))
170170
tbl_exp <- tbl_in
171171
tbl_exp$a <- ctx$tweaks$logical_return(tbl_exp$a)
172-
test_table_roundtrip(use_append = TRUE, con, tbl_in, tbl_exp)
172+
append_table_test_roundtrip(con, tbl_in, tbl_exp)
173173
},
174174

175175
append_roundtrip_null = function(con) {
176176
#' - `NA` as NULL
177177
tbl_in <- data.frame(a = NA)
178-
test_table_roundtrip(
179-
use_append = TRUE,
178+
append_table_test_roundtrip(
180179
con, tbl_in,
181180
transform = function(tbl_out) {
182181
tbl_out$a <- as.logical(tbl_out$a) # Plain NA is of type logical
@@ -188,8 +187,7 @@ spec_sql_append_table <- list(
188187
#' - 64-bit values (using `"bigint"` as field type); the result can be
189188
append_roundtrip_64_bit_numeric = function(ctx, con) {
190189
tbl_in <- data.frame(a = c(-1e14, 1e15))
191-
test_table_roundtrip(
192-
use_append = TRUE,
190+
append_table_test_roundtrip(
193191
con, tbl_in,
194192
transform = function(tbl_out) {
195193
#' - converted to a numeric, which may lose precision,
@@ -204,8 +202,7 @@ spec_sql_append_table <- list(
204202
tbl_in <- data.frame(a = c(-1e14, 1e15))
205203
tbl_exp <- tbl_in
206204
tbl_exp$a <- format(tbl_exp$a, scientific = FALSE)
207-
test_table_roundtrip(
208-
use_append = TRUE,
205+
append_table_test_roundtrip(
209206
con, tbl_in, tbl_exp,
210207
transform = function(tbl_out) {
211208
#' - converted a character vector, which gives the full decimal
@@ -222,7 +219,7 @@ spec_sql_append_table <- list(
222219
dbWriteTable(con, table_name, tbl_in, field.types = c(a = "BIGINT"))
223220
tbl_out <- dbReadTable(con, table_name)
224221
#' - written to another table and read again unchanged
225-
test_table_roundtrip(use_append = TRUE, con, tbl_out, tbl_expected = tbl_out)
222+
append_table_test_roundtrip(con, tbl_out, tbl_expected = tbl_out)
226223
},
227224

228225
append_roundtrip_character = function(con) {
@@ -232,7 +229,7 @@ spec_sql_append_table <- list(
232229
a = get_texts(),
233230
stringsAsFactors = FALSE
234231
)
235-
test_table_roundtrip(use_append = TRUE, con, tbl_in)
232+
append_table_test_roundtrip(con, tbl_in)
236233
},
237234

238235
append_roundtrip_character_native = function(con) {
@@ -241,7 +238,7 @@ spec_sql_append_table <- list(
241238
a = c(enc2native(get_texts())),
242239
stringsAsFactors = FALSE
243240
)
244-
test_table_roundtrip(use_append = TRUE, con, tbl_in)
241+
append_table_test_roundtrip(con, tbl_in)
245242
},
246243

247244
append_roundtrip_character_empty = function(con) {
@@ -250,7 +247,7 @@ spec_sql_append_table <- list(
250247
a = c("", "a"),
251248
stringsAsFactors = FALSE
252249
)
253-
test_table_roundtrip(use_append = TRUE, con, tbl_in)
250+
append_table_test_roundtrip(con, tbl_in)
254251
},
255252

256253
append_roundtrip_character_empty_after = function(con) {
@@ -259,7 +256,7 @@ spec_sql_append_table <- list(
259256
a = c("a", ""),
260257
stringsAsFactors = FALSE
261258
)
262-
test_table_roundtrip(use_append = TRUE, con, tbl_in)
259+
append_table_test_roundtrip(con, tbl_in)
263260
},
264261

265262
append_roundtrip_factor = function(con) {
@@ -272,7 +269,7 @@ spec_sql_append_table <- list(
272269
#' with a warning)
273270
suppressWarnings(
274271
expect_warning(
275-
test_table_roundtrip(use_append = TRUE, con, tbl_in, tbl_exp)
272+
append_table_test_roundtrip(con, tbl_in, tbl_exp)
276273
)
277274
)
278275
},
@@ -287,8 +284,7 @@ spec_sql_append_table <- list(
287284
tbl_in <- data.frame(id = 1L, a = I(list(as.raw(0:10))))
288285
tbl_exp <- tbl_in
289286
tbl_exp$a <- blob::as_blob(unclass(tbl_in$a))
290-
test_table_roundtrip(
291-
use_append = TRUE,
287+
append_table_test_roundtrip(
292288
con, tbl_in, tbl_exp,
293289
transform = function(tbl_out) {
294290
tbl_out$a <- blob::as_blob(tbl_out$a)
@@ -305,8 +301,7 @@ spec_sql_append_table <- list(
305301
}
306302

307303
tbl_in <- data.frame(id = 1L, a = blob::blob(as.raw(0:10)))
308-
test_table_roundtrip(
309-
use_append = TRUE,
304+
append_table_test_roundtrip(
310305
con, tbl_in,
311306
transform = function(tbl_out) {
312307
tbl_out$a <- blob::as_blob(tbl_out$a)
@@ -324,8 +319,7 @@ spec_sql_append_table <- list(
324319

325320
#' returned as `Date`)
326321
tbl_in <- data.frame(a = as_numeric_date(c(Sys.Date() + 1:5)))
327-
test_table_roundtrip(
328-
use_append = TRUE,
322+
append_table_test_roundtrip(
329323
con, tbl_in,
330324
transform = function(tbl_out) {
331325
expect_type(unclass(tbl_out$a), "double")
@@ -352,8 +346,7 @@ spec_sql_append_table <- list(
352346
"2040-01-01",
353347
"2999-09-09"
354348
)))
355-
test_table_roundtrip(
356-
use_append = TRUE,
349+
append_table_test_roundtrip(
357350
con, tbl_in,
358351
transform = function(tbl_out) {
359352
expect_type(unclass(tbl_out$a), "double")
@@ -376,7 +369,7 @@ spec_sql_append_table <- list(
376369
tbl_exp$a <- hms::as_hms(tbl_exp$a)
377370
tbl_exp$b <- hms::as_hms(tbl_exp$b)
378371

379-
test_table_roundtrip(
372+
append_table_test_roundtrip(
380373
con, tbl_in, tbl_exp,
381374
transform = function(tbl_out) {
382375
#' returned as objects that inherit from `difftime`)
@@ -412,8 +405,7 @@ spec_sql_append_table <- list(
412405

413406
#' respecting the time zone but not necessarily preserving the
414407
#' input time zone),
415-
test_table_roundtrip(
416-
use_append = TRUE,
408+
append_table_test_roundtrip(
417409
con, tbl_in,
418410
transform = function(out) {
419411
dates <- vapply(out, inherits, "POSIXt", FUN.VALUE = logical(1L))
@@ -453,8 +445,7 @@ spec_sql_append_table <- list(
453445

454446
#' respecting the time zone but not necessarily preserving the
455447
#' input time zone)
456-
test_table_roundtrip(
457-
use_append = TRUE,
448+
append_table_test_roundtrip(
458449
con, tbl_in,
459450
transform = function(out) {
460451
dates <- vapply(out, inherits, "POSIXt", FUN.VALUE = logical(1L))
@@ -479,7 +470,7 @@ spec_sql_append_table <- list(
479470
}
480471
)
481472

482-
lapply(tbl_in_list, test_table_roundtrip, con = con)
473+
lapply(tbl_in_list, append_table_test_roundtrip, con = con)
483474
},
484475

485476
append_table_name = function(ctx, con) {
@@ -617,3 +608,37 @@ spec_sql_append_table <- list(
617608
#
618609
NULL
619610
)
611+
612+
613+
append_table_test_roundtrip <- function(...) {
614+
append_table_test_roundtrip_one(..., .add_na = "none")
615+
append_table_test_roundtrip_one(..., .add_na = "above")
616+
append_table_test_roundtrip_one(..., .add_na = "below")
617+
}
618+
619+
append_table_test_roundtrip_one <- function(
620+
con, tbl_in, tbl_expected = tbl_in, transform = identity, name = NULL,
621+
field.types = NULL, .add_na = "none"
622+
) {
623+
force(tbl_expected)
624+
if (.add_na == "above") {
625+
tbl_in <- add_na_above(tbl_in)
626+
tbl_expected <- add_na_above(tbl_expected)
627+
} else if (.add_na == "below") {
628+
tbl_in <- add_na_below(tbl_in)
629+
tbl_expected <- add_na_below(tbl_expected)
630+
}
631+
632+
if (is.null(name)) {
633+
name <- random_table_name()
634+
}
635+
636+
local_remove_test_table(con, name = name)
637+
638+
dbCreateTable(con, name, field.types %||% tbl_in)
639+
dbAppendTable(con, name, tbl_in)
640+
641+
tbl_read <- check_df(dbReadTable(con, name, check.names = FALSE))
642+
tbl_out <- transform(tbl_read)
643+
expect_equal_df(tbl_out, tbl_expected)
644+
}

0 commit comments

Comments
 (0)