forked from tidyverse/dplyr
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
228 additions
and
225 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,64 @@ | ||
#' Connect to temporary data sources. | ||
#' Infrastructure for testing dplyr | ||
#' | ||
#' These functions make it easy to take a local data frame and make available | ||
#' as a tbl in every known src. All local srcs will work on any computer. | ||
#' DBMS srcs will only currently work on Hadley's computer. | ||
#' 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 | ||
#' @export | ||
#' @examples | ||
#' \dontrun{ | ||
#' local <- c("df", "dt") | ||
#' db <- c("sqlite", "mysql", "postgres") | ||
#' test_register_src("df", src_df(env = new.env())) | ||
#' test_register_src("sqlite", src_sqlite(":memory:", create = TRUE)) | ||
#' | ||
#' temp_srcs(local) | ||
#' temp_srcs(db) | ||
#' test_frame(x = 1:3, y = 3:1) | ||
#' test_load(mtcars) | ||
#' } | ||
temp_srcs <- function(..., quiet = NULL) { | ||
load_srcs(temp_src, c(...), quiet = quiet) | ||
} | ||
|
||
temp_src <- function(type, ...) { | ||
cache_name <- paste("temp", type, "src", collapse = "-") | ||
if (is_cached(cache_name)) return(get_cache(cache_name)) | ||
#' @name testing | ||
NULL | ||
|
||
env <- new.env(parent = emptyenv()) | ||
src <- switch(type, | ||
df = src_df(env = env), | ||
dt = dtplyr::src_dt(env = env), | ||
sqlite = src_sqlite(tempfile(), create = TRUE), | ||
mysql = src_mysql("test", ...), | ||
postgres = src_postgres("test", ...), | ||
stop("Unknown src type ", type, call. = FALSE) | ||
) | ||
|
||
set_cache(cache_name, src) | ||
#' @export | ||
#' @rdname testing | ||
test_register_src <- function(name, src) { | ||
message("Registering testing src: ", name) | ||
test_srcs$add(name, src) | ||
} | ||
|
||
reset <- function(x) UseMethod("reset") | ||
#' @export | ||
reset.default <- function(x) NULL | ||
#' @export | ||
reset.src_sql <- function(x) { | ||
for (tbl in src_tbls(x)) { | ||
dbRemoveTable(x$con, tbl) | ||
} | ||
#' @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 | ||
reset.list <- function(x) { | ||
for (y in x) reset(y) | ||
#' @rdname testing | ||
test_frame <- function(..., srcs = test_srcs$get()) { | ||
df <- data_frame(...) | ||
test_load(df, srcs = srcs) | ||
} | ||
|
||
#' @rdname temp_srcs | ||
temp_load <- function(srcs, df, name = NULL) { | ||
if (is.character(srcs)) { | ||
srcs <- temp_srcs(srcs) | ||
} | ||
# Manage cache of testing srcs | ||
test_srcs <- local({ | ||
e <- new.env(parent = emptyenv()) | ||
e$srcs <- list() | ||
|
||
if (is.data.frame(df)) { | ||
if (is.null(name)) name <- random_table_name() | ||
lapply(srcs, copy_to, df, name = name) | ||
} else { | ||
if (is.null(name)) { | ||
name <- replicate(length(df), random_table_name()) | ||
} else { | ||
stopifnot(length(name) == length(df)) | ||
} | ||
list( | ||
get = function() e$srcs, | ||
|
||
lapply(srcs, function(x) { | ||
Map(function(df, name) copy_to(x, df, name), df, name) | ||
}) | ||
} | ||
} | ||
add = function(name, src) { | ||
stopifnot(is.src(src)) | ||
e$srcs[[name]] <- src | ||
}, | ||
|
||
set = function(...) { | ||
old <- e$srcs | ||
e$srcs <- list(...) | ||
invisible(old) | ||
} | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
library("testthat") | ||
library("dplyr") | ||
library(testthat) | ||
library(dplyr) | ||
|
||
test_check("dplyr") |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
test_register_src("df", src_df(env = new.env(parent = emptyenv()))) | ||
test_register_src("sqlite", src_sqlite(":memory:", create = TRUE)) | ||
|
||
if (identical(Sys.info()[["user"]], "hadley")) { | ||
test_register_src("postgres", src_postgres("test")) | ||
} else if (identical(Sys.getenv("TRAVIS"), "true")) { | ||
test_register_src("postgres", src_postgres("test", user = "travis", password = "")) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,26 @@ | ||
context("Equivalence (manip)") | ||
|
||
df <- data.frame(x = 5:1, y = 1:5) | ||
|
||
srcs <- temp_srcs(c("df", "sqlite", "postgres")) | ||
tbls <- temp_load(srcs, df) | ||
|
||
test_that("mutate happens before summarise", { | ||
# FIXME: only needed because postgresql returns integer for sum | ||
compare_tbls(tbls, function(x) { | ||
mutate(x, z = x + y) %>% summarise(sum_z = sum(z)) | ||
}, compare = equal_data_frame, convert = TRUE) | ||
test_f <- function(tbl) { | ||
res <- tbl %>% | ||
mutate(x, z = x + y) %>% | ||
summarise(sum_z = sum(z)) %>% | ||
collect() | ||
expect_equal(res$sum_z, 30) | ||
} | ||
|
||
test_frame(x = 5:1, y = 1:5) %>% lapply(test_f) | ||
}) | ||
|
||
test_that("select operates on mutated vars", { | ||
compare_tbls(tbls, function(x) mutate(x, z = x + y) %>% select(z)) | ||
test_f <- function(tbl) { | ||
res <- tbl %>% | ||
mutate(x, z = x + y) %>% | ||
select(z) %>% | ||
collect() | ||
expect_equal(res$z, rep(4, 3)) | ||
} | ||
|
||
test_frame(x = 1:3, y = 3:1) %>% lapply(test_f) | ||
}) |
Oops, something went wrong.