forked from tidyverse/dplyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata-temp.r
72 lines (62 loc) · 1.59 KB
/
data-temp.r
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
#' Infrastructure for testing dplyr
#'
#' Register testing sources, then use \code{test_load} to load an existing
#' data frame into each source. To create a new table in each source,
#' use \code{test_frame}.
#'
#' @keywords internal
#' @examples
#' \dontrun{
#' test_register_src("df", src_df(env = new.env()))
#' test_register_src("sqlite", src_sqlite(":memory:", create = TRUE))
#'
#' test_frame(x = 1:3, y = 3:1)
#' test_load(mtcars)
#' }
#' @name testing
NULL
#' @export
#' @rdname testing
test_register_src <- function(name, src) {
message("Registering testing src: ", name, " ", appendLF = FALSE)
tryCatch(
{
test_srcs$add(name, src)
message("OK")
},
error = function(e) message(conditionMessage(e))
)
}
#' @export
#' @rdname testing
test_load <- function(df, name = random_table_name(), srcs = test_srcs$get(),
ignore = character()) {
stopifnot(is.data.frame(df))
stopifnot(is.character(ignore))
srcs <- srcs[setdiff(names(srcs), ignore)]
lapply(srcs, copy_to, df, name = name)
}
#' @export
#' @rdname testing
test_frame <- function(..., srcs = test_srcs$get(), ignore = character()) {
df <- data_frame(...)
test_load(df, srcs = srcs, ignore = ignore)
}
# Manage cache of testing srcs
test_srcs <- local({
e <- new.env(parent = emptyenv())
e$srcs <- list()
list(
get = function() e$srcs,
has = function(x) x %in% names(e$srcs),
add = function(name, src) {
stopifnot(is.src(src))
e$srcs[[name]] <- src
},
set = function(...) {
old <- e$srcs
e$srcs <- list(...)
invisible(old)
}
)
})