Skip to content

Commit

Permalink
fix test for bad urls
Browse files Browse the repository at this point in the history
  • Loading branch information
polettif committed Oct 31, 2018
1 parent fb4ad2b commit 6f258e6
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 13 deletions.
13 changes: 5 additions & 8 deletions R/import.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ read_gtfs <- function(path, local = FALSE,
# download zip file
if(!local) {
path <- download_from_url(url = path, quiet = quiet)
if(is.null(path)) { return() }
}

# extract zip file
Expand Down Expand Up @@ -120,13 +121,9 @@ download_from_url <- function(url, path=tempfile(fileext = ".zip"), quiet=FALSE)
}

# check if url links to a zip file
valid <- valid_url(url)
if(!valid) {
if(!quiet) {
stop1 <- sprintf("Link '%s' is invalid; failed to connect. NULL was returned.", url)
stop(stop1)
}
return(NULL)
if(!valid_url(url)) {
stop1 <- sprintf("Link '%s' is invalid; failed to connect.", url)
stop(stop1)
}

r <- httr::GET(url)
Expand All @@ -149,7 +146,7 @@ download_from_url <- function(url, path=tempfile(fileext = ".zip"), quiet=FALSE)
check <- try(normalizePath(path), silent = TRUE)
if(assertthat::is.error(check)) {
warn <- 'Invalid file path. NULL is returned.'
if(!quiet) warning(warn)
warning(warn)
return(NULL)
}
return(path)
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-import-gtfs.R
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ test_that('the read_gtfs function fails gracefully on bad urls', {
bad_url <- "https://developers.google.com/transit/gtfs/examples/sample-feed-bad.zip"

# non-specified path
expect_error(read_gtfs(not_zip, quiet=TRUE))
expect_error(read_gtfs(bad_url, quiet=TRUE)) # not zip file warning
expect_error(tidytransit::read_gtfs(not_zip, quiet=TRUE))
expect_error(tidytransit::read_gtfs(bad_url, quiet=TRUE)) # not zip file warning
}

})
Expand Down
5 changes: 2 additions & 3 deletions tests/testthat/test-main.R
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
context('Import metadata from transitfeeds')

working <- function() {
url <- "https://developers.google.com/transit/gtfs/examples/sample-feed.zip"
url <- "https://github.com/r-transit/tidytransit/raw/master/inst/extdata/sample-feed-fixed.zip"
connecting <- function(url) {
r <- base::try(httr::GET(url, httr::timeout(5)))
if(!assertthat::is.error(r)) r$status_code == 200 else FALSE
}
connecting(url)
}

#read_gtfs()
test_that('the metadata from transitfeeds is a data frame that is not empty', {
skip_on_cran()
tfkey <- Sys.getenv('TRANSITFEED_API')
if(working()==FALSE){
if(!working()){
skip("no internet, skipping")
}
else if (identical(tfkey, "")) {
Expand Down

0 comments on commit 6f258e6

Please sign in to comment.