From e931662787b7c176b82e4a4ad9ae42efbefdeccc Mon Sep 17 00:00:00 2001 From: Jeremy Tay Date: Wed, 2 Oct 2019 14:25:55 +1000 Subject: [PATCH] RS-3782: Added unit tests and touch ups --- NAMESPACE | 2 +- R/DataMart.R | 13 +-- man/{close.qpostconn.Rd => close.qpostcon.Rd} | 10 +- tests/testthat/helper-datamart.R | 1 + tests/testthat/test-datamart.R | 92 +++++++++++++++++++ 5 files changed, 107 insertions(+), 11 deletions(-) rename man/{close.qpostconn.Rd => close.qpostcon.Rd} (65%) create mode 100644 tests/testthat/helper-datamart.R create mode 100644 tests/testthat/test-datamart.R diff --git a/NAMESPACE b/NAMESPACE index 49f22b9..4bd89b5 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,6 +1,6 @@ # Generated by roxygen2: do not edit by hand -S3method(close,qpostconn) +S3method(close,qpostcon) export(CheckDropboxToken) export(DownloadXLSX) export(ExportToDropbox) diff --git a/R/DataMart.R b/R/DataMart.R index 39446f3..e6ca4e1 100644 --- a/R/DataMart.R +++ b/R/DataMart.R @@ -85,7 +85,7 @@ QFileOpen <- function(filename, open = "r", blocking = TRUE, tmpfile <- paste0(tempfile(), ".", file_ext(filename)) conn <- file(tmpfile, mode, blocking, encoding, raw, method) - class(conn) = append("qpostconn", class(conn)) + class(conn) = append("qpostcon", class(conn)) # store attributes for later access attr(conn, "tmpfile") <- tmpfile @@ -103,7 +103,8 @@ QFileOpen <- function(filename, open = "r", blocking = TRUE, #' This is an overload for close.connection which writes the file contents #' of a connection opened using QFileOpen to the Data Mart. #' -#' @param conn connection object of class 'qpostconn'. Connection opened with QFileOpen +#' @param con connection object of class 'qpostconn'. Connection opened with QFileOpen +#' @param ... arguments passed to or from other methods. #' #' @importFrom httr POST add_headers upload_file #' @importFrom mime guess_type @@ -113,11 +114,11 @@ QFileOpen <- function(filename, open = "r", blocking = TRUE, #' and assumed to succeed if no errors are thrown. #' #' @export -close.qpostconn = function(conn, ...) +close.qpostcon = function(con, ...) { - close.connection(conn, ...) - filename <- attr(conn, "filename") - tmpfile <- attr(conn, "tmpfile") + close.connection(con, ...) + filename <- attr(con, "filename") + tmpfile <- attr(con, "tmpfile") on.exit(if(file.exists(tmpfile)) file.remove(tmpfile)) companySecret <- get0("companySecret", ifnotfound = "") diff --git a/man/close.qpostconn.Rd b/man/close.qpostcon.Rd similarity index 65% rename from man/close.qpostconn.Rd rename to man/close.qpostcon.Rd index 4bca9a6..b13c028 100644 --- a/man/close.qpostconn.Rd +++ b/man/close.qpostcon.Rd @@ -1,13 +1,15 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/DataMart.R -\name{close.qpostconn} -\alias{close.qpostconn} +\name{close.qpostcon} +\alias{close.qpostcon} \title{Closes a QFileOpen connection} \usage{ -\method{close}{qpostconn}(conn, ...) +\method{close}{qpostcon}(con, ...) } \arguments{ -\item{conn}{connection object of class 'qpostconn'. Connection opened with QFileOpen} +\item{con}{connection object of class 'qpostconn'. Connection opened with QFileOpen} + +\item{...}{arguments passed to or from other methods.} } \value{ NULL invisibly. Called for the purpose of uploading data diff --git a/tests/testthat/helper-datamart.R b/tests/testthat/helper-datamart.R new file mode 100644 index 0000000..991bf96 --- /dev/null +++ b/tests/testthat/helper-datamart.R @@ -0,0 +1 @@ +companySecret <- get0("companySecret", ifnotfound = Sys.getenv("companySecret")) \ No newline at end of file diff --git a/tests/testthat/test-datamart.R b/tests/testthat/test-datamart.R new file mode 100644 index 0000000..5ced0d9 --- /dev/null +++ b/tests/testthat/test-datamart.R @@ -0,0 +1,92 @@ +library (testthat) + +test_that("SaveData/LoadData", { + skip_if(!nzchar(companySecret), "Not in test environment or no company set up") + + # RDS + expect_invisible(QSaveData(mtcars, "mtcars.rds")) + expect_true(QFileExists("mtcars.rds")) + + rds <- QLoadData("mtcars.rds") + expect_equivalent(mtcars, rds) +}) + +test_that("Save/Load Data: bad cases", { + skip_if(!nzchar(companySecret), "Not in test environment or no company set up") + + # Bad cases + bad_name <- "anamethatdoesnotexistfortesting" + expect_warning(expect_false(QFileExists(bad_name))) + expect_error(QLoadData(bad_name)) + expect_error(QSaveData(mtcars,"mtcars.notrds")) +}) + +test_that("File Connection: raw", { + skip_if(!nzchar(companySecret), "Not in test environment or no company set up") + + # Test various file formats + # raw file + filename <- "raw_test" + expect_silent(conn <- QFileOpen(filename, "w")) + txt_string <- "This is a test line." + + writeLines(txt_string, conn) + expect_message(expect_invisible(close(conn))) + + expect_silent(conn <- QFileOpen(filename)) + expect_silent(read_lines <- readLines(conn, warn = FALSE)) + expect_equal(txt_string, read_lines) + + expect_silent(expect_invisible(close(conn))) +}) + +test_that("File Connection: rds", { + skip_if(!nzchar(companySecret), "Not in test environment or no company set up") + + # csv file + filename <- "test.rds" + + expect_silent(conn <- QFileOpen(filename, "w")) + + expect_silent(saveRDS(mtcars, conn, ascii = TRUE)) + expect_message(expect_invisible(close(conn))) + + expect_silent(conn <- QFileOpen(filename)) + expect_silent(csv <- readRDS(gzcon(conn))) + + expect_silent(expect_invisible(close(conn))) +}) + +test_that("File Connection: csv", { + skip_if(!nzchar(companySecret), "Not in test environment or no company set up") + + # csv file + filename <- "test.csv" + + expect_silent(conn <- QFileOpen(filename, "w")) + + expect_silent(write.csv(mtcars, conn)) + expect_message(expect_invisible(close(conn))) + + expect_silent(conn <- QFileOpen(filename)) + expect_silent(csv <- read.csv(conn)) + + expect_silent(expect_invisible(close(conn))) +}) + +test_that("File Connection: json", { + skip_if(!nzchar(companySecret), "Not in test environment or no company set up") + # json file + filename <- "test.json" + + expect_silent(conn <- QFileOpen(filename, "w")) + + expect_silent(writeLines(jsonlite::toJSON(mtcars), conn)) + expect_message(expect_invisible(close(conn))) + + expect_silent(conn <- QFileOpen(filename)) + expect_silent(json <- jsonlite::fromJSON(readLines(conn, warn = FALSE))) + expect_equivalent(json, mtcars) + + expect_silent(expect_invisible(close(conn))) +})