diff --git a/R/src_h2.r b/R/src_h2.r index 19a99d8..71aca8e 100644 --- a/R/src_h2.r +++ b/R/src_h2.r @@ -18,16 +18,16 @@ NULL #' @export src_h2 <- function(x) UseMethod("src_h2") -#' @rdname src_h2 #' @export +#' @rdname src_h2 src_h2.character <- function(x, ...) { assert_that(requireNamespace("dbj.h2", quietly = TRUE)) con <- DBI::dbConnect(dbj.h2::driver(), x, ...) src_h2(con, ...) } -#' @rdname src_h2 #' @export +#' @rdname src_h2 src_h2.H2Connection <- function(x, ...) { assert_that(requireNamespace("dbj.h2", quietly = TRUE)) info <- DBI::dbGetInfo(x) @@ -41,18 +41,23 @@ tbl.src_h2 <- function(src, from, ...) { } #' @export +#' @rdname src_h2 src_desc.src_h2 <- function(x) { - info <- x$info - - sprintf("H2 %s [%s@%s]", x$database_product_version, x$user_name, x$url) + paste0(x$info$dbname, x$info$db.version, " [", x$info$url, "]") } #' @export +#' @rdname src_h2 src_translate_env.src_h2 <- function(x) { sql_variant( base_scalar, sql_translator(.parent = base_agg, - n = function(arg="*") { build_sql("COUNT(", ident(arg), ")") } + n = function(arg="*") { build_sql("COUNT(", ifelse(arg == "*", arg, ident(arg)), ")") }, + n_distinct = function(x) { + build_sql("COUNT(DISTINCT ", ident(x), ")") + }, + sd = sql_prefix("stddev_samp"), + var = sql_prefix("var_samp") ) ) } diff --git a/man/src_h2.Rd b/man/src_h2.Rd index 39e7dc0..90ec8a3 100644 --- a/man/src_h2.Rd +++ b/man/src_h2.Rd @@ -1,9 +1,11 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/src_h2.r \name{src_h2} +\alias{src_desc.src_h2} \alias{src_h2} \alias{src_h2.H2Connection} \alias{src_h2.character} +\alias{src_translate_env.src_h2} \alias{tbl.src_h2} \title{Connect to a H2 database.} \usage{ @@ -14,6 +16,10 @@ src_h2(x) \method{src_h2}{H2Connection}(x, ...) \method{tbl}{src_h2}(src, from, ...) + +\method{src_desc}{src_h2}(x) + +\method{src_translate_env}{src_h2}(x) } \arguments{ \item{x}{an URL for the H2 database connection as defined at \url{http://h2database.com/html/features.html#database_url} diff --git a/tests/testthat.R b/tests/testthat.R index fa5b3c9..0ce48db 100644 --- a/tests/testthat.R +++ b/tests/testthat.R @@ -1,3 +1,4 @@ library(testthat) +library(dplyr.src.h2) test_check("dplyr.src.h2") diff --git a/tests/testthat/test-integration-data-nycflights13.r b/tests/testthat/test-integration-data-nycflights13.r index ff21ef7..d56efa5 100644 --- a/tests/testthat/test-integration-data-nycflights13.r +++ b/tests/testthat/test-integration-data-nycflights13.r @@ -1,12 +1,71 @@ context("data-nycflights13") +library(nycflights13) + test_that("nycflights13_h2 has flights table", { # when - library(nycflights13) flights_db <- tbl(nycflights13_h2(), "flights") # then - expect_that(flights_db, is_a("tbl_h2")) + expect_is(flights_db, "tbl_h2") expect_equal(nrow(flights_db), 336776) expect_equal(ncol(flights_db), 16) }) + +test_that("filter", { + flights_db <- tbl(nycflights13_h2(), "flights") + + filtered <- filter(flights_db, month == 1, day == 1) + + expect_is(filtered, "tbl_h2") +}) + +test_that("arrange", { + flights_db <- tbl(nycflights13_h2(), "flights") + + arranged <- arrange(flights_db, year, month, day) + + expect_is(arranged, "tbl_h2") +}) + +test_that("select", { + flights_db <- tbl(nycflights13_h2(), "flights") + + selected <- select(flights_db, year, month, day) + + expect_is(selected, "tbl_h2") +}) + +test_that("distinct", { + flights_db <- tbl(nycflights13_h2(), "flights") + + distinct_tailnum <- distinct(select(flights_db, tailnum)) + + expect_is(distinct_tailnum, "tbl_h2") +}) + +test_that("group_by", { + flights_db <- tbl(nycflights13_h2(), "flights") + + grouped <- group_by(flights_db, tailnum) + + expect_is(grouped, "tbl_h2") +}) + +test_that("summarize", { + flights_db <- tbl(nycflights13_h2(), "flights") + + summarized <- summarise(flights_db, + count = n(), + distinct = n_distinct("flight"), + dist = mean(distance), + delay = mean(arr_delay), + min_delay = min(arr_delay), + max_delay = max(arr_delay), + sum_delay = sum(arr_delay), + sd_delay = sd(arr_delay) + ) + + expect_is(summarized, "tbl_h2") +}) +