Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chainsawriot committed May 20, 2024
1 parent a081a1a commit dc30878
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
6 changes: 1 addition & 5 deletions tests/testthat/test_compress.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")))
})
Expand Down
13 changes: 6 additions & 7 deletions tests/testthat/test_export_list.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})

Expand Down
24 changes: 24 additions & 0 deletions tests/testthat/test_import_list.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit dc30878

Please sign in to comment.