From dc308788d98897701aba94835ac5aaf231eed3f1 Mon Sep 17 00:00:00 2001 From: chainsawriot Date: Mon, 20 May 2024 10:36:32 +0200 Subject: [PATCH] Update tests --- tests/testthat/test_compress.R | 6 +----- tests/testthat/test_export_list.R | 13 ++++++------- tests/testthat/test_import_list.R | 24 ++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 12 deletions(-) diff --git a/tests/testthat/test_compress.R b/tests/testthat/test_compress.R index 8a31d09..4f4aa3f 100644 --- a/tests/testthat/test_compress.R +++ b/tests/testthat/test_compress.R @@ -70,11 +70,7 @@ test_that("Prevent the reuse of `which` for zip and tar", { withr::with_tempfile("data_path", fileext = paste0(".xlsx.", format), code = { rio::export(list(some_iris = head(iris)), data_path) expect_error(import(data_path), NA) - if (format == "zip") { - raw_file <- utils::unzip(data_path, list = TRUE)$Name[1] - } else { - raw_file <- utils::untar(data_path, list = TRUE) - } + raw_file <- .list_archive(data_path, find_compress(data_path)$compress)[1] expect_error(import(data_path, which = raw_file), NA) expect_error(suppressWarnings(import(data_path, which = "some_iris"))) }) diff --git a/tests/testthat/test_export_list.R b/tests/testthat/test_export_list.R index c995dbc..d77fa7b 100644 --- a/tests/testthat/test_export_list.R +++ b/tests/testthat/test_export_list.R @@ -39,13 +39,12 @@ test_that("export_list() works", { test_that("archive formats, #415", { withr::with_tempdir({ mylist <- list(mtcars3 = mtcars[1:10, ], mtcars2 = mtcars[11:20, ], mtcars1 = mtcars[21:32, ]) - expect_error(export_list(mylist, file = paste0("file_", 1:3, ".csv"), archive = "archive.gz"), "specified but format is not supported") - expect_error(export_list(mylist, file = paste0("file_", 1:3, ".csv"), archive = "archive.bz2"), "specified but format is not supported") - expect_error(export_list(mylist, file = paste0("file_", 1:3, ".csv"), archive = "archive.zip"), NA) - expect_error(export_list(mylist, file = paste0("file_", 1:3, ".csv"), archive = "archive.tar"), NA) - expect_error(export_list(mylist, file = paste0("file_", 1:3, ".csv"), archive = "archive.tar.gz"), NA) - expect_error(export_list(mylist, file = paste0("file_", 1:3, ".csv"), archive = "archive.tar.bz2"), NA) - ## TODO import them + expect_error(export_list(mylist, file = paste0("file_", 1:3, ".csv"), archive = "archive.csv.gz"), "specified but format is not supported") + expect_error(export_list(mylist, file = paste0("file_", 1:3, ".csv"), archive = "archive.csv.bz2"), "specified but format is not supported") + expect_error(export_list(mylist, file = paste0("file_", 1:3, ".csv"), archive = "archive.csv.zip"), NA) + expect_error(export_list(mylist, file = paste0("file_", 1:3, ".csv"), archive = "archive.csv.tar"), NA) + expect_error(export_list(mylist, file = paste0("file_", 1:3, ".csv"), archive = "archive.csv.tar.gz"), NA) + expect_error(export_list(mylist, file = paste0("file_", 1:3, ".csv"), archive = "archive.csv.tar.bz2"), NA) }) }) diff --git a/tests/testthat/test_import_list.R b/tests/testthat/test_import_list.R index a44d17d..8e79b1f 100644 --- a/tests/testthat/test_import_list.R +++ b/tests/testthat/test_import_list.R @@ -71,6 +71,30 @@ test_that("Import single file from zip via import_list()", { }) }) +test_that("Import multiple files from zip / tar.gz via import_list()", { + withr::with_tempfile("data_file", fileext = ".csv.zip", code = { + mylist <- list(mtcars3 = mtcars[1:10, ], mtcars2 = mtcars[11:20, ], mtcars1 = mtcars[21:32, ]) + expect_error(export_list(mylist, file = paste0("mtcars", 1:3, ".csv"), archive = data_file), NA) + expect_error(res <- import_list(data_file), NA) + expect_true(is.list(res)) + expect_equal(length(res), 3) + expect_true(is.data.frame(res[[1]])) + expect_true(is.data.frame(res[[2]])) + expect_true(is.data.frame(res[[3]])) + }) + withr::with_tempfile("data_file", fileext = ".csv.tar.gz", code = { + mylist <- list(mtcars3 = mtcars[1:10, ], mtcars2 = mtcars[11:20, ], mtcars1 = mtcars[21:32, ]) + expect_error(export_list(mylist, file = paste0("mtcars", 1:3, ".csv"), archive = data_file), NA) + expect_error(res <- import_list(data_file), NA) + expect_true(is.list(res)) + expect_equal(length(res), 3) + expect_true(is.data.frame(res[[1]])) + expect_true(is.data.frame(res[[2]])) + expect_true(is.data.frame(res[[3]])) + }) +}) + + test_that("Using setclass in import_list()", { withr::with_tempfile("data_file", fileext = ".rds", code = { export(mtcars, data_file)