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 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879
|
#' spec_sql_write_table
#' @family sql specifications
#' @usage NULL
#' @format NULL
#' @keywords NULL
#' @importFrom lubridate with_tz
spec_sql_write_table <- list(
write_table_formals = function() {
# <establish formals of described functions>
expect_equal(names(formals(dbWriteTable)), c("conn", "name", "value", "..."))
},
write_table_return = function(con, table_name) {
#' @return
#' `dbWriteTable()` returns `TRUE`, invisibly.
expect_invisible_true(dbWriteTable(con, table_name, data.frame(a = 1L)))
},
#'
write_table_overwrite = function(con, table_name) {
#' @section Failure modes:
#' If the table exists, and both `append` and `overwrite` arguments are unset,
test_in <- data.frame(a = 1L)
dbWriteTable(con, table_name, test_in)
expect_error(dbWriteTable(con, table_name, data.frame(a = 2L)))
test_out <- check_df(dbReadTable(con, table_name))
expect_equal_df(test_out, test_in)
},
write_table_append_incompatible = function(con, table_name) {
#' or `append = TRUE` and the data frame with the new data has different
#' column names,
#' an error is raised; the remote table remains unchanged.
test_in <- data.frame(a = 1L)
dbWriteTable(con, table_name, test_in)
expect_error(dbWriteTable(con, table_name, data.frame(b = 2L), append = TRUE))
test_out <- check_df(dbReadTable(con, table_name))
expect_equal_df(test_out, test_in)
},
#'
write_table_closed_connection = function(ctx, closed_con) {
#' An error is raised when calling this method for a closed
expect_error(dbWriteTable(closed_con, "test", data.frame(a = 1)))
},
write_table_invalid_connection = function(ctx, invalid_con) {
#' or invalid connection.
expect_error(dbWriteTable(invalid_con, "test", data.frame(a = 1)))
},
write_table_error = function(ctx, con, table_name) {
#' An error is also raised
test_in <- data.frame(a = 1L)
#' if `name` cannot be processed with [dbQuoteIdentifier()] or
expect_error(dbWriteTable(con, NA, test_in))
#' if this results in a non-scalar.
expect_error(dbWriteTable(con, c(table_name, table_name), test_in))
#' Invalid values for the additional arguments `row.names`,
#' `overwrite`, `append`, `field.types`, and `temporary`
#' (non-scalars,
expect_error(dbWriteTable(con, table_name, test_in, row.names = letters))
expect_error(dbWriteTable(con, table_name, test_in, overwrite = c(TRUE, FALSE)))
expect_error(dbWriteTable(con, table_name, test_in, append = c(TRUE, FALSE)))
expect_error(dbWriteTable(con, table_name, test_in, temporary = c(TRUE, FALSE)))
#' unsupported data types,
expect_error(dbWriteTable(con, table_name, test_in, row.names = list(1L)))
expect_error(dbWriteTable(con, table_name, test_in, overwrite = 1L))
expect_error(dbWriteTable(con, table_name, test_in, append = 1L))
expect_error(dbWriteTable(con, table_name, test_in, field.types = 1L))
expect_error(dbWriteTable(con, table_name, test_in, temporary = 1L))
#' `NA`,
expect_error(dbWriteTable(con, table_name, test_in, overwrite = NA))
expect_error(dbWriteTable(con, table_name, test_in, append = NA))
expect_error(dbWriteTable(con, table_name, test_in, field.types = NA))
expect_error(dbWriteTable(con, table_name, test_in, temporary = NA))
#' incompatible values,
expect_error(dbWriteTable(con, table_name, test_in, field.types = letters))
expect_error(dbWriteTable(con, table_name, test_in, field.types = c(b = "INTEGER")))
expect_error(dbWriteTable(con, table_name, test_in, overwrite = TRUE, append = TRUE))
expect_error(dbWriteTable(con, table_name, test_in, append = TRUE, field.types = c(a = "INTEGER")))
#' duplicate
expect_error(dbWriteTable(con, table_name, test_in, field.types = c(a = "INTEGER", a = "INTEGER")))
#' or missing names,
expect_error(dbWriteTable(con, table_name, test_in, field.types = c("INTEGER")))
#' incompatible columns)
dbWriteTable(con, table_name, test_in)
expect_error(dbWriteTable(con, table_name, data.frame(b = 2L, c = 3L), append = TRUE))
#' also raise an error.
},
#' @section Additional arguments:
#' The following arguments are not part of the `dbWriteTable()` generic
#' (to improve compatibility across backends)
#' but are part of the DBI specification:
#' - `row.names` (default: `FALSE`)
#' - `overwrite` (default: `FALSE`)
#' - `append` (default: `FALSE`)
#' - `field.types` (default: `NULL`)
#' - `temporary` (default: `FALSE`)
#'
#' They must be provided as named arguments.
#' See the "Specification" and "Value" sections for details on their usage.
write_table_name = function(ctx, con) {
#' @section Specification:
#' The `name` argument is processed as follows,
#' to support databases that allow non-syntactic names for their objects:
if (isTRUE(ctx$tweaks$strict_identifier)) {
table_names <- "a"
} else {
table_names <- c("a", "with spaces", "with,comma")
}
for (table_name in table_names) {
test_in <- data.frame(a = 1)
local_remove_test_table(con, table_name)
#' - If an unquoted table name as string: `dbWriteTable()` will do the quoting,
dbWriteTable(con, table_name, test_in)
test_out <- check_df(dbReadTable(con, dbQuoteIdentifier(con, table_name)))
expect_equal_df(test_out, test_in)
#' perhaps by calling `dbQuoteIdentifier(conn, x = name)`
}
},
write_table_name_quoted = function(ctx, con) {
#' - If the result of a call to [dbQuoteIdentifier()]: no more quoting is done
skip_if_not_dbitest(ctx, "1.7.2")
if (isTRUE(ctx$tweaks$strict_identifier)) {
table_names <- "a"
} else {
table_names <- c("a", "with spaces", "with,comma")
}
for (table_name in table_names) {
test_in <- data.frame(a = 1)
local_remove_test_table(con, table_name)
dbWriteTable(con, dbQuoteIdentifier(con, table_name), test_in)
test_out <- check_df(dbReadTable(con, table_name))
expect_equal_df(test_out, test_in)
}
},
#'
write_table_value_df = function(con, table_name) {
#' The `value` argument must be a data frame
test_in <- trivial_df()
dbWriteTable(con, table_name, test_in)
test_out <- check_df(dbReadTable(con, table_name))
expect_equal_df(test_out, test_in)
},
write_table_value_subset = function(ctx, con, table_name) {
#' with a subset of the columns of the existing table if `append = TRUE`.
test_in <- trivial_df(3, letters[1:3])
dbCreateTable(con, table_name, test_in)
dbWriteTable(con, table_name, test_in[2], append = TRUE)
test_out <- check_df(dbReadTable(con, table_name))
test_in[c(1, 3)] <- NA_real_
expect_equal_df(test_out, test_in)
},
write_table_value_shuffle = function(ctx, con, table_name) {
#' The order of the columns does not matter with `append = TRUE`.
test_in <- trivial_df(3, letters[1:3])
dbCreateTable(con, table_name, test_in)
dbWriteTable(con, table_name, test_in[c(2, 3, 1)], append = TRUE)
test_out <- check_df(dbReadTable(con, table_name))
expect_equal_df(test_out, test_in)
},
#
write_table_value_shuffle_subset = function(ctx, con, table_name) {
test_in <- trivial_df(4, letters[1:4])
dbCreateTable(con, table_name, test_in)
dbWriteTable(con, table_name, test_in[c(4, 1, 3)], append = TRUE)
test_out <- check_df(dbReadTable(con, table_name))
test_in[2] <- NA_real_
expect_equal_df(test_out, test_in)
},
#'
overwrite_table = function(ctx, con, table_name) {
#' If the `overwrite` argument is `TRUE`, an existing table of the same name
#' will be overwritten.
penguins <- get_penguins(ctx)
dbWriteTable(con, table_name, penguins)
expect_error(
dbWriteTable(con, table_name, penguins[1, ], overwrite = TRUE),
NA
)
penguins_out <- check_df(dbReadTable(con, table_name))
expect_equal_df(penguins_out, penguins[1, ])
},
overwrite_table_missing = function(ctx, con, table_name) {
#' This argument doesn't change behavior if the table does not exist yet.
penguins_in <- get_penguins(ctx)
expect_error(
dbWriteTable(con, table_name, penguins_in[1, ], overwrite = TRUE),
NA
)
penguins_out <- check_df(dbReadTable(con, table_name))
expect_equal_df(penguins_out, penguins_in[1, ])
},
#'
append_table = function(ctx, con, table_name) {
#' If the `append` argument is `TRUE`, the rows in an existing table are
#' preserved, and the new data are appended.
penguins <- get_penguins(ctx)
dbWriteTable(con, table_name, penguins)
expect_error(dbWriteTable(con, table_name, penguins[1, ], append = TRUE), NA)
penguins_out <- check_df(dbReadTable(con, table_name))
expect_equal_df(penguins_out, rbind(penguins, penguins[1, ]))
},
append_table_new = function(ctx, con, table_name) {
#' If the table doesn't exist yet, it is created.
penguins <- get_penguins(ctx)
expect_error(dbWriteTable(con, table_name, penguins[1, ], append = TRUE), NA)
penguins_out <- check_df(dbReadTable(con, table_name))
expect_equal_df(penguins_out, penguins[1, ])
},
#'
temporary_table_1 = function(ctx, con, table_name = "dbit08") {
#' If the `temporary` argument is `TRUE`, the table is not available in a
#' second connection and is gone after reconnecting.
#' Not all backends support this argument.
if (!isTRUE(ctx$tweaks$temporary_tables)) {
skip("tweak: temporary_tables")
}
penguins <- get_penguins(ctx)
dbWriteTable(con, table_name, penguins, temporary = TRUE)
penguins_out <- check_df(dbReadTable(con, table_name))
expect_equal_df(penguins_out, penguins)
con2 <- local_connection(ctx)
expect_error(dbReadTable(con2, table_name))
},
# second stage
temporary_table_2 = function(ctx, con) {
if (!isTRUE(ctx$tweaks$temporary_tables)) {
skip("tweak: temporary_tables")
}
# table_name not in formals on purpose: this means that this table won't be
# removed at the end of the test
table_name <- "dbit08"
expect_error(dbReadTable(con, table_name))
},
table_visible_in_other_connection_1 = function(ctx, local_con) {
#' A regular, non-temporary table is visible in a second connection,
penguins30 <- get_penguins(ctx)
# table_name not in formals on purpose: this means that this table won't be
# removed at the end of the test
table_name <- "dbit09"
dbWriteTable(local_con, table_name, penguins30)
penguins_out <- check_df(dbReadTable(local_con, table_name))
expect_equal_df(penguins_out, penguins30)
con2 <- local_connection(ctx)
expect_equal_df(dbReadTable(con2, table_name), penguins30)
},
# second stage
table_visible_in_other_connection_2 = function(ctx, con) {
#' in a pre-existing connection,
penguins30 <- get_penguins(ctx)
# table_name not in formals on purpose: this means that this table won't be
# removed at the end of the test
table_name <- "dbit09"
expect_equal_df(check_df(dbReadTable(con, table_name)), penguins30)
},
# third stage
table_visible_in_other_connection_3 = function(ctx, local_con, table_name = "dbit09") {
#' and after reconnecting to the database.
penguins30 <- get_penguins(ctx)
expect_equal_df(check_df(dbReadTable(local_con, table_name)), penguins30)
},
#'
roundtrip_keywords = function(ctx, con) {
#' SQL keywords can be used freely in table names, column names, and data.
tbl_in <- data.frame(
select = "unique", from = "join", where = "order",
stringsAsFactors = FALSE
)
test_table_roundtrip(con, tbl_in, name = "exists")
},
roundtrip_quotes = function(ctx, con, table_name) {
#' Quotes, commas, spaces, and other special characters such as newlines and tabs,
#' can also be used in the data,
tbl_in <- data.frame(
as.character(dbQuoteString(con, "")),
as.character(dbQuoteIdentifier(con, "")),
"with space",
"a,b", "a\nb", "a\tb", "a\rb", "a\bb",
"a\\Nb", "a\\tb", "a\\rb", "a\\bb", "a\\Zb",
stringsAsFactors = FALSE
)
names(tbl_in) <- letters[seq_along(tbl_in)]
test_table_roundtrip(con, tbl_in)
},
roundtrip_quotes_table_names = function(ctx, con) {
#' and, if the database supports non-syntactic identifiers,
#' also for table names
if (isTRUE(ctx$tweaks$strict_identifier)) {
skip("tweak: strict_identifier")
}
table_names <- c(
as.character(dbQuoteIdentifier(con, "")),
as.character(dbQuoteString(con, "")),
"with space",
"a,b", "a\nb", "a\tb", "a\rb", "a\bb",
"a\\Nb", "a\\tb", "a\\rb", "a\\bb", "a\\Zb"
)
tbl_in <- trivial_df()
for (table_name in table_names) {
test_table_roundtrip_one(con, tbl_in, .add_na = "none")
}
},
roundtrip_quotes_column_names = function(ctx, con) {
#' and column names.
skip_if_not_dbitest(ctx, "1.7.2")
if (isTRUE(ctx$tweaks$strict_identifier)) {
skip("tweak: strict_identifier")
}
column_names <- c(
as.character(dbQuoteIdentifier(con, "")),
as.character(dbQuoteString(con, "")),
"with space",
"a,b", "a\nb", "a\tb", "a\rb", "a\bb",
"a\\nb", "a\\tb", "a\\rb", "a\\bb", "a\\zb"
)
tbl_in <- trivial_df(length(column_names), column_names)
test_table_roundtrip_one(con, tbl_in, .add_na = "none")
},
#'
roundtrip_integer = function(ctx, con) {
#' The following data types must be supported at least,
#' and be read identically with [dbReadTable()]:
#' - integer
tbl_in <- data.frame(a = c(1:5))
test_table_roundtrip(con, tbl_in)
},
roundtrip_numeric = function(ctx, con) {
#' - numeric
tbl_in <- data.frame(a = c(seq(1, 3, by = 0.5)))
test_table_roundtrip(con, tbl_in)
#' (the behavior for `Inf` and `NaN` is not specified)
},
roundtrip_logical = function(ctx, con) {
#' - logical
tbl_in <- data.frame(a = c(TRUE, FALSE, NA))
tbl_exp <- tbl_in
tbl_exp$a <- ctx$tweaks$logical_return(tbl_exp$a)
test_table_roundtrip(con, tbl_in, tbl_exp)
},
roundtrip_null = function(ctx, con) {
#' - `NA` as NULL
tbl_in <- data.frame(a = NA)
test_table_roundtrip(
con, tbl_in,
transform = function(tbl_out) {
tbl_out$a <- as.logical(tbl_out$a) # Plain NA is of type logical
tbl_out
}
)
},
#' - 64-bit values (using `"bigint"` as field type); the result can be
roundtrip_64_bit_numeric = function(ctx, con) {
tbl_in <- data.frame(a = c(-1e14, 1e15))
test_table_roundtrip(
con, tbl_in,
transform = function(tbl_out) {
#' - converted to a numeric, which may lose precision,
tbl_out$a <- as.numeric(tbl_out$a)
tbl_out
},
field.types = c(a = "BIGINT")
)
},
#
roundtrip_64_bit_character = function(ctx, con) {
tbl_in <- data.frame(a = c(-1e14, 1e15))
tbl_exp <- tbl_in
tbl_exp$a <- format(tbl_exp$a, scientific = FALSE)
test_table_roundtrip(
con, tbl_in, tbl_exp,
transform = function(tbl_out) {
#' - converted a character vector, which gives the full decimal
#' representation
tbl_out$a <- as.character(tbl_out$a)
tbl_out
},
field.types = c(a = "BIGINT")
)
},
#
roundtrip_64_bit_roundtrip = function(con, table_name) {
tbl_in <- data.frame(a = c(-1e14, 1e15))
dbWriteTable(con, table_name, tbl_in, field.types = c(a = "BIGINT"))
tbl_out <- dbReadTable(con, table_name)
#' - written to another table and read again unchanged
test_table_roundtrip(con, tbl_out, tbl_expected = tbl_out)
},
roundtrip_character = function(ctx, con) {
#' - character (in both UTF-8
tbl_in <- data.frame(
id = seq_along(get_texts()),
a = get_texts(),
stringsAsFactors = FALSE
)
test_table_roundtrip(con, tbl_in)
},
roundtrip_character_native = function(ctx, con) {
#' and native encodings),
tbl_in <- data.frame(
a = c(enc2native(get_texts())),
stringsAsFactors = FALSE
)
test_table_roundtrip(con, tbl_in)
},
roundtrip_character_empty = function(ctx, con) {
#' supporting empty strings
tbl_in <- data.frame(
a = c("", "a"),
stringsAsFactors = FALSE
)
test_table_roundtrip(con, tbl_in)
},
roundtrip_character_empty_after = function(ctx, con) {
#' before and after a non-empty string
tbl_in <- data.frame(
a = c("a", ""),
stringsAsFactors = FALSE
)
test_table_roundtrip(con, tbl_in)
},
roundtrip_factor = function(ctx, con) {
#' - factor (returned as character)
tbl_in <- data.frame(
a = factor(get_texts())
)
tbl_exp <- tbl_in
tbl_exp$a <- as.character(tbl_exp$a)
test_table_roundtrip(con, tbl_in, tbl_exp)
},
roundtrip_raw = function(ctx, con) {
#' - list of raw
#' (if supported by the database)
if (isTRUE(ctx$tweaks$omit_blob_tests)) {
skip("tweak: omit_blob_tests")
}
tbl_in <- data.frame(id = 1L, a = I(list(as.raw(0:10))))
tbl_exp <- tbl_in
tbl_exp$a <- blob::as_blob(unclass(tbl_in$a))
test_table_roundtrip(
con, tbl_in, tbl_exp,
transform = function(tbl_out) {
tbl_out$a <- blob::as_blob(tbl_out$a)
tbl_out
}
)
},
roundtrip_blob = function(ctx, con) {
#' - objects of type [blob::blob]
#' (if supported by the database)
if (isTRUE(ctx$tweaks$omit_blob_tests)) {
skip("tweak: omit_blob_tests")
}
tbl_in <- data.frame(id = 1L, a = blob::blob(as.raw(0:10)))
test_table_roundtrip(
con, tbl_in,
transform = function(tbl_out) {
tbl_out$a <- blob::as_blob(tbl_out$a)
tbl_out
}
)
},
roundtrip_date = function(ctx, con) {
#' - date
#' (if supported by the database;
if (!isTRUE(ctx$tweaks$date_typed)) {
skip("tweak: !date_typed")
}
#' returned as `Date`),
tbl_in <- data.frame(a = as_numeric_date(c(Sys.Date() + 1:5)))
test_table_roundtrip(
con, tbl_in,
transform = function(tbl_out) {
expect_type(unclass(tbl_out$a), "double")
tbl_out
}
)
},
roundtrip_date_extended = function(ctx, con) {
#' also for dates prior to 1970 or 1900 or after 2038
if (!isTRUE(ctx$tweaks$date_typed)) {
skip("tweak: !date_typed")
}
tbl_in <- data.frame(a = as_numeric_date(c(
"1811-11-11",
"1899-12-31",
"1900-01-01",
"1950-05-05",
"1969-12-31",
"1970-01-01",
"2037-01-01",
"2038-01-01",
"2040-01-01",
"2999-09-09"
)))
test_table_roundtrip(
con, tbl_in,
transform = function(tbl_out) {
expect_type(unclass(tbl_out$a), "double")
tbl_out
}
)
},
roundtrip_time = function(ctx, con) {
#' - time
#' (if supported by the database;
if (!isTRUE(ctx$tweaks$time_typed)) {
skip("tweak: !time_typed")
}
tbl_in <- data.frame(a = hms::hms(minutes = 1:5))
tbl_in$b <- .difftime(as.numeric(tbl_in$a) / 60, "mins")
tbl_exp <- tbl_in
tbl_exp$a <- hms::as_hms(tbl_exp$a)
tbl_exp$b <- hms::as_hms(tbl_exp$b)
test_table_roundtrip(
con, tbl_in, tbl_exp,
transform = function(tbl_out) {
#' returned as objects that inherit from `difftime`)
expect_s3_class(tbl_out$a, "difftime")
expect_s3_class(tbl_out$b, "difftime")
tbl_out$a <- hms::as_hms(tbl_out$a)
tbl_out$b <- hms::as_hms(tbl_out$b)
tbl_out
}
)
},
roundtrip_timestamp = function(ctx, con) {
#' - timestamp
#' (if supported by the database;
if (!isTRUE(ctx$tweaks$timestamp_typed)) {
skip("tweak: !timestamp_typed")
}
#' returned as `POSIXct`
local <- round(Sys.time()) +
c(
1, 60, 3600, 86400,
86400 * 90, 86400 * 180, 86400 * 270,
1e9, 5e9
)
attr(local, "tzone") <- ""
tbl_in <- data.frame(id = seq_along(local))
tbl_in$local <- local
tbl_in$gmt <- lubridate::with_tz(local, tzone = "GMT")
tbl_in$pst8pdt <- lubridate::with_tz(local, tzone = "PST8PDT")
tbl_in$utc <- lubridate::with_tz(local, tzone = "UTC")
#' respecting the time zone but not necessarily preserving the
#' input time zone),
test_table_roundtrip(
con, tbl_in,
transform = function(out) {
dates <- map_lgl(out, inherits, "POSIXt")
tz <- toupper(names(out))
tz[tz == "LOCAL"] <- ""
out[dates] <- Map(lubridate::with_tz, out[dates], tz[dates])
out
}
)
},
roundtrip_timestamp_extended = function(ctx, con) {
#' also for timestamps prior to 1970 or 1900 or after 2038
if (!isTRUE(ctx$tweaks$timestamp_typed)) {
skip("tweak: !timestamp_typed")
}
local <- as.POSIXct(c(
"1811-11-11",
"1899-12-31",
"1900-01-01",
"1950-05-05",
"1969-12-31",
"1970-01-01",
"2037-01-01",
"2038-01-01",
"2040-01-01",
"2999-09-09"
))
attr(local, "tzone") <- ""
tbl_in <- data.frame(id = seq_along(local))
tbl_in$local <- local
tbl_in$gmt <- lubridate::with_tz(local, tzone = "GMT")
tbl_in$pst8pdt <- lubridate::with_tz(local, tzone = "PST8PDT")
tbl_in$utc <- lubridate::with_tz(local, tzone = "UTC")
#' respecting the time zone but not necessarily preserving the
#' input time zone)
test_table_roundtrip(
con, tbl_in,
transform = function(out) {
dates <- map_lgl(out, inherits, "POSIXt")
tz <- toupper(names(out))
tz[tz == "LOCAL"] <- ""
out[dates] <- Map(lubridate::with_tz, out[dates], tz[dates])
out
}
)
},
#'
roundtrip_mixed = function(ctx, con) {
#' Mixing column types in the same table is supported.
data <- list("a", 1L, 1.5)
data <- map(data, c, NA)
expanded <- expand.grid(a = data, b = data, c = data)
tbl_in_list <- map(
seq_len(nrow(expanded)),
function(i) {
as.data.frame(map(expanded[i, ], unlist, recursive = FALSE))
}
)
map(tbl_in_list, test_table_roundtrip, con = con)
},
#'
roundtrip_field_types = function(ctx, con) {
#' The `field.types` argument must be a named character vector with at most
#' one entry for each column.
#' It indicates the SQL data type to be used for a new column.
tbl_in <- data.frame(a = numeric(), b = character())
#' If a column is missed from `field.types`, the type is inferred
#' from the input data with [dbDataType()].
tbl_exp <- data.frame(a = integer(), b = character())
test_table_roundtrip(
con, tbl_in, tbl_exp,
field.types = c(a = "INTEGER")
)
tbl_in <- data.frame(a = numeric(), b = integer())
tbl_exp <- data.frame(a = integer(), b = numeric())
test_table_roundtrip(
con, tbl_in, tbl_exp,
field.types = c(b = "REAL", a = "INTEGER")
)
},
#'
write_table_row_names_false = function(ctx, con) {
#' The interpretation of [rownames] depends on the `row.names` argument,
#' see [sqlRownamesToColumn()] for details:
#' - If `FALSE` or `NULL`, row names are ignored.
for (row.names in list(FALSE, NULL)) {
table_name <- random_table_name()
local_remove_test_table(con, table_name)
mtcars_in <- datasets::mtcars
dbWriteTable(con, table_name, mtcars_in, row.names = row.names)
mtcars_out <- check_df(dbReadTable(con, table_name, row.names = FALSE))
expect_false("row_names" %in% names(mtcars_out))
expect_equal_df(mtcars_out, unrowname(mtcars_in))
}
},
#
write_table_row_names_true_exists = function(ctx, con, table_name) {
#' - If `TRUE`, row names are converted to a column named "row_names",
row.names <- TRUE
mtcars_in <- datasets::mtcars
dbWriteTable(con, table_name, mtcars_in, row.names = row.names)
mtcars_out <- check_df(dbReadTable(con, table_name, row.names = FALSE))
expect_true("row_names" %in% names(mtcars_out))
expect_true(all(rownames(mtcars_in) %in% mtcars_out$row_names))
expect_true(all(mtcars_out$row_names %in% rownames(mtcars_in)))
expect_equal_df(mtcars_out[names(mtcars_out) != "row_names"], unrowname(mtcars_in))
},
#
write_table_row_names_true_missing = function(ctx, con, table_name) {
#' even if the input data frame only has natural row names from 1 to `nrow(...)`.
row.names <- TRUE
penguins_in <- get_penguins(ctx)
dbWriteTable(con, table_name, penguins_in, row.names = row.names)
penguins_out <- check_df(dbReadTable(con, table_name, row.names = FALSE))
expect_true("row_names" %in% names(penguins_out))
expect_true(all(rownames(penguins_in) %in% penguins_out$row_names))
expect_true(all(penguins_out$row_names %in% rownames(penguins_in)))
expect_equal_df(penguins_out[names(penguins_out) != "row_names"], penguins_in)
},
#
write_table_row_names_na_exists = function(ctx, con, table_name) {
#' - If `NA`, a column named "row_names" is created if the data has custom row names,
row.names <- NA
mtcars_in <- datasets::mtcars
dbWriteTable(con, table_name, mtcars_in, row.names = row.names)
mtcars_out <- check_df(dbReadTable(con, table_name, row.names = FALSE))
expect_true("row_names" %in% names(mtcars_out))
expect_true(all(rownames(mtcars_in) %in% mtcars_out$row_names))
expect_true(all(mtcars_out$row_names %in% rownames(mtcars_in)))
expect_equal_df(mtcars_out[names(mtcars_out) != "row_names"], unrowname(mtcars_in))
},
#
write_table_row_names_na_missing = function(ctx, con, table_name) {
#' no extra column is created in the case of natural row names.
row.names <- NA
penguins_in <- get_penguins(ctx)
dbWriteTable(con, table_name, penguins_in, row.names = row.names)
penguins_out <- check_df(dbReadTable(con, table_name, row.names = FALSE))
expect_equal_df(penguins_out, penguins_in)
},
#
write_table_row_names_string_exists = function(ctx, con, table_name) {
row.names <- "make_model"
#' - If a string, this specifies the name of the column in the remote table
#' that contains the row names,
mtcars_in <- datasets::mtcars
dbWriteTable(con, table_name, mtcars_in, row.names = row.names)
mtcars_out <- check_df(dbReadTable(con, table_name, row.names = FALSE))
expect_true("make_model" %in% names(mtcars_out))
expect_true(all(mtcars_out$make_model %in% rownames(mtcars_in)))
expect_true(all(rownames(mtcars_in) %in% mtcars_out$make_model))
expect_equal_df(mtcars_out[names(mtcars_out) != "make_model"], unrowname(mtcars_in))
},
#
write_table_row_names_string_missing = function(ctx, con, table_name) {
row.names <- "seq"
#' even if the input data frame only has natural row names.
penguins_in <- get_penguins(ctx)
dbWriteTable(con, table_name, penguins_in, row.names = row.names)
penguins_out <- check_df(dbReadTable(con, table_name, row.names = FALSE))
expect_true("seq" %in% names(penguins_out))
expect_true(all(penguins_out$seq %in% rownames(penguins_in)))
expect_true(all(rownames(penguins_in) %in% penguins_out$seq))
expect_equal_df(penguins_out[names(penguins_out) != "seq"], penguins_in)
},
#
#'
write_table_row_names_default = function(ctx, con, table_name) {
#' The default is `row.names = FALSE`.
mtcars_in <- datasets::mtcars
dbWriteTable(con, table_name, mtcars_in)
mtcars_out <- check_df(dbReadTable(con, table_name, row.names = FALSE))
expect_false("row_names" %in% names(mtcars_out))
expect_equal_df(mtcars_out, unrowname(mtcars_in))
},
#
NULL
)
test_table_roundtrip <- function(...) {
test_table_roundtrip_one(..., .add_na = "none")
test_table_roundtrip_one(..., .add_na = "above")
test_table_roundtrip_one(..., .add_na = "below")
}
test_table_roundtrip_one <- function(
con,
tbl_in,
tbl_expected = tbl_in,
transform = identity,
name = NULL,
field.types = NULL,
use_append = FALSE,
.add_na = "none") {
force(tbl_expected)
if (.add_na == "above") {
tbl_in <- add_na_above(tbl_in)
tbl_expected <- add_na_above(tbl_expected)
} else if (.add_na == "below") {
tbl_in <- add_na_below(tbl_in)
tbl_expected <- add_na_below(tbl_expected)
}
if (is.null(name)) {
name <- random_table_name()
}
local_remove_test_table(con, name = name)
if (use_append) {
dbCreateTable(con, name, field.types %||% tbl_in)
dbAppendTable(con, name, tbl_in)
} else {
dbWriteTable(con, name, tbl_in, field.types = field.types)
}
tbl_read <- check_df(dbReadTable(con, name, check.names = FALSE))
tbl_out <- transform(tbl_read)
expect_equal_df(tbl_out, tbl_expected)
}
add_na_above <- function(tbl) {
idx <- c(NA, seq_len(nrow(tbl)))
tbl <- tbl[idx, , drop = FALSE]
unrowname(tbl)
}
add_na_below <- function(tbl) {
idx <- c(seq_len(nrow(tbl)), NA)
tbl <- tbl[idx, , drop = FALSE]
unrowname(tbl)
}
|