From 4a60f11496af6ec5e9c161f4f25f95a8ac1316db Mon Sep 17 00:00:00 2001 From: chainsawriot Date: Thu, 25 Apr 2024 16:35:38 +0200 Subject: [PATCH] Fix #380 (#394) * 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] --- NEWS.md | 1 + tests/testthat/test_compress.R | 38 ++-- tests/testthat/test_convert.R | 42 ++-- tests/testthat/test_errors.R | 28 +-- tests/testthat/test_export_corner_cases.R | 35 ++-- tests/testthat/test_export_list.R | 100 +++++----- tests/testthat/test_extensions.R | 35 ++-- tests/testthat/test_format_R.R | 22 +-- tests/testthat/test_format_arff.R | 17 +- tests/testthat/test_format_csv.R | 92 +++++---- tests/testthat/test_format_csv_gz.R | 17 +- tests/testthat/test_format_csvy.R | 21 +- tests/testthat/test_format_dbf.R | 22 +-- tests/testthat/test_format_dif.R | 4 +- tests/testthat/test_format_dta.R | 53 ++--- tests/testthat/test_format_eviews.R | 4 +- .../testthat/test_format_external_packages.R | 20 +- tests/testthat/test_format_feather.R | 18 +- tests/testthat/test_format_fortran.R | 11 +- tests/testthat/test_format_fst.R | 18 +- tests/testthat/test_format_fwf.R | 51 ++--- tests/testthat/test_format_html.R | 43 ++-- tests/testthat/test_format_json.R | 38 ++-- tests/testthat/test_format_matlab.R | 20 +- tests/testthat/test_format_mtp.R | 5 +- tests/testthat/test_format_ods.R | 52 ++--- tests/testthat/test_format_parquet.R | 6 +- tests/testthat/test_format_psv.R | 25 ++- tests/testthat/test_format_pzfx.R | 36 ++-- tests/testthat/test_format_qs.R | 18 +- tests/testthat/test_format_rdata.R | 85 +++++--- tests/testthat/test_format_rds.R | 27 ++- tests/testthat/test_format_rec.R | 3 - tests/testthat/test_format_sas.R | 24 +-- tests/testthat/test_format_sav.R | 123 ++++++------ tests/testthat/test_format_syd.R | 5 +- tests/testthat/test_format_tsv.R | 24 ++- tests/testthat/test_format_xls.R | 66 +++---- tests/testthat/test_format_xml.R | 34 ++-- tests/testthat/test_format_yml.R | 35 ++-- tests/testthat/test_gather_attrs.R | 4 - tests/testthat/test_guess.R | 10 +- tests/testthat/test_identical.R | 73 ++++--- tests/testthat/test_import_list.R | 184 ++++++++++-------- tests/testthat/test_install_formats.R | 2 - tests/testthat/test_mapping.R | 47 ++--- tests/testthat/test_matrix.R | 23 +-- tests/testthat/test_remote.R | 2 - tests/testthat/test_set_class.R | 43 ++-- 49 files changed, 832 insertions(+), 874 deletions(-) diff --git a/NEWS.md b/NEWS.md index 8571c9ff..f9b7f37c 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 diff --git a/tests/testthat/test_compress.R b/tests/testthat/test_compress.R index bb55afc3..b78e3841 100644 --- a/tests/testthat/test_compress.R +++ b/tests/testthat/test_compress.R @@ -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") diff --git a/tests/testthat/test_convert.R b/tests/testthat/test_convert.R index 5b249847..0e351bec 100644 --- a/tests/testthat/test_convert.R +++ b/tests/testthat/test_convert.R @@ -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") diff --git a/tests/testthat/test_errors.R b/tests/testthat/test_errors.R index 9a705330..6790e6e2 100644 --- a/tests/testthat/test_errors.R +++ b/tests/testthat/test_errors.R @@ -1,4 +1,3 @@ -context("Errors") library("datasets") test_that("Function suggestions for unsupported export", { @@ -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 }) diff --git a/tests/testthat/test_export_corner_cases.R b/tests/testthat/test_export_corner_cases.R index d58c3764..bfaf8098 100644 --- a/tests/testthat/test_export_corner_cases.R +++ b/tests/testthat/test_export_corner_cases.R @@ -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"))) + }) }) diff --git a/tests/testthat/test_export_list.R b/tests/testthat/test_export_list.R index e5415dc8..8452b898 100644 --- a/tests/testthat/test_export_list.R +++ b/tests/testthat/test_export_list.R @@ -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) diff --git a/tests/testthat/test_extensions.R b/tests/testthat/test_extensions.R index 2d351261..b04f832b 100644 --- a/tests/testthat/test_extensions.R +++ b/tests/testthat/test_extensions.R @@ -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") diff --git a/tests/testthat/test_format_R.R b/tests/testthat/test_format_R.R index 2c890a0d..bb6eee1d 100644 --- a/tests/testthat/test_format_R.R +++ b/tests/testthat/test_format_R.R @@ -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") diff --git a/tests/testthat/test_format_arff.R b/tests/testthat/test_format_arff.R index 0ebcf819..23cba32d 100644 --- a/tests/testthat/test_format_arff.R +++ b/tests/testthat/test_format_arff.R @@ -1,12 +1,7 @@ -context("Weka (.arff) imports/exports") -require("datasets") - -test_that("Export to Weka", { - expect_true(export(iris, "iris.arff") %in% dir()) +test_that("Weka (.arff) imports/exports", { + withr::with_tempfile("iris_file", fileext = ".arff", code = { + export(iris, iris_file) + expect_true(file.exists(iris_file)) + expect_true(is.data.frame(import(iris_file))) + }) }) - -test_that("Import from Weka", { - expect_true(is.data.frame(import("iris.arff"))) -}) - -unlink("iris.arff") diff --git a/tests/testthat/test_format_csv.R b/tests/testthat/test_format_csv.R index 19413abb..c872de60 100644 --- a/tests/testthat/test_format_csv.R +++ b/tests/testthat/test_format_csv.R @@ -1,19 +1,21 @@ -context("CSV imports/exports") require("datasets") test_that("Export to CSV", { - expect_true(export(iris, "iris.csv") %in% dir()) - unlink("iris.csv") + withr::with_tempfile("iris_file", fileext = ".csv", code = { + export(iris, iris_file) + expect_true(file.exists(iris_file)) + }) }) test_that("Export (Append) to CSV", { - export(iris, "iris.csv") - nlines <- length(readLines("iris.csv")) - export(iris, "iris.csv", append = FALSE) - expect_true(identical(length(readLines("iris.csv")), nlines)) - export(iris, "iris.csv", append = TRUE) - expect_true(identical(length(readLines("iris.csv")), (2L*nlines)-1L)) - unlink("iris.csv") + withr::with_tempfile("iris_file", fileext = ".csv", code = { + export(iris, iris_file) + nlines <- length(readLines(iris_file)) + export(iris, iris_file, append = FALSE) + expect_true(identical(length(readLines(iris_file)), nlines)) + export(iris, iris_file, append = TRUE) + expect_true(identical(length(readLines(iris_file)), (2L * nlines) - 1L)) + }) }) test_that("Import from CSV", { @@ -22,49 +24,43 @@ test_that("Import from CSV", { }) test_that("Import from (European-style) CSV with semicolon separator", { - write.table(iris, "iris2.csv", dec = ",", sep = ";", row.names = FALSE) - expect_true("iris2.csv" %in% dir()) - # import works (even if column classes are incorrect) - expect_true(is.data.frame(import("iris2.csv", header = TRUE))) - iris_imported <- import("iris2.csv", format = ";", header = TRUE) - # import works with correct, numeric column classes - expect_true(is.data.frame(iris_imported)) - expect_true(is.numeric(iris_imported[["Sepal.Length"]])) + withr::with_tempfile("iris_file", fileext = ".csv", code = { + write.table(iris, iris_file, dec = ",", sep = ";", row.names = FALSE) + expect_true(file.exists(iris_file)) + ## import works (even if column classes are incorrect) + expect_true(is.data.frame(import(iris_file, header = TRUE))) + iris_imported <- import(iris_file, format = ";", header = TRUE) + ## import works with correct, numeric column classes + expect_true(is.data.frame(iris_imported)) + expect_true(is.numeric(iris_imported[["Sepal.Length"]])) + }) }) - -context("CSV (.csv2) imports/exports") - -test_that("Export to CSV", { - expect_true(export(iris, "iris.csv", format = "csv2") %in% dir()) +test_that("Export to and Import from CSV2", { + withr::with_tempfile("iris_file", fileext = ".csv", code = { + export(iris, iris_file, format = "csv2") + expect_true(file.exists(iris_file)) + expect_true(is.data.frame(import(iris_file, format = "csv2"))) + }) }) -test_that("Import from CSV (read.csv)", { - expect_true(is.data.frame(import("iris.csv", format = "csv2"))) -}) - -test_that("Import from CSV (fread)", { - expect_true(is.data.frame(import("iris.csv", format = "csv2"))) -}) - -test_that("Export to TSV with CSV extension", { - expect_true(export(iris, "iris.csv", format = "tsv") %in% dir()) -}) - -test_that("Import from TSV with CSV extension", { - expect_true(ncol(import("iris.csv")) == 5L) - expect_true(ncol(import("iris.csv", format = "tsv")) == 5L) - expect_true(ncol(import("iris.csv", format = "tsv", sep = "\t")) == 5L) - expect_true(ncol(import("iris.csv", sep = ",")) == 5L) # use `data.table::fread(sep = "auto")` even if `sep` set explicitly to "," - expect_true(ncol(import("iris.csv", format = "csv")) == 5L) - expect_true(ncol(import("iris.csv", sep = "auto")) == 5L) +test_that("Export to and Import from TSV with CSV extension", { + withr::with_tempfile("iris_file", fileext = ".csv", code = { + export(iris, iris_file, format = "tsv") + expect_true(file.exists(iris_file)) + expect_true(ncol(import(iris_file)) == 5L) + expect_true(ncol(import(iris_file, format = "tsv")) == 5L) + expect_true(ncol(import(iris_file, format = "tsv", sep = "\t")) == 5L) + expect_true(ncol(import(iris_file, sep = ",")) == 5L) # use `data.table::fread(sep = "auto")` even if `sep` set explicitly to "," + expect_true(ncol(import(iris_file, format = "csv")) == 5L) + expect_true(ncol(import(iris_file, sep = "auto")) == 5L) + }) }) test_that("fread is deprecated", { - lifecycle::expect_deprecated(import("iris.csv", fread = TRUE)) - lifecycle::expect_deprecated(import("iris.csv", fread = FALSE)) + withr::with_tempfile("iris_file", fileext = ".csv", code = { + export(iris, iris_file) + lifecycle::expect_deprecated(import(iris_file, fread = TRUE)) + lifecycle::expect_deprecated(import(iris_file, fread = FALSE)) + }) }) - - -unlink("iris.csv") -unlink("iris2.csv") diff --git a/tests/testthat/test_format_csv_gz.R b/tests/testthat/test_format_csv_gz.R index 113fb4c9..399a4c80 100644 --- a/tests/testthat/test_format_csv_gz.R +++ b/tests/testthat/test_format_csv_gz.R @@ -1,12 +1,7 @@ -context(".csv.gz imports/exports") -require("datasets") - -test_that("Export to csv.gz", { - expect_true(export(iris, "iris.csv.gz") %in% dir()) +test_that("Export to and import from csv.gz", { + withr::with_tempfile("iris_file", fileext = ".csv.gz", code = { + export(iris, iris_file) + expect_true(file.exists(iris_file)) + expect_true(is.data.frame(import(iris_file))) + }) }) - -test_that("Import from csv.gz", { - expect_true(inherits(import("iris.csv.gz"), "data.frame")) -}) - -unlink("iris.csv.gz") diff --git a/tests/testthat/test_format_csvy.R b/tests/testthat/test_format_csvy.R index 775d046d..dfc0a435 100644 --- a/tests/testthat/test_format_csvy.R +++ b/tests/testthat/test_format_csvy.R @@ -1,15 +1,8 @@ -context("CSVY imports/exports") -require("datasets") - -tmp <- tempfile(fileext = ".csvy") - -test_that("Export to CSVY", { - suppressWarnings(expect_true(file.exists(export(iris, tmp)))) +test_that("Export to and import from CSVY", { + withr::with_tempfile("iris_file", fileext = ".csvy", code = { + suppressWarnings(export(iris, iris_file)) + expect_true(file.exists(export(iris, iris_file))) + suppressWarnings(d <- import(iris_file)) + expect_true(is.data.frame(d)) + }) }) - -test_that("Import from CSVY", { - suppressWarnings(d <- import(tmp)) - expect_true(inherits(d, "data.frame")) -}) - -unlink(tmp) diff --git a/tests/testthat/test_format_dbf.R b/tests/testthat/test_format_dbf.R index 73ee8ad8..b9eb95a8 100644 --- a/tests/testthat/test_format_dbf.R +++ b/tests/testthat/test_format_dbf.R @@ -1,16 +1,10 @@ -context("XBASE (.dbf) imports/exports") -require("datasets") - -test_that("Export to XBASE (.dbf)", { +test_that("Export to and import from XBASE (.dbf)", { skip_if_not_installed("foreign") - expect_true(export(iris, "iris.dbf") %in% dir()) + withr::with_tempfile("iris_file", fileext = ".dbf", code = { + export(iris, iris_file) + expect_true(file.exists(iris_file)) + d <- import(iris_file) + expect_true(is.data.frame(d)) + expect_true(!"factor" %in% vapply(d, class, character(1))) + }) }) - -test_that("Import from XBASE (.dbf)", { - skip_if_not_installed("foreign") - d <- import("iris.dbf") - expect_true(is.data.frame(d)) - expect_true(!"factor" %in% vapply(d, class, character(1))) -}) - -unlink("iris.dbf") diff --git a/tests/testthat/test_format_dif.R b/tests/testthat/test_format_dif.R index b42e63d9..d293f53a 100644 --- a/tests/testthat/test_format_dif.R +++ b/tests/testthat/test_format_dif.R @@ -1,6 +1,4 @@ -context("DIF imports/exports") - test_that("Import from DIF", { dd <- import(file.path(system.file("misc", package = "utils"), "exDIF.dif"), transpose = TRUE) - expect_true(inherits(dd, "data.frame")) + expect_true(is.data.frame(dd)) }) diff --git a/tests/testthat/test_format_dta.R b/tests/testthat/test_format_dta.R index 5c1007ed..4a2c6164 100644 --- a/tests/testthat/test_format_dta.R +++ b/tests/testthat/test_format_dta.R @@ -1,35 +1,40 @@ -context("Stata (.dta) imports/exports") -require("datasets") - test_that("Export to Stata", { - expect_true(export(mtcars, "mtcars.dta") %in% dir()) - mtcars3 <- mtcars - names(mtcars3)[1] <- "foo.bar" - expect_error(export(mtcars3, "mtcars3.dta"), label = "Export fails on invalid Stata names") + withr::with_tempfile("mtcars_file", fileext = ".dta", code = { + export(mtcars, mtcars_file) + expect_true(file.exists(mtcars_file)) + mtcars3 <- mtcars + names(mtcars3)[1] <- "foo.bar" + expect_error(export(mtcars3, mtcars_file), label = "Export fails on invalid Stata names") + }) }) test_that("Import from Stata (read_dta)", { - expect_true(is.data.frame(import("mtcars.dta"))) - # arguments ignored - expect_error(import("mtcars.dta", extraneous.argument = TRUE), NA) - expect_silent(import("mtcars.dta", - extraneous.argument = TRUE - )) + withr::with_tempfile("mtcars_file", fileext = ".dta", code = { + export(mtcars, mtcars_file) + expect_true(is.data.frame(import(mtcars_file))) + ## arguments ignored + expect_error(import(mtcars_file, extraneous.argument = TRUE), NA) + expect_silent(import(mtcars_file,extraneous.argument = TRUE)) + }) }) test_that("Import from Stata with extended Haven features (read_dta)", { - expect_true(is.data.frame(mtcars_wtam <- import("mtcars.dta", - col_select = c("wt", "am"), - n_max = 10 - ))) - expect_identical(c(10L, 2L), dim(mtcars_wtam)) - expect_identical(c("wt", "am"), names(mtcars_wtam)) + withr::with_tempfile("mtcars_file", fileext = ".dta", code = { + export(mtcars, mtcars_file) + + expect_true(is.data.frame(mtcars_wtam <- import(mtcars_file, + col_select = c("wt", "am"), + n_max = 10 + ))) + expect_identical(c(10L, 2L), dim(mtcars_wtam)) + expect_identical(c("wt", "am"), names(mtcars_wtam)) + }) }) test_that("haven is deprecated", { - lifecycle::expect_deprecated(import("mtcars.dta", haven = TRUE)) - lifecycle::expect_deprecated(import("mtcars.dta", haven = FALSE)) + withr::with_tempfile("mtcars_file", fileext = ".dta", code = { + export(mtcars, mtcars_file) + lifecycle::expect_deprecated(import(mtcars_file, haven = TRUE)) + lifecycle::expect_deprecated(import(mtcars_file, haven = FALSE)) + }) }) - -unlink("mtcars.dta") -unlink("mtcars3.dta") diff --git a/tests/testthat/test_format_eviews.R b/tests/testthat/test_format_eviews.R index 6ba69015..0a0c042f 100644 --- a/tests/testthat/test_format_eviews.R +++ b/tests/testthat/test_format_eviews.R @@ -1,6 +1,4 @@ -context("EViews import") - test_that("Import from EViews", { - skip_if_not_installed(pkg="hexView") + skip_if_not_installed(pkg = "hexView") expect_true(is.data.frame(suppressWarnings(import(hexView::hexViewFile("data4-1.wf1"))))) }) diff --git a/tests/testthat/test_format_external_packages.R b/tests/testthat/test_format_external_packages.R index 00db2d3c..43184fe6 100644 --- a/tests/testthat/test_format_external_packages.R +++ b/tests/testthat/test_format_external_packages.R @@ -1,23 +1,19 @@ -context("External package Import/Export support") -require("datasets") - test_that("External read functions without an import method", { extensions <- c("bib", "bmp", "gexf", "gnumeric", "jpeg", "jpg", "npy", "png", "sss", "sdmx", "tiff") for (ext in extensions) { - file <- file.path(tempdir(), paste0("test.", ext)) - file.create(file) - on.exit(unlink(file)) - expect_error(import(file)) + withr::with_tempfile("dummyfile", fileext = paste0(".", ext), code = { + file.create(dummyfile) + expect_error(import(dummyfile)) + }) } }) test_that("import method exported by an external package", { extensions <- c("bean", "beancount", "ledger", "hledger") for (ext in extensions) { - file <- file.path(tempdir(), paste0("test.", ext)) - file.create(file) - on.exit(unlink(file)) - expect_error(import(file)) + withr::with_tempfile("dummyfile", fileext = paste0(".", ext), code = { + file.create(dummyfile) + expect_error(import(dummyfile)) + }) } }) - diff --git a/tests/testthat/test_format_feather.R b/tests/testthat/test_format_feather.R index d7b772cc..6cbf76e2 100644 --- a/tests/testthat/test_format_feather.R +++ b/tests/testthat/test_format_feather.R @@ -1,15 +1,9 @@ -context("feather imports/exports") skip_if_not_installed("arrow") -require("datasets") -test_that("Export to feather", { - skip_if_not_installed("arrow") - expect_true(export(iris, "iris.feather") %in% dir()) +test_that("Export to and import from feather", { + withr::with_tempfile("iris_file", fileext = ".feather", code = { + export(iris, iris_file) + expect_true(file.exists(iris_file)) + expect_true(is.data.frame(import(iris_file))) + }) }) - -test_that("Import from feather", { - skip_if_not_installed("arrow") - expect_true(is.data.frame(import("iris.feather"))) -}) - -unlink("iris.feather") diff --git a/tests/testthat/test_format_fortran.R b/tests/testthat/test_format_fortran.R index cfe1481a..b12c795a 100644 --- a/tests/testthat/test_format_fortran.R +++ b/tests/testthat/test_format_fortran.R @@ -1,8 +1,7 @@ -context("Fortran imports/exports") - test_that("Import from Fortran", { - ff <- tempfile() - cat(file = ff, "123456", "987654", sep = "\n") - expect_true(inherits(import(ff, format = "fortran", style = c("F2.1","F2.0","I2")), "data.frame")) - unlink(ff) + ## no fixed file extension + withr::with_tempfile("fortran_file", code = { + cat(file = fortran_file, "123456", "987654", sep = "\n") + expect_true(is.data.frame(import(fortran_file, format = "fortran", style = c("F2.1","F2.0","I2")))) + }) }) diff --git a/tests/testthat/test_format_fst.R b/tests/testthat/test_format_fst.R index 03a20dd9..7f539ada 100644 --- a/tests/testthat/test_format_fst.R +++ b/tests/testthat/test_format_fst.R @@ -1,13 +1,9 @@ -context("fst imports/exports") -require("datasets") +skip_if_not_installed(pkg="fst") -test_that("Export to fst", { - skip_if_not_installed(pkg="fst") - expect_true(export(iris, "iris.fst") %in% dir()) +test_that("Export to and import from fst", { + withr::with_tempfile("iris_file", fileext = ".fst", code = { + export(iris, iris_file) + expect_true(file.exists(iris_file)) + expect_true(is.data.frame(import(iris_file))) + }) }) - -test_that("Import from fst", { - skip_if_not_installed(pkg="fst") - expect_true(is.data.frame(import("iris.fst"))) -}) -unlink("iris.fst") diff --git a/tests/testthat/test_format_fwf.R b/tests/testthat/test_format_fwf.R index 38e0a92e..e791bbc1 100644 --- a/tests/testthat/test_format_fwf.R +++ b/tests/testthat/test_format_fwf.R @@ -1,33 +1,34 @@ -context("FWF imports/exports") -require("datasets") - -test_that("Export to FWF", { - expect_true(export(iris, "iris.fwf") %in% dir()) - expect_true(export(iris, "iris.txt", format = "fwf") %in% dir()) -}) - -test_that("Deprecation of `width` and `col.names`", { - lifecycle::expect_deprecated(import("iris.fwf", widths = c(-3, 3, 3, 3, 1))) - lifecycle::expect_deprecated(import("iris.fwf", col.names = names(iris))) -}) - simport <- function(...) { suppressWarnings(import(...)) } -test_that("Import from FWF (read.fwf)", { - expect_true(is.data.frame(simport("iris.fwf", widths = c(3, 3, 3, 3, 1)))) - expect_true(is.data.frame(simport("iris.fwf", widths = list(c(3, 3, 3, 3, 1))))) - expect_true(is.data.frame(simport("iris.fwf", widths = c(3, 3, 3, 3, 1), col.names = names(iris)))) - expect_true(is.data.frame(simport("iris.txt", widths = c(3, 3, 3, 3, 1), format = "fwf"))) - # negative column widths - expect_true(is.data.frame(simport("iris.fwf", widths = c(-3, 3, 3, 3, 1)))) +test_that("Export to and import from FWF .fwf", { + withr::with_tempfile("iris_file", fileext = ".fwf", code = { + export(iris, iris_file) + expect_true(file.exists(iris_file)) + expect_true(is.data.frame(simport(iris_file, widths = c(3, 3, 3, 3, 1)))) + expect_true(is.data.frame(simport(iris_file, widths = list(c(3, 3, 3, 3, 1))))) + expect_true(is.data.frame(simport(iris_file, widths = c(3, 3, 3, 3, 1), col.names = names(iris)))) + ## negative column widths + expect_true(is.data.frame(simport(iris_file, widths = c(-3, 3, 3, 3, 1)))) + ##use col_position instead + expect_error(x <- import(iris_file, col_position = readr::fwf_widths(c(1, 3, 3, 3, 1), col_names = names(iris))), NA) + expect_equal(x[1,1, drop = TRUE], 5) + }) }) -test_that("use col_position instead", { - expect_error(x <- import("iris.fwf", col_position = readr::fwf_widths(c(1, 3, 3, 3, 1), col_names = names(iris))), NA) - expect_equal(x[1,1, drop = TRUE], 5) +test_that("Export to and import from FWF .txt", { + withr::with_tempfile("iris_file", fileext = ".txt", code = { + export(iris, iris_file) + expect_true(file.exists(iris_file)) + expect_true(is.data.frame(simport(iris_file, widths = c(3, 3, 3, 3, 1), format = "fwf"))) + }) }) -unlink("iris.fwf") -unlink("iris.txt") +test_that("Deprecation of `width` and `col.names`", { + withr::with_tempfile("iris_file", fileext = ".fwf", code = { + export(iris, iris_file) + lifecycle::expect_deprecated(import(iris_file, widths = c(-3, 3, 3, 3, 1))) + lifecycle::expect_deprecated(import(iris_file, col.names = names(iris))) + }) +}) diff --git a/tests/testthat/test_format_html.R b/tests/testthat/test_format_html.R index 2d2111bf..b1e27fa0 100644 --- a/tests/testthat/test_format_html.R +++ b/tests/testthat/test_format_html.R @@ -1,50 +1,51 @@ -context("HTML imports/exports") -require("datasets") +skip_if_not_installed("xml2") test_html <- function(breaker = "&") { mtcars2 <- mtcars colnames(mtcars2)[1] <- paste0("mp", breaker, breaker, "g") mtcars2[1,1] <- paste0("mp", breaker, breaker, "g") - expect_error(x <- rio::export(mtcars2, tempfile(fileext = ".html")), NA) - temp_df <- rio::import(x) - expect_equal(colnames(temp_df)[1], paste0("mp", breaker, breaker, "g")) - expect_equal(temp_df[1,1], paste0("mp", breaker, breaker, "g")) + withr::with_tempfile("mtcars_file", fileext = ".html", code = { + expect_error(x <- rio::export(mtcars2, mtcars_file), NA) + temp_df <- rio::import(x) + expect_equal(colnames(temp_df)[1], paste0("mp", breaker, breaker, "g")) + expect_equal(temp_df[1,1], paste0("mp", breaker, breaker, "g")) + }) } -test_that("Export to HTML", { - skip_if_not_installed("xml2") - expect_true(export(iris, "iris.html") %in% dir(), label = "export to html works") +test_that("Export to and import from HTML", { + withr::with_tempfile("iris_file", fileext = ".html", code = { + export(iris, iris_file) + expect_true(file.exists(iris_file)) + expect_true(is.data.frame(import(iris_file)), label = "import from single-table html works") + }) }) test_that("Export to HTML with ampersands",{ - skip_if_not_installed("xml2") - iris$`R & D` <- paste(sample(letters,nrow(iris),rep=T), - '&', - sample(LETTERS,nrow(iris),rep=TRUE)) - expect_true(export(iris, "iris2.html") %in% dir(), - label = "export to html with ampersands works") + withr::with_tempfile("iris_file", fileext = ".html", code = { + + iris$`R & D` <- paste(sample(letters,nrow(iris),rep = TRUE), + "&", + sample(LETTERS,nrow(iris),rep = TRUE)) + export(iris, iris_file) + expect_true(file.exists(iris_file), + label = "export to html with ampersands works") + }) }) test_that("Import from HTML", { - skip_if_not_installed("xml2") - expect_true(is.data.frame(import("iris.html")), label = "import from single-table html works") f <- "../testdata/twotables.html" expect_true(all(dim(import(f, which = 1)) == c(32, 11)), label = "import from two-table html works (which = 1)") expect_true(all(dim(import(f, which = 2)) == c(150, 5)), label = "import from two-table html works (which = 2)") }) test_that("Import from HTML with multiple tbody elements", { - skip_if_not_installed("xml2") expect_true(is.data.frame(import("../testdata/two-tbody.html")), label="import with two tbody elements in a single html table works") expect_true(is.data.frame(import("../testdata/br-in-header.html")), label="import with an empty header cell in an html table works") expect_true(is.data.frame(import("../testdata/br-in-td.html")), label="import with an empty data cell in a single html table works") expect_true(is.data.frame(import("../testdata/th-as-row-element.html")), label="import with th instead of td in a non-header row in a single html table works") }) -unlink(c("iris.xml", "iris2.xml", "iris2.html")) - test_that("html with &, >, ', \", >, <", { - skip_if_not_installed("xml2") ## test all useless <- lapply(c("&", "\"", "'", "<", ">"), test_html) }) diff --git a/tests/testthat/test_format_json.R b/tests/testthat/test_format_json.R index 0848ddac..a3719f82 100644 --- a/tests/testthat/test_format_json.R +++ b/tests/testthat/test_format_json.R @@ -1,30 +1,28 @@ -context("JSON imports/exports") -require("datasets") +skip_if_not_installed("jsonlite") -test_that("Export to JSON", { - skip_if_not_installed("jsonlite") - expect_true(export(iris, "iris.json") %in% dir()) -}) - -test_that("Import from JSON", { - skip_if_not_installed("jsonlite") - expect_true(is.data.frame(import("iris.json"))) +test_that("Export to and import from JSON", { + withr::with_tempfile("iris_file", fileext = ".json", code = { + export(iris, iris_file) + expect_true(file.exists(iris_file)) + expect_true(is.data.frame(import(iris_file))) + }) }) test_that("Export to JSON (non-data frame)", { - skip_if_not_installed("jsonlite") - expect_true(export(list(1:10, letters), "list.json") %in% dir()) - expect_true(inherits(import("list.json"), "list")) - expect_true(length(import("list.json")) == 2L) + withr::with_tempfile("list_file", fileext = ".json", code = { + export(list(1:10, letters), list_file) + expect_true(file.exists(list_file)) + expect_true(inherits(import(list_file), "list")) + expect_false(inherits(import(list_file), "data.frame")) + expect_true(length(import(list_file)) == 2L) + }) }) test_that("utf-8", { content <- c("\"", "\u010d", "\u0161", "\u00c4", "\u5b57", "\u30a2", "\u30a2\u30e0\u30ed") x <- data.frame(col = content) - tempjson <- tempfile(fileext = ".json") - y <- import(export(x, tempjson)) - testthat::expect_equal(content, y$col) + withr::with_tempfile("tempjson", fileext = ".json", code = { + y <- import(export(x, tempjson)) + testthat::expect_equal(content, y$col) + }) }) - -unlink("iris.json") -unlink("list.json") diff --git a/tests/testthat/test_format_matlab.R b/tests/testthat/test_format_matlab.R index 5cc7cb47..fdb7a83b 100644 --- a/tests/testthat/test_format_matlab.R +++ b/tests/testthat/test_format_matlab.R @@ -1,16 +1,12 @@ -context("rmatio imports/exports") -require("datasets") +skip_if_not_installed("rmatio") -test_that("Export to matlab", { +test_that("Export to and import from matlab", { skip("failing mysteriously") - # skip_if_not_installed(pkg="rmatio") - expect_true(export(iris, "iris.matlab") %in% dir()) -}) + withr::with_tempfile("iris_file", fileext = ".matlab", code = { + export(iris, iris_file) + expect_true(file.exists(iris_file)) + expect_true(is.data.frame(import(iris_file))) + expect_true(identical(dim(import(iris_file)), dim(iris))) -test_that("Import from matlab", { - skip("failing mysteriously") - # skip_if_not_installed(pkg="rmatio") - expect_true(is.data.frame(import("iris.matlab"))) - expect_true(identical(dim(import("iris.matlab")), dim(iris))) + }) }) -unlink("iris.matlab") diff --git a/tests/testthat/test_format_mtp.R b/tests/testthat/test_format_mtp.R index bab7edcb..e90ae7db 100644 --- a/tests/testthat/test_format_mtp.R +++ b/tests/testthat/test_format_mtp.R @@ -1,4 +1 @@ -context("Minitab (.mtp) imports/exports") -require("datasets") - -#test_that("Import from Minitab", {}) +##test_that("Import from Minitab", {}) diff --git a/tests/testthat/test_format_ods.R b/tests/testthat/test_format_ods.R index b5aa06dc..939f3e31 100644 --- a/tests/testthat/test_format_ods.R +++ b/tests/testthat/test_format_ods.R @@ -1,8 +1,6 @@ -context("ODS imports/exports") -require("datasets") +skip_if_not_installed("readODS") test_that("Import from ODS", { - skip_if_not_installed(pkg="readODS") ods0 <- import("../testdata/mtcars.ods") expect_error(ods <- import("../testdata/mtcars.ods", sheet = 1, col_names = TRUE, @@ -14,35 +12,39 @@ test_that("Import from ODS", { expect_equivalent(ods, mtcars, label = "ODS import returns correct values") }) -test_that("Export to ODS", { - skip_if_not_installed(pkg="readODS") - expect_true(export(iris, "iris.ods") %in% dir()) +test_that("Export to and import from ODS", { + withr::with_tempfile("iris_file", fileext = ".ods", code = { + export(iris, iris_file) + expect_true(file.exists(iris_file)) + expect_true(is.data.frame(import(iris_file))) + }) }) test_that("... correctly passed #318", { - skip_if_not_installed(pkg = "readODS") - x <- tempfile(fileext = ".ods") - rio::export(mtcars, file = x, sheet = "mtcars") - expect_equal(readODS::list_ods_sheets(x), "mtcars") + withr::with_tempfile("mtcars_file", fileext = ".ods", code = { + export(iris, mtcars_file, sheet = "mtcars") + expect_equal(readODS::list_ods_sheets(mtcars_file), "mtcars") + }) }) test_that("Export and Import FODS", { - skip_if_not_installed(pkg = "readODS") - x <- tempfile(fileext = ".fods") - rio::export(mtcars, file = x, sheet = "mtcars") - expect_equal(readODS::list_fods_sheets(x), "mtcars") - expect_error(y <- import(x), NA) - expect_true(is.data.frame(y)) + withr::with_tempfile("fods_file", fileext = ".fods", code = { + export(iris, fods_file) + expect_true(file.exists(fods_file)) + expect_true(is.data.frame(import(fods_file))) + export(iris, fods_file, sheet = "mtcars") + expect_equal(readODS::list_fods_sheets(fods_file), "mtcars") + expect_error(y <- import(fods_file), NA) + expect_true(is.data.frame(y)) + + }) }) test_that("Export list of data frames", { - skip_if_not_installed(pkg = "readODS") - dfs <- list("cars" = mtcars, "flowers" = iris) - x1 <- tempfile(fileext = ".ods") - x2 <- tempfile(fileext = ".fods") - expect_error(export(dfs, x1), NA) - expect_error(export(dfs, x2), NA) - expect_equal(import(x1, which = "flowers"), import(x2, which = "flowers")) + withr::with_tempfile("ods_files", fileext = c(".ods", ".fods"), code = { + dfs <- list("cars" = mtcars, "flowers" = iris) + expect_error(export(dfs, ods_files[1]), NA) + expect_error(export(dfs, ods_files[2]), NA) + expect_equal(import(ods_files[1], which = "flowers"), import(ods_files[2], which = "flowers")) + }) }) - -unlink("iris.ods") diff --git a/tests/testthat/test_format_parquet.R b/tests/testthat/test_format_parquet.R index 9d3ac5c1..49739ef9 100644 --- a/tests/testthat/test_format_parquet.R +++ b/tests/testthat/test_format_parquet.R @@ -1,9 +1,7 @@ -context("Parquet imports/exports") -require("datasets") +skip_if_not_installed("arrow") +skip_on_os("mac") ## apache/arrow#40991 test_that("Export to and import from parquet", { - skip_if_not_installed("arrow") - skip_on_os("mac") ## apache/arrow#40991 withr::with_tempfile("iris_path", fileext = ".parquet", code = { export(iris, iris_path) expect_true(file.exists(iris_path)) diff --git a/tests/testthat/test_format_psv.R b/tests/testthat/test_format_psv.R index 5fca00ee..c26a1784 100644 --- a/tests/testthat/test_format_psv.R +++ b/tests/testthat/test_format_psv.R @@ -1,18 +1,17 @@ -context("PSV imports/exports") -require("datasets") - -test_that("Export to PSV", { - expect_true(export(iris, "iris.psv") %in% dir()) +test_that("Export to and import from PSV", { + withr::with_tempfile("iris_file", fileext = ".psv", code = { + export(iris, iris_file) + expect_true(file.exists(iris_file)) + expect_true(is.data.frame(import(iris_file))) + }) }) -test_that("Import from PSV", { - expect_true(is.data.frame(import("iris.psv"))) -}) test_that("fread is deprecated", { - lifecycle::expect_deprecated(import("iris.psv", fread = TRUE)) - lifecycle::expect_deprecated(import("iris.psv", fread = FALSE)) -}) + withr::with_tempfile("iris_file", fileext = ".psv", code = { + export(iris, iris_file) + lifecycle::expect_deprecated(import(iris_file, fread = TRUE)) + lifecycle::expect_deprecated(import(iris_file, fread = FALSE)) + }) - -unlink("iris.psv") +}) diff --git a/tests/testthat/test_format_pzfx.R b/tests/testthat/test_format_pzfx.R index 9d7d9ccf..4d44012a 100644 --- a/tests/testthat/test_format_pzfx.R +++ b/tests/testthat/test_format_pzfx.R @@ -1,22 +1,18 @@ -context("pzfx imports/exports") -require("datasets") +skip_if_not_installed("pzfx") -iris_numeric <- iris -iris_numeric$Species <- as.numeric(iris_numeric$Species) -test_that("Export to pzfx", { - skip_if_not_installed(pkg="pzfx") - expect_true(export(iris_numeric, "iris.pzfx") %in% dir()) +test_that("Export to and import from pzfx", { + ## pzfx support only numeric data + iris_numeric <- iris + iris_numeric$Species <- as.numeric(iris_numeric$Species) + withr::with_tempfile("iris_file", fileext = ".pzfx", code = { + export(iris_numeric, iris_file) + expect_true(file.exists(iris_file)) + expect_true(is.data.frame(import(iris_file))) + ## Note that the dim test is only true as long as the data are exported with + ## write_pzfx(..., row_names=FALSE) which is the default in the export + ## method, but it is not default in pzfx::write_pzfx() + expect_true(identical(dim(import(iris_file)), dim(iris_numeric))) + expect_true(identical(dim(import(iris_file, which = "Data 1")), dim(iris_numeric))) + expect_true(identical(dim(import(iris_file, table = "Data 1")), dim(iris_numeric))) + }) }) - -test_that("Import from pzfx", { - skip_if_not_installed(pkg="pzfx") - expect_true(is.data.frame(import("iris.pzfx"))) - # Note that the dim test is only true as long as the data are exported with - # write_pzfx(..., row_names=FALSE) which is the default in the export - # method, but it is not default in pzfx::write_pzfx() - expect_true(identical(dim(import("iris.pzfx")), dim(iris_numeric))) - expect_true(identical(dim(import("iris.pzfx", which = "Data 1")), dim(iris_numeric))) - expect_true(identical(dim(import("iris.pzfx", table = "Data 1")), dim(iris_numeric))) -}) - -unlink("iris.pzfx") diff --git a/tests/testthat/test_format_qs.R b/tests/testthat/test_format_qs.R index d5e54bc8..89c9718b 100644 --- a/tests/testthat/test_format_qs.R +++ b/tests/testthat/test_format_qs.R @@ -1,13 +1,9 @@ -context("qs import/export") -require("datasets") +skip_if_not_installed("qs") -test_that("Export to qs", { - skip_if_not_installed(pkg = "qs") - expect_true(export(iris, "iris.qs") %in% dir()) +test_that("Export to and import from qs", { + withr::with_tempfile("iris_file", fileext = ".qs", code = { + export(iris, iris_file) + expect_true(file.exists(iris_file)) + expect_true(is.data.frame(import(iris_file))) + }) }) - -test_that("Import from qs", { - skip_if_not_installed(pkg = "qs") - expect_true(is.data.frame(import("iris.qs"))) -}) -unlink("iris.qs") diff --git a/tests/testthat/test_format_rdata.R b/tests/testthat/test_format_rdata.R index 22cc7abc..4a9ea5e2 100644 --- a/tests/testthat/test_format_rdata.R +++ b/tests/testthat/test_format_rdata.R @@ -1,32 +1,59 @@ -context("Rdata imports/exports") -require("datasets") - -test_that("Export to Rdata", { - # data.frame - expect_true(export(iris, "iris.Rdata") %in% dir()) - # environment - e <- new.env() - e$iris <- iris - expect_true(export(e, "iris.Rdata") %in% dir()) - # character - expect_true(export("iris", "iris.Rdata") %in% dir()) - # expect error otherwise - expect_error(export(iris$Species, "iris.Rdata") %in% dir()) -}) - -test_that("Import from Rdata", { - expect_true(is.data.frame(import("iris.Rdata"))) - expect_true(is.data.frame(import("iris.Rdata", which = 1))) +test_that("Export to and import from Rdata", { + withr::with_tempfile("iris_file", fileext = ".Rdata", code = { + ## data frame + export(iris, iris_file) + expect_true(file.exists(iris_file)) + expect_true(is.data.frame(import(iris_file))) + expect_true(is.data.frame(import(iris_file, which = 1))) + }) + withr::with_tempfile("iris_file", fileext = ".Rdata", code = { + ## environment + e <- new.env() + e$iris <- iris + export(e, iris_file) + expect_true(file.exists(iris_file)) + expect_true(is.data.frame(import(iris_file))) + expect_true(is.data.frame(import(iris_file, which = 1))) + }) + withr::with_tempfile("iris_file", fileext = ".Rdata", code = { + ## character + export("iris", iris_file) + expect_true(file.exists(iris_file)) + expect_true(is.data.frame(import(iris_file))) + expect_true(is.data.frame(import(iris_file, which = 1))) + }) + withr::with_tempfile("iris_file", fileext = ".Rdata", code = { + ## expect error otherwise + expect_error(export(iris$Species, iris_file)) + }) }) -test_that("Export to rda", { - expect_true(export(iris, "iris.rda") %in% dir()) +test_that("Export to and import from rda", { + withr::with_tempfile("iris_file", fileext = ".rda", code = { + ## data frame + export(iris, iris_file) + expect_true(file.exists(iris_file)) + expect_true(is.data.frame(import(iris_file))) + expect_true(is.data.frame(import(iris_file, which = 1))) + }) + withr::with_tempfile("iris_file", fileext = ".rda", code = { + ## environment + e <- new.env() + e$iris <- iris + export(e, iris_file) + expect_true(file.exists(iris_file)) + expect_true(is.data.frame(import(iris_file))) + expect_true(is.data.frame(import(iris_file, which = 1))) + }) + withr::with_tempfile("iris_file", fileext = ".rda", code = { + ## character + export("iris", iris_file) + expect_true(file.exists(iris_file)) + expect_true(is.data.frame(import(iris_file))) + expect_true(is.data.frame(import(iris_file, which = 1))) + }) + withr::with_tempfile("iris_file", fileext = ".rda", code = { + ## expect error otherwise + expect_error(export(iris$Species, iris_file)) + }) }) - -test_that("Import from rda", { - expect_true(is.data.frame(import("iris.rda"))) - expect_true(is.data.frame(import("iris.rda", which = 1))) -}) - -unlink("iris.Rdata") -unlink("iris.rda") diff --git a/tests/testthat/test_format_rds.R b/tests/testthat/test_format_rds.R index cd9b933b..545786f8 100644 --- a/tests/testthat/test_format_rds.R +++ b/tests/testthat/test_format_rds.R @@ -1,19 +1,16 @@ -context("Rds imports/exports") -require("datasets") - -test_that("Export to rds", { - expect_true(export(iris, "iris.rds") %in% dir()) -}) - -test_that("Import from rds", { - expect_true(is.data.frame(import("iris.rds"))) +test_that("Export to and import from rds", { + withr::with_tempfile("iris_file", fileext = ".rds", code = { + export(iris, iris_file) + expect_true(file.exists(iris_file)) + expect_true(is.data.frame(import(iris_file))) + }) }) test_that("Export to rds (non-data frame)", { - expect_true(export(list(1:10, letters), "list.rds") %in% dir()) - expect_true(inherits(import("list.rds"), "list")) - expect_true(length(import("list.rds")) == 2L) + withr::with_tempfile("list_file", fileext = ".rds", code = { + export(list(1:10, letters), list_file) + expect_true(file.exists(list_file)) + expect_true(inherits(import(list_file), "list")) + expect_true(length(import(list_file)) == 2L) + }) }) - -unlink("iris.rds") -unlink("list.rds") diff --git a/tests/testthat/test_format_rec.R b/tests/testthat/test_format_rec.R index 30e61025..727c30d4 100644 --- a/tests/testthat/test_format_rec.R +++ b/tests/testthat/test_format_rec.R @@ -1,4 +1 @@ -context("Epiinfo (.rec) imports/exports") -require("datasets") - #test_that("Import from Epiinfo", {}) diff --git a/tests/testthat/test_format_sas.R b/tests/testthat/test_format_sas.R index 5068d1a6..def71372 100644 --- a/tests/testthat/test_format_sas.R +++ b/tests/testthat/test_format_sas.R @@ -1,18 +1,14 @@ -context("SAS imports/exports") -require("datasets") - -#test_that("Import from SAS (.sas7bdat)", {}) -test_that("Export SAS (.xpt)", { - expect_true(export(mtcars, "mtcars.xpt") %in% dir()) -}) - -test_that("Import from SAS (.xpt)", { - expect_true(inherits(import("mtcars.xpt"), "data.frame")) +test_that("Export to and import from SAS (.xpt)", { + withr::with_tempfile("mtcars_file", fileext = ".xpt", code = { + export(mtcars, mtcars_file) + expect_true(file.exists(mtcars_file)) + expect_true(is.data.frame(import(mtcars_file))) + }) }) test_that("Export SAS (.sas7bdat)", { - expect_true(suppressWarnings(export(mtcars, "mtcars.sas7bdat")) %in% dir()) + withr::with_tempfile("mtcars_file", fileext = ".sas7bdat", code = { + suppressWarnings(export(mtcars, mtcars_file)) + expect_true(file.exists(mtcars_file)) + }) }) - -unlink("mtcars.sas7bdat") -unlink("mtcars.xpt") diff --git a/tests/testthat/test_format_sav.R b/tests/testthat/test_format_sav.R index ef2e4894..80d88e9c 100644 --- a/tests/testthat/test_format_sav.R +++ b/tests/testthat/test_format_sav.R @@ -1,74 +1,65 @@ -context("SPSS (.sav) imports/exports") -require("datasets") - -mtcars2 <- mtcars -# label and value labels -mtcars2[["cyl"]] <- factor(mtcars2[["cyl"]], c(4, 6, 8), c("four", "six", "eight")) -attr(mtcars2[["cyl"]], "label") <- "cylinders" -# value labels only -mtcars2[["am"]] <- factor(mtcars2[["am"]], c(0, 1), c("automatic", "manual")) -# variable label only -attr(mtcars2[["mpg"]], "label") <- "miles per gallon" test_that("Export to SPSS (.sav)", { - expect_true(export(mtcars2, "mtcars.sav") %in% dir()) -}) - -test_that("Export to SPSS compressed (.zsav)", { - expect_true(export(mtcars2, "mtcars.zsav") %in% dir()) -}) - -test_that("Import from SPSS (.sav; read_sav)", { - expect_true(d <- is.data.frame(import("mtcars.sav"))) - expect_true(!"labelled" %in% unlist(lapply(d, class))) - rm(d) -}) - -test_that("Import from SPSS (.zsav; read_sav)", { - expect_true(d <- is.data.frame(import("mtcars.zsav"))) - expect_true(!"labelled" %in% unlist(lapply(d, class))) - rm(d) -}) - -test_that("Variable label and value labels preserved on SPSS (.sav) roundtrip", { - d <- import("mtcars.sav") - a_cyl <- attributes(d[["cyl"]]) - expect_true("label" %in% names(a_cyl)) - expect_true("labels" %in% names(a_cyl)) - expect_true(identical(a_cyl[["label"]], "cylinders")) - expect_true(identical(a_cyl[["labels"]], stats::setNames(c(1.0, 2.0, 3.0), c("four", "six", "eight")))) + mtcars2 <- mtcars + ## label and value labels + mtcars2[["cyl"]] <- factor(mtcars2[["cyl"]], c(4, 6, 8), c("four", "six", "eight")) + attr(mtcars2[["cyl"]], "label") <- "cylinders" + ## value labels only + mtcars2[["am"]] <- factor(mtcars2[["am"]], c(0, 1), c("automatic", "manual")) + ## variable label only + attr(mtcars2[["mpg"]], "label") <- "miles per gallon" + withr::with_tempfile("mtcars_file", fileext = ".sav", code = { + export(mtcars2, mtcars_file) + expect_true(file.exists(mtcars_file)) + expect_true(d <- is.data.frame(import(mtcars_file))) + expect_true(!"labelled" %in% unlist(lapply(d, class))) + ##Variable label and value labels preserved on SPSS (.sav) roundtrip + d <- import(mtcars_file) + a_cyl <- attributes(d[["cyl"]]) + expect_true("label" %in% names(a_cyl)) + expect_true("labels" %in% names(a_cyl)) + expect_true(identical(a_cyl[["label"]], "cylinders")) + expect_true(identical(a_cyl[["labels"]], stats::setNames(c(1.0, 2.0, 3.0), c("four", "six", "eight")))) + a_am <- attributes(d[["am"]]) + expect_true("labels" %in% names(a_am)) + expect_true(identical(a_am[["labels"]], stats::setNames(c(1.0, 2.0), c("automatic", "manual")))) - a_am <- attributes(d[["am"]]) - expect_true("labels" %in% names(a_am)) - expect_true(identical(a_am[["labels"]], stats::setNames(c(1.0, 2.0), c("automatic", "manual")))) - - a_mpg <- attributes(d[["mpg"]]) - expect_true("label" %in% names(a_mpg)) - expect_true(identical(a_mpg[["label"]], "miles per gallon")) + a_mpg <- attributes(d[["mpg"]]) + expect_true("label" %in% names(a_mpg)) + expect_true(identical(a_mpg[["label"]], "miles per gallon")) + ##haven is deprecated" + lifecycle::expect_deprecated(import(mtcars_file, haven = TRUE)) + lifecycle::expect_deprecated(import(mtcars_file, haven = FALSE)) + }) }) -test_that("Variable label and value labels preserved on SPSS compressed (.zsav) roundtrip", { - d <- import("mtcars.zsav") - a_cyl <- attributes(d[["cyl"]]) - expect_true("label" %in% names(a_cyl)) - expect_true("labels" %in% names(a_cyl)) - expect_true(identical(a_cyl[["label"]], "cylinders")) - expect_true(identical(a_cyl[["labels"]], stats::setNames(c(1.0, 2.0, 3.0), c("four", "six", "eight")))) - - a_am <- attributes(d[["am"]]) - expect_true("labels" %in% names(a_am)) - expect_true(identical(a_am[["labels"]], stats::setNames(c(1.0, 2.0), c("automatic", "manual")))) +test_that("Export to SPSS compressed (.zsav)", { + mtcars2 <- mtcars + ## label and value labels + mtcars2[["cyl"]] <- factor(mtcars2[["cyl"]], c(4, 6, 8), c("four", "six", "eight")) + attr(mtcars2[["cyl"]], "label") <- "cylinders" + ## value labels only + mtcars2[["am"]] <- factor(mtcars2[["am"]], c(0, 1), c("automatic", "manual")) + ## variable label only + attr(mtcars2[["mpg"]], "label") <- "miles per gallon" + withr::with_tempfile("mtcars_file", fileext = ".zsav", code = { + export(mtcars2, mtcars_file) + expect_true(file.exists(mtcars_file)) + expect_true(d <- is.data.frame(import(mtcars_file))) + expect_true(!"labelled" %in% unlist(lapply(d, class))) + d <- import(mtcars_file) + a_cyl <- attributes(d[["cyl"]]) + expect_true("label" %in% names(a_cyl)) + expect_true("labels" %in% names(a_cyl)) + expect_true(identical(a_cyl[["label"]], "cylinders")) + expect_true(identical(a_cyl[["labels"]], stats::setNames(c(1.0, 2.0, 3.0), c("four", "six", "eight")))) - a_mpg <- attributes(d[["mpg"]]) - expect_true("label" %in% names(a_mpg)) - expect_true(identical(a_mpg[["label"]], "miles per gallon")) -}) + a_am <- attributes(d[["am"]]) + expect_true("labels" %in% names(a_am)) + expect_true(identical(a_am[["labels"]], stats::setNames(c(1.0, 2.0), c("automatic", "manual")))) -test_that("haven is deprecated", { - lifecycle::expect_deprecated(import("mtcars.sav", haven = TRUE)) - lifecycle::expect_deprecated(import("mtcars.sav", haven = FALSE)) + a_mpg <- attributes(d[["mpg"]]) + expect_true("label" %in% names(a_mpg)) + expect_true(identical(a_mpg[["label"]], "miles per gallon")) + }) }) - - -unlink("mtcars.sav") -unlink("mtcars.zsav") diff --git a/tests/testthat/test_format_syd.R b/tests/testthat/test_format_syd.R index 2f1a5bff..6d4a91db 100644 --- a/tests/testthat/test_format_syd.R +++ b/tests/testthat/test_format_syd.R @@ -1,6 +1,5 @@ -context("Systat (.syd) imports/exports") +skip_if_not_installed("foreign") test_that("Import from Systat", { - skip_if_not_installed("foreign") - expect_true(inherits(import(system.file("files/Iris.syd", package="foreign")[1]), "data.frame")) + expect_true(is.data.frame(import(system.file("files/Iris.syd", package = "foreign")[1]))) }) diff --git a/tests/testthat/test_format_tsv.R b/tests/testthat/test_format_tsv.R index e8d5fcd5..59f548cc 100644 --- a/tests/testthat/test_format_tsv.R +++ b/tests/testthat/test_format_tsv.R @@ -1,17 +1,15 @@ -context("TSV imports/exports") -require("datasets") - -test_that("Export to TSV", { - expect_true(export(iris, "iris.tsv") %in% dir()) -}) - -test_that("Import from TSV", { - expect_true(is.data.frame(import("iris.tsv"))) +test_that("Export to and import from TSV", { + withr::with_tempfile("iris_file", fileext = ".tsv", code = { + export(iris, iris_file) + expect_true(file.exists(iris_file)) + expect_true(is.data.frame(import(iris_file))) + }) }) test_that("fread is deprecated", { - lifecycle::expect_deprecated(import("iris.tsv", fread = TRUE)) - lifecycle::expect_deprecated(import("iris.tsv", fread = FALSE)) + withr::with_tempfile("iris_file", fileext = ".tsv", code = { + export(iris, iris_file) + lifecycle::expect_deprecated(import(iris_file, fread = TRUE)) + lifecycle::expect_deprecated(import(iris_file, fread = FALSE)) + }) }) - -unlink("iris.tsv") diff --git a/tests/testthat/test_format_xls.R b/tests/testthat/test_format_xls.R index 438d57e5..4c3004e6 100644 --- a/tests/testthat/test_format_xls.R +++ b/tests/testthat/test_format_xls.R @@ -1,44 +1,47 @@ -context("Excel (xlsx) imports/exports") -require("datasets") - test_that("Export to Excel (.xlsx)", { - expect_true(export(iris, "iris.xlsx") %in% dir()) + withr::with_tempfile("iris_file", fileext = ".xlsx", code = { + export(iris, iris_file) + expect_true(file.exists(iris_file)) + expect_true(is.data.frame(import(iris_file))) + expect_true(is.data.frame(import(iris_file, sheet = 1))) + expect_true(is.data.frame(import(iris_file, which = 1))) + expect_true(nrow(import(iris_file, n_max = 42)) == 42) + }) }) test_that("Expert to Excel (.xlsx) a list", { - tempxlsx <- tempfile(fileext = ".xlsx") - export(list( - mtcars3 = mtcars[1:10, ], - mtcars2 = mtcars[11:20, ], - mtcars1 = mtcars[21:32, ] - ), tempxlsx) - expect_equal(readxl::excel_sheets(tempxlsx), c("mtcars3", "mtcars2", "mtcars1")) + withr::with_tempfile("tempxlsx", fileext = ".xlsx", code = { + export(list( + mtcars3 = mtcars[1:10, ], + mtcars2 = mtcars[11:20, ], + mtcars1 = mtcars[21:32, ] + ), tempxlsx) + expect_equal(readxl::excel_sheets(tempxlsx), c("mtcars3", "mtcars2", "mtcars1")) + }) }) test_that("Is `sheet` passed?", { - tempxlsx <- tempfile(fileext = ".xlsx") - export(list( - mtcars3 = mtcars[1:10, ], - mtcars2 = mtcars[11:20, ], - mtcars1 = mtcars[21:32, ] - ), tempxlsx) - expect_equal(readxl::excel_sheets(tempxlsx), c("mtcars3", "mtcars2", "mtcars1")) - content <- import(tempxlsx, sheet = "mtcars2") - expect_equal(content$mpg, mtcars[11:20, ]$mpg) - content <- import(tempxlsx, which = 2) - expect_equal(content$mpg, mtcars[11:20, ]$mpg) + withr::with_tempfile("tempxlsx", fileext = ".xlsx", code = { + export(list( + mtcars3 = mtcars[1:10, ], + mtcars2 = mtcars[11:20, ], + mtcars1 = mtcars[21:32, ] + ), tempxlsx) + expect_equal(readxl::excel_sheets(tempxlsx), c("mtcars3", "mtcars2", "mtcars1")) + content <- import(tempxlsx, sheet = "mtcars2") + expect_equal(content$mpg, mtcars[11:20, ]$mpg) + content <- import(tempxlsx, which = 2) + expect_equal(content$mpg, mtcars[11:20, ]$mpg) + }) }) test_that("readxl is deprecated", { - lifecycle::expect_deprecated(import("iris.xlsx", readxl = TRUE)) - lifecycle::expect_deprecated(import("iris.xlsx", readxl = FALSE)) -}) - -test_that("Import from Excel (.xlsx)", { - expect_true(is.data.frame(import("iris.xlsx", sheet = 1))) - expect_true(is.data.frame(import("iris.xlsx", which = 1))) - expect_true(nrow(import("iris.xlsx", n_max = 42)) == 42) + withr::with_tempfile("iris_file", fileext = ".xlsx", code = { + export(iris, iris_file) + lifecycle::expect_deprecated(import(iris_file, readxl = TRUE)) + lifecycle::expect_deprecated(import(iris_file, readxl = FALSE)) + }) }) test_that("Import from Excel (.xls)", { @@ -46,6 +49,3 @@ test_that("Import from Excel (.xls)", { expect_true(is.data.frame(import("../testdata/iris.xls", sheet = 1))) expect_true(is.data.frame(import("../testdata/iris.xls", which = 1))) }) - - -unlink("iris.xlsx") diff --git a/tests/testthat/test_format_xml.R b/tests/testthat/test_format_xml.R index cdc1f5cc..7d7a8438 100644 --- a/tests/testthat/test_format_xml.R +++ b/tests/testthat/test_format_xml.R @@ -1,32 +1,26 @@ -context("XML imports/exports") -require("datasets") +skip_if_not_installed("xml2") test_xml <- function(breaker = "&") { mtcars2 <- mtcars colnames(mtcars2)[1] <- paste0("mp", breaker, breaker, "g") mtcars2[1,1] <- paste0("mp", breaker, breaker, "g") - expect_error(x <- rio::export(mtcars2, tempfile(fileext = ".xml")), NA) - temp_df <- rio::import(x) - expect_equal(colnames(temp_df)[1], paste0("mp..g")) - expect_equal(temp_df[1,1], paste0("mp", breaker, breaker, "g")) + withr::with_tempfile("mtcars_file", fileext = ".xml", code = { + expect_error(x <- rio::export(mtcars2, mtcars_file), NA) + temp_df <- rio::import(mtcars_file) + expect_equal(colnames(temp_df)[1], paste0("mp..g")) + expect_equal(temp_df[1,1], paste0("mp", breaker, breaker, "g")) + }) } -test_that("Export to XML", { - skip_if_not_installed("xml2") - #skip("temporarily skipping (https://github.com/r-lib/xml2/issues/339)") - expect_true(export(iris, "iris.xml") %in% dir())}) +test_that("Export to and import from XML", { + withr::with_tempfile("iris_file", fileext = ".xml", code = { + export(iris, iris_file) + expect_true(file.exists(iris_file)) + expect_true(is.data.frame(import(iris_file))) + }) +}) test_that("Export to XML with &, >, ', \", >, <",{ - skip_if_not_installed("xml2") ## test all useless <- lapply(c("&", "\"", "'", "<", ">"), test_xml) - }) - -test_that("Import from XML", { - skip_if_not_installed("xml2") - skip("temporarily skipping (https://github.com/r-lib/xml2/issues/339)") - expect_true(is.data.frame(import("iris.xml"))) -}) - -unlink(c("iris.xml","iris2.xml")) diff --git a/tests/testthat/test_format_yml.R b/tests/testthat/test_format_yml.R index ac441a64..143e9b04 100644 --- a/tests/testthat/test_format_yml.R +++ b/tests/testthat/test_format_yml.R @@ -1,26 +1,21 @@ -context("YAML imports/exports") -require("datasets") +skip_if_not_installed("yaml") -test_that("Export to YAML", { - skip_if_not_installed("yaml") - expect_true(export(iris, "iris.yml") %in% dir()) -}) - -test_that("Import from YAML", { - skip_if_not_installed("yaml") - expect_true(is.data.frame(import("iris.yml"))) - expect_identical(import("iris.yml")[, 1:4], iris[, 1:4]) - expect_identical(import("iris.yml")$Species, as.character(iris$Species)) +test_that("Export to and import from YAML", { + withr::with_tempfile("iris_file", fileext = ".yaml", code = { + export(iris, iris_file) + expect_true(file.exists(iris_file)) + expect_true(is.data.frame(import(iris_file))) + expect_identical(import(iris_file)[, 1:4], iris[, 1:4]) + expect_identical(import(iris_file)$Species, as.character(iris$Species)) + }) }) test_that("utf-8", { skip_if(getRversion() <= "4.2") - content <- c("\"", "\u010d", "\u0161", "\u00c4", "\u5b57", "\u30a2", "\u30a2\u30e0\u30ed") - x <- data.frame(col = content) - tempyaml <- tempfile(fileext = ".yaml") - y <- import(export(x, tempyaml)) - testthat::expect_equal(content, y$col) + withr::with_tempfile("tempyaml", fileext = ".yaml", code = { + content <- c("\"", "\u010d", "\u0161", "\u00c4", "\u5b57", "\u30a2", "\u30a2\u30e0\u30ed") + x <- data.frame(col = content) + y <- import(export(x, tempyaml)) + testthat::expect_equal(content, y$col) + }) }) - - -unlink("iris.yml") diff --git a/tests/testthat/test_gather_attrs.R b/tests/testthat/test_gather_attrs.R index 42bc91b4..3fdb1ec8 100644 --- a/tests/testthat/test_gather_attrs.R +++ b/tests/testthat/test_gather_attrs.R @@ -1,4 +1,3 @@ -context("Gather attrs") e <- try(import("http://www.stata-press.com/data/r13/auto.dta")) if (!inherits(e, "try-error")) { @@ -45,7 +44,4 @@ if (!inherits(e, "try-error")) { test_that("spread_attrs() fails on non-data frame", { expect_error(spread_attrs(letters)) }) - - rm(e) - } diff --git a/tests/testthat/test_guess.R b/tests/testthat/test_guess.R index 71c5046c..a8aa6aa0 100644 --- a/tests/testthat/test_guess.R +++ b/tests/testthat/test_guess.R @@ -1,6 +1,3 @@ -context("Get File Extension") -library("datasets") - test_that("File extension converted correctly", { expect_that(get_ext("hello.csv"), equals("csv")) expect_that(get_ext("hello.CSV"), equals("csv")) @@ -21,8 +18,11 @@ test_that("Format converted correctly", { }) test_that("Export without file specified", { - expect_true(export(iris, format = "csv") %in% dir()) - unlink("iris.csv") + withr::with_tempdir(code = { + project_path <- getwd() + export(iris, format = "csv") + expect_true(file.exists(file.path(project_path, "iris.csv"))) + }) }) test_that(".check_pkg_availability", { diff --git a/tests/testthat/test_identical.R b/tests/testthat/test_identical.R index c5541557..efed7561 100644 --- a/tests/testthat/test_identical.R +++ b/tests/testthat/test_identical.R @@ -1,57 +1,56 @@ -context("Check Data Identical") - test_that("Data identical (text formats)", { - expect_equivalent(import(export(mtcars, "mtcars.txt")), mtcars) - expect_equivalent(import(export(mtcars, "mtcars.csv")), mtcars) - expect_equivalent(import(export(mtcars, "mtcars.tsv")), mtcars) + withr::with_tempdir(code = { + expect_equivalent(import(export(mtcars, "mtcars.txt")), mtcars) + expect_equivalent(import(export(mtcars, "mtcars.csv")), mtcars) + expect_equivalent(import(export(mtcars, "mtcars.tsv")), mtcars) + }) }) -unlink("mtcars.txt") -unlink("mtcars.csv") -unlink("mtcars.tsv") test_that("Data identical (R formats)", { - expect_equivalent(import(export(mtcars, "mtcars.rds")), mtcars) - expect_equivalent(import(export(mtcars, "mtcars.R")), mtcars) - expect_equivalent(import(export(mtcars, "mtcars.RData")), mtcars) - expect_equivalent(import(export(mtcars, "mtcars.R", format = "dump")), mtcars) + withr::with_tempdir(code = { + expect_equivalent(import(export(mtcars, "mtcars.rds")), mtcars) + expect_equivalent(import(export(mtcars, "mtcars.R")), mtcars) + expect_equivalent(import(export(mtcars, "mtcars.RData")), mtcars) + expect_equivalent(import(export(mtcars, "mtcars.R", format = "dump")), mtcars) + }) }) -unlink("mtcars.rds") -unlink("mtcars.R") -unlink("mtcars.RData") + test_that("Data identical (R formats), feather", { skip_if_not_installed("arrow") - expect_equivalent(import(export(mtcars, "mtcars.feather")), mtcars) - unlink("mtcars.feather") + withr::with_tempdir(code = { + expect_equivalent(import(export(mtcars, "mtcars.feather")), mtcars) + }) }) test_that("Data identical (haven formats)", { - expect_equivalent(import(export(mtcars, "mtcars.dta")), mtcars) - expect_equivalent(import(export(mtcars, "mtcars.sav")), mtcars) + withr::with_tempdir(code = { + expect_equivalent(import(export(mtcars, "mtcars.dta")), mtcars) + expect_equivalent(import(export(mtcars, "mtcars.sav")), mtcars) + }) }) -unlink("mtcars.dta") -unlink("mtcars.sav") test_that("Data identical (Excel formats)", { - expect_equivalent(import(export(mtcars, "mtcars.xlsx")), mtcars) + withr::with_tempdir(code = { + expect_equivalent(import(export(mtcars, "mtcars.xlsx")), mtcars) + }) }) -unlink("mtcars.xlsx") test_that("Data identical (other formats)", { - expect_equivalent(import(export(mtcars, "mtcars.dbf")), mtcars) - expect_equivalent(import(export(mtcars, "mtcars.json")), mtcars) - expect_equivalent(import(export(mtcars, "mtcars.arff")), mtcars) - expect_equivalent(import(export(mtcars, "mtcars.xml")), mtcars) + skip_if_not_installed("xml2") + skip_if_not_installed("jsonlite") + withr::with_tempdir(code = { + expect_equivalent(import(export(mtcars, "mtcars.dbf")), mtcars) + expect_equivalent(import(export(mtcars, "mtcars.json")), mtcars) + expect_equivalent(import(export(mtcars, "mtcars.arff")), mtcars) + expect_equivalent(import(export(mtcars, "mtcars.xml")), mtcars) + }) }) -unlink("mtcars.dbf") -unlink("mtcars.json") -unlink("mtcars.arff") -unlink("mtcars.xml") test_that("Data identical (optional arguments)", { - #expect_equivalent(import(export(mtcars, "mtcars.csv", format = "csv2"), format = "csv2"), mtcars) - expect_equivalent(import(export(mtcars, "mtcars.csv"), nrows = 4), mtcars[1:4,]) - expect_equivalent(import(export(mtcars, "mtcars.csv", format = "tsv"), format = "tsv"), mtcars) - expect_true(all.equal(import(export(mtcars, "mtcars", format = "csv"), format = "csv"), mtcars, check.attributes = FALSE)) + withr::with_tempdir(code = { + ##expect_equivalent(import(export(mtcars, "mtcars.csv", format = "csv2"), format = "csv2"), mtcars) + expect_equivalent(import(export(mtcars, "mtcars.csv"), nrows = 4), mtcars[1:4,]) + expect_equivalent(import(export(mtcars, "mtcars.csv", format = "tsv"), format = "tsv"), mtcars) + expect_true(all.equal(import(export(mtcars, "mtcars", format = "csv"), format = "csv"), mtcars, check.attributes = FALSE)) + }) }) -unlink("mtcars.csv") -unlink("mtcars") diff --git a/tests/testthat/test_import_list.R b/tests/testthat/test_import_list.R index d263d4a1..e9e51b2f 100644 --- a/tests/testthat/test_import_list.R +++ b/tests/testthat/test_import_list.R @@ -1,22 +1,22 @@ -context("Test import_list()") -library("datasets") - -export(list(mtcars = mtcars, iris = iris), "data.rdata") -export(mtcars, "mtcars.rds") - test_that("Data identical (import_list)", { - expect_equivalent(import_list(rep("mtcars.rds", 2)), list(mtcars, mtcars)) - mdat <- rbind(mtcars, mtcars) - dat <- import_list(rep("mtcars.rds", 2), rbind = TRUE) - expect_true(ncol(dat) == ncol(mdat) + 1) - expect_true(nrow(dat) == nrow(mdat)) - expect_true("_file" %in% names(dat)) + withr::with_tempfile("mtcars_file", fileext = ".rds", code = { + export(mtcars, mtcars_file) + expect_equivalent(import_list(rep(mtcars_file, 2)), list(mtcars, mtcars)) + mdat <- rbind(mtcars, mtcars) + dat <- import_list(rep(mtcars_file, 2), rbind = TRUE) + expect_true(ncol(dat) == ncol(mdat) + 1) + expect_true(nrow(dat) == nrow(mdat)) + expect_true("_file" %in% names(dat)) + }) }) test_that("Import multi-object .Rdata in import_list()", { - dat <- import_list("data.rdata") - expect_true(identical(dat[[1]], mtcars)) - expect_true(identical(dat[[2]], iris)) + withr::with_tempfile("rdata_file", fileext = ".rdata", code = { + export(list(mtcars = mtcars, iris = iris), rdata_file) + dat <- import_list(rdata_file) + expect_true(identical(dat[[1]], mtcars)) + expect_true(identical(dat[[2]], iris)) + }) }) test_that("Import multiple HTML tables in import_list()", { @@ -28,76 +28,85 @@ test_that("Import multiple HTML tables in import_list()", { }) test_that("Import multiple HTML tables in import_list() but with htm #350", { - temphtm <- tempfile(fileext = ".htm") - file.copy("../testdata/twotables.html", temphtm) - dat <- import_list(temphtm) - expect_true(identical(dim(dat[[1]]), dim(mtcars))) - expect_true(identical(names(dat[[1]]), names(mtcars))) - expect_true(identical(dim(dat[[2]]), dim(iris))) - expect_true(identical(names(dat[[2]]), names(iris))) + withr::with_tempfile("temphtm", fileext = ".htm", code = { + temphtm <- tempfile(fileext = ".htm") + file.copy("../testdata/twotables.html", temphtm) + dat <- import_list(temphtm) + expect_true(identical(dim(dat[[1]]), dim(mtcars))) + expect_true(identical(names(dat[[1]]), names(mtcars))) + expect_true(identical(dim(dat[[2]]), dim(iris))) + expect_true(identical(names(dat[[2]]), names(iris))) + }) }) - test_that("import_list() preserves 'which' names when specified", { - export(list(a = mtcars, b = iris), "foo.xlsx") - expect_true(identical(names(import_list("foo.xlsx")), c("a", "b"))) - expect_true(identical(names(import_list("foo.xlsx", which = 1)), "a")) - expect_true(identical(names(import_list("foo.xlsx", which = "a")), "a")) - expect_true(identical(names(import_list("foo.xlsx", which = 2)), "b")) - expect_true(identical(names(import_list("foo.xlsx", which = "b")), "b")) - expect_true(identical(names(import_list("foo.xlsx", which = 1:2)), c("a", "b"))) - expect_true(identical(names(import_list("foo.xlsx", which = 2:1)), c("b", "a"))) - expect_true(identical(names(import_list("foo.xlsx", which = c("a", "b"))), c("a", "b"))) - expect_true(identical(names(import_list("foo.xlsx", which = c("b", "a"))), c("b", "a"))) - unlink("foo.xlsx") + withr::with_tempfile("data_file", fileext = ".xlsx", code = { + export(list(a = mtcars, b = iris), data_file) + expect_true(identical(names(import_list(data_file)), c("a", "b"))) + expect_true(identical(names(import_list(data_file, which = 1)), "a")) + expect_true(identical(names(import_list(data_file, which = "a")), "a")) + expect_true(identical(names(import_list(data_file, which = 2)), "b")) + expect_true(identical(names(import_list(data_file, which = "b")), "b")) + expect_true(identical(names(import_list(data_file, which = 1:2)), c("a", "b"))) + expect_true(identical(names(import_list(data_file, which = 2:1)), c("b", "a"))) + expect_true(identical(names(import_list(data_file, which = c("a", "b"))), c("a", "b"))) + expect_true(identical(names(import_list(data_file, which = c("b", "a"))), c("b", "a"))) + }) }) test_that("Import single file via import_list()", { - expect_true(identical(import_list("mtcars.rds", rbind = TRUE), mtcars)) + withr::with_tempfile("data_file", fileext = ".rds", code = { + export(mtcars, data_file) + expect_true(identical(import_list(data_file, rbind = TRUE), mtcars)) + }) }) test_that("Import single file from zip via import_list()", { - export(mtcars, "mtcars.csv.zip", format = "csv") - expect_true(inherits(import_list("mtcars.csv.zip")[[1L]], "data.frame")) - expect_true(inherits(import_list("mtcars.csv.zip", which = 1)[[1L]], "data.frame")) - expect_true(inherits(import_list("mtcars.csv.zip", which = "mtcars.csv")[[1L]], "data.frame")) + withr::with_tempfile("data_file", fileext = ".csv.zip", code = { + export(mtcars, data_file, format = "csv") + expect_true(is.data.frame(import_list(data_file)[[1L]])) + expect_true(is.data.frame(import_list(data_file, which = 1)[[1L]])) + basefile_name <- gsub(".zip$", "", basename(data_file)) + expect_true(is.data.frame(import_list(data_file, which = basefile_name)[[1L]])) + }) }) test_that("Using setclass in import_list()", { - dat1 <- import_list(rep("mtcars.rds", 2), setclass = "data.table", rbind = TRUE) - expect_true(inherits(dat1, "data.table")) - dat2 <- import_list(rep("mtcars.rds", 2), setclass = "tbl", rbind = TRUE) - expect_true(inherits(dat2, "tbl")) + withr::with_tempfile("data_file", fileext = ".rds", code = { + export(mtcars, data_file) + dat1 <- import_list(rep(data_file, 2), setclass = "data.table", rbind = TRUE) + expect_true(inherits(dat1, "data.table")) + dat2 <- import_list(rep(data_file, 2), setclass = "tbl", rbind = TRUE) + expect_true(inherits(dat2, "tbl")) + }) }) test_that("Object names are preserved by import_list()", { - export(list(mtcars1 = mtcars[1:10,], - mtcars2 = mtcars[11:20,], - mtcars3 = mtcars[21:32,]), "mtcars.xlsx") - export(mtcars[1:10,], "mtcars1.csv") - export(mtcars[11:20,], "mtcars2.tsv") - export(mtcars[21:32,], "mtcars3.csv") - expected_names <- c("mtcars1", "mtcars2", "mtcars3") - dat_xls <- import_list("mtcars.xlsx") - dat_csv <- import_list(c("mtcars1.csv","mtcars2.tsv","mtcars3.csv")) - - expect_identical(names(dat_xls), expected_names) - expect_identical(names(dat_csv), expected_names) - - unlink(c("mtcars.xlsx", "mtcars1.csv","mtcars2.tsv","mtcars3.csv")) + withr::with_tempdir(code = { + export(list(mtcars1 = mtcars[1:10,], + mtcars2 = mtcars[11:20,], + mtcars3 = mtcars[21:32,]), "mtcars.xlsx") + export(mtcars[1:10,], "mtcars1.csv") + export(mtcars[11:20,], "mtcars2.tsv") + export(mtcars[21:32,], "mtcars3.csv") + expected_names <- c("mtcars1", "mtcars2", "mtcars3") + dat_xls <- import_list("mtcars.xlsx") + dat_csv <- import_list(c("mtcars1.csv","mtcars2.tsv","mtcars3.csv")) + expect_identical(names(dat_xls), expected_names) + expect_identical(names(dat_csv), expected_names) + }) }) test_that("File names are added as attributes by import_list()", { - export(mtcars[1:10,], "mtcars.csv") - export(mtcars[11:20,], "mtcars.tsv") - expected_names <- c("mtcars", "mtcars") - expected_attrs <- c(mtcars = "mtcars.csv", mtcars = "mtcars.tsv") - dat <- import_list(c("mtcars.csv","mtcars.tsv")) - - expect_identical(names(dat), expected_names) - expect_identical(unlist(lapply(dat, attr, "filename")), expected_attrs) - - unlink(c("mtcars.csv", "mtcars.tsv")) + withr::with_tempdir(code = { + export(mtcars[1:10,], "mtcars.csv") + export(mtcars[11:20,], "mtcars.tsv") + expected_names <- c("mtcars", "mtcars") + expected_attrs <- c(mtcars = "mtcars.csv", mtcars = "mtcars.tsv") + dat <- import_list(c("mtcars.csv","mtcars.tsv")) + expect_identical(names(dat), expected_names) + expect_identical(unlist(lapply(dat, attr, "filename")), expected_attrs) + }) }) test_that("URL #294", { @@ -120,10 +129,11 @@ test_that("URL #294", { test_that("Universal dummy `which` #326", { formats <- c("xlsx", "dta", "sav", "csv", "csv2") for (format in formats) { - tempzip <- tempfile(fileext = paste0(".", format, ".zip")) - rio::export(mtcars, tempzip, format = format) - expect_warning(rio::import(tempzip), NA) - expect_warning(rio::import_list(tempzip), NA) + withr::with_tempfile("tempzip", fileext = paste0(".", format, ".zip"), code = { + rio::export(mtcars, tempzip, format = format) + expect_warning(rio::import(tempzip), NA) + expect_warning(rio::import_list(tempzip), NA) + }) } }) @@ -134,23 +144,31 @@ test_that("Universal dummy `which` (Suggests) #326", { skip_on_os("mac") ## apache/arrow#40991 formats <- c("qs", "parquet", "ods") for (format in formats) { - tempzip <- tempfile(fileext = paste0(".", format, ".zip")) - rio::export(mtcars, tempzip, format = format) - expect_warning(rio::import(tempzip), NA) - expect_warning(rio::import_list(tempzip), NA) + withr::with_tempfile("tempzip", fileext = paste0(".", format, ".zip"), code = { + rio::export(mtcars, tempzip, format = format) + expect_warning(rio::import(tempzip), NA) + expect_warning(rio::import_list(tempzip), NA) + }) } }) test_that("Informative message when files are not found #389", { - expect_warning(import_list(c("mtcars.rds", "nonexisting.rds")), "^Import failed for nonexisting") + withr::with_tempfile("mtcars_file", fileext = ".rds", code = { + export(mtcars, mtcars_file) + expect_true(file.exists(mtcars_file)) + expect_false(file.exists("nonexisting.rds")) + expect_warning(import_list(c(mtcars_file, "nonexisting.rds")), "^Import failed for nonexisting") + }) }) test_that("Missing files and rbind", { - expect_warning(x <- import_list(c("mtcars.rds", "nonexisting.rds"), rbind = TRUE), "^Import failed for nonexisting") - expect_warning(x <- import_list(c("nonexisting.rds", "nonexisting2.rds"), rbind = TRUE), "^Import failed for nonexisting") - expect_true(is.data.frame(x)) + withr::with_tempfile("mtcars_file", fileext = ".rds", code = { + export(mtcars, mtcars_file) + expect_true(file.exists(mtcars_file)) + expect_false(file.exists("nonexisting.rds")) + expect_false(file.exists("nonexisting2.rds")) + expect_warning(x <- import_list(c(mtcars_file, "nonexisting.rds"), rbind = TRUE), "^Import failed for nonexisting") + expect_true(is.data.frame(x)) + expect_warning(x <- import_list(c("nonexisting.rds", "nonexisting2.rds"), rbind = TRUE), "^Import failed for nonexisting") + }) }) - -unlink("data.rdata") -unlink("mtcars.rds") -unlink("mtcars.csv.zip") diff --git a/tests/testthat/test_install_formats.R b/tests/testthat/test_install_formats.R index d4f00763..96fc6f27 100644 --- a/tests/testthat/test_install_formats.R +++ b/tests/testthat/test_install_formats.R @@ -1,5 +1,3 @@ -context("Install uninstalled formats") - test_that("uninstalled_formats()", { skip_on_cran() formats <- uninstalled_formats() diff --git a/tests/testthat/test_mapping.R b/tests/testthat/test_mapping.R index 7fca3e5e..a2524560 100644 --- a/tests/testthat/test_mapping.R +++ b/tests/testthat/test_mapping.R @@ -1,30 +1,33 @@ test_that("mapping; both base and tidy conventions work", { - tempxlsx <- tempfile(fileext = ".xlsx") - export(list("mtcars" = mtcars, "iris" = iris), tempxlsx) - expect_error(y <- import(tempxlsx, n_max = 42, sheet = "iris"), NA) - expect_equal(nrow(y), 42) - expect_error(y2 <- import(tempxlsx, n_max = 42, which = "iris"), NA) - expect_equal(nrow(y2), 42) - expect_equal(y, y2) - expect_error(y <- import(tempxlsx, n_max = 42, col_names = FALSE, which = 2), NA) - expect_equal(nrow(y), 42) - expect_error(y2 <- import(tempxlsx, n_max = 42, header = FALSE, which = 2), NA) - expect_equal(y, y2) + withr::with_tempfile("tempxlsx", fileext = ".xlsx", code = { + export(list("mtcars" = mtcars, "iris" = iris), tempxlsx) + expect_error(y <- import(tempxlsx, n_max = 42, sheet = "iris"), NA) + expect_equal(nrow(y), 42) + expect_error(y2 <- import(tempxlsx, n_max = 42, which = "iris"), NA) + expect_equal(nrow(y2), 42) + expect_equal(y, y2) + expect_error(y <- import(tempxlsx, n_max = 42, col_names = FALSE, which = 2), NA) + expect_equal(nrow(y), 42) + expect_error(y2 <- import(tempxlsx, n_max = 42, header = FALSE, which = 2), NA) + expect_equal(y, y2) + }) }) test_that("Unused arguments are by default ignored silently", { - tempxlsx <- tempfile(fileext = ".xlsx") - export(list("mtcars" = mtcars, "iris" = iris), tempxlsx) - expect_error(y <- import(tempxlsx, n_max = 42, whatever = TRUE, sheet = 2), NA) + withr::with_tempfile("tempxlsx", fileext = ".xlsx", code = { + export(list("mtcars" = mtcars, "iris" = iris), tempxlsx) + expect_error(y <- import(tempxlsx, n_max = 42, whatever = TRUE, sheet = 2), NA) + }) }) test_that("Unused arguments with option", { - tempxlsx <- tempfile(fileext = ".xlsx") - export(list("mtcars" = mtcars, "iris" = iris), tempxlsx) - expect_error(R.utils::withOptions({ - y <- import(tempxlsx, n_max = 42, whatever = TRUE) - }, rio.ignoreunusedargs = FALSE)) - expect_error(R.utils::withOptions({ - y <- import(tempxlsx, n_max = 42, sheet = 2, whatever = TRUE) - }, rio.ignoreunusedargs = FALSE), "whatever") ## not sheet + withr::with_tempfile("tempxlsx", fileext = ".xlsx", code = { + export(list("mtcars" = mtcars, "iris" = iris), tempxlsx) + expect_error(R.utils::withOptions({ + y <- import(tempxlsx, n_max = 42, whatever = TRUE) + }, rio.ignoreunusedargs = FALSE)) + expect_error(R.utils::withOptions({ + y <- import(tempxlsx, n_max = 42, sheet = 2, whatever = TRUE) + }, rio.ignoreunusedargs = FALSE), "whatever") ## not sheet + }) }) diff --git a/tests/testthat/test_matrix.R b/tests/testthat/test_matrix.R index 561df6f9..1dbef977 100644 --- a/tests/testthat/test_matrix.R +++ b/tests/testthat/test_matrix.R @@ -1,15 +1,10 @@ -context("Matrix imports/exports") -require("datasets") - -test_that("Export matrix to CSV", { - expect_true(export(warpbreaks, "temp1.csv") %in% dir()) - expect_true(export(as.matrix(warpbreaks), "temp2.csv") %in% dir()) +test_that("Export matrix to and import from CSV", { + withr::with_tempfile("temp_files", fileext = c(".csv", ".csv"), code = { + export(warpbreaks, temp_files[1]) + export(as.matrix(warpbreaks), temp_files[2]) + expect_true(file.exists(temp_files[1])) + expect_true(file.exists(temp_files[2])) + expect_true(identical(import(temp_files[1], colClasses = rep("character", 3)), + import(temp_files[2], colClasses = rep("character", 3)))) + }) }) - -test_that("Import from matrix export", { - expect_true(identical(import("temp1.csv", colClasses = rep("character", 3)), - import("temp2.csv", colClasses = rep("character", 3)))) -}) - -unlink("temp1.csv") -unlink("temp2.csv") diff --git a/tests/testthat/test_remote.R b/tests/testthat/test_remote.R index b078b3cd..a6e922ad 100644 --- a/tests/testthat/test_remote.R +++ b/tests/testthat/test_remote.R @@ -1,5 +1,3 @@ -context("Remote Files") - test_that("Import Remote Stata File", { f <- try(import("http://www.stata-press.com/data/r13/auto.dta")) if (!inherits(f, "try-error")) { diff --git a/tests/testthat/test_set_class.R b/tests/testthat/test_set_class.R index b8eb5685..5f9562a4 100644 --- a/tests/testthat/test_set_class.R +++ b/tests/testthat/test_set_class.R @@ -1,9 +1,9 @@ -context("Set object class") -library("datasets") mtcars_tibble <- tibble::as_tibble(mtcars) mtcars_datatable <- data.table::as.data.table(mtcars) test_that("Set object class", { + mtcars_tibble <- tibble::as_tibble(mtcars) + mtcars_datatable <- data.table::as.data.table(mtcars) expect_true(inherits(set_class(mtcars), "data.frame")) expect_true(inherits(set_class(mtcars_tibble), "data.frame")) expect_true(inherits(set_class(mtcars_datatable), "data.frame")) @@ -12,6 +12,8 @@ test_that("Set object class", { }) test_that("Set object class as tibble", { + mtcars_tibble <- tibble::as_tibble(mtcars) + mtcars_datatable <- data.table::as.data.table(mtcars) expect_true(inherits(set_class(mtcars, class = "tbl_df"), "tbl_df")) expect_true(inherits(set_class(mtcars, class = "tibble"), "tbl_df")) expect_true(inherits(set_class(mtcars_tibble, class = "tibble"), "tbl_df")) @@ -19,10 +21,11 @@ test_that("Set object class as tibble", { test_that("Set object class as data.table", { expect_true(inherits(set_class(mtcars, class = "data.table"), "data.table")) - export(mtcars, "mtcars.csv") - expect_true(inherits(import("mtcars.csv", setclass = "data.table"), "data.table")) - expect_true(inherits(import("mtcars.csv", data.table = TRUE, setclass = "data.table"), "data.table")) - unlink("mtcars.csv") + withr::with_tempfile("data_file", fileext = ".csv", code = { + export(mtcars, data_file) + expect_true(inherits(import(data_file, setclass = "data.table"), "data.table")) + expect_true(inherits(import(data_file, data.table = TRUE, setclass = "data.table"), "data.table")) + }) }) test_that("Set object class as arrow table", { @@ -32,27 +35,31 @@ test_that("Set object class as arrow table", { expect_false(inherits(set_class(mtcars_arrow), "data.frame")) ## arrow table is not data.frame expect_true(inherits(set_class(mtcars, class = "arrow"), "ArrowTabular")) expect_true(inherits(set_class(mtcars, class = "arrow_table"), "ArrowTabular")) - export(mtcars, "mtcars.csv") - expect_true(inherits(import("mtcars.csv", setclass = "arrow"), "ArrowTabular")) - expect_true(inherits(import("mtcars.csv", data.table = TRUE, setclass = "arrow"), "ArrowTabular")) - unlink("mtcars.csv") + withr::with_tempfile("data_file", fileext = ".csv", code = { + export(mtcars, data_file) + expect_true(inherits(import(data_file, setclass = "arrow"), "ArrowTabular")) + expect_true(inherits(import(data_file, data.table = TRUE, setclass = "arrow"), "ArrowTabular")) + }) }) test_that("ArrowTabular can be exported", { skip_if(getRversion() <= "4.2") skip_if_not_installed("arrow") mtcars_arrow <- arrow::arrow_table(mtcars) - expect_error(export(mtcars_arrow, "mtcars.csv"), NA) ## no concept of rownames - expect_true(inherits(import("mtcars.csv"), "data.frame")) - unlink("mtcars.csv") + withr::with_tempfile("data_file", fileext = ".csv", code = { + expect_error(export(mtcars_arrow, data_file), NA) ## no concept of rownames + expect_true(inherits(import(data_file), "data.frame")) + }) }) test_that("Simulate arrow is not installed, #376", { ## although this is pretty meaningless - with_mocked_bindings({ - export(mtcars, "mtcars.csv") - expect_error(import("mtcars.csv", setclass = "arrow"), "Suggested package") - }, .check_pkg_availability = function(pkg, lib.loc = NULL) { - stop("Suggested package `", pkg, "` is not available. Please install it individually or use `install_formats()`", call. = FALSE) + withr::with_tempfile("data_file", fileext = ".csv", code = { + with_mocked_bindings({ + export(mtcars, data_file) + expect_error(import(data_file, setclass = "arrow"), "Suggested package") + }, .check_pkg_availability = function(pkg, lib.loc = NULL) { + stop("Suggested package `", pkg, "` is not available. Please install it individually or use `install_formats()`", call. = FALSE) + }) }) })