rio/ 0000755 0001762 0000144 00000000000 14651027446 011054 5 ustar ligges users rio/tests/ 0000755 0001762 0000144 00000000000 14476051106 012211 5 ustar ligges users rio/tests/testthat/ 0000755 0001762 0000144 00000000000 14651027446 014056 5 ustar ligges users rio/tests/testthat/test_format_dbf.R 0000644 0001762 0000144 00000000572 14612465270 017345 0 ustar ligges users test_that("Export to and import from XBASE (.dbf)", {
skip_if_not_installed("foreign")
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)))
})
})
rio/tests/testthat/test_gather_attrs.R 0000644 0001762 0000144 00000003357 14612465270 017735 0 ustar ligges users e <- try(import("http://www.stata-press.com/data/r13/auto.dta"))
if (!inherits(e, "try-error")) {
g <- gather_attrs(e)
test_that("Gather attrs from Stata", {
expect_true(length(attributes(e[[1]])) >= 1)
expect_true(length(attributes(g[[1]])) == 0)
expect_true(length(attributes(e)) == 5)
expect_true(length(attributes(g)) == 8)
expect_true("label" %in% names(attributes(e[[1]])))
expect_true(!"label" %in% names(attributes(g[[1]])))
expect_true("label" %in% names(attributes(g)))
expect_true("labels" %in% names(attributes(g)))
expect_true("format.stata" %in% names(attributes(g)))
expect_true(!"format.stata" %in% names(attributes(g[[1]])))
})
test_that("Spread attrs from Stata", {
s <- spread_attrs(g)
# df-level attributes
expect_true("label" %in% names(attributes(s)))
expect_true("notes" %in% names(attributes(s)))
# spread attributes
expect_true("format.stata" %in% names(attributes(s[[1]])))
expect_true(!"format.stata" %in% names(attributes(s)))
expect_true("label" %in% names(attributes(s[[1]])))
expect_true(!"labels" %in% names(attributes(s)))
})
test_that("Gather empty attributes", {
require("datasets")
g <- gather_attrs(iris)
expect_true(length(attributes(iris[[1]])) == 0)
expect_true(length(attributes(g[[1]])) == 0)
expect_true(length(attributes(iris)) == 3)
expect_true(length(attributes(g)) == 3)
})
test_that("gather_attrs() fails on non-data frame", {
expect_error(gather_attrs(letters))
})
test_that("spread_attrs() fails on non-data frame", {
expect_error(spread_attrs(letters))
})
}
rio/tests/testthat/test_characterize.R 0000644 0001762 0000144 00000002166 14047423624 017706 0 ustar ligges users context("characterize()/factorize()")
x <- structure(1:4, labels = c("A" = 1, "B" = 2, "C" = 3))
xdf <- data.frame(v1 = structure(1:4, labels = c("A" = 1, "B" = 2, "C" = 3), label = "variable 1"),
v2 = structure(c(1,0,0,1), labels = c("foo" = 0, "bar" = 1)),
v3 = 4:1, label = "variable 2")
test_that("test characterize.default()", {
expect_true(identical(characterize(x), c(LETTERS[1:3], NA)))
})
test_that("test characterize.default()", {
expect_true(identical(characterize(xdf), {xdf[] <- lapply(xdf, characterize); xdf}))
})
test_that("test factorize.data.frame()", {
expect_true(identical(factorize(x), factor(x, attributes(x)$labels, names(attributes(x)$labels))))
})
test_that("test factorize.data.frame()", {
expect_true(identical(factorize(xdf), {xdf[] <- lapply(xdf, factorize); xdf}))
})
test_that("test factorize coerce_character", {
expect_true(identical(letters[1:3], factorize(letters[1:3])))
expect_true(
identical(
factorize(letters[3:1], coerce_character = TRUE),
factor(letters[3:1], levels = letters[1:3])
)
)
}) rio/tests/testthat/test_check_file.R 0000644 0001762 0000144 00000005140 14643763372 017322 0 ustar ligges users test_that(".check_file", {
data <- data.frame(
x = sample(1:10, 10000, replace = TRUE),
y = sample(1:10, 10000, replace = TRUE)
)
expect_error(.check_file(1))
expect_error(.check_file(TRUE))
expect_error(.check_file(data))
expect_error(.check_file(iris))
expect_error(.check_file(c("a.csv", "b.csv")))
expect_error(.check_file(NA))
expect_error(.check_file(NA_character_))
expect_error(.check_file(c(NA, "a.csv")))
expect_error(.check_file(c(NA_character_, "a.csv")))
expect_error(.check_file("a.csv"), NA)
expect_error(.check_file(), NA)
## single_only FALSE
expect_error(.check_file(1, single_only = FALSE))
expect_error(.check_file(TRUE, single_only = FALSE))
expect_error(.check_file(data, single_only = FALSE))
expect_error(.check_file(iris, single_only = FALSE))
expect_error(.check_file(c("a.csv", "b.csv"), single_only = FALSE), NA)
expect_error(.check_file("a.csv"), NA)
expect_error(.check_file(single_only = FALSE), NA)
})
test_that("Invalid file argument - import(), #301", {
data <- data.frame(
x = sample(1:10, 10000, replace = TRUE),
y = sample(1:10, 10000, replace = TRUE)
)
expect_error(import(data), "Invalid")
expect_error(import(iris), "Invalid")
expect_error(import(1), "Invalid")
expect_error(import(TRUE), "Invalid")
expect_error(import(c("a.csv", "b.csv")), "Invalid")
})
test_that("Invalid file argument - import_list(), #301", {
data <- data.frame(
x = sample(1:10, 10000, replace = TRUE),
y = sample(1:10, 10000, replace = TRUE)
)
expect_error(import_list(data), "Invalid")
expect_error(import_list(iris), "Invalid")
expect_error(import_list(1), "Invalid")
expect_error(import_list(TRUE), "Invalid")
})
test_that("Invalid file argument - export(), #301", {
data <- data.frame(
x = sample(1:10, 10000, replace = TRUE),
y = sample(1:10, 10000, replace = TRUE)
)
expect_error(export(iris, data), "Invalid")
expect_error(export(iris, iris), "Invalid")
expect_error(export(iris, 1), "Invalid")
expect_error(export(iris, TRUE), "Invalid")
expect_error(export(iris, c("abc.csv", "123.csv")), "Invalid")
})
test_that("Invalid file argument - export_list(), #301", {
data <- data.frame(
x = sample(1:10, 10000, replace = TRUE),
y = sample(1:10, 10000, replace = TRUE)
)
expect_error(export_list(iris, data), "Invalid")
expect_error(export_list(iris, iris), "Invalid")
expect_error(export_list(iris, 1), "Invalid")
expect_error(export_list(iris, TRUE), "Invalid")
})
rio/tests/testthat/test_format_eviews.R 0000644 0001762 0000144 00000000256 14612465270 020113 0 ustar ligges users test_that("Import from EViews", {
skip_if_not_installed(pkg = "hexView")
expect_true(is.data.frame(suppressWarnings(import(hexView::hexViewFile("data4-1.wf1")))))
})
rio/tests/testthat/test_format_pzfx.R 0000644 0001762 0000144 00000001604 14612465270 017576 0 ustar ligges users skip_if_not_installed("pzfx")
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)))
})
})
rio/tests/testthat/test_guess.R 0000644 0001762 0000144 00000002345 14612465270 016370 0 ustar ligges users test_that("File extension converted correctly", {
expect_that(get_ext("hello.csv"), equals("csv"))
expect_that(get_ext("hello.CSV"), equals("csv"))
expect_that(get_ext("hello.sav.CSV"), equals("csv"))
expect_that(get_ext("clipboard"), equals("clipboard"))
expect_error(get_ext(1L))
})
test_that("Format converted correctly", {
expect_that(.standardize_format(","), equals("csv"))
expect_that(.standardize_format(";"), equals("csv2"))
expect_that(.standardize_format("|"), equals("psv"))
expect_that(.standardize_format("\t"), equals("tsv"))
expect_that(.standardize_format("excel"), equals("xlsx"))
expect_that(.standardize_format("stata"), equals("dta"))
expect_that(.standardize_format("spss"), equals("sav"))
expect_that(.standardize_format("sas"), equals("sas7bdat"))
})
test_that("Export without file specified", {
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", {
expect_error(.check_pkg_availability("nonexistingpkg1233222"), "Suggested package `nonexisting")
expect_error(.check_pkg_availability("rio"), NA)
})
rio/tests/testthat/test_format_rdata.R 0000644 0001762 0000144 00000004563 14621337153 017707 0 ustar ligges users 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, trust = TRUE)))
expect_true(is.data.frame(import(iris_file, which = 1, trust = TRUE)))
})
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, trust = TRUE)))
expect_true(is.data.frame(import(iris_file, which = 1, trust = TRUE)))
})
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, trust = TRUE)))
expect_true(is.data.frame(import(iris_file, which = 1, trust = TRUE)))
})
withr::with_tempfile("iris_file", fileext = ".Rdata", code = {
## expect error otherwise
expect_error(export(iris$Species, iris_file))
})
})
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, trust = TRUE)))
expect_true(is.data.frame(import(iris_file, which = 1, trust = TRUE)))
})
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, trust = TRUE)))
expect_true(is.data.frame(import(iris_file, which = 1, trust = TRUE)))
})
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, trust = TRUE)))
expect_true(is.data.frame(import(iris_file, which = 1, trust = TRUE)))
})
withr::with_tempfile("iris_file", fileext = ".rda", code = {
## expect error otherwise
expect_error(export(iris$Species, iris_file))
})
})
rio/tests/testthat/test_format_html.R 0000644 0001762 0000144 00000004474 14612657206 017565 0 ustar ligges users 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")
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 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",{
withr::with_tempfile("iris_file", fileext = ".html", code = {
iris$`R & D` <- paste(sample(letters,nrow(iris), replace = TRUE),
"&",
sample(LETTERS,nrow(iris), replace = TRUE))
export(iris, iris_file)
expect_true(file.exists(iris_file),
label = "export to html with ampersands works")
})
})
test_that("Import from HTML", {
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", {
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")
})
test_that("html with &, >, ', \", >, <", {
## test all
useless <- lapply(c("&", "\"", "'", "<", ">"), test_html)
})
rio/tests/testthat/test_set_class.R 0000644 0001762 0000144 00000005763 14612465270 017231 0 ustar ligges users 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"))
expect_true(inherits(set_class(mtcars, class = "fakeclass"), "data.frame"))
expect_true(!"fakeclass" %in% class(set_class(mtcars, class = "fakeclass")))
})
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"))
})
test_that("Set object class as data.table", {
expect_true(inherits(set_class(mtcars, class = "data.table"), "data.table"))
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", {
skip_if(getRversion() <= "4.2")
skip_if_not_installed("arrow")
mtcars_arrow <- arrow::arrow_table(mtcars)
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"))
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)
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
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)
})
})
})
rio/tests/testthat/test_export_list.R 0000644 0001762 0000144 00000007613 14643571002 017614 0 ustar ligges users library("datasets")
test_that("export_list() works", {
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("archive formats, #415", {
skip_if(getRversion() <= "4.0")
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.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)
})
})
test_that("List length of one, #385", {
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)))
})
})
test_that("tar export error for R < 4.0.3", {
skip_if(getRversion() >= "4.0.3")
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.csv.tar"), "^Exporting")
})
})
rio/tests/testthat/test_format_sav.R 0000644 0001762 0000144 00000005757 14612465270 017415 0 ustar ligges users
test_that("Export to SPSS (.sav)", {
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_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("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_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"))
})
})
rio/tests/testthat/test_format_qs.R 0000644 0001762 0000144 00000000425 14612465270 017232 0 ustar ligges users skip_if_not_installed("qs")
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)))
})
})
rio/tests/testthat/test_mapping.R 0000644 0001762 0000144 00000002736 14612465270 016701 0 ustar ligges users test_that("mapping; both base and tidy conventions work", {
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", {
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", {
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
})
})
rio/tests/testthat/test_format_json.R 0000644 0001762 0000144 00000001737 14612465270 017567 0 ustar ligges users skip_if_not_installed("jsonlite")
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)", {
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)
withr::with_tempfile("tempjson", fileext = ".json", code = {
y <- import(export(x, tempjson))
testthat::expect_equal(content, y$col)
})
})
rio/tests/testthat/test_export_corner_cases.R 0000644 0001762 0000144 00000002127 14612465270 021307 0 ustar ligges users test_that("export to nonexisting directory #347", {
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", {
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", {
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")))
})
})
rio/tests/testthat/test_install_formats.R 0000644 0001762 0000144 00000000340 14612465270 020434 0 ustar ligges users test_that("uninstalled_formats()", {
skip_on_cran()
formats <- uninstalled_formats()
if (is.null(formats)) {
expect_true(install_formats())
} else {
expect_type(formats, "character")
}
})
rio/tests/testthat/test_identical.R 0000644 0001762 0000144 00000004433 14621337153 017174 0 ustar ligges users test_that("Data identical (text formats)", {
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)
})
})
test_that("Data identical (R formats)", {
withr::with_tempdir(code = {
expect_equivalent(import(export(mtcars, "mtcars.rds"), trust = TRUE), mtcars)
expect_equivalent(import(export(mtcars, "mtcars.R"), trust = TRUE), mtcars)
expect_equivalent(import(export(mtcars, "mtcars.RData"), trust = TRUE), mtcars)
expect_equivalent(import(export(mtcars, "mtcars.R", format = "dump"), trust = TRUE), mtcars)
})
})
test_that("Data identical (R formats), feather", {
skip_if_not_installed("arrow")
withr::with_tempdir(code = {
expect_equivalent(import(export(mtcars, "mtcars.feather")), mtcars)
})
})
test_that("Data identical (haven formats)", {
withr::with_tempdir(code = {
expect_equivalent(import(export(mtcars, "mtcars.dta")), mtcars)
expect_equivalent(import(export(mtcars, "mtcars.sav")), mtcars)
})
})
test_that("Data identical (Excel formats)", {
withr::with_tempdir(code = {
expect_equivalent(import(export(mtcars, "mtcars.xlsx")), mtcars)
})
})
test_that("Data identical (other formats)", {
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)
})
})
test_that("Data identical (optional arguments)", {
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))
})
})
rio/tests/testthat/test_import_list.R 0000644 0001762 0000144 00000021542 14622630120 017574 0 ustar ligges users test_that("Data identical (import_list)", {
withr::with_tempfile("mtcars_file", fileext = ".rds", code = {
export(mtcars, mtcars_file)
expect_equivalent(import_list(rep(mtcars_file, 2), trust = TRUE), list(mtcars, mtcars))
mdat <- rbind(mtcars, mtcars)
dat <- import_list(rep(mtcars_file, 2), rbind = TRUE, trust = 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()", {
withr::with_tempfile("rdata_file", fileext = ".rdata", code = {
export(list(mtcars = mtcars, iris = iris), rdata_file)
dat <- import_list(rdata_file, trust = TRUE)
expect_true(identical(dat[[1]], mtcars))
expect_true(identical(dat[[2]], iris))
})
})
test_that("Import multiple HTML tables in import_list()", {
dat <- import_list("../testdata/twotables.html")
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 multiple HTML tables in import_list() but with htm #350", {
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", {
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()", {
withr::with_tempfile("data_file", fileext = ".rds", code = {
export(mtcars, data_file)
expect_true(identical(import_list(data_file, rbind = TRUE, trust = TRUE), mtcars))
})
})
test_that("Import single file from zip via import_list()", {
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("Import multiple files from zip 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]]))
})
})
test_that("Import multiple files from zip via import_list()", {
skip_if(getRversion() <= "4.0")
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)
dat1 <- import_list(rep(data_file, 2), setclass = "data.table", rbind = TRUE, trust = TRUE)
expect_true(inherits(dat1, "data.table"))
dat2 <- import_list(rep(data_file, 2), setclass = "tbl", rbind = TRUE, trust = TRUE)
expect_true(inherits(dat2, "tbl"))
})
})
test_that("Object names are preserved by import_list()", {
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()", {
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", {
skip_on_cran()
## url <- "https://evs.nci.nih.gov/ftp1/CDISC/SDTM/SDTM%20Terminology.xls" That's 10MB!
url <- "https://github.com/tidyverse/readxl/raw/main/tests/testthat/sheets/sheet-xml-lookup.xlsx"
expect_error(x <- import_list(url), NA)
expect_true(inherits(x, "list"))
expect_true("Asia" %in% names(x))
expect_true("Africa" %in% x[[1]]$continent)
expect_false("Africa" %in% x[[2]]$continent)
## double URLs; it reads twice the first sheet by default
urls <- c(url, url)
expect_error(x2 <- import_list(urls), NA)
expect_true("sheet-xml-lookup" %in% names(x2))
expect_true("Africa" %in% x2[[1]]$continent)
expect_true("Africa" %in% x2[[2]]$continent)
})
test_that("Universal dummy `which` #326", {
formats <- c("xlsx", "dta", "sav", "csv", "csv2")
for (format in formats) {
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("Universal dummy `which` (Suggests) #326", {
skip_if_not_installed("qs")
skip_if_not_installed("arrow")
skip_if_not_installed("readODS")
skip_on_os("mac") ## apache/arrow#40991
formats <- c("qs", "parquet", "ods")
for (format in formats) {
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", {
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"), trust = TRUE), "^Import failed for nonexisting")
})
})
test_that("Missing files and rbind", {
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, trust = TRUE), "^Import failed for nonexisting")
expect_true(is.data.frame(x))
expect_warning(x <- import_list(c("nonexisting.rds", "nonexisting2.rds"), rbind = TRUE, trust = TRUE), "^Import failed for nonexisting")
})
})
rio/tests/testthat/test_trust.R 0000644 0001762 0000144 00000005620 14621622473 016422 0 ustar ligges users test_that("Deprecation of untrusted dump", {
withr::with_tempfile("iris_file", fileext = ".dump", code = {
export(iris, iris_file)
## expect deprecation to work
lifecycle::expect_deprecated(import(iris_file), regexp = "set to FALSE by default")
## expect false to error
expect_error(import(iris_file, trust = FALSE))
})
})
test_that("Deprecation of untrusted Rdata", {
withr::with_tempfile("iris_file", fileext = ".Rdata", code = {
export(iris, iris_file)
## expect deprecation to work
lifecycle::expect_deprecated(import(iris_file), regexp = "set to FALSE by default")
## expect false to error
expect_error(import(iris_file, trust = FALSE))
})
})
test_that("Deprecation of untrusted rds", {
withr::with_tempfile("iris_file", fileext = ".rds", code = {
export(iris, iris_file)
## expect deprecation to work
lifecycle::expect_deprecated(import(iris_file), regexp = "set to FALSE by default")
## expect false to error
expect_error(import(iris_file, trust = FALSE))
})
})
test_that("No deprecation warning if `trust` is explicit", {
withr::with_tempfile("iris_file", fileext = ".rds", code = {
export(iris, iris_file)
expect_silent(import(iris_file, trust = TRUE))
expect_error(import(iris_file, trust = FALSE)) ## no warning right?
})
})
test_that("Undocumented feature, options", {
withr::with_options(list(rio.import.trust = TRUE), {
withr::with_tempfile("iris_file", fileext = ".rds", code = {
export(iris, iris_file)
expect_silent(import(iris_file))
expect_error(import(iris_file), NA)
})
})
withr::with_options(list(rio.import.trust = FALSE), {
withr::with_tempfile("iris_file", fileext = ".rds", code = {
export(iris, iris_file)
expect_error(import(iris_file))
})
})
})
test_that("`trust` wont cause problems for other import methods", {
withr::with_tempfile("iris_file", fileext = ".xlsx", code = {
export(iris, iris_file)
expect_silent(import(iris_file, trust = TRUE))
expect_error(import(iris_file, trust = FALSE), NA)
})
})
test_that("`trust` for import_list()", {
withr::with_tempfile("iris_file", fileext = ".rdata", code = {
export(iris, iris_file)
lifecycle::expect_deprecated(import_list(iris_file), regexp = "set to FALSE by default")
expect_silent(import_list(iris_file, trust = TRUE))
expect_error(import_list(iris_file, trust = FALSE))
})
})
test_that("`trust` wont cause problems for other formats in import_list", {
withr::with_tempfile("data_file", fileext = ".xlsx", code = {
export(list(a = mtcars, b = iris), data_file)
expect_silent(import(data_file))
expect_silent(import(data_file, trust = TRUE))
expect_error(import(data_file, trust = FALSE), NA)
})
})
rio/tests/testthat/test_format_matlab.R 0000644 0001762 0000144 00000000605 14612465270 020047 0 ustar ligges users skip_if_not_installed("rmatio")
test_that("Export to and import from matlab", {
skip("failing mysteriously")
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)))
})
})
rio/tests/testthat/test_format_fortran.R 0000644 0001762 0000144 00000000453 14612465270 020263 0 ustar ligges users test_that("Import from Fortran", {
## 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"))))
})
})
rio/tests/testthat/test_format_external_packages.R 0000644 0001762 0000144 00000001306 14612465270 022266 0 ustar ligges users 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) {
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) {
withr::with_tempfile("dummyfile", fileext = paste0(".", ext), code = {
file.create(dummyfile)
expect_error(import(dummyfile))
})
}
})
rio/tests/testthat/test_errors.R 0000644 0001762 0000144 00000003107 14612465270 016553 0 ustar ligges users library("datasets")
test_that("Function suggestions for unsupported export", {
expect_error(export(data.frame(1), "test.jpg"),
"jpg format not supported. Consider using the 'jpeg::writeJPEG()' function",
fixed = TRUE)
})
test_that("Error for unsupported file types", {
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")
})
test_that("Error for mixed support file types", {
expect_error(import("test.por"), "No such file")
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", {
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", {
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")) ## no error
})
rio/tests/testthat/test_format_tsv.R 0000644 0001762 0000144 00000001026 14612465270 017421 0 ustar ligges users 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", {
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))
})
})
rio/tests/testthat/test_format_ods.R 0000644 0001762 0000144 00000004027 14612465270 017376 0 ustar ligges users skip_if_not_installed("readODS")
test_that("Import from ODS", {
ods0 <- import("../testdata/mtcars.ods")
expect_error(ods <- import("../testdata/mtcars.ods",
sheet = 1, col_names = TRUE,
invalid_argument = 42), NA)
expect_identical(ods0, ods, label = "ODS import ignored arguments don't affect output")
expect_true(is.data.frame(ods), label = "ODS import returns data.frame")
expect_true(identical(names(mtcars), names(ods)), label = "ODS import returns correct names")
expect_true(identical(dim(mtcars), dim(ods)), label = "ODS import returns correct dimensions")
expect_equivalent(ods, mtcars, label = "ODS import returns correct values")
})
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", {
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", {
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", {
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"))
})
})
rio/tests/testthat/test_convert.R 0000644 0001762 0000144 00000002245 14612465270 016721 0 ustar ligges users library("datasets")
test_that("Basic file conversion", {
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", {
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", {
withr::with_tempfile("mtcars_file", fileext = ".dta", {
export(mtcars, mtcars_file)
expect_error(convert(mtcars_file))
})
})
rio/tests/testthat/test_matrix.R 0000644 0001762 0000144 00000000771 14612465270 016547 0 ustar ligges users 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))))
})
})
rio/tests/testthat/test_create_outfiles.R 0000644 0001762 0000144 00000001776 14500016150 020407 0 ustar ligges users test_that(".create_outfiles works", {
x <- list(
a = data.frame(),
b = data.frame(),
c = data.frame()
)
y <- list(
data.frame(),
data.frame(),
data.frame()
)
expect_identical(.create_outfiles("d_%s.csv", x), c("d_a.csv", "d_b.csv", "d_c.csv"))
expect_identical(.create_outfiles("d_%s.csv", y), c("d_1.csv", "d_2.csv", "d_3.csv"))
expect_identical(.create_outfiles(c("a.csv", "b.csv", "c.csv"), x), c("a.csv", "b.csv", "c.csv"))
})
test_that(".create_outfiles errors", {
x <- list(
a = data.frame(),
a = data.frame(),
c = data.frame()
)
y <- list(
a = data.frame(),
b = data.frame(),
data.frame()
)
expect_error(.create_outfiles("d_%s.csv", x))
expect_error(.create_outfiles(c("a.csv", "a.csv", "c.csv"), x))
expect_error(.create_outfiles(c("a.csv", "b.csv"), x))
expect_error(.create_outfiles(c("a.csv"), x))
expect_error(.create_outfiles(c("d_%s.csv"), y))
})
rio/tests/testthat/test_format_yml.R 0000644 0001762 0000144 00000001422 14612465270 017406 0 ustar ligges users skip_if_not_installed("yaml")
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")
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)
})
})
rio/tests/testthat/test_format_dif.R 0000644 0001762 0000144 00000000250 14612465270 017345 0 ustar ligges users test_that("Import from DIF", {
dd <- import(file.path(system.file("misc", package = "utils"), "exDIF.dif"), transpose = TRUE)
expect_true(is.data.frame(dd))
})
rio/tests/testthat/test_format_sas.R 0000644 0001762 0000144 00000000750 14612465270 017376 0 ustar ligges users 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)", {
withr::with_tempfile("mtcars_file", fileext = ".sas7bdat", code = {
suppressWarnings(export(mtcars, mtcars_file))
expect_true(file.exists(mtcars_file))
})
})
rio/tests/testthat/test_format_mtp.R 0000644 0001762 0000144 00000000047 14612465270 017407 0 ustar ligges users ##test_that("Import from Minitab", {})
rio/tests/testthat/test_format_rec.R 0000644 0001762 0000144 00000000046 14612465270 017357 0 ustar ligges users #test_that("Import from Epiinfo", {})
rio/tests/testthat/test_format_xml.R 0000644 0001762 0000144 00000001617 14612465270 017413 0 ustar ligges users 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")
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 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 &, >, ', \", >, <",{
## test all
useless <- lapply(c("&", "\"", "'", "<", ">"), test_xml)
})
rio/tests/testthat/test_compress.R 0000644 0001762 0000144 00000014176 14643763372 017112 0 ustar ligges users context("Compressed files")
test_that("Recognize compressed file types", {
expect_true(rio:::find_compress("file.zip")$compress == "zip")
expect_true(rio:::find_compress("file.tar")$compress == "tar")
expect_true(rio:::find_compress("file.tar.gz")$compress == "tar.gz")
expect_true(rio:::find_compress("file.gzip")$compress == "gzip")
expect_true(rio:::find_compress("file.gz")$compress == "gzip")
expect_true(is.na(rio:::find_compress("file.notcompressed")$compress))
})
test_that("Export to compressed (zip, tar) / import", {
skip_if(getRversion() <= "4.0")
formats <- c("zip", "tar", "tar.gz", "tgz", "tar.bz2", "tbz2")
for (format in formats) {
withr::with_tempfile("iris_path", fileext = paste0(".csv.", format), 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, which = 1)))
base_file_name <- gsub(paste0("\\.", format), "", basename(iris_path))
expect_true(is.data.frame(import(iris_path, which = base_file_name)))
if (format %in% c("tar.gz", "tgz")) {
expect_true(R.utils::isGzipped(iris_path, method = "content"))
}
if (format %in% c("tar.bzip2", "tbz2")) {
expect_true(R.utils::isBzipped(iris_path, method = "content"))
}
})
}
})
test_that("Multi-item zip", {
formats <- c("zip")
for (format in formats) {
withr::with_tempfile("iris_path", fileext = paste0(".csv.", format), code = {
list_of_df <- list(mtcars1 = mtcars[1:10, ], mtcars2 = mtcars[11:20, ], mtcars3 = mtcars[21:32, ])
export_list(list_of_df, file = "%s.csv", archive = iris_path)
y <- import(iris_path)
expect_true(is.data.frame(y))
expect_silent(z1 <- import(iris_path, which = 1))
expect_silent(z2 <- import(iris_path, which = 2))
expect_true(identical(y, z1))
expect_false(identical(y, z2))
})
}
})
test_that("Export to compressed (gz, bz2) / import", {
formats <- c("gz", "gzip", "bz2", "bzip2")
for (format in formats) {
withr::with_tempfile("iris_path", fileext = paste0(".csv.", format), code = {
e1 <- export(iris, iris_path)
expect_true(file.exists(iris_path))
expect_true(is.data.frame(import(iris_path)))
if (format %in% c("gzip", "gz")) {
expect_true(R.utils::isGzipped(iris_path, method = "content"))
}
if (format %in% c("bzip2", "bz2")) {
expect_true(R.utils::isBzipped(iris_path, method = "content"))
}
})
}
})
test_that("Prevent the reuse of `which` for zip and tar", {
skip_if(getRversion() <= "4.0")
formats <- c("zip", "tar", "tar.gz", "tgz", "tar.bz2", "tbz2")
for (format in formats) {
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)
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")))
})
}
## but not for non-archive format, e.g. .xlsx.gz
formats <- c("gz", "bz2")
for (format in formats) {
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)
expect_error(import(data_path, which = "some_iris"), NA)
})
}
})
test_that(".check_tar_support ref #421", {
expect_error(.check_tar_support("tar", R_system_version("4.0.2")), "^Exporting")
expect_error(.check_tar_support("tar.gz", R_system_version("4.0.2")), "^Exporting")
expect_error(.check_tar_support("tar.bz2", R_system_version("4.0.2")), "^Exporting")
expect_error(.check_tar_support("tar", R_system_version("4.0.3")), NA)
expect_error(.check_tar_support("tar.gz", R_system_version("4.0.3")), NA)
expect_error(.check_tar_support("tar.bz2", R_system_version("4.0.3")), NA)
expect_error(.check_tar_support("zip", R_system_version("4.0.2")), NA)
expect_error(.check_tar_support(NA, R_system_version("4.0.2")), NA)
})
test_that("tar export error for R < 4.0.3", {
skip_if(getRversion() >= "4.0.3")
withr::with_tempfile("iris_path", fileext = paste0(".csv.", "tar"), code = {
expect_error(export(iris, iris_path), "^Exporting")
})
})
test_that("Wild zip and tar ref $425", {
skip_if(getRversion() <= "4.0")
##skip_on_os("windows")
withr::with_tempfile("test_files", fileext = c(".csv", ".zip"), code = {
filename <- test_files[1]
zip_file <- test_files[2]
write.csv(1, filename)
zip(zip_file, filename)
expect_error(rio::import(zip_file), NA)
})
for (tar_format in c("tar", "tar.gz", "tar.bz2")) {
withr::with_tempfile("test_files", fileext = c(".csv", paste0(".", tar_format)), code = {
filename <- test_files[1]
tar_file <- test_files[2]
write.csv(1, filename)
compress_out(tar_file, filename, type = tar_format)
expect_error(rio::import(tar_file), NA)
})
}
})
test_that("Relative path #438", {
## see test_export_list.R
withr::with_tempdir({
mylist <- list(
mtcars3 = mtcars[1:10, ],
mtcars2 = mtcars[11:20, ],
mtcars1 = mtcars[21:32, ]
)
rio::export_list(mylist, file = paste0("file_", 1:3, ".csv"), archive = "arch/archive.zip")
expect_true(file.exists("arch/archive.zip"))
})
})
test_that("Integration test #435", {
## filter out all NA cases so that #437 works
withr::with_tempfile("iris_path", fileext = "csv.zip", code = {
expect_error(rio::export(iris, NA), "must be character") ## strange to be honest
expect_error(rio::export(iris, NA_character_), "must not be NA")
})
})
rio/tests/testthat/test_format_psv.R 0000644 0001762 0000144 00000001030 14612465270 017410 0 ustar ligges users 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("fread is deprecated", {
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))
})
})
rio/tests/testthat/test_remote.R 0000644 0001762 0000144 00000004142 14614503454 016531 0 ustar ligges users skip_on_cran()
test_that("Import Remote Stata File", {
f <- try(import("http://www.stata-press.com/data/r13/auto.dta"))
if (!inherits(f, "try-error")) {
expect_true(is.data.frame(f))
}
})
test_that("Import Remote GitHub File", {
rfile <- "https://raw.githubusercontent.com/gesistsa/rio/main/tests/testdata/noheader.csv"
rfile_imported1 <- try(import(rfile))
if (!inherits(rfile_imported1, "try-error")) {
expect_true(inherits(rfile_imported1, "data.frame"), label = "Import remote file (implied format)")
}
rfile_imported2 <- try(import(rfile, format = "csv"))
if (!inherits(rfile_imported2, "try-error")) {
expect_true(inherits(rfile_imported2, "data.frame"), label = "Import remote file (explicit format)")
}
lfile <- remote_to_local(rfile)
if (!inherits(lfile, "try-error")) {
expect_true(file.exists(lfile), label = "Remote file copied successfully")
expect_true(inherits(import(lfile), "data.frame"), label = "Import local copy successfully")
}
## short url
payload <- try(import("https://is.gd/NLAxtg"))
if (!inherits(payload, "try-error")) {
expect_true(inherits(payload, "data.frame"), label = "Import remote file from shorten url (implied format)")
}
## no extension
noextension_url <- "https://github.com/gesistsa/rio/raw/main/tests/testdata/iris_no_extension_xls"
expect_error(import(noextension_url))
expect_error(import(noextension_url, format = "xls"), NA)
})
test_that("Import from Google Sheets", {
googleurl1 <- "https://docs.google.com/spreadsheets/d/1I9mJsS5QnXF2TNNntTy-HrcdHmIF9wJ8ONYvEJTXSNo/edit#gid=0"
expect_true(inherits(import(googleurl1), "data.frame"), label = "Import google sheets (specified sheet)")
googleurl2 <- "https://docs.google.com/spreadsheets/d/1I9mJsS5QnXF2TNNntTy-HrcdHmIF9wJ8ONYvEJTXSNo/edit"
expect_true(inherits(import(googleurl2), "data.frame"), label = "Import google sheets (unspecified sheet)")
expect_true(inherits(import(googleurl1, format = "tsv"), "data.frame"), label = "Import google sheets (specified sheet, specified format)")
})
rio/tests/testthat/test_format_xls.R 0000644 0001762 0000144 00000003576 14612465270 017427 0 ustar ligges users test_that("Export to Excel (.xlsx)", {
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", {
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?", {
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", {
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)", {
expect_true(is.data.frame(import("../testdata/iris.xls")))
expect_true(is.data.frame(import("../testdata/iris.xls", sheet = 1)))
expect_true(is.data.frame(import("../testdata/iris.xls", which = 1)))
})
rio/tests/testthat/test_format_dta.R 0000644 0001762 0000144 00000003121 14612465270 017353 0 ustar ligges users test_that("Export to Stata", {
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)", {
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)", {
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", {
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))
})
})
rio/tests/testthat/test_format_parquet.R 0000644 0001762 0000144 00000000520 14650747025 020267 0 ustar ligges users skip_if_not_installed("nanoparquet")
skip_on_os("mac") ## apache/arrow#40991
test_that("Export to and import from parquet", {
withr::with_tempfile("iris_path", fileext = ".parquet", code = {
export(iris, iris_path)
expect_true(file.exists(iris_path))
expect_true(is.data.frame(import(iris_path)))
})
})
rio/tests/testthat/test_format_rds.R 0000644 0001762 0000144 00000001147 14621337153 017377 0 ustar ligges users 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, trust = TRUE)))
})
})
test_that("Export to rds (non-data frame)", {
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, trust = TRUE), "list"))
expect_true(length(import(list_file, trust = TRUE)) == 2L)
})
})
rio/tests/testthat/test_format_syd.R 0000644 0001762 0000144 00000000245 14612465270 017406 0 ustar ligges users skip_if_not_installed("foreign")
test_that("Import from Systat", {
expect_true(is.data.frame(import(system.file("files/Iris.syd", package = "foreign")[1])))
})
rio/tests/testthat/test_format_fwf.R 0000644 0001762 0000144 00000002724 14612465270 017375 0 ustar ligges users simport <- function(...) {
suppressWarnings(import(...))
}
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("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")))
})
})
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)))
})
})
rio/tests/testthat/test_format_csv.R 0000644 0001762 0000144 00000006117 14631522466 017410 0 ustar ligges users require("datasets")
test_that("Export to 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", {
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", {
noheadercsv <- import("../testdata/noheader.csv", header = FALSE)
expect_that(colnames(noheadercsv)[1], equals("V1"), label = "Header is correctly specified")
})
test_that("Import from (European-style) CSV with semicolon separator", {
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"]]))
})
})
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("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", {
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))
})
})
test_that("dat (ambiguous file format) will be attempted #430", {
withr::with_tempfile("iris_file", fileext = ".dat", code = {
readr::write_delim(iris, file = iris_file)
expect_error(suppressMessages(import(iris_file)), NA)
expect_message(z <- import(iris_file))
## export is not supported though
expect_error(export(iris, iris_file))
})
})
rio/tests/testthat/test_format_fst.R 0000644 0001762 0000144 00000000434 14612465270 017403 0 ustar ligges users skip_if_not_installed(pkg="fst")
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)))
})
})
rio/tests/testthat/test_format_feather.R 0000644 0001762 0000144 00000000442 14612465270 020224 0 ustar ligges users skip_if_not_installed("arrow")
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)))
})
})
rio/tests/testthat/test_format_csvy.R 0000644 0001762 0000144 00000000475 14612465270 017600 0 ustar ligges users 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))
})
})
rio/tests/testthat/test_extensions.R 0000644 0001762 0000144 00000001354 14612465270 017440 0 ustar ligges users library("datasets")
test_that("S3 extension mechanism works for imports", {
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", {
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)
})
})
rio/tests/testthat/test_format_R.R 0000644 0001762 0000144 00000000767 14621337153 017017 0 ustar ligges users require("datasets")
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, trust = TRUE)))
})
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, trust = TRUE)))
})
})
rio/tests/testthat/test_format_arff.R 0000644 0001762 0000144 00000000372 14612465270 017526 0 ustar ligges users 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)))
})
})
rio/tests/testthat.R 0000644 0001762 0000144 00000000113 14476051106 014167 0 ustar ligges users library("testthat")
library("rio")
test_check("rio", reporter = "summary")
rio/tests/testdata/ 0000755 0001762 0000144 00000000000 14613732533 014025 5 ustar ligges users rio/tests/testdata/twotables.html 0000644 0001762 0000144 00000036702 14476051106 016724 0 ustar ligges users
R Exported Data
mpg | cyl | disp | hp | drat | wt | qsec | vs | am | gear | carb |
---|
21 | 6 | 160 | 110 | 3.9 | 2.62 | 16.46 | 0 | 1 | 4 | 4 |
21 | 6 | 160 | 110 | 3.9 | 2.875 | 17.02 | 0 | 1 | 4 | 4 |
22.8 | 4 | 108 | 93 | 3.85 | 2.32 | 18.61 | 1 | 1 | 4 | 1 |
21.4 | 6 | 258 | 110 | 3.08 | 3.215 | 19.44 | 1 | 0 | 3 | 1 |
18.7 | 8 | 360 | 175 | 3.15 | 3.44 | 17.02 | 0 | 0 | 3 | 2 |
18.1 | 6 | 225 | 105 | 2.76 | 3.46 | 20.22 | 1 | 0 | 3 | 1 |
14.3 | 8 | 360 | 245 | 3.21 | 3.57 | 15.84 | 0 | 0 | 3 | 4 |
24.4 | 4 | 146.7 | 62 | 3.69 | 3.19 | 20 | 1 | 0 | 4 | 2 |
22.8 | 4 | 140.8 | 95 | 3.92 | 3.15 | 22.9 | 1 | 0 | 4 | 2 |
19.2 | 6 | 167.6 | 123 | 3.92 | 3.44 | 18.3 | 1 | 0 | 4 | 4 |
17.8 | 6 | 167.6 | 123 | 3.92 | 3.44 | 18.9 | 1 | 0 | 4 | 4 |
16.4 | 8 | 275.8 | 180 | 3.07 | 4.07 | 17.4 | 0 | 0 | 3 | 3 |
17.3 | 8 | 275.8 | 180 | 3.07 | 3.73 | 17.6 | 0 | 0 | 3 | 3 |
15.2 | 8 | 275.8 | 180 | 3.07 | 3.78 | 18 | 0 | 0 | 3 | 3 |
10.4 | 8 | 472 | 205 | 2.93 | 5.25 | 17.98 | 0 | 0 | 3 | 4 |
10.4 | 8 | 460 | 215 | 3 | 5.424 | 17.82 | 0 | 0 | 3 | 4 |
14.7 | 8 | 440 | 230 | 3.23 | 5.345 | 17.42 | 0 | 0 | 3 | 4 |
32.4 | 4 | 78.7 | 66 | 4.08 | 2.2 | 19.47 | 1 | 1 | 4 | 1 |
30.4 | 4 | 75.7 | 52 | 4.93 | 1.615 | 18.52 | 1 | 1 | 4 | 2 |
33.9 | 4 | 71.1 | 65 | 4.22 | 1.835 | 19.9 | 1 | 1 | 4 | 1 |
21.5 | 4 | 120.1 | 97 | 3.7 | 2.465 | 20.01 | 1 | 0 | 3 | 1 |
15.5 | 8 | 318 | 150 | 2.76 | 3.52 | 16.87 | 0 | 0 | 3 | 2 |
15.2 | 8 | 304 | 150 | 3.15 | 3.435 | 17.3 | 0 | 0 | 3 | 2 |
13.3 | 8 | 350 | 245 | 3.73 | 3.84 | 15.41 | 0 | 0 | 3 | 4 |
19.2 | 8 | 400 | 175 | 3.08 | 3.845 | 17.05 | 0 | 0 | 3 | 2 |
27.3 | 4 | 79 | 66 | 4.08 | 1.935 | 18.9 | 1 | 1 | 4 | 1 |
26 | 4 | 120.3 | 91 | 4.43 | 2.14 | 16.7 | 0 | 1 | 5 | 2 |
30.4 | 4 | 95.1 | 113 | 3.77 | 1.513 | 16.9 | 1 | 1 | 5 | 2 |
15.8 | 8 | 351 | 264 | 4.22 | 3.17 | 14.5 | 0 | 1 | 5 | 4 |
19.7 | 6 | 145 | 175 | 3.62 | 2.77 | 15.5 | 0 | 1 | 5 | 6 |
15 | 8 | 301 | 335 | 3.54 | 3.57 | 14.6 | 0 | 1 | 5 | 8 |
21.4 | 4 | 121 | 109 | 4.11 | 2.78 | 18.6 | 1 | 1 | 4 | 2 |
Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | Species |
---|
5.1 | 3.5 | 1.4 | 0.2 | setosa |
4.9 | 3 | 1.4 | 0.2 | setosa |
4.7 | 3.2 | 1.3 | 0.2 | setosa |
4.6 | 3.1 | 1.5 | 0.2 | setosa |
5 | 3.6 | 1.4 | 0.2 | setosa |
5.4 | 3.9 | 1.7 | 0.4 | setosa |
4.6 | 3.4 | 1.4 | 0.3 | setosa |
5 | 3.4 | 1.5 | 0.2 | setosa |
4.4 | 2.9 | 1.4 | 0.2 | setosa |
4.9 | 3.1 | 1.5 | 0.1 | setosa |
5.4 | 3.7 | 1.5 | 0.2 | setosa |
4.8 | 3.4 | 1.6 | 0.2 | setosa |
4.8 | 3 | 1.4 | 0.1 | setosa |
4.3 | 3 | 1.1 | 0.1 | setosa |
5.8 | 4 | 1.2 | 0.2 | setosa |
5.7 | 4.4 | 1.5 | 0.4 | setosa |
5.4 | 3.9 | 1.3 | 0.4 | setosa |
5.1 | 3.5 | 1.4 | 0.3 | setosa |
5.7 | 3.8 | 1.7 | 0.3 | setosa |
5.1 | 3.8 | 1.5 | 0.3 | setosa |
5.4 | 3.4 | 1.7 | 0.2 | setosa |
5.1 | 3.7 | 1.5 | 0.4 | setosa |
4.6 | 3.6 | 1 | 0.2 | setosa |
5.1 | 3.3 | 1.7 | 0.5 | setosa |
4.8 | 3.4 | 1.9 | 0.2 | setosa |
5 | 3 | 1.6 | 0.2 | setosa |
5 | 3.4 | 1.6 | 0.4 | setosa |
5.2 | 3.5 | 1.5 | 0.2 | setosa |
5.2 | 3.4 | 1.4 | 0.2 | setosa |
4.7 | 3.2 | 1.6 | 0.2 | setosa |
4.8 | 3.1 | 1.6 | 0.2 | setosa |
5.4 | 3.4 | 1.5 | 0.4 | setosa |
5.2 | 4.1 | 1.5 | 0.1 | setosa |
5.5 | 4.2 | 1.4 | 0.2 | setosa |
4.9 | 3.1 | 1.5 | 0.2 | setosa |
5 | 3.2 | 1.2 | 0.2 | setosa |
5.5 | 3.5 | 1.3 | 0.2 | setosa |
4.9 | 3.6 | 1.4 | 0.1 | setosa |
4.4 | 3 | 1.3 | 0.2 | setosa |
5.1 | 3.4 | 1.5 | 0.2 | setosa |
5 | 3.5 | 1.3 | 0.3 | setosa |
4.5 | 2.3 | 1.3 | 0.3 | setosa |
4.4 | 3.2 | 1.3 | 0.2 | setosa |
5 | 3.5 | 1.6 | 0.6 | setosa |
5.1 | 3.8 | 1.9 | 0.4 | setosa |
4.8 | 3 | 1.4 | 0.3 | setosa |
5.1 | 3.8 | 1.6 | 0.2 | setosa |
4.6 | 3.2 | 1.4 | 0.2 | setosa |
5.3 | 3.7 | 1.5 | 0.2 | setosa |
5 | 3.3 | 1.4 | 0.2 | setosa |
7 | 3.2 | 4.7 | 1.4 | versicolor |
6.4 | 3.2 | 4.5 | 1.5 | versicolor |
6.9 | 3.1 | 4.9 | 1.5 | versicolor |
5.5 | 2.3 | 4 | 1.3 | versicolor |
6.5 | 2.8 | 4.6 | 1.5 | versicolor |
5.7 | 2.8 | 4.5 | 1.3 | versicolor |
6.3 | 3.3 | 4.7 | 1.6 | versicolor |
4.9 | 2.4 | 3.3 | 1 | versicolor |
6.6 | 2.9 | 4.6 | 1.3 | versicolor |
5.2 | 2.7 | 3.9 | 1.4 | versicolor |
5 | 2 | 3.5 | 1 | versicolor |
5.9 | 3 | 4.2 | 1.5 | versicolor |
6 | 2.2 | 4 | 1 | versicolor |
6.1 | 2.9 | 4.7 | 1.4 | versicolor |
5.6 | 2.9 | 3.6 | 1.3 | versicolor |
6.7 | 3.1 | 4.4 | 1.4 | versicolor |
5.6 | 3 | 4.5 | 1.5 | versicolor |
5.8 | 2.7 | 4.1 | 1 | versicolor |
6.2 | 2.2 | 4.5 | 1.5 | versicolor |
5.6 | 2.5 | 3.9 | 1.1 | versicolor |
5.9 | 3.2 | 4.8 | 1.8 | versicolor |
6.1 | 2.8 | 4 | 1.3 | versicolor |
6.3 | 2.5 | 4.9 | 1.5 | versicolor |
6.1 | 2.8 | 4.7 | 1.2 | versicolor |
6.4 | 2.9 | 4.3 | 1.3 | versicolor |
6.6 | 3 | 4.4 | 1.4 | versicolor |
6.8 | 2.8 | 4.8 | 1.4 | versicolor |
6.7 | 3 | 5 | 1.7 | versicolor |
6 | 2.9 | 4.5 | 1.5 | versicolor |
5.7 | 2.6 | 3.5 | 1 | versicolor |
5.5 | 2.4 | 3.8 | 1.1 | versicolor |
5.5 | 2.4 | 3.7 | 1 | versicolor |
5.8 | 2.7 | 3.9 | 1.2 | versicolor |
6 | 2.7 | 5.1 | 1.6 | versicolor |
5.4 | 3 | 4.5 | 1.5 | versicolor |
6 | 3.4 | 4.5 | 1.6 | versicolor |
6.7 | 3.1 | 4.7 | 1.5 | versicolor |
6.3 | 2.3 | 4.4 | 1.3 | versicolor |
5.6 | 3 | 4.1 | 1.3 | versicolor |
5.5 | 2.5 | 4 | 1.3 | versicolor |
5.5 | 2.6 | 4.4 | 1.2 | versicolor |
6.1 | 3 | 4.6 | 1.4 | versicolor |
5.8 | 2.6 | 4 | 1.2 | versicolor |
5 | 2.3 | 3.3 | 1 | versicolor |
5.6 | 2.7 | 4.2 | 1.3 | versicolor |
5.7 | 3 | 4.2 | 1.2 | versicolor |
5.7 | 2.9 | 4.2 | 1.3 | versicolor |
6.2 | 2.9 | 4.3 | 1.3 | versicolor |
5.1 | 2.5 | 3 | 1.1 | versicolor |
5.7 | 2.8 | 4.1 | 1.3 | versicolor |
6.3 | 3.3 | 6 | 2.5 | virginica |
5.8 | 2.7 | 5.1 | 1.9 | virginica |
7.1 | 3 | 5.9 | 2.1 | virginica |
6.3 | 2.9 | 5.6 | 1.8 | virginica |
6.5 | 3 | 5.8 | 2.2 | virginica |
7.6 | 3 | 6.6 | 2.1 | virginica |
4.9 | 2.5 | 4.5 | 1.7 | virginica |
7.3 | 2.9 | 6.3 | 1.8 | virginica |
6.7 | 2.5 | 5.8 | 1.8 | virginica |
7.2 | 3.6 | 6.1 | 2.5 | virginica |
6.5 | 3.2 | 5.1 | 2 | virginica |
6.4 | 2.7 | 5.3 | 1.9 | virginica |
6.8 | 3 | 5.5 | 2.1 | virginica |
5.7 | 2.5 | 5 | 2 | virginica |
5.8 | 2.8 | 5.1 | 2.4 | virginica |
6.4 | 3.2 | 5.3 | 2.3 | virginica |
6.5 | 3 | 5.5 | 1.8 | virginica |
7.7 | 3.8 | 6.7 | 2.2 | virginica |
7.7 | 2.6 | 6.9 | 2.3 | virginica |
6 | 2.2 | 5 | 1.5 | virginica |
6.9 | 3.2 | 5.7 | 2.3 | virginica |
5.6 | 2.8 | 4.9 | 2 | virginica |
7.7 | 2.8 | 6.7 | 2 | virginica |
6.3 | 2.7 | 4.9 | 1.8 | virginica |
6.7 | 3.3 | 5.7 | 2.1 | virginica |
7.2 | 3.2 | 6 | 1.8 | virginica |
6.2 | 2.8 | 4.8 | 1.8 | virginica |
6.1 | 3 | 4.9 | 1.8 | virginica |
6.4 | 2.8 | 5.6 | 2.1 | virginica |
7.2 | 3 | 5.8 | 1.6 | virginica |
7.4 | 2.8 | 6.1 | 1.9 | virginica |
7.9 | 3.8 | 6.4 | 2 | virginica |
6.4 | 2.8 | 5.6 | 2.2 | virginica |
6.3 | 2.8 | 5.1 | 1.5 | virginica |
6.1 | 2.6 | 5.6 | 1.4 | virginica |
7.7 | 3 | 6.1 | 2.3 | virginica |
6.3 | 3.4 | 5.6 | 2.4 | virginica |
6.4 | 3.1 | 5.5 | 1.8 | virginica |
6 | 3 | 4.8 | 1.8 | virginica |
6.9 | 3.1 | 5.4 | 2.1 | virginica |
6.7 | 3.1 | 5.6 | 2.4 | virginica |
6.9 | 3.1 | 5.1 | 2.3 | virginica |
5.8 | 2.7 | 5.1 | 1.9 | virginica |
6.8 | 3.2 | 5.9 | 2.3 | virginica |
6.7 | 3.3 | 5.7 | 2.5 | virginica |
6.7 | 3 | 5.2 | 2.3 | virginica |
6.3 | 2.5 | 5 | 1.9 | virginica |
6.5 | 3 | 5.2 | 2 | virginica |
6.2 | 3.4 | 5.4 | 2.3 | virginica |
5.9 | 3 | 5.1 | 1.8 | virginica |
rio/tests/testdata/br-in-header.html 0000644 0001762 0000144 00000000232 14476051106 017142 0 ustar ligges users
| Date |
1 | December 15, 2003 13:14:17.123 |