Skip to content

Commit

Permalink
tests for SymbolixAU#170
Browse files Browse the repository at this point in the history
  • Loading branch information
SymbolixAU committed Sep 17, 2018
1 parent c4c7e23 commit c8ca3a0
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 69 deletions.
23 changes: 14 additions & 9 deletions R/google_directions.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
#' list of unnamed elements, each element is either a numeric vector of lat/lon
#' coordinates, an address string or place_id, or a vector of a pair of lat / lon coordinates
#' @param mode \code{string} One of 'driving', 'walking', 'bicycling' or 'transit'.
#' @param departure_time a \code{POSIXct} time since 1st January 1970.
#' Specifies the desired time of departure. If no value
#' is specified it defaults to \code{Sys.time()}
#' @param arrival_time a \code{POSIXct} time since 1st January 1970.
#' Specifies the desired time of arrival. Note you
#' can only specify one of \code{arrival_time} or \code{departure_time}, not both.
#' @param departure_time The desired time of departure.
#' Use either a \code{POSIXct} time since 1st January 1970, or the string 'now'.
#' If no value is specified it defaults to \code{Sys.time()}.
#' @param arrival_time Specifies the desired time of arrival for transit requests.
#' Use either a \code{POSIXct} time since 1st January 1970.
#' Note you can only specify one of \code{arrival_time} or \code{departure_time}, not both.
#' If both are supplied, \code{departure_time} will be used.
#' @param waypoints list of waypoints, expressed as either \code{vectors} of
#' lat/lon coordinates, or a \code{string} address to be geocoded, or an encoded
Expand Down Expand Up @@ -88,6 +88,11 @@
#' units = "imperial",
#' simplify = TRUE)
#'
#' ## using 'now' as departure time
#' google_directions(origin = "Flinders Street Station, Melbourne",
#' destination = "MCG, Melbourne",
#' departure_time = 'now')
#'
#' ## waypoints expressed as an encoded polyline
#' polyWaypoints <- encode_pl(tram_stops[1:2, c("stop_lat")], tram_stops[1:2, c("stop_lon")])
#' polyWaypoints <- list(via = paste0("enc:", polyWaypoints, ":"))
Expand All @@ -98,7 +103,7 @@
#'
#'
#' ## using bus and less walking
#' google_directions(origin = "Melbourne Airport, Australia",
#' res <- google_directions(origin = "Melbourne Airport, Australia",
#' destination = "Portsea, Melbourne, Australia",
#' departure_time = Sys.time() + (24 * 60 * 60),
#' mode = "transit",
Expand All @@ -107,7 +112,7 @@
#' simplify = FALSE)
#'
#' ## using arrival time
#' google_directions(origin = "Melbourne Airport, Australia",
#' res <- google_directions(origin = "Melbourne Airport, Australia",
#' destination = "Portsea, Melbourne, Australia",
#' arrival_time = Sys.time() + (24 * 60 * 60),
#' mode = "transit",
Expand All @@ -116,7 +121,7 @@
#' simplify = FALSE)
#'
#' ## return results in French
#' google_directions(origin = "Melbourne Airport, Australia",
#' res <- google_directions(origin = "Melbourne Airport, Australia",
#' destination = "Portsea, Melbourne, Australia",
#' arrival_time = Sys.time() + (24 * 60 * 60),
#' mode = "transit",
Expand Down
1 change: 0 additions & 1 deletion R/google_map_layer_marker.R
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ add_markers <- function(map,
shape <- createInfoWindowChart(shape, infoWindowChart, id)
shape <- jsonlite::toJSON(shape, digits = digits)

# print(shape)
map <- addDependency(map, googleMarkerDependency())

invoke_method(map, 'add_markers', shape, cluster, update_map_view, layer_id, usePolyline, load_interval, focus_layer, close_info_window)
Expand Down
7 changes: 0 additions & 7 deletions R/google_map_layer_polyline.R
Original file line number Diff line number Diff line change
Expand Up @@ -183,17 +183,10 @@ add_polylines <- function(map,
keep <- setdiff(n, c('id', 'lat', 'lng'))

lst_polyline <- objPolylineCoords(shape, ids, keep)
# print(lst_polyline)
# print("--shape--")
# print(shape)

#print(infoWindowChart)
lst_polyline <- createInfoWindowChart(lst_polyline, infoWindowChart, id)
# print(lst_polyline)

shape <- jsonlite::toJSON(lst_polyline, digits = digits, auto_unbox = T)

# print(shape)
}

map <- addDependency(map, googlePolylineDependency())
Expand Down
10 changes: 5 additions & 5 deletions R/googleway-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,22 @@ directions_data <- function(base_url,

alternatives <- validateAlternatives(alternatives)

if(!is.null(traffic_model) & is.null(departure_time))
departure_time <- Sys.time()
# if(!is.null(traffic_model) & is.null(departure_time))
# departure_time <- as.integer( Sys.time() )

traffic_model <- validateTrafficModel(traffic_model)

## check origin/destinations are valid
if(information_type == "directions"){
if( information_type == "directions" ) {
origin <- check_location(origin, "Origin")
destination <- check_location(destination, "Destination")
}else if(information_type == "distance"){
}else if( information_type == "distance" ) {
origin <- check_multiple_locations(origin, "Origins elements")
destination <- check_multiple_locations(destination, "Destinations elements")
}

## times as integers
departure_time <- ifelse(is.null(departure_time), as.integer(Sys.time()), as.integer(departure_time))
#departure_time <- ifelse(is.null(departure_time), as.integer(Sys.time()), as.integer(departure_time))
arrival_time <- as.integer(arrival_time)

waypoints <- validateWaypoints(waypoints, optimise_waypoints, mode)
Expand Down
2 changes: 1 addition & 1 deletion R/legend.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ formatPalette <- function(palette, type){

if(rw[1] != 1) rw <- c(1, rw)
if(rw[length(rw)] != nrow(palette)) rw <- c(rw, nrow(palette))
#print(rw)

palette <- palette[rw, ]
}
return(palette)
Expand Down
27 changes: 19 additions & 8 deletions R/parameter_checks.R
Original file line number Diff line number Diff line change
Expand Up @@ -226,12 +226,14 @@ validateAlternatives <- function(alternatives){

validateArrivalTime <- function(arrival_time){

if(is.null(arrival_time)) return(NULL)
if(is.null(arrival_time)) return( NULL )

checkPosix(arrival_time)

return(arrival_time)
}


validateArrivalDepartureTimes <- function(arrival_time, departure_time){

if(!is.null(arrival_time) & !is.null(departure_time)){
Expand Down Expand Up @@ -306,13 +308,21 @@ validateComponentsCountries <- function(components){
validateDepartureTime <- function(departure_time, mode){
if(is.null(departure_time)) return(NULL)

checkPosix(departure_time)
if (inherits(departure_time, "POSIXct")) {
checkPosix( departure_time )

if(mode == "driving" && departure_time < Sys.time()){
stop("departure_time for driving mode must not be in the past")
}
if(mode == "driving" && ( departure_time + 60) < Sys.time() ){ ## allowing a buffer
stop("departure_time for driving mode must not be in the past")
}

return(departure_time)
return( as.integer(departure_time ) )

} else if ( inherits(departure_time, "character") ) {
if( departure_time != "now") {
stop("when using a string for departure_time you may only use 'now'")
}
}
return( departure_time )
}

validateFindInput <- function( input, inputtype ) {
Expand Down Expand Up @@ -643,9 +653,10 @@ validateTrafficModel <- function(traffic_model){
}

## transit_mode is only valid where mode = transit
validateTransitMode <- function(transit_mode, mode){
validateTransitMode <- function(transit_mode, mode) {

if(!is.null(transit_mode) & mode != "transit") {

if(!is.null(transit_mode) & mode != "transit"){
warning("You have specified a transit_mode, but are not using mode = 'transit'. Therefore this argument will be ignored")
return(NULL)
}else if(!is.null(transit_mode) & mode == "transit"){
Expand Down
23 changes: 14 additions & 9 deletions man/google_directions.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions man/google_distance.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 12 additions & 11 deletions tests/testthat/test-google_directions.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,18 @@ test_that("Avoid is a valid type", {

})

# test_that("Departure time is not in the past",{
#
# expect_error(
# google_directions(origin = c(-37.8179746, 144.9668636),
# destination = c(-37.81659, 144.9841),
# departure_time = as.POSIXct("2015-01-01"),
# key = "abc",
# simplify = TRUE)
# )
#
# })
test_that("Departure time is not in the past",{

expect_error(
google_directions(origin = c(-37.8179746, 144.9668636),
destination = c(-37.81659, 144.9841),
departure_time = as.POSIXct("2015-01-01"),
key = "abc",
simplify = TRUE),
"departure_time for driving mode must not be in the past"
)

})

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

Expand Down
31 changes: 19 additions & 12 deletions tests/testthat/test-google_distances.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,28 @@ 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",
simplify = TRUE),
key = "abc"),
"avoid can only include 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",
# simplify = TRUE),
# "departure_time must not be in the past")
#
# })
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"),
"departure_time for driving mode must not be in the past")

})

test_that("now is a valid departure time", {

expect_silent(google_distance(origins = list(c(-37.8179746, 144.9668636)),
destinations = list(c(-37.81659, 144.9841)),
departure_time = 'now',
key = "abc"))
})


# test_that("waypoints only valid for certain modes",{
#
Expand Down

0 comments on commit c8ca3a0

Please sign in to comment.