From addad44452e96b68e833d71dd9b8d317755c5226 Mon Sep 17 00:00:00 2001 From: Ian Cook Date: Thu, 25 May 2017 16:52:07 -0400 Subject: [PATCH] Add testing setup --- DESCRIPTION | 3 ++- tests/testthat.R | 4 ++++ tests/testthat/helper-src.R | 40 +++++++++++++++++++++++++++++++++ tests/testthat/test-basic.R | 9 ++++++++ tests/testthat/zzz-disconnect.R | 1 + 5 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 tests/testthat.R create mode 100644 tests/testthat/helper-src.R create mode 100644 tests/testthat/test-basic.R create mode 100644 tests/testthat/zzz-disconnect.R diff --git a/DESCRIPTION b/DESCRIPTION index 5f50709..631c25e 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -27,7 +27,8 @@ Imports: Suggests: odbc, RJDBC, - nycflights13 + nycflights13, + testthat SystemRequirements: Impala driver to support a 'DBI'-compatible R interface NeedsCompilation: no License: Apache License 2.0 | file LICENSE diff --git a/tests/testthat.R b/tests/testthat.R new file mode 100644 index 0000000..74137fa --- /dev/null +++ b/tests/testthat.R @@ -0,0 +1,4 @@ +library(testthat) +library(implyr) + +test_check("implyr") diff --git a/tests/testthat/helper-src.R b/tests/testthat/helper-src.R new file mode 100644 index 0000000..01e757f --- /dev/null +++ b/tests/testthat/helper-src.R @@ -0,0 +1,40 @@ +library(RJDBC) +library(nycflights13) + +java_home <- Sys.getenv("JAVA_HOME", unset = NA) +jdbc_url <- Sys.getenv("IMPLYR_TEST_JDBC", unset = NA) +jdbc_host <- Sys.getenv("IMPLYR_TEST_HOST", unset = NA) +jdbc_port <- Sys.getenv("IMPLYR_TEST_PORT", unset = NA) +jdbc_user <- Sys.getenv("IMPLYR_TEST_USER", unset = NA) +jdbc_pass <- Sys.getenv("IMPLYR_TEST_PASS", unset = NA) + +run_tests <- FALSE +if (!is.na(java_home) & !is.na(jdbc_url) & !is.na(jdbc_host) & + !is.na(jdbc_port) & !is.na(jdbc_user) & !is.na(jdbc_pass)) { + jdbc_path <- tempdir() + jdbc_zip <- file.path(jdbc_path, basename(jdbc_url)) + unlink(jdbc_zip) + download.file(jdbc_url, jdbc_zip) + jdbc_jars <- tools::file_path_sans_ext(jdbc_zip) + unlink(jdbc_jars, recursive = TRUE) + dir.create(jdbc_jars) + unzip(jdbc_zip, exdir = jdbc_jars) + impala_classpath <- list.files(path = jdbc_jars, pattern = "\\.jar$", full.names = TRUE) + rJava::.jinit(classpath = impala_classpath) + drv <- RJDBC::JDBC("com.cloudera.impala.jdbc41.Driver", impala_classpath, "`") + jdbc_conn_str <- paste0("jdbc:impala://", jdbc_host, ":", jdbc_port) + impala <- implyr::src_impala(drv, jdbc_conn_str, jdbc_user, jdbc_pass) + run_tests <- TRUE +} + +check_impala <- function() { + if (!run_tests) { + skip("This environment not configured for tests") + } +} + +disconnect <- function() { + if (run_tests) { + dbDisconnect(impala) + } +} diff --git a/tests/testthat/test-basic.R b/tests/testthat/test-basic.R new file mode 100644 index 0000000..2497d3b --- /dev/null +++ b/tests/testthat/test-basic.R @@ -0,0 +1,9 @@ +context("basic tests") + +test_that("tbl_impala matches tbl_df", { + check_impala() + test_op <- function(x) { + x %>% arrange(carrier) %>% collect() + } + compare_tbls(list(tbl(impala, "airlines"), nycflights13::airlines), op = test_op) +}) diff --git a/tests/testthat/zzz-disconnect.R b/tests/testthat/zzz-disconnect.R new file mode 100644 index 0000000..958bc78 --- /dev/null +++ b/tests/testthat/zzz-disconnect.R @@ -0,0 +1 @@ +disconnect()