-
Notifications
You must be signed in to change notification settings - Fork 216
/
distancematrix_test.go
405 lines (368 loc) · 12.4 KB
/
distancematrix_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package maps
import (
"context"
"reflect"
"testing"
"time"
)
func TestDistanceMatrixWithCoordinatesAndTraffic(t *testing.T) {
// Distance Matrix response from 1.315125,103.76471334 to 1.280776, 103.8487 with most steps elided.
response := `{
"destination_addresses": ["3150 Commonwealth Ave W, Singapore 129580"],
"origin_addresses": ["105 Cecil St, Singapore 069534"],
"rows": [
{
"elements": [
{
"distance": {
"text": "12.5 km",
"value": 12535
},
"duration": {
"text": "18 mins",
"value": 1083
},
"duration_in_traffic": {
"text": "19 mins",
"value": 1134
},
"status": "OK"
}
]
}
],
"status": "OK"
}`
server := mockServer(200, response)
defer server.Close()
c, _ := NewClient(WithAPIKey(apiKey), WithBaseURL(server.URL))
r := &DistanceMatrixRequest{
Origins: []string{"1.315125,103.76471334"},
Destinations: []string{"1.280776,103.8487"},
DepartureTime: `now`,
Units: `UnitsMetric`,
Mode: TravelModeDriving,
}
resp, err := c.DistanceMatrix(context.Background(), r)
if err != nil {
t.Errorf("r.Get returned non nil error, was %+v", err)
}
correctResponse := &DistanceMatrixResponse{
OriginAddresses: []string{"105 Cecil St, Singapore 069534"},
DestinationAddresses: []string{"3150 Commonwealth Ave W, Singapore 129580"},
Rows: []DistanceMatrixElementsRow{
{
Elements: []*DistanceMatrixElement{
{
Status: "OK",
Duration: 1083000000000,
DurationInTraffic: 1134000000000,
Distance: Distance{HumanReadable: "12.5 km", Meters: 12535},
},
},
},
},
}
if !reflect.DeepEqual(resp, correctResponse) {
t.Errorf("expected %+v, was %+v", correctResponse, resp)
}
}
func TestDistanceMatrixWithMexicanOriginAndDestination(t *testing.T) {
// view-source:https://maps.googleapis.com/maps/api/distancematrix/json?origins=19.3021022,-99.2298197&destinations=19.51018,-99.12585&mode=driving&units=metric&departure_time=now&traffic_model=best_guess
response := `{
"destination_addresses" : [
"Av Instituto Politécnico Nacional 3600, San Pedro Zacatenco, 07360 Ciudad de México, CDMX, Mexico"
],
"origin_addresses" : [
"Cto. Fuentes del Pedregal 555, Los Framboyanes, 14150 Ciudad de México, CDMX, Mexico"
],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "30.5 km",
"value" : 30539
},
"duration" : {
"text" : "50 mins",
"value" : 3001
},
"duration_in_traffic" : {
"text" : "51 mins",
"value" : 3040
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}`
server := mockServer(200, response)
defer server.Close()
c, _ := NewClient(WithAPIKey(apiKey), WithBaseURL(server.URL))
r := &DistanceMatrixRequest{
Origins: []string{"19.3021022,-99.2298197"},
Destinations: []string{"19.51018,-99.12585"},
DepartureTime: `now`,
Units: `UnitsMetric`,
Mode: TravelModeDriving,
TrafficModel: TrafficModelBestGuess,
}
resp, err := c.DistanceMatrix(context.Background(), r)
if err != nil {
t.Errorf("r.Get returned non nil error, was %+v", err)
}
if resp.OriginAddresses[0] != "Cto. Fuentes del Pedregal 555, Los Framboyanes, 14150 Ciudad de México, CDMX, Mexico" {
t.Error("Incorrect origin address")
}
if resp.DestinationAddresses[0] != "Av Instituto Politécnico Nacional 3600, San Pedro Zacatenco, 07360 Ciudad de México, CDMX, Mexico" {
t.Error("Incorrect destination address")
}
if resp.Rows[0].Elements[0].Distance.HumanReadable != "30.5 km" {
t.Error("Incorrect human readable distance")
}
if resp.Rows[0].Elements[0].Distance.Meters != 30539 {
t.Error("Incorrect distance value")
}
if resp.Rows[0].Elements[0].Duration != time.Second*3001 {
t.Errorf("Incorrect Duration: %v", resp.Rows[0].Elements[0].Duration)
}
if resp.Rows[0].Elements[0].DurationInTraffic != time.Second*3040 {
t.Errorf("Incorrect DurationInTraffic: %v", resp.Rows[0].Elements[0].DurationInTraffic)
}
if resp.Rows[0].Elements[0].Status != "OK" {
t.Error("Incorrect element status")
}
}
func TestDistanceMatrixSydPyrToPar(t *testing.T) {
// Distance Matrix from Sydney And Pyrmont to Parramatta with most steps elided.
response := `{
"destination_addresses" : [ "Parramatta NSW, Australia" ],
"origin_addresses" : [ "Sydney NSW, Australia", "Pyrmont NSW, Australia" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "23.8 km",
"value" : 23846
},
"duration" : {
"text" : "37 mins",
"value" : 2215
},
"status" : "OK"
}
]
},
{
"elements" : [
{
"distance" : {
"text" : "22.2 km",
"value" : 22242
},
"duration" : {
"text" : "34 mins",
"value" : 2058
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}`
server := mockServer(200, response)
defer server.Close()
c, _ := NewClient(WithAPIKey(apiKey), WithBaseURL(server.URL))
r := &DistanceMatrixRequest{
Origins: []string{"Sydney", "Pyrmont"},
Destinations: []string{"Parramatta"},
}
resp, err := c.DistanceMatrix(context.Background(), r)
if err != nil {
t.Errorf("r.Get returned non nil error, was %+v", err)
}
correctResponse := &DistanceMatrixResponse{
OriginAddresses: []string{"Sydney NSW, Australia", "Pyrmont NSW, Australia"},
DestinationAddresses: []string{"Parramatta NSW, Australia"},
Rows: []DistanceMatrixElementsRow{
{
Elements: []*DistanceMatrixElement{
{
Status: "OK",
Duration: 2215000000000,
Distance: Distance{HumanReadable: "23.8 km", Meters: 23846},
},
},
},
{
Elements: []*DistanceMatrixElement{
{
Status: "OK",
Duration: 2058000000000,
Distance: Distance{HumanReadable: "22.2 km", Meters: 22242},
},
},
},
},
}
if !reflect.DeepEqual(resp, correctResponse) {
t.Errorf("expected %+v, was %+v", correctResponse, resp)
}
}
func TestDistanceMatrixMissingOrigins(t *testing.T) {
c, _ := NewClient(WithAPIKey(apiKey))
r := &DistanceMatrixRequest{
Origins: []string{},
Destinations: []string{"Parramatta"},
}
if _, err := c.DistanceMatrix(context.Background(), r); err == nil {
t.Errorf("Missing Origins should return error")
}
}
func TestDistanceMatrixMissingDestinations(t *testing.T) {
c, _ := NewClient(WithAPIKey(apiKey))
r := &DistanceMatrixRequest{
Origins: []string{"Sydney", "Pyrmont"},
Destinations: []string{},
}
if _, err := c.DistanceMatrix(context.Background(), r); err == nil {
t.Errorf("Missing Destinations should return error")
}
}
func TestDistanceMatrixDepartureAndArrivalTime(t *testing.T) {
c, _ := NewClient(WithAPIKey(apiKey))
r := &DistanceMatrixRequest{
Origins: []string{"Sydney", "Pyrmont"},
Destinations: []string{"Parramatta", "Perth"},
DepartureTime: "now",
ArrivalTime: "4pm",
}
if _, err := c.DistanceMatrix(context.Background(), r); err == nil {
t.Errorf("Having both Departure time and Arrival time should return error")
}
}
func TestDistanceMatrixTravelModeTransit(t *testing.T) {
c, _ := NewClient(WithAPIKey(apiKey))
var transitModes []TransitMode
transitModes = append(transitModes, TransitModeBus)
r := &DistanceMatrixRequest{
Origins: []string{"Sydney"},
Destinations: []string{"Parramatta"},
TransitMode: transitModes,
}
if _, err := c.DistanceMatrix(context.Background(), r); err == nil {
t.Errorf("Declaring TransitMode without Mode=Transit should return error")
}
}
func TestDistanceMatrixTransitRoutingPreference(t *testing.T) {
c, _ := NewClient(WithAPIKey(apiKey))
r := &DistanceMatrixRequest{
Origins: []string{"Sydney"},
Destinations: []string{"Parramatta"},
TransitRoutingPreference: TransitRoutingPreferenceFewerTransfers,
}
if _, err := c.DistanceMatrix(context.Background(), r); err == nil {
t.Errorf("Declaring TransitRoutingPreference without Mode=TravelModeTransit should return error")
}
}
func TestDistanceMatrixTrafficTransitPreference(t *testing.T) {
c, _ := NewClient(WithAPIKey(apiKey))
r := &DistanceMatrixRequest{
Origins: []string{"Sydney"},
Destinations: []string{"Parramatta"},
TransitRoutingPreference: TransitRoutingPreferenceFewerTransfers,
TrafficModel: TrafficModelPessimistic,
}
if _, err := c.DistanceMatrix(context.Background(), r); err == nil {
t.Errorf("Declaring TransitRoutingPreference without Mode=TravelModeTransit should return error")
}
}
func TestDistanceMatrixWithCancelledContext(t *testing.T) {
c, _ := NewClient(WithAPIKey(apiKey))
r := &DistanceMatrixRequest{
Origins: []string{"Sydney", "Pyrmont"},
Destinations: []string{"Parramatta", "Perth"},
}
ctx, cancel := context.WithCancel(context.Background())
cancel()
if _, err := c.DistanceMatrix(ctx, r); err == nil {
t.Errorf("Cancelled context should return non-nil err")
}
}
func TestDistanceMatrixFailingServer(t *testing.T) {
server := mockServer(500, `{"status" : "ERROR"}`)
defer server.Close()
c, _ := NewClient(WithAPIKey(apiKey), WithBaseURL(server.URL))
r := &DistanceMatrixRequest{
Origins: []string{"Sydney", "Pyrmont"},
Destinations: []string{},
}
if _, err := c.DistanceMatrix(context.Background(), r); err == nil {
t.Errorf("Failing server should return error")
}
}
func TestDistanceMatrixTransitRequestURL(t *testing.T) {
expectedQuery := "avoid=tolls&departure_time=now&destinations=Perth%7CParramatta&key=AIzaNotReallyAnAPIKey&language=en&mode=transit&origins=Sydney%7CPyrmont&transit_mode=rail&transit_routing_preference=less_walking&units=imperial"
server := mockServerForQuery(expectedQuery, 200, `{"status":"OK"}"`)
defer server.s.Close()
c, _ := NewClient(WithAPIKey(apiKey), WithBaseURL(server.s.URL))
r := &DistanceMatrixRequest{
Origins: []string{"Sydney", "Pyrmont"},
Destinations: []string{"Perth", "Parramatta"},
Mode: TravelModeTransit,
Language: "en",
Avoid: AvoidTolls,
Units: UnitsImperial,
DepartureTime: "now",
TransitMode: []TransitMode{TransitModeRail},
TransitRoutingPreference: TransitRoutingPreferenceLessWalking,
}
_, err := c.DistanceMatrix(context.Background(), r)
if err != nil {
t.Errorf("Unexpected error in constructing request URL: %+v", err)
}
if server.successful != 1 {
t.Errorf("Got URL(s) %v, want %s", server.failed, expectedQuery)
}
}
func TestDistanceMatrixTrafficRequestURL(t *testing.T) {
expectedQuery := "avoid=tolls&departure_time=now&destinations=Perth%7CParramatta&key=AIzaNotReallyAnAPIKey&language=en&mode=driving&origins=Sydney%7CPyrmont&traffic_model=pessimistic&units=imperial"
server := mockServerForQuery(expectedQuery, 200, `{"status":"OK"}"`)
defer server.s.Close()
c, _ := NewClient(WithAPIKey(apiKey), WithBaseURL(server.s.URL))
r := &DistanceMatrixRequest{
Origins: []string{"Sydney", "Pyrmont"},
Destinations: []string{"Perth", "Parramatta"},
Avoid: AvoidTolls,
Mode: TravelModeDriving,
Language: "en",
DepartureTime: "now",
TrafficModel: TrafficModelPessimistic,
Units: UnitsImperial,
}
_, err := c.DistanceMatrix(context.Background(), r)
if err != nil {
t.Errorf("Unexpected error in constructing request URL: %+v", err)
}
if server.successful != 1 {
t.Errorf("Got URL(s) %v, want %s", server.failed, expectedQuery)
}
}