Skip to content

Commit

Permalink
tests for google_distance()
Browse files Browse the repository at this point in the history
  • Loading branch information
SymbolixAU committed Jun 5, 2016
1 parent 697e4e8 commit 907f1b7
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 2 deletions.
2 changes: 1 addition & 1 deletion R/googleway-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ directions_data <- function(base_url,
origin <- fun_check_location(origin, "Origin")
destination <- fun_check_location(destination, "Destination")
}else if(information_type == "distance"){
origin <- fun_check_multiple_locations(origin, "Origins elements ")
origin <- fun_check_multiple_locations(origin, "Origins elements")
destination <- fun_check_multiple_locations(destination, "Destinations elements")
}

Expand Down
11 changes: 10 additions & 1 deletion tests/testthat/test-google_directions.R
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ test_that("transit_mode issues warning when mode != transit",{

expect_warning(google_directions(origin = "Melbourne Airport, Australia",
destination = "Portsea, Melbourne, Australia",
departure_time = as.POSIXct("2016-06-08 07:00:00"),
departure_time = Sys.time() + (24 * 60 * 60),
waypoints = list(via = c(-37.81659, 144.9841),
via = "Ringwood, Victoria"),
transit_mode = "bus",
Expand All @@ -128,7 +128,16 @@ test_that("transit_mode issues warning when mode != transit",{
})


test_that("warning when both arrival and departure times supplied", {

expect_warning(google_directions(origin = "Melbourne Airport, Australia",
destination = "Portsea, Melbourne, Australia",
departure_time = Sys.time() + (24 * 60 * 60),
arrival_time = Sys.time() + (24 * 60 * 60),
key = "abc"),
"you have supplied both an arrival_time and a departure_time - only one is allowed. The arrival_time will be ignored")

})



Expand Down
124 changes: 124 additions & 0 deletions tests/testthat/test-google_distances.R
Original file line number Diff line number Diff line change
@@ -1,2 +1,126 @@
context("Google distance")

## api keys are not included in the package
## and so can't be tested
skip_api_key <- function() {
skip("key not available")
}

test_that("google_distance returns data.frame", {
## skip this test
skip_api_key()

df <- google_distance(origins = c(-37.8179746, 144.9668636),
destinations = c(-37.81659, 144.9841),
mode = "walking",
key = key,
output_format = "data.frame")

expect_equal(class(df), "list")
expect_equal(names(df), c("geocoded_waypoints","routes","status"))
expect_equal(length(df[[1]]), 3)
})



test_that("origins are lat/lon", {

expect_error(google_distance(origins = list(c(144.9668636)),
destinations = c(-37.81659, 144.9841),
mode = "driving",
key = "abc"),
"Origins elements must be either a numeric vector of lat/lon coordinates, or an address string")

})

test_that("Destinations are lat/lon", {

expect_error(google_distance(origins = list(c(-37.8179746, 144.9668636)),
destinations = c(-37.81659),
mode = "driving",
key = "abc"),
"Destinations elements must be either a numeric vector of lat/lon coordinates, or an address string")

})

test_that("Avoid is a valid type", {

expect_error(google_distance(origins = list(c(-37.8179746, 144.9668636)),
destinations = list(c(-37.81659, 144.9841)),
avoid = "dont avoid",
key = "abc",
output_format = "data.frame"),
"avoid must be one of tolls, highways, ferries or indoor")
})

test_that("Departure time is not in the past",{

expect_error(google_distance(origins = list(c(-37.8179746, 144.9668636)),
destinations = list(c(-37.81659, 144.9841)),
departure_time = as.POSIXct("2015-01-01"),
key = "abc",
output_format = "data.frame"),
"departure_time must not be in the past")

})

test_that("waypoints only valid for certain modes",{

expect_error(google_distance(origins = "Melbourne Airport",
destinations = "Sorrento",
waypoints = list(c(-37.81659, 144.9841),
"Ringwood, Victoria"),
mode = "transit",
key = "abc"),
"waypoints are only valid for driving, walking or bicycling modes")
})

test_that("waypoints are a list",{

expect_error(google_distance(origins = "Melbourne Airport",
destinations = "Sorrento",
waypoints = c(-37.81659, 144.9841),
key = "abc"),
"waypoints must be a list")
})

test_that("map url is a single string",{

expect_error(google_distance(origins = "Melbourne Airport",
destinations = "Sorrento",
alternatives = c(FALSE, TRUE),
key = "abc"),
"invalid map_url")
})


test_that("transit_mode issues warning when mode != transit",{

expect_warning(google_distance(origins = "Melbourne Airport, Australia",
destinations = "Portsea, Melbourne, Australia",
departure_time = Sys.time() + (24 * 60 * 60),
waypoints = list(via = c(-37.81659, 144.9841),
via = "Ringwood, Victoria"),
transit_mode = "bus",
key = "abc"),
"You have specified a transit_mode, but are not using mode = 'transit'. Therefore this argument will be ignored")

})


test_that("warning when both arrival and departure times supplied", {

expect_warning(google_distance(origins = "Melbourne Airport, Australia",
destinations = "Portsea, Melbourne, Australia",
departure_time = Sys.time() + (24 * 60 * 60),
arrival_time = Sys.time() + (24 * 60 * 60),
key = "abc"),
"you have supplied both an arrival_time and a departure_time - only one is allowed. The arrival_time will be ignored")

})






0 comments on commit 907f1b7

Please sign in to comment.