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
|
#' spec_result_roundtrip
#' @family result specifications
#' @usage NULL
#' @format NULL
#' @keywords NULL
spec_result_roundtrip <- list(
data_integer = function(ctx, con) {
#' @section Specification:
#' The column types of the returned data frame depend on the data returned:
#' - [integer] (or coercible to an integer) for integer values between -2^31 and 2^31 - 1,
#' with [NA] for SQL `NULL` values
test_select_with_null(.ctx = ctx, con, 1L ~ equals_one, -100L ~ equals_minus_100)
},
data_numeric = function(ctx, con) {
#' - [numeric] for numbers with a fractional component,
#' with NA for SQL `NULL` values
test_select_with_null(.ctx = ctx, con, 1.5, -100.5)
},
data_logical = function(ctx, con) {
#' - [logical] for Boolean values (some backends may return an integer);
int_values <- 1:0
values <- ctx$tweaks$logical_return(as.logical(int_values))
sql_names <- paste0("CAST(", int_values, " AS ", dbDataType(con, logical()), ")")
#' with NA for SQL `NULL` values
test_select_with_null(.ctx = ctx, con, !!!setNames(values, sql_names))
},
data_character = function(ctx, con) {
#' - [character] for text,
values <- get_texts()
test_funs <- rep(list(has_utf8_or_ascii_encoding), length(values))
sql_names <- as.character(dbQuoteString(con, values))
#' with NA for SQL `NULL` values
test_select_with_null(.ctx = ctx, con, !!!setNames(values, sql_names))
test_select_with_null(.ctx = ctx, con, !!!setNames(test_funs, sql_names))
},
data_raw = function(ctx, con) {
#' - lists of [raw] for blobs
if (isTRUE(ctx$tweaks$omit_blob_tests)) {
skip("tweak: omit_blob_tests")
}
is_raw_list <- function(x) {
is.list(x) && is.raw(x[[1L]])
}
values <- list(is_raw_list)
sql_names <- ctx$tweaks$blob_cast(DBI::dbQuoteLiteral(con, list(raw(1))))
#' with [NULL] entries for SQL NULL values
test_select_with_null(.ctx = ctx, con, !!!setNames(values, sql_names))
},
data_date = function(ctx, con) {
#' - coercible using [as.Date()] for dates,
as_date_equals_to <- function(x) {
map(x, function(xx) {
function(value) as.Date(value) == xx
})
}
char_values <- paste0("2015-01-", sprintf("%.2d", 1:12))
values <- as_date_equals_to(as.Date(char_values))
sql_names <- ctx$tweaks$date_cast(char_values)
#' with NA for SQL `NULL` values
test_select_with_null(.ctx = ctx, con, !!!setNames(values, sql_names))
},
data_date_current = function(ctx, con) {
#' (also applies to the return value of the SQL function `current_date`)
test_select_with_null(
.ctx = ctx, con,
"current_date" ~ is_roughly_current_date
)
},
data_time = function(ctx, con) {
#' - coercible using [hms::as_hms()] for times,
as_hms_equals_to <- function(x) {
map(x, function(xx) {
function(value) hms::as_hms(value) == xx
})
}
char_values <- c("00:00:00", "12:34:56")
time_values <- as_hms_equals_to(hms::as_hms(char_values))
sql_names <- ctx$tweaks$time_cast(char_values)
#' with NA for SQL `NULL` values
test_select_with_null(.ctx = ctx, con, !!!setNames(time_values, sql_names))
},
data_time_current = function(ctx, con) {
#' (also applies to the return value of the SQL function `current_time`)
test_select_with_null(
.ctx = ctx, con,
"current_time" ~ coercible_to_time
)
},
data_timestamp = function(ctx, con) {
#' - coercible using [as.POSIXct()] for timestamps,
coercible_to_timestamp <- function(x) {
x_timestamp <- try_silent(as.POSIXct(x))
!is.null(x_timestamp) && all(is.na(x) == is.na(x_timestamp))
}
char_values <- c("2015-10-11 00:00:00", "2015-10-11 12:34:56")
time_values <- rep(list(coercible_to_timestamp), 2L)
sql_names <- ctx$tweaks$timestamp_cast(char_values)
#' with NA for SQL `NULL` values
test_select_with_null(.ctx = ctx, con, !!!setNames(time_values, sql_names))
},
data_timestamp_current = function(ctx, con) {
#' (also applies to the return value of the SQL function `current_timestamp`)
test_select_with_null(
.ctx = ctx, con,
"current_timestamp" ~ function(x) {
coercible_to_timestamp <- function(x) {
x_timestamp <- try_silent(as.POSIXct(x))
!is.null(x_timestamp) && all(is.na(x) == is.na(x_timestamp))
}
coercible_to_timestamp(x) && (Sys.time() - as.POSIXct(x, tz = "UTC") <= hms::hms(2))
}
)
},
#'
data_date_typed = function(ctx, con) {
#' If dates and timestamps are supported by the backend, the following R types are
#' used:
#' - [Date] for dates
if (!isTRUE(ctx$tweaks$date_typed)) {
skip("tweak: !date_typed")
}
char_values <- paste0("2015-01-", sprintf("%.2d", 1:12))
values <- map(char_values, as_numeric_date)
sql_names <- ctx$tweaks$date_cast(char_values)
test_select_with_null(.ctx = ctx, con, !!!setNames(values, sql_names))
},
data_date_current_typed = function(ctx, con) {
#' (also applies to the return value of the SQL function `current_date`)
if (!isTRUE(ctx$tweaks$date_typed)) {
skip("tweak: !date_typed")
}
test_select_with_null(
.ctx = ctx, con,
"current_date" ~ is_roughly_current_date_typed
)
},
data_timestamp_typed = function(ctx, con) {
#' - [POSIXct] for timestamps
if (!isTRUE(ctx$tweaks$timestamp_typed)) {
skip("tweak: !timestamp_typed")
}
char_values <- c("2015-10-11 00:00:00", "2015-10-11 12:34:56")
timestamp_values <- rep(list(is_timestamp), 2L)
sql_names <- ctx$tweaks$timestamp_cast(char_values)
test_select_with_null(.ctx = ctx, con, !!!setNames(timestamp_values, sql_names))
},
data_timestamp_current_typed = function(ctx, con) {
#' (also applies to the return value of the SQL function `current_timestamp`)
if (!isTRUE(ctx$tweaks$timestamp_typed)) {
skip("tweak: !timestamp_typed")
}
test_select_with_null(
.ctx = ctx, con,
"current_timestamp" ~ is_roughly_current_timestamp_typed
)
},
#'
#' R has no built-in type with lossless support for the full range of 64-bit
#' or larger integers. If 64-bit integers are returned from a query,
#' the following rules apply:
#' - Values are returned in a container with support for the full range of
#' valid 64-bit values (such as the `integer64` class of the \pkg{bit64}
#' package)
#' - Coercion to numeric always returns a number that is as close as possible
#' to the true value
data_64_bit_numeric = function(ctx, con) {
as_numeric_identical_to <- function(x) {
map(x, function(xx) {
function(value) as.numeric(value) == xx
})
}
char_values <- c("10000000000", "-10000000000")
test_values <- as_numeric_identical_to(as.numeric(char_values))
test_select_with_null(.ctx = ctx, con, !!!setNames(test_values, char_values))
},
#' - Loss of precision when converting to numeric gives a warning
data_64_bit_numeric_warning = function(ctx, con) {
as_numeric_equals_to <- function(x) {
map(x, function(xx) {
function(value) isTRUE(all.equal(as.numeric(value), xx))
})
}
char_values <- c(" 1234567890123456789", "-1234567890123456789")
num_values <- as.numeric(char_values)
test_values <- as_numeric_equals_to(num_values)
suppressWarnings(
expect_warning(
test_select(.ctx = ctx, con, !!!setNames(test_values, char_values), .add_null = "none")
)
)
suppressWarnings(
expect_warning(
test_select(.ctx = ctx, con, !!!setNames(test_values, char_values), .add_null = "above")
)
)
suppressWarnings(
expect_warning(
test_select(.ctx = ctx, con, !!!setNames(test_values, char_values), .add_null = "below")
)
)
},
#' - Conversion to character always returns a lossless decimal representation
#' of the data
data_64_bit_lossless = function(ctx, con) {
as_character_equals_to <- function(x) {
map(x, function(xx) {
function(value) as.character(value) == xx
})
}
char_values <- c("1234567890123456789", "-1234567890123456789")
test_values <- as_character_equals_to(char_values)
test_select_with_null(.ctx = ctx, con, !!!setNames(test_values, char_values))
},
#
NULL
)
test_select_with_null <- function(...) {
test_select(..., .add_null = "none")
test_select(..., .add_null = "above")
test_select(..., .add_null = "below")
}
test_select <- function(
con,
...,
.add_null = "none",
.ctx,
.envir = parent.frame()) {
values <- list2(...)
value_is_formula <- map_lgl(values, is.call)
names(values)[value_is_formula] <- map(values[value_is_formula], "[[", 2L)
values[value_is_formula] <- map(
values[value_is_formula],
function(x) {
eval(x[[3]], envir = .envir)
}
)
if (is.null(names(values))) {
sql_values <- map(values, as.character)
} else {
sql_values <- names(values)
}
if (isTRUE(.ctx$tweaks$current_needs_parens)) {
sql_values <- gsub(
"^(current_(?:date|time|timestamp))$", "\\1()",
sql_values
)
}
sql_names <- letters[seq_along(sql_values)]
query <- paste(
"SELECT",
paste(sql_values, "as", sql_names, collapse = ", ")
)
if (.add_null != "none") {
query_null <- paste(
"SELECT",
paste("NULL as", sql_names, collapse = ", ")
)
query <- c(query, query_null)
if (.add_null == "above") {
query <- rev(query)
}
query <- paste0(query, ", ", 1:2, " as id")
query <- sql_union(.ctx = .ctx, query)
}
rows <- check_df(dbGetQuery(con, query))
if (.add_null != "none") {
rows <- rows[order(rows$id), -(length(sql_names) + 1L), drop = FALSE]
if (.add_null == "above") {
rows <- rows[2:1, , drop = FALSE]
}
}
expect_identical(names(rows), sql_names)
for (i in seq_along(values)) {
value_or_testfun <- values[[i]]
if (is.function(value_or_testfun)) {
eval(bquote(expect_true(value_or_testfun(rows[1L, .(i)]))))
} else {
eval(bquote(expect_identical(rows[1L, .(i)], .(value_or_testfun))))
}
}
if (.add_null != "none") {
expect_equal(nrow(rows), 2L)
if (is.list(rows[[1L]])) {
expect_true(is.null(rows[2L, 1L][[1L]]))
} else {
expect_true(is.na(rows[2L, 1L]))
}
} else {
expect_equal(nrow(rows), 1L)
}
}
equals_one <- function(x) {
identical(as.integer(x), 1L) && identical(as.numeric(x), 1)
}
equals_minus_100 <- function(x) {
identical(as.integer(x), -100L) && identical(as.numeric(x), -100)
}
all_have_utf8_or_ascii_encoding <- function(x) {
all(map_lgl(x, has_utf8_or_ascii_encoding))
}
has_utf8_or_ascii_encoding <- function(x) {
if (Encoding(x) == "UTF-8") {
TRUE
} else if (Encoding(x) == "unknown") {
# Characters encoded as "unknown" must be ASCII only, and remain "unknown"
# after attempting to assign an encoding. From ?Encoding :
# > ASCII strings will never be marked with a declared encoding, since their
# > representation is the same in all supported encodings.
Encoding(x) <- "UTF-8"
Encoding(x) == "unknown"
} else {
FALSE
}
}
coercible_to_date <- function(x) {
x_date <- try_silent(as.Date(x))
!is.null(x_date) && all(is.na(x) == is.na(x_date))
}
is_roughly_current_date <- function(x) {
coercible_to_date(x) && (abs(Sys.Date() - as.Date(x)) <= 1)
}
coercible_to_time <- function(x) {
x_hms <- try_silent(hms::as_hms(x))
!is.null(x_hms) && all(is.na(x) == is.na(x_hms))
}
as_timestamp_equals_to <- function(x) {
map(x, function(xx) {
function(value) as.POSIXct(value) == xx
})
}
is_date <- function(x) {
inherits(x, "Date")
}
is_roughly_current_date_typed <- function(x) {
is_date(x) && (abs(Sys.Date() - x) <= 1)
}
is_timestamp <- function(x) {
inherits(x, "POSIXct")
}
is_roughly_current_timestamp_typed <- function(x) {
is_timestamp(x) && (Sys.time() - x <= hms::hms(2))
}
as_numeric_date <- function(d) {
d <- as.Date(d)
structure(as.numeric(unclass(d)), class = class(d))
}
|