Skip to content

Commit

Permalink
Fixed aggregate functions, added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hoesler committed Mar 10, 2016
1 parent 5ba2aeb commit 0a60d07
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 8 deletions.
17 changes: 11 additions & 6 deletions R/src_h2.r
Expand Up @@ -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)
Expand All @@ -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")
)
)
}
Expand Down
6 changes: 6 additions & 0 deletions man/src_h2.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tests/testthat.R
@@ -1,3 +1,4 @@
library(testthat)
library(dplyr.src.h2)

test_check("dplyr.src.h2")
63 changes: 61 additions & 2 deletions 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")
})

0 comments on commit 0a60d07

Please sign in to comment.