Skip to content

Commit

Permalink
Fix #380 (#394)
Browse files Browse the repository at this point in the history
* 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
chainsawriot authored Apr 25, 2024
1 parent eb244d1 commit 4a60f11
Show file tree
Hide file tree
Showing 49 changed files with 832 additions and 874 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Move `stringi` to Suggests to reduce compilation time. Add an attribution to the internal data to list out all required packages #378
* Move `readr` to Imports for `fwf`. `readr` is a dependency of `haven` so it does not increase the number of dependencies. Remove the original `read.fwf2` which doesn't guess `widths`. Keep the `widths` and `col.names` to maintain compatibility. #381
* Add (back) a pkgdown website: [https://gesistsa.github.io/rio/](https://gesistsa.github.io/rio/)
* Update all test cases #380

# rio 1.0.1

Expand Down
38 changes: 24 additions & 14 deletions tests/testthat/test_compress.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,33 @@ test_that("Recognize compressed file types", {
expect_true(is.na(rio:::find_compress("file.notcompressed")$compress))
})

test_that("Export to compressed (zip)", {
e1 <- export(iris, "iris.csv.zip")
expect_true(e1 %in% dir())
test_that("Export to compressed (zip) / import", {
withr::with_tempfile("iris_path", fileext = ".csv.zip", code = {
e1 <- export(iris, iris_path)
expect_true(file.exists(iris_path))
expect_true(is.data.frame(import(iris_path)))
expect_true(is.data.frame(import(iris_path)))
expect_true(is.data.frame(import(iris_path, which = 1)))
base_file_name <- gsub("\\.zip", "", basename(iris_path))
expect_true(is.data.frame(import(iris_path, which = base_file_name)))
})
})

test_that("Export to compressed (tar)", {
e2 <- export(iris, "iris.csv.tar")
expect_true(e2 %in% dir())
withr::with_tempfile("iris_path", fileext = ".csv.tar", code = {
e2 <- export(iris, iris_path)
expect_true(file.exists(iris_path))
## tar export does not work due to: https://bugs.r-project.org/bugzilla3/show_bug.cgi?id=16716
##expect_true(is.data.frame(import("iris.csv.tar")))
})
})

test_that("Import from compressed", {
expect_true(is.data.frame(import("iris.csv.zip")))
expect_true(is.data.frame(import("iris.csv.zip", which = 1)))
expect_true(is.data.frame(import("iris.csv.zip", which = "iris.csv")))
# tar export does not work due to: https://bugs.r-project.org/bugzilla3/show_bug.cgi?id=16716
#expect_true(is.data.frame(import("iris.csv.tar")))
})
## test_that("Import from compressed", {
## expect_true(is.data.frame(import("iris.csv.zip")))
## expect_true(is.data.frame(import("iris.csv.zip", which = 1)))
## expect_true(is.data.frame(import("iris.csv.zip", which = "iris.csv")))
## # tar export does not work due to: https://bugs.r-project.org/bugzilla3/show_bug.cgi?id=16716
## #expect_true(is.data.frame(import("iris.csv.tar")))
## })

unlink("iris.csv.zip")
unlink("iris.csv.tar")
## unlink("iris.csv.tar")
42 changes: 21 additions & 21 deletions tests/testthat/test_convert.R
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")
28 changes: 16 additions & 12 deletions tests/testthat/test_errors.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
context("Errors")
library("datasets")

test_that("Function suggestions for unsupported export", {
Expand All @@ -8,30 +7,35 @@ test_that("Function suggestions for unsupported export", {
})

test_that("Error for unsupported file types", {
writeLines("123", con = "test.faketype")
expect_error(import("test.faketype"), "Format not supported")
expect_error(export(mtcars, "mtcars.faketype"), "Format not supported")
withr::with_tempfile("test_file", fileext = ".faketype", code = {
writeLines("123", con = test_file)
expect_error(import(test_file), "Format not supported")
expect_error(export(mtcars, test_file), "Format not supported")
})
expect_equal(.standardize_format("faketype"), "faketype")
expect_error(get_ext("noextension"), "'file' has no extension")
unlink("test.faketype")
})

test_that("Error for mixed support file types", {
expect_error(import("test.por"), "No such file")
expect_error(export(mtcars, "mtcars.por"), "Format not supported")
expect_error(export(mtcars, "mtcars.faketype"), "Format not supported")
withr::with_tempfile("mtcars_file", fileext = ".por", code = {
expect_error(export(mtcars, mtcars_file), "Format not supported")
})
})

test_that("Only export data.frame or matrix", {
expect_error(export(1, "test.csv"), "'x' is not a data.frame or matrix")
withr::with_tempfile("test_file", fileext = ".csv", code = {
expect_error(export(1, test_file), "'x' is not a data.frame or matrix")
})
})

test_that("Column widths printed for fixed-width format", {
expect_true(is.character(export(data.frame(1), "test.txt", format = "fwf", verbose = FALSE)))
expect_message(export(data.frame(1), "test.txt", format = "fwf", verbose = TRUE))
unlink("test.txt")
withr::with_tempfile("test_file", fileext = ".txt", code = {
expect_true(is.character(export(data.frame(1), test_file, format = "fwf", verbose = FALSE)))
expect_message(export(data.frame(1), test_file, format = "fwf", verbose = TRUE))
})
})

test_that("Warning for import_list() with missing file", {
expect_warning(import_list("fake_file.csv"))
expect_warning(import_list("fake_file.csv")) ## no error
})
35 changes: 19 additions & 16 deletions tests/testthat/test_export_corner_cases.R
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")))
})
})
100 changes: 44 additions & 56 deletions tests/testthat/test_export_list.R
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)
35 changes: 18 additions & 17 deletions tests/testthat/test_extensions.R
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")
22 changes: 11 additions & 11 deletions tests/testthat/test_format_R.R
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")
Loading

0 comments on commit 4a60f11

Please sign in to comment.