Skip to content

Commit

Permalink
Merge pull request #322 from chainsawriot/check
Browse files Browse the repository at this point in the history
Check
  • Loading branch information
chainsawriot authored Aug 30, 2023
2 parents 0f5e459 + 7ec4155 commit 121b879
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
5 changes: 4 additions & 1 deletion R/import.R
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,14 @@
#' @importFrom tibble as_tibble is_tibble
#' @export
import <- function(file, format, setclass, which, ...) {
if (isFALSE(inherits(file, "character")) || isFALSE(length(file) == 1)) {
stop("Invalid `file` argument.", call. = FALSE)
}
if (grepl("^http.*://", file)) {
file <- remote_to_local(file, format = format)
}
if ((file != "clipboard") && !file.exists(file)) {
stop("No such file")
stop("No such file: ", file, call. = FALSE)
}
if (grepl("\\.zip$", file)) {
if (missing(which)) {
Expand Down
1 change: 0 additions & 1 deletion tests/testthat/test_format_parquet.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ require("datasets")

test_that("Export to and import from parquet", {
skip_if_not_installed("arrow")
skip_if_not(arrow::arrow_available())
expect_true(export(iris, "iris.parquet") %in% dir())
expect_true(is.data.frame(import("iris.parquet")))
unlink("iris.parquet")
Expand Down
12 changes: 12 additions & 0 deletions tests/testthat/test_import.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

test_that("Invalid file argument, #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")
})
18 changes: 9 additions & 9 deletions tests/testthat/test_remote.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,26 @@ test_that("Import Remote GitHub File", {
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")
}
})

test_that("Import Remote File from Shortened URL", {
skip_if_not_installed(pkg="data.table")
shorturl <- try(import("https://goo.gl/KPFiaK"))
if (!inherits(shorturl, "try-error")) {
expect_true(inherits(shorturl, "data.frame"), label = "Import remote file")
}
})
## test_that("Import Remote File from Shortened URL", {
## skip_if_not_installed(pkg = "data.table")
## shorturl <- try(import("https://raw.githubusercontent.com/chainsawriot/rio/main/tests/testdata/example.csvy"))
## if (!inherits(shorturl, "try-error")) {
## expect_true(inherits(shorturl, "data.frame"), label = "Import remote file")
## }
## })

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)")

Expand Down

0 comments on commit 121b879

Please sign in to comment.