Skip to content

Commit

Permalink
Add trust to tests fix #414
Browse files Browse the repository at this point in the history
  • Loading branch information
chainsawriot committed May 14, 2024
1 parent f0b24fd commit 88ea16a
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 30 deletions.
4 changes: 2 additions & 2 deletions tests/testthat/test_format_R.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ 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)))
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)))
expect_true(is.data.frame(import(iris_file, trust = TRUE)))
})
})
24 changes: 12 additions & 12 deletions tests/testthat/test_format_rdata.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@ test_that("Export to and import from Rdata", {
## 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)))
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)))
expect_true(is.data.frame(import(iris_file, which = 1)))
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)))
expect_true(is.data.frame(import(iris_file, which = 1)))
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
Expand All @@ -33,24 +33,24 @@ test_that("Export to and import from rda", {
## 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)))
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)))
expect_true(is.data.frame(import(iris_file, which = 1)))
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)))
expect_true(is.data.frame(import(iris_file, which = 1)))
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
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/test_format_rds.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ 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)))
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), "list"))
expect_true(length(import(list_file)) == 2L)
expect_true(inherits(import(list_file, trust = TRUE), "list"))
expect_true(length(import(list_file, trust = TRUE)) == 2L)
})
})
8 changes: 4 additions & 4 deletions tests/testthat/test_identical.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ test_that("Data identical (text formats)", {

test_that("Data identical (R formats)", {
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)
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)
})
})

Expand Down
18 changes: 9 additions & 9 deletions tests/testthat/test_import_list.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
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)), list(mtcars, mtcars))
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)
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))
Expand All @@ -13,7 +13,7 @@ test_that("Data identical (import_list)", {
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)
dat <- import_list(rdata_file, trust = TRUE)
expect_true(identical(dat[[1]], mtcars))
expect_true(identical(dat[[2]], iris))
})
Expand Down Expand Up @@ -57,7 +57,7 @@ test_that("import_list() preserves 'which' names when specified", {
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), mtcars))
expect_true(identical(import_list(data_file, rbind = TRUE, trust = TRUE), mtcars))
})
})

Expand All @@ -74,9 +74,9 @@ test_that("Import single file from zip via import_list()", {
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)
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)
dat2 <- import_list(rep(data_file, 2), setclass = "tbl", rbind = TRUE, trust = TRUE)
expect_true(inherits(dat2, "tbl"))
})
})
Expand Down Expand Up @@ -157,7 +157,7 @@ test_that("Informative message when files are not found #389", {
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")
expect_warning(import_list(c(mtcars_file, "nonexisting.rds"), trust = TRUE), "^Import failed for nonexisting")
})
})

Expand All @@ -167,8 +167,8 @@ test_that("Missing files and rbind", {
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_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), "^Import failed for nonexisting")
expect_warning(x <- import_list(c("nonexisting.rds", "nonexisting2.rds"), rbind = TRUE, trust = TRUE), "^Import failed for nonexisting")
})
})

0 comments on commit 88ea16a

Please sign in to comment.