From c8ca3a09a90b64eab5acecfdee634c13036045e7 Mon Sep 17 00:00:00 2001 From: SymbolixAU Date: Mon, 17 Sep 2018 21:15:47 +1000 Subject: [PATCH] tests for #170 --- R/google_directions.R | 23 +++++++++++------- R/google_map_layer_marker.R | 1 - R/google_map_layer_polyline.R | 7 ------ R/googleway-package.R | 10 ++++---- R/legend.R | 2 +- R/parameter_checks.R | 27 ++++++++++++++------- man/google_directions.Rd | 23 +++++++++++------- man/google_distance.Rd | 12 +++++----- tests/testthat/test-google_directions.R | 23 +++++++++--------- tests/testthat/test-google_distances.R | 31 +++++++++++++++---------- 10 files changed, 90 insertions(+), 69 deletions(-) diff --git a/R/google_directions.R b/R/google_directions.R index 01d5dbe3..d0cdaa25 100644 --- a/R/google_directions.R +++ b/R/google_directions.R @@ -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 @@ -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, ":")) @@ -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", @@ -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", @@ -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", diff --git a/R/google_map_layer_marker.R b/R/google_map_layer_marker.R index c6198c6d..20f3ad2b 100644 --- a/R/google_map_layer_marker.R +++ b/R/google_map_layer_marker.R @@ -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) diff --git a/R/google_map_layer_polyline.R b/R/google_map_layer_polyline.R index dd15fb3e..e87527ee 100644 --- a/R/google_map_layer_polyline.R +++ b/R/google_map_layer_polyline.R @@ -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()) diff --git a/R/googleway-package.R b/R/googleway-package.R index 1616d688..6f752586 100644 --- a/R/googleway-package.R +++ b/R/googleway-package.R @@ -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) diff --git a/R/legend.R b/R/legend.R index 799c9a98..96f929a7 100644 --- a/R/legend.R +++ b/R/legend.R @@ -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) diff --git a/R/parameter_checks.R b/R/parameter_checks.R index 0e27f1d9..6350c1ca 100644 --- a/R/parameter_checks.R +++ b/R/parameter_checks.R @@ -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)){ @@ -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 ) { @@ -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"){ diff --git a/man/google_directions.Rd b/man/google_directions.Rd index 26da845d..c107babc 100644 --- a/man/google_directions.Rd +++ b/man/google_directions.Rd @@ -23,13 +23,13 @@ coordinates, an address string or place_id, or a vector of a pair of lat / lon c \item{mode}{\code{string} One of 'driving', 'walking', 'bicycling' or 'transit'.} -\item{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()}} +\item{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()}.} -\item{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. +\item{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.} \item{waypoints}{list of waypoints, expressed as either \code{vectors} of @@ -134,6 +134,11 @@ google_directions(origin = "Melbourne Airport, Australia", 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, ":")) @@ -144,7 +149,7 @@ google_directions(origin = "Melbourne Zoo, Melbourne", ## 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", @@ -153,7 +158,7 @@ google_directions(origin = "Melbourne Airport, Australia", 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", @@ -162,7 +167,7 @@ google_directions(origin = "Melbourne Airport, Australia", 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", diff --git a/man/google_distance.Rd b/man/google_distance.Rd index c5268ae5..3e89b068 100644 --- a/man/google_distance.Rd +++ b/man/google_distance.Rd @@ -21,13 +21,13 @@ coordinates, an address string or place_id, or a vector of a pair of lat / lon c \item{mode}{\code{string} One of 'driving', 'walking', 'bicycling' or 'transit'.} -\item{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()}} +\item{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()}.} -\item{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. +\item{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.} \item{avoid}{\code{character} vector stating which features should be avoided. diff --git a/tests/testthat/test-google_directions.R b/tests/testthat/test-google_directions.R index 98930715..fb286fcc 100644 --- a/tests/testthat/test-google_directions.R +++ b/tests/testthat/test-google_directions.R @@ -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",{ diff --git a/tests/testthat/test-google_distances.R b/tests/testthat/test-google_distances.R index 05b3575e..2b64f40e 100644 --- a/tests/testthat/test-google_distances.R +++ b/tests/testthat/test-google_distances.R @@ -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",{ #