-
Notifications
You must be signed in to change notification settings - Fork 2
/
integration_test.go
375 lines (322 loc) · 8.65 KB
/
integration_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
package mapbox
import (
"context"
"math"
"os"
"reflect"
"strings"
"testing"
"time"
)
var (
testAPIDelay = time.Second
testLocations = map[string]struct {
Lat float64
Lng float64
Country string
Place string
POI string
Query string
QueryMatch string
Brand []string
}{
"Eiffel Tower": {
Lat: 48.858415953144025,
Lng: 2.2944920264583892,
Country: "France",
Place: "Paris",
POI: "Les Boutiques Officielles de la Tour Eiffel",
Query: "Av. Gustave Eiffel, 75007 Paris, France",
QueryMatch: "Paris",
},
"Golden Gate Bridge": {
Lat: 37.81999562350779,
Lng: -122.47855980298934,
Country: "United States",
Place: "Sausalito",
POI: "Plaza Park Square",
Query: "Golden Gate Bridge, San Francisco, CA, United States",
QueryMatch: "Sausalito",
},
"Machu Picchu": {
Lat: -13.163104764687816,
Lng: -72.54525137460071,
Country: "Peru",
Place: "Machu Picchu",
Query: "Santuario Histórico de Machu Picchu, 08680, Peru",
QueryMatch: "Machupicchu",
},
"Victoria Falls": {
Lat: -17.925510375019098,
Lng: 25.858544325497473,
Country: "Zimbabwe",
Place: "Victoria Falls",
Query: "2 Livingstone Way, Victoria Falls, Zimbabwe",
QueryMatch: "Victoria Falls",
},
"Tower of London": {
Lat: 51.508159042792094,
Lng: -0.07592785723634357,
Place: "London",
Country: "United Kingdom",
POI: "The Tower of London",
Query: "Tower of London, London EC3N 4AB, United Kingdom",
QueryMatch: "London",
},
"Hanging Gardens of Babylon": {
Lat: 32.54417286881489,
Lng: 44.42049788351785,
Country: "Iraq",
Query: "GCVC+J54, Mahawil, Babylon Governorate, Iraq",
QueryMatch: "Babylon",
// No Mapbox POI for this one
},
"Brand Test": {
Lat: 35.2176833,
Lng: -97.4949642,
Place: "Norman",
Country: "United States",
POI: "LIDS",
Brand: []string{"LIDS", "LIDS / Hat World"},
Query: "3600 W Main St #350, Norman, OK 73072, United States",
QueryMatch: "Norman",
},
}
)
func TestIntegration_ReverseGeocode(t *testing.T) {
// ask for all supported even though some won't exist for the coordinate
features := Types{
TypeCountry,
TypeRegion,
TypePostcode,
TypeDistrict,
TypePlace,
TypeLocality,
TypeNeighborhood,
TypeStreet,
TypeAddress,
}
client, err := NewClient(&MapboxConfig{
Timeout: 30 * time.Second,
APIKey: os.Getenv("API_KEY"),
})
if err != nil {
t.Fatal(err)
}
for name, loc := range testLocations {
t.Run(name, func(t *testing.T) {
request := &ReverseGeocodeRequest{
Coordinate: Coordinate{Lat: loc.Lat, Lng: loc.Lng},
Language: "en",
Types: features,
}
resp, err := client.ReverseGeocode(context.Background(), request)
if err != nil {
t.Fatal(err)
}
if resp == nil {
t.Fatal("response should not be nil")
}
// just check the obvious ones
compared := make(map[Type]struct{})
for _, feature := range resp.Features {
//nolint:exhaustive
switch feature.Properties.FeatureType {
case TypeCountry:
compared[TypeCountry] = struct{}{}
if feature.Properties.Name != loc.Country {
t.Errorf("expected %v country %v to be %v", name, feature.Properties.Name, loc.Country)
}
case TypePlace:
compared[TypePlace] = struct{}{}
if feature.Properties.Name != loc.Place {
t.Errorf("expected %v place %v to be %v", name, feature.Properties.Name, loc.Country)
}
}
}
if _, seen := compared[TypeCountry]; !seen {
t.Error("response did not include a country feature")
}
if _, seen := compared[TypePlace]; !seen && loc.Place != "" {
t.Error("response did not include a place feature")
}
})
time.Sleep(testAPIDelay)
}
}
func TestIntegration_ReverseGeocodeBatch(t *testing.T) {
// ask for all supported even though some won't exist for the coordinate
features := Types{
TypeCountry,
TypeRegion,
TypePostcode,
TypeDistrict,
TypePlace,
TypeLocality,
TypeNeighborhood,
TypeStreet,
TypeAddress,
}
client, err := NewClient(&MapboxConfig{
Timeout: 30 * time.Second,
APIKey: os.Getenv("API_KEY"),
})
if err != nil {
t.Fatal(err)
}
var keys []string // for maintaining order for response
var requests ReverseGeocodeBatchRequest
for key, loc := range testLocations {
keys = append(keys, key)
requests = append(requests, ReverseGeocodeRequest{
Coordinate: Coordinate{Lat: loc.Lat, Lng: loc.Lng},
Language: "en",
Types: features,
})
}
resps, err := client.ReverseGeocodeBatch(context.Background(), requests)
if err != nil {
t.Fatal(err)
}
if len(resps.Batch) != len(keys) {
t.Fatalf("expected batch response length %v to be %v", len(resps.Batch), len(keys))
}
// just check the obvious ones
for i, key := range keys {
expected := testLocations[key]
compared := make(map[Type]struct{})
resp := resps.Batch[i]
for _, feature := range resp.Features {
//nolint:exhaustive
switch feature.Properties.FeatureType {
case TypeCountry:
compared[TypeCountry] = struct{}{}
if feature.Properties.Name != expected.Country {
t.Errorf("expected %v country %v to be %v", key, feature.Properties.Name, expected.Country)
}
case TypePlace:
compared[TypePlace] = struct{}{}
if feature.Properties.Name != expected.Place {
t.Errorf("expected %v place %v to be %v", key, feature.Properties.Name, expected.Country)
}
}
}
if _, seen := compared[TypeCountry]; !seen {
t.Errorf("response for %v did not include a country feature", key)
}
if _, seen := compared[TypePlace]; !seen && expected.Place != "" {
t.Errorf("response for %v did not include a place feature", key)
}
}
}
func TestIntegration_ForwardGeocode(t *testing.T) {
// ask for all supported even though some won't exist for the coordinate
features := Types{
TypeCountry,
TypeRegion,
TypePostcode,
TypeDistrict,
TypePlace,
TypeLocality,
TypeNeighborhood,
TypeStreet,
TypeAddress,
}
client, err := NewClient(&MapboxConfig{
Timeout: 30 * time.Second,
APIKey: os.Getenv("API_KEY"),
})
if err != nil {
t.Fatal(err)
}
for name, loc := range testLocations {
t.Run(name, func(t *testing.T) {
request := ForwardGeocodeRequest{SearchText: loc.Query, Types: features}
resp, err := client.ForwardGeocode(context.Background(), &request)
if err != nil {
t.Fatal(err)
}
if resp == nil {
t.Fatal("resp should not be nil")
}
// Check if response includes a nearby place
var found bool
for _, feature := range resp.Features {
if !strings.Contains(feature.Properties.FullAddress, loc.QueryMatch) {
continue
}
// not the match we're looking for: latitude out of bounds
if math.Abs(feature.Properties.Coordinates.Latitude-loc.Lat) > 0.5 {
continue
}
// not the match we're looking for: longitude out of bounds
if math.Abs(feature.Properties.Coordinates.Longitude-loc.Lng) > 0.5 {
continue
}
// match found!
found = true
break
}
if !found {
t.Errorf("response did not include a feature with for %v and near coordinates [%v, %v]", loc.QueryMatch, loc.Lat, loc.Lng)
}
})
}
}
func TestIntegration_SearchboxReverse(t *testing.T) {
// ask for all supported even though some won't exist for the coordinate
features := Types{
TypePOI,
}
client, err := NewClient(&MapboxConfig{
Timeout: 30 * time.Second,
APIKey: os.Getenv("API_KEY"),
})
if err != nil {
t.Fatal(err)
}
for name, loc := range testLocations {
t.Run(name, func(t *testing.T) {
if loc.POI == "" {
t.Skipf("no expected POI for location %s", name)
}
request := &SearchboxReverseRequest{
Coordinate: Coordinate{Lat: loc.Lat, Lng: loc.Lng},
Language: "en",
Types: features,
}
resp, err := client.SearchboxReverse(context.Background(), request)
if err != nil {
t.Fatal(err)
}
if resp == nil {
t.Fatal("response should not be nil")
}
// just check the obvious ones
var found bool
for _, feature := range resp.Features {
if feature.Properties.FeatureType != TypePOI {
continue
}
// not the match we're looking for: incorrect POI
if feature.Properties.Name != loc.POI {
continue
}
if loc.Brand == nil {
found = true
break
}
if !reflect.DeepEqual(feature.Properties.Brand, loc.Brand) {
t.Logf("feature %v had brand %v but expected %v", loc.POI, feature.Properties.Brand, loc.Brand)
continue
}
found = true
break
}
if !found {
t.Error("response did not include the specified point of interest")
}
})
time.Sleep(testAPIDelay)
}
}