-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fix #380 In progress * Update CSV tests * Update csvy and csv_gz tests * Up to fwf * Up to qs * Almost there * All done Also fixed a leaky test case in `test_set_class.R` * Update NEWS [no ci]
- Loading branch information
1 parent
eb244d1
commit 4a60f11
Showing
49 changed files
with
832 additions
and
874 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,31 @@ | ||
context("Convert") | ||
library("datasets") | ||
|
||
export(mtcars, "mtcars.dta") | ||
|
||
test_that("Basic file conversion", { | ||
convert("mtcars.dta", "mtcars.csv") | ||
convert("mtcars.csv", "mtcars.dta") | ||
x <- import("mtcars.dta") | ||
expect_true(identical(names(mtcars), names(x))) | ||
expect_true(identical(dim(mtcars), dim(x))) | ||
unlink("mtcars.csv") | ||
withr::with_tempfile("mtcars_files", fileext = c(".dta", ".csv"), code = { | ||
export(mtcars, mtcars_files[1]) | ||
convert(mtcars_files[1], mtcars_files[2]) | ||
convert(mtcars_files[2], mtcars_files[1]) | ||
x <- import(mtcars_files[1]) | ||
expect_true(identical(names(mtcars), names(x))) | ||
expect_true(identical(dim(mtcars), dim(x))) | ||
}) | ||
}) | ||
|
||
test_that("File conversion with arguments", { | ||
export(mtcars, "mtcars.csv", format = "tsv") | ||
convert("mtcars.csv", "mtcars.csv", in_opts = list(format = "tsv")) | ||
expect_true("mtcars.csv" %in% dir()) | ||
expect_true(!("mtcars.tsv" %in% dir())) | ||
convert("mtcars.csv", "mtcars.tsv", | ||
in_opts = list(format = "tsv"), out_opts = list(format = "csv")) | ||
expect_true("mtcars.tsv" %in% dir()) | ||
unlink("mtcars.csv") | ||
unlink("mtcars.tsv") | ||
withr::with_tempfile("mtcars_files", fileext = c(".csv", ".tsv"), code = { | ||
export(mtcars, mtcars_files[1], format = "tsv") | ||
convert(mtcars_files[1], mtcars_files[1], in_opts = list(format = "tsv")) | ||
expect_true(file.exists(mtcars_files[1])) | ||
expect_true(!file.exists(mtcars_files[2])) | ||
convert(mtcars_files[1], mtcars_files[2], | ||
in_opts = list(format = "tsv"), out_opts = list(format = "csv")) | ||
expect_true(file.exists(mtcars_files[2])) | ||
}) | ||
}) | ||
|
||
test_that("File conversion w/o out_file errors", { | ||
expect_error(convert("mtcars.dta")) | ||
withr::with_tempfile("mtcars_file", fileext = ".dta", { | ||
export(mtcars, mtcars_file) | ||
expect_error(convert(mtcars_file)) | ||
}) | ||
}) | ||
|
||
unlink("mtcars.dta") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,27 @@ | ||
test_that("export to nonexisting directory #347", { | ||
output_dir <- file.path(tempdir(), "this_doesnt_exist") | ||
expect_false(dir.exists(output_dir)) | ||
expect_error(export(iris, file.path(output_dir, "iris.csv")), NA) | ||
expect_true(file.exists(file.path(output_dir, "iris.csv"))) | ||
unlink(output_dir, recursive = TRUE) | ||
withr::with_tempdir({ | ||
output_dir <- file.path(".", "this_doesnt_exist") | ||
expect_false(dir.exists(output_dir)) | ||
expect_error(export(iris, file.path(output_dir, "iris.csv")), NA) | ||
expect_true(file.exists(file.path(output_dir, "iris.csv"))) | ||
}) | ||
}) | ||
|
||
test_that("export to existing directory (contra previous one) #347", { | ||
output_dir <- file.path(tempdir(), "this_surely_exists1") | ||
dir.create(output_dir, recursive = TRUE) | ||
expect_true(dir.exists(output_dir)) | ||
expect_error(export(iris, file.path(output_dir, "iris.csv")), NA) | ||
expect_true(file.exists(file.path(output_dir, "iris.csv"))) | ||
unlink(output_dir, recursive = TRUE) | ||
withr::with_tempdir({ | ||
output_dir <- file.path(".", "this_surely_exist1") | ||
dir.create(output_dir, recursive = TRUE) ## right? | ||
expect_true(dir.exists(output_dir)) | ||
expect_error(export(iris, file.path(output_dir, "iris.csv")), NA) | ||
expect_true(file.exists(file.path(output_dir, "iris.csv"))) | ||
}) | ||
}) | ||
|
||
test_that("export to nonexisting directory also for compressed file #347", { | ||
output_dir <- file.path(tempdir(), "this_doesnt_exist") | ||
expect_false(dir.exists(output_dir)) | ||
expect_error(export(iris, file.path(output_dir, "iris.csv.gz")), NA) | ||
expect_true(file.exists(file.path(output_dir, "iris.csv.gz"))) | ||
unlink(output_dir, recursive = TRUE) | ||
withr::with_tempdir({ | ||
output_dir <- file.path(".", "this_doesnt_exist") | ||
expect_false(dir.exists(output_dir)) | ||
expect_error(export(iris, file.path(output_dir, "iris.csv.gz")), NA) | ||
expect_true(file.exists(file.path(output_dir, "iris.csv.gz"))) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,51 @@ | ||
context("Test export_list()") | ||
library("datasets") | ||
|
||
export(list( | ||
mtcars3 = mtcars[1:10, ], | ||
mtcars2 = mtcars[11:20, ], | ||
mtcars1 = mtcars[21:32, ] | ||
), "mtcars.xlsx") | ||
mylist <- import_list("mtcars.xlsx") | ||
|
||
test_that("export_list() works", { | ||
expect_error(export_list(mtcars), label = "export_list() fails on exporting single data frame") | ||
expect_error(export_list(mylist, file = NULL), label = "export_list() fails when file is NULL") | ||
|
||
expect_true(identical(export_list(mylist, file = paste0("mtcars_", 3:1, ".csv")), paste0("mtcars_", 3:1, ".csv"))) | ||
expect_true(identical(export_list(mylist, file = "%s.csv"), paste0("mtcars", 3:1, ".csv"))) | ||
|
||
expect_true(identical(export_list(mylist, file = paste0("file_", 1:3, ".csv"), archive = "archive.zip"), "archive.zip")) | ||
expect_true(identical(export_list(mylist, file = paste0("file_", 1:3, ".csv"), archive = "arch/archive.zip"), "arch/archive.zip")) | ||
|
||
expect_true(all.equal(mylist[["mtcars1"]], import("mtcars1.csv"))) | ||
expect_true(all.equal(mylist[["mtcars2"]], import("mtcars2.csv"))) | ||
expect_true(all.equal(mylist[["mtcars3"]], import("mtcars3.csv"))) | ||
|
||
names(mylist) <- NULL | ||
expect_true(identical(export_list(mylist, file = "mtcars_%s.csv"), paste0("mtcars_", 1:3, ".csv"))) | ||
|
||
names(mylist) <- c("a", "", "c") | ||
expect_error(export_list(mylist), label = "export_list() fails without 'file' argument") | ||
expect_error(export_list(mylist, file = "%.csv"), label = "export_list() fails without missing names") | ||
expect_error(export_list(mylist, file = c("a.csv", "b.csv")), label = "export_list() fails with mismatched argument lengths") | ||
|
||
names(mylist) <- c("a", "a", "c") | ||
expect_error(export_list(mylist, file = "mtcars_%s.csv"), label = "export_list() fails with duplicated data frame names") | ||
expect_error(export_list(mylist, file = c("mtcars1.csv", "mtcars1.csv", "mtcars3.csv")), label = "export_list() fails with duplicated data frame names") | ||
withr::with_tempdir({ | ||
export(list( | ||
mtcars3 = mtcars[1:10, ], | ||
mtcars2 = mtcars[11:20, ], | ||
mtcars1 = mtcars[21:32, ] | ||
), "mtcars.xlsx") | ||
mylist <- import_list("mtcars.xlsx") | ||
|
||
expect_error(export_list(mtcars), label = "export_list() fails on exporting single data frame") | ||
expect_error(export_list(mylist, file = NULL), label = "export_list() fails when file is NULL") | ||
|
||
expect_true(identical(export_list(mylist, file = paste0("mtcars_", 3:1, ".csv")), paste0("mtcars_", 3:1, ".csv"))) | ||
expect_true(identical(export_list(mylist, file = "%s.csv"), paste0("mtcars", 3:1, ".csv"))) | ||
|
||
expect_true(identical(export_list(mylist, file = paste0("file_", 1:3, ".csv"), archive = "archive.zip"), "archive.zip")) | ||
expect_true(identical(export_list(mylist, file = paste0("file_", 1:3, ".csv"), archive = "arch/archive.zip"), "arch/archive.zip")) | ||
|
||
expect_true(all.equal(mylist[["mtcars1"]], import("mtcars1.csv"))) | ||
expect_true(all.equal(mylist[["mtcars2"]], import("mtcars2.csv"))) | ||
expect_true(all.equal(mylist[["mtcars3"]], import("mtcars3.csv"))) | ||
|
||
names(mylist) <- NULL | ||
expect_true(identical(export_list(mylist, file = "mtcars_%s.csv"), paste0("mtcars_", 1:3, ".csv"))) | ||
|
||
names(mylist) <- c("a", "", "c") | ||
expect_error(export_list(mylist), label = "export_list() fails without 'file' argument") | ||
expect_error(export_list(mylist, file = "%.csv"), label = "export_list() fails without missing names") | ||
expect_error(export_list(mylist, file = c("a.csv", "b.csv")), label = "export_list() fails with mismatched argument lengths") | ||
|
||
names(mylist) <- c("a", "a", "c") | ||
expect_error(export_list(mylist, file = "mtcars_%s.csv"), label = "export_list() fails with duplicated data frame names") | ||
expect_error(export_list(mylist, file = c("mtcars1.csv", "mtcars1.csv", "mtcars3.csv")), label = "export_list() fails with duplicated data frame names") | ||
}) | ||
}) | ||
|
||
test_that("List length of one, #385", { | ||
example1 <- list("iris" = iris) | ||
tempfile <- tempfile(fileext = ".csv") | ||
expect_error(export(example1, tempfile), NA) | ||
tempfile <- tempfile(fileext = ".xlsx") | ||
expect_error(export(example1, tempfile), NA) | ||
expect_equal(readxl::excel_sheets(tempfile), "iris") ## name is retained | ||
tempfile <- tempfile(fileext = ".rds") | ||
expect_error(export(example1, tempfile), NA) | ||
expect_true(is.list(readRDS(tempfile)) && !is.data.frame(readRDS(tempfile))) | ||
withr::with_tempdir({ | ||
example1 <- list("iris" = iris) | ||
tempfile <- tempfile(fileext = ".csv") | ||
expect_error(export(example1, tempfile), NA) | ||
tempfile <- tempfile(fileext = ".xlsx") | ||
expect_error(export(example1, tempfile), NA) | ||
expect_equal(readxl::excel_sheets(tempfile), "iris") ## name is retained | ||
tempfile <- tempfile(fileext = ".rds") | ||
expect_error(export(example1, tempfile), NA) | ||
expect_true(is.list(readRDS(tempfile)) && !is.data.frame(readRDS(tempfile))) | ||
}) | ||
}) | ||
|
||
unlink("mtcars.xlsx") | ||
unlink("mtcars1.csv") | ||
unlink("mtcars2.csv") | ||
unlink("mtcars3.csv") | ||
unlink("mtcars_1.csv") | ||
unlink("mtcars_2.csv") | ||
unlink("mtcars_3.csv") | ||
|
||
unlink("a.csv") | ||
unlink("b.csv") | ||
|
||
unlink("archive.zip") | ||
unlink("arch/archive.zip") | ||
unlink("arch/", recursive = TRUE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,25 @@ | ||
context("Extensions") | ||
library("datasets") | ||
|
||
test_that("S3 extension mechanism works for imports", { | ||
write.csv(iris, 'iris.custom') | ||
expect_error(import("iris.custom")) | ||
.import.rio_custom <- function(file, ...){ | ||
read.csv(file, ...) | ||
} | ||
#expect_true(is.data.frame(import('iris.custom'))) | ||
rm(.import.rio_custom) | ||
withr::with_tempdir({ | ||
write.csv(iris, "iris.custom") | ||
expect_error(import("iris.custom")) | ||
.import.rio_custom <- function(file, ...) { | ||
read.csv(file, ...) | ||
} | ||
##expect_true(is.data.frame(import('iris.custom'))) | ||
rm(.import.rio_custom) | ||
}) | ||
}) | ||
|
||
test_that("S3 extension mechanism works for exports", { | ||
expect_error(export("iris.custom")) | ||
.export.rio_custom <- function(file, data, ...){ | ||
write.csv(data, file, ...) | ||
invisible(file) | ||
} | ||
expect_error(is.character(export(iris, "iris.custom"))) | ||
rm(.export.rio_custom) | ||
withr::with_tempdir({ | ||
expect_error(export("iris.custom")) | ||
.export.rio_custom <- function(file, data, ...) { | ||
write.csv(data, file, ...) | ||
invisible(file) | ||
} | ||
expect_error(is.character(export(iris, "iris.custom"))) | ||
rm(.export.rio_custom) | ||
}) | ||
}) | ||
|
||
unlink("iris.custom") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
context("R dump imports/exports") | ||
require("datasets") | ||
|
||
test_that("Export to .R dump file", { | ||
expect_true(export(iris, "iris.R") %in% dir()) | ||
expect_true(export(iris, "iris.dump") %in% dir()) | ||
}) | ||
test_that("Export / Import to .R dump file", { | ||
withr::with_tempfile("iris_file", fileext = ".R", code = { | ||
export(iris, iris_file) | ||
expect_true(file.exists(iris_file)) | ||
expect_true(is.data.frame(import(iris_file))) | ||
|
||
test_that("Import from .R dump file", { | ||
expect_true(is.data.frame(import("iris.R"))) | ||
expect_true(is.data.frame(import("iris.dump"))) | ||
}) | ||
withr::with_tempfile("iris_file", fileext = ".dump", code = { | ||
export(iris, iris_file) | ||
expect_true(file.exists(iris_file)) | ||
expect_true(is.data.frame(import(iris_file))) | ||
}) | ||
}) | ||
|
||
unlink("iris.R") | ||
unlink("iris.dump") |
Oops, something went wrong.