Skip to content

Commit

Permalink
RS-3782: Added unit tests and touch ups
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremytay90 committed Oct 2, 2019
1 parent 22c5547 commit e931662
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 11 deletions.
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Generated by roxygen2: do not edit by hand

S3method(close,qpostconn)
S3method(close,qpostcon)
export(CheckDropboxToken)
export(DownloadXLSX)
export(ExportToDropbox)
Expand Down
13 changes: 7 additions & 6 deletions R/DataMart.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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 = "")
Expand Down
10 changes: 6 additions & 4 deletions man/close.qpostconn.Rd → man/close.qpostcon.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/helper-datamart.R
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
companySecret <- get0("companySecret", ifnotfound = Sys.getenv("companySecret"))
92 changes: 92 additions & 0 deletions tests/testthat/test-datamart.R
Original file line number Diff line number Diff line change
@@ -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)))
})

0 comments on commit e931662

Please sign in to comment.