Skip to content

Commit

Permalink
Merge pull request #39 from polettif/change-test-url
Browse files Browse the repository at this point in the history
Use test feed on repository
  • Loading branch information
polettif authored Nov 21, 2018
2 parents d58e0ff + d83ca76 commit 02cd236
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 29 deletions.
17 changes: 7 additions & 10 deletions R/import.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#' @importFrom dplyr %>% arrange summarise group_by inner_join
#' @examples \donttest{
#' library(dplyr)
#' u1 <- "https://developers.google.com/transit/gtfs/examples/sample-feed.zip"
#' u1 <- "https://github.com/r-transit/tidytransit/raw/master/inst/extdata/sample-feed-fixed.zip"
#' sample_gtfs <- read_gtfs(u1)
#' attach(sample_gtfs)
#' #list routes by the number of stops they have
Expand All @@ -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 @@ -79,7 +80,7 @@ read_gtfs <- function(path, local = FALSE,
#' @importFrom dplyr %>% arrange summarise group_by inner_join
#' @examples \donttest{
#' library(dplyr)
#' u1 <- "https://developers.google.com/transit/gtfs/examples/sample-feed.zip"
#' u1 <- "https://github.com/r-transit/tidytransit/raw/master/inst/extdata/sample-feed-fixed.zip"
#' sample_gtfs <- import_gtfs(u1)
#' attach(sample_gtfs)
#' #list routes by the number of stops they have
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
12 changes: 5 additions & 7 deletions R/spec.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,11 @@ get_gtfs_meta <- function() {

# calendar
assign("calendar", list())
calendar$field <- c('service_id', 'service_name', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday', 'start_date', 'end_date')
calendar$field <- c('service_id', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday', 'start_date', 'end_date')
calendar$field_spec <- rep('req', times = length(calendar$field))
calendar$field_spec[2] <- 'opt'
names(calendar$field_spec) <- calendar$field
calendar$coltype <- rep('i', length(calendar$field))
calendar$coltype[calendar$field %in% c('service_id', 'service_name')] <- 'c'
calendar$coltype[calendar$field %in% c('service_id')] <- 'c'
calendar$coltype[calendar$field %in% c('start_date', 'end_date')] <- 'D'
calendar$file_spec <- 'req'

Expand Down Expand Up @@ -120,14 +119,13 @@ get_gtfs_meta <- function() {
transfers$field_spec <- c('req', 'req', 'req', 'opt')
names(transfers$field_spec) <- transfers$field
transfers$coltype <- rep('c', length(transfers$field))
transfers$coltype[transfers$field %in% c('exception_type')] <- 'i'
transfers$coltype[transfers$field %in% c('min_transfer_time')] <- 'i'
transfers$file_spec <- 'opt'

# feed_info
assign("feed_info", list())
feed_info$field <- c('feed_id', 'feed_publisher_name', 'feed_publisher_url', 'feed_lang', 'feed_version', 'feed_license', 'feed_contact_email', 'feed_contact_url', 'feed_start_date', 'feed_end_date')
feed_info$field_spec <- rep('opt', times = length(feed_info$field))
feed_info$field_spec[c(2:4)] <- 'req'
feed_info$field <- c('feed_publisher_name', 'feed_publisher_url', 'feed_lang', 'feed_start_date', 'feed_end_date', 'feed_version', 'feed_contact_email', 'feed_contact_url')
feed_info$field_spec <- c('req', 'req', 'req', 'opt', 'opt', 'opt', 'opt', 'opt')
names(feed_info$field_spec) <- feed_info$field
feed_info$coltype <- rep('c', length(feed_info$field))
feed_info$coltype[transfers$field %in% c('feed_start_date', 'feed_end_date')] <- 'D'
Expand Down
17 changes: 8 additions & 9 deletions tests/testthat/test-import-gtfs.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
context('Import and Validation')

gtfs_example_url <- "https://developers.google.com/transit/gtfs/examples/sample-feed.zip"
gtfs_example_url <- "https://github.com/r-transit/tidytransit/raw/master/inst/extdata/sample-feed-fixed.zip"
local_gtfs_path <- system.file("extdata", "google_transit_nyc_subway.zip", package = "tidytransit")

working <- function() {
skip_on_cran()
connecting <- function(gtfs_example_url) {
r <- base::try(httr::GET(gtfs_example_url, httr::timeout(5)))
if(!assertthat::is.error(r)) r$status_code == 200 else FALSE
Expand All @@ -23,11 +22,11 @@ test_that('read_gtfs() imports a local file to a list of dataframes and doesnt d

test_that('Downloading a zip file from a gtfs_example_url returns a file', {
skip_on_cran()
if(working()==FALSE){
if(!working()){
skip("no internet, skipping")
}
else {
zip <- tidytransit:::download_from_url(gtfs_example_url)
zip <- tidytransit:::download_from_url(gtfs_example_url, quiet = T)

expect_true(file.exists(zip))
}
Expand All @@ -42,7 +41,7 @@ test_that('import-bad paths throw good errors', {
# parse_gtfs()
test_that('import-empty txt files are not imported and non-empty ones are imported', {
skip_on_cran()
if(working()==FALSE){
if(!working()){
skip("no internet, skipping")
}
else {
Expand All @@ -64,7 +63,7 @@ test_that('import-empty txt files are not imported and non-empty ones are import
#read_gtfs()
test_that('the read_gtfs function works', {
skip_on_cran()
if(working()==FALSE){
if(!working()){
skip("no internet, skipping")
}
else {
Expand All @@ -78,16 +77,16 @@ test_that('the read_gtfs function works', {
#read_gtfs()
test_that('the read_gtfs function fails gracefully on bad urls', {
skip_on_cran()
if(working()==FALSE){
if(!working()){
skip("no internet, skipping")
}
else {
not_zip <- "https://developers.google.com/transit/gtfs/examples/sample-feed.zippy"
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 02cd236

Please sign in to comment.