@@ -17,13 +17,6 @@ poolClose(pool)
the objects that the pool will hold (ex: for DBI database connections,
this function is \code{dbConnect}). It must take no arguments.}

%\item{...}{Additional arguments needed by the \code{factory}
%function. Depending on your \code{factory}, these may or may
%not exist. For example, if \code{factory} is \code{dbConnect},
%you need to specify the database driver and possibly other
%arguments such as \code{user}, \code{password}, \code{host}, etc.
%For more info, see \code{\link{dbPool}}.}

\item{minSize}{An optional number specifying the minimum
number of objects that the pool should have at all times.}

@@ -60,22 +53,3 @@ object pooling applied to DBI database connections.
\seealso{\code{\link{dbPool}},
\code{\link{poolCheckout}}, \code{\link{poolReturn}}}
\keyword{datasets}
%\examples{
%pool <- poolCreate(dbConnect,
% drv = RMySQL::MySQL(),
% dbname = "shinydemo",
% host = "shiny-demo.csa7qlmguqrf.us-east-1.rds.amazonaws.com",
% username = "guest",
% password = "guest"
%)
%
%dbGetQuery(pool, "SELECT * from City LIMIT 5;")
%#> ID Name CountryCode District Population
%#> 1 1 Kabul AFG Kabol 1780000
%#> 2 2 Qandahar AFG Qandahar 237500
%#> 3 3 Herat AFG Herat 186800
%#> 4 4 Mazar-e-Sharif AFG Balkh 127800
%#> 5 5 Amsterdam NLD Noord-Holland 731200
%
%poolClose()
%}
@@ -4,86 +4,89 @@ context("dplyr compatibility")

describe("pool package", {

pool <- dbPool(RSQLite::SQLite(), dbname = ":memory:")
if (requireNamespace("RSQLite", quietly = TRUE)) {

it("can create local SQLite pool", {
expect_equal(class(pool), c("Pool", "R6"))
info <- list(
class = "Pool",
valid = TRUE,
minSize = 1,
maxSize = Inf,
idleTimeout = 60,
pooledObjectClass = "SQLiteConnection",
numberFreeObjects = 0,
numberTakenObjects = 1
)
expect_equal(dbGetInfo(pool), info)
checkCounts(pool, free = 1, taken = 0)
})
pool <- dbPool(RSQLite::SQLite(), dbname = ":memory:")

it("can use dplyr syntax to copy table to DB", {
checkCounts(pool, free = 1, taken = 0)
copy_to(pool, flights, "flights",
temporary = FALSE,
indexes = list(
c("year", "month", "day"),
"carrier",
"tailnum",
"dest"
it("can create local SQLite pool", {
expect_equal(class(pool), c("Pool", "R6"))
info <- list(
class = "Pool",
valid = TRUE,
minSize = 1,
maxSize = Inf,
idleTimeout = 60,
pooledObjectClass = "SQLiteConnection",
numberFreeObjects = 0,
numberTakenObjects = 1
)
)
checkCounts(pool, free = 1, taken = 0)
expect_true(db_has_table(pool, "flights"))
})
expect_equal(dbGetInfo(pool), info)
checkCounts(pool, free = 1, taken = 0)
})

it("can use dplyr syntax to get a table from DB", {
checkCounts(pool, free = 1, taken = 0)
flights_db <- tbl(pool, "flights")
checkCounts(pool, free = 1, taken = 0)
expect_equal(class(flights_db), c("tbl_dbi", "tbl_sql", "tbl_lazy", "tbl"))
})
it("can use dplyr syntax to copy table to DB", {
checkCounts(pool, free = 1, taken = 0)
copy_to(pool, flights, "flights",
temporary = FALSE,
indexes = list(
c("year", "month", "day"),
"carrier",
"tailnum",
"dest"
)
)
checkCounts(pool, free = 1, taken = 0)
expect_true(db_has_table(pool, "flights"))
})

it("can use dplyr syntax to get a table from DB", {
checkCounts(pool, free = 1, taken = 0)
flights_db <- tbl(pool, "flights")
checkCounts(pool, free = 1, taken = 0)
expect_equal(class(flights_db), c("tbl_dbi", "tbl_sql", "tbl_lazy", "tbl"))
})

it("can use dplyr syntax to select", {
checkCounts(pool, free = 1, taken = 0)
flights_db <- tbl(pool, "flights")
s <- dplyr::select(flights_db, year:day, dep_delay, arr_delay)
expect_equal(tibble::as_tibble(s),
tibble::tibble(
year = rep(2013L, 10),
month = rep(1L, 10),
day = rep(1L, 10),
dep_delay = c(2, 4, 2, -1, -6, -4, -5, -3, -3, -2),
arr_delay = c(11, 20, 33, -18, -25, 12, 19, -14, -8, 8)
it("can use dplyr syntax to select", {
checkCounts(pool, free = 1, taken = 0)
flights_db <- tbl(pool, "flights")
s <- dplyr::select(flights_db, year:day, dep_delay, arr_delay)
expect_equal(tibble::as_tibble(s),
tibble::tibble(
year = rep(2013L, 10),
month = rep(1L, 10),
day = rep(1L, 10),
dep_delay = c(2, 4, 2, -1, -6, -4, -5, -3, -3, -2),
arr_delay = c(11, 20, 33, -18, -25, 12, 19, -14, -8, 8)
)
)
)
checkCounts(pool, free = 1, taken = 0)
})
checkCounts(pool, free = 1, taken = 0)
})

it("can use dplyr syntax to filter", {
checkCounts(pool, free = 1, taken = 0)
flights_db <- tbl(pool, "flights")
f <- dplyr::filter(flights_db, dep_delay > 0)
ft <- tibble::as_tibble(f)
expect_equal(ft$dep_time, c(517, 533, 542))
expect_equal(ft$arr_time, c(830, 850, 923))
checkCounts(pool, free = 1, taken = 0)
})
it("can use dplyr syntax to filter", {
checkCounts(pool, free = 1, taken = 0)
flights_db <- tbl(pool, "flights")
f <- dplyr::filter(flights_db, dep_delay > 0)
ft <- tibble::as_tibble(f)
expect_equal(ft$dep_time, c(517, 533, 542))
expect_equal(ft$arr_time, c(830, 850, 923))
checkCounts(pool, free = 1, taken = 0)
})

it("can use dplyr syntax to `collect`", {
checkCounts(pool, free = 1, taken = 0)
flights_db <- tbl(pool, "flights")
c <- dplyr::collect(flights_db)
expect_equal(c, flights)
expect_equal(nrow(c), 10)
checkCounts(pool, free = 1, taken = 0)
})
it("can use dplyr syntax to `collect`", {
checkCounts(pool, free = 1, taken = 0)
flights_db <- tbl(pool, "flights")
c <- dplyr::collect(flights_db)
expect_equal(c, flights)
expect_equal(nrow(c), 10)
checkCounts(pool, free = 1, taken = 0)
})

it("throws error when `temporary = TRUE`", {
expect_error(copy_to(pool, flights, "temp"),
"You cannot use `temporary = TRUE`"
)
})
it("throws error when `temporary = TRUE`", {
expect_error(copy_to(pool, flights, "temp"),
"You cannot use `temporary = TRUE`"
)
})

poolClose(pool)
poolClose(pool)
}
})