forked from linguanostra/GoogleMapsAPI.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeocodingAPITests.cs
More file actions
582 lines (458 loc) · 19.9 KB
/
Copy pathGeocodingAPITests.cs
File metadata and controls
582 lines (458 loc) · 19.9 KB
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
using FluentAssertions;
using GoogleMapsAPI.NET.API.Common.Components;
using GoogleMapsAPI.NET.API.Common.Components.Locations;
using GoogleMapsAPI.NET.API.Geocoding.Enums;
using GoogleMapsAPI.NET.API.Places.Enums;
using GoogleMapsAPI.NET.Requests;
using GoogleMapsAPI.NET.Tests.API.Common;
using GoogleMapsAPI.NET.Tests.API.Extensions;
using GoogleMapsAPI.NET.Tests.API.Utils.MockConfig;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace GoogleMapsAPI.NET.Tests.API.Geocoding
{
/// <summary>
/// Geocoding API tests
/// </summary>
[TestClass]
public class GeocodingAPITests : APITests
{
#region Test methods
/// <summary>
/// Test simple geocode
/// </summary>
[TestMethod]
public void TestSimpleGeocode()
{
// Get mocked client
using (var client = GetMockedAPIClient())
{
// Arrange mocks for result
var webMocks = client.ArrangeWebResponseValidResultsMocks();
// Make client call
client.Geocoding.Geocode("Sydney");
// Assertions
webMocks.WebRequestUtil.AssertGetWasCalledOnceWithUrl(
"https://maps.googleapis.com/maps/api/geocode/json?" +
"address=Sydney");
}
}
/// <summary>
/// Test reverse geocode
/// </summary>
[TestMethod]
public void TestReverseGeocode()
{
// Get mocked client
using (var client = GetMockedAPIClient())
{
// Arrange mocks for result
var webMocks = client.ArrangeWebResponseValidResultsMocks();
// Make client call
client.Geocoding.ReverseGeocode(
new GeoCoordinatesLocation(-33.867486, 151.206990));
// Assertions
webMocks.WebRequestUtil.AssertGetWasCalledOnceWithUrl(
"https://maps.googleapis.com/maps/api/geocode/json?" +
"latlng=-33.867486%2C151.20699");
}
}
/// <summary>
/// Test geocoding the GooglePlex
/// </summary>
[TestMethod]
public void TestGeocodingTheGoogleplex()
{
// Get mocked client
using (var client = GetMockedAPIClient())
{
// Arrange mocks for result
var webMocks = client.ArrangeWebResponseValidResultsMocks();
// Make client call
client.Geocoding.Geocode("1600 Amphitheatre Parkway, Mountain View, CA");
// Assertions
webMocks.WebRequestUtil.AssertGetWasCalledOnceWithUrl(
"https://maps.googleapis.com/maps/api/geocode/json?" +
"address=1600+Amphitheatre+Parkway%2C+Mountain+View%2C+CA");
}
}
/// <summary>
/// Test geocode with bounds
/// </summary>
[TestMethod]
public void TestGeocodeWithBounds()
{
// Get mocked client
using (var client = GetMockedAPIClient())
{
// Arrange mocks for result
var webMocks = client.ArrangeWebResponseValidResultsMocks();
// Make client call
client.Geocoding.Geocode("Winnetka",
bounds: new ViewportBoundingBox(
new GeoCoordinatesLocation(34.172684, -118.604794),
new GeoCoordinatesLocation(34.236144, -118.500938)
));
// Assertions
webMocks.WebRequestUtil.AssertGetWasCalledOnceWithUrl(
"https://maps.googleapis.com/maps/api/geocode/json?" +
"address=Winnetka&bounds=34.172684%2C-118.604794%7C" +
"34.236144%2C-118.500938");
}
}
/// <summary>
/// Test geocode with region biasing
/// </summary>
[TestMethod]
public void TestGeocodeWithRegionBiasing()
{
// Get mocked client
using (var client = GetMockedAPIClient())
{
// Arrange mocks for result
var webMocks = client.ArrangeWebResponseValidResultsMocks();
// Make client call
client.Geocoding.Geocode("Toledo", region: "es");
// Assertions
webMocks.WebRequestUtil.AssertGetWasCalledOnceWithUrl(
"https://maps.googleapis.com/maps/api/geocode/json?" +
"address=Toledo®ion=es");
}
}
/// <summary>
/// Test geocode with component filter
/// </summary>
[TestMethod]
public void TestGeocodeWithComponentFilter()
{
// Get mocked client
using (var client = GetMockedAPIClient())
{
// Arrange mocks for result
var webMocks = client.ArrangeWebResponseValidResultsMocks();
// Make client call
client.Geocoding.Geocode("santa cruz", new ComponentsFilter()
{
["country"] = "ES"
});
// Assertions
webMocks.WebRequestUtil.AssertGetWasCalledOnceWithUrl(
"https://maps.googleapis.com/maps/api/geocode/json?" +
"address=santa+cruz&components=country%3AES");
}
}
/// <summary>
/// Test geocode with multiple component filters
/// </summary>
[TestMethod]
public void TestGeocodeWithMultipleComponentFilters()
{
// Get mocked client
using (var client = GetMockedAPIClient())
{
// Arrange mocks for result
var webMocks = client.ArrangeWebResponseValidResultsMocks();
// Make client call
client.Geocoding.Geocode("Torun", new ComponentsFilter()
{
["administrative_area"] = "TX",
["country"] = "US"
});
// Assertions
webMocks.WebRequestUtil.AssertGetWasCalledOnceWithUrl(
"https://maps.googleapis.com/maps/api/geocode/json?" +
"address=Torun&components=administrative_area%3ATX%7C" +
"country%3AUS");
}
}
/// <summary>
/// Test geocode with only components
/// </summary>
[TestMethod]
public void TestGeocodeWithJustComponents()
{
// Get mocked client
using (var client = GetMockedAPIClient())
{
// Arrange mocks for result
var webMocks = client.ArrangeWebResponseValidResultsMocks();
// Make client call
client.Geocoding.Geocode(components: new ComponentsFilter()
{
["route"] = "Annegatan",
["administrative_area"] = "Helsinki",
["country"] = "Finland"
});
// Assertions
webMocks.WebRequestUtil.AssertGetWasCalledOnceWithUrl(
"https://maps.googleapis.com/maps/api/geocode/json?" +
"components=administrative_area%3AHelsinki%7Ccountry" +
"%3AFinland%7Croute%3AAnnegatan");
}
}
/// <summary>
/// Test simple reverse geocode
/// </summary>
[TestMethod]
public void TestSimpleReverseGeocode()
{
// Get mocked client
using (var client = GetMockedAPIClient())
{
// Arrange mocks for result
var webMocks = client.ArrangeWebResponseValidResultsMocks();
// Make client call
client.Geocoding.ReverseGeocode(
new GeoCoordinatesLocation(40.714224, -73.961452));
// Assertions
webMocks.WebRequestUtil.AssertGetWasCalledOnceWithUrl(
"https://maps.googleapis.com/maps/api/geocode/json?" +
"latlng=40.714224%2C-73.961452");
}
}
/// <summary>
/// Test reverse geocode restricted by type
/// </summary>
[TestMethod]
public void TestReverseGeocodeRestrictedByType()
{
// Get mocked client
using (var client = GetMockedAPIClient())
{
// Arrange mocks for result
var webMocks = client.ArrangeWebResponseValidResultsMocks();
// Make client call
client.Geocoding.ReverseGeocode(
new GeoCoordinatesLocation(40.714224, -73.961452),
locationType: GeometryLocationType.Rooftop,
addressType: AddressTypeEnum.StreetAddress);
// Assertions
webMocks.WebRequestUtil.AssertGetWasCalledOnceWithUrl(
"https://maps.googleapis.com/maps/api/geocode/json?" +
"latlng=40.714224%2C-73.961452&result_type=street_address" +
"&location_type=ROOFTOP");
}
}
/// <summary>
/// Test reverse geocode with multiple location types
/// </summary>
[TestMethod]
public void TestReverseGeocodeMultipleLocationTypes()
{
// Get mocked client
using (var client = GetMockedAPIClient())
{
// Arrange mocks for result
var webMocks = client.ArrangeWebResponseValidResultsMocks();
// Make client call
client.Geocoding.ReverseGeocode(
new GeoCoordinatesLocation(40.714224, -73.961452),
locationType: GeometryLocationType.Rooftop | GeometryLocationType.RangeInterpolated,
addressType: AddressTypeEnum.StreetAddress);
// Assertions
webMocks.WebRequestUtil.AssertGetWasCalledOnceWithUrl(
"https://maps.googleapis.com/maps/api/geocode/json?" +
"latlng=40.714224%2C-73.961452&result_type=street_address" +
"&location_type=RANGE_INTERPOLATED%7CROOFTOP");
}
}
/// <summary>
/// Test reverse geocode multiple result types
/// </summary>
[TestMethod]
public void TestReverseGeocodeMultipleResultTypes()
{
// Get mocked client
using (var client = GetMockedAPIClient())
{
// Arrange mocks for result
var webMocks = client.ArrangeWebResponseValidResultsMocks();
// Make client call
client.Geocoding.ReverseGeocode(
new GeoCoordinatesLocation(40.714224, -73.961452),
locationType: GeometryLocationType.Rooftop,
addressType: AddressTypeEnum.StreetAddress | AddressTypeEnum.Route);
// Assertions
webMocks.WebRequestUtil.AssertGetWasCalledOnceWithUrl(
"https://maps.googleapis.com/maps/api/geocode/json?" +
"latlng=40.714224%2C-73.961452&result_type=route" +
"%7Cstreet_address&location_type=ROOFTOP");
}
}
/// <summary>
/// Test partial match
/// </summary>
[TestMethod]
public void TestPartialMatch()
{
// Get mocked client
using (var client = GetMockedAPIClient())
{
// Arrange mocks for result
var webMocks = client.ArrangeWebResponseValidResultsMocks();
// Make client call
client.Geocoding.Geocode("Pirrama Pyrmont");
// Assertions
webMocks.WebRequestUtil.AssertGetWasCalledOnceWithUrl(
"https://maps.googleapis.com/maps/api/geocode/json?" +
"address=Pirrama+Pyrmont");
}
}
/// <summary>
/// Test UTF result
/// </summary>
[TestMethod]
public void TestUtfResults()
{
// Get mocked client
using (var client = GetMockedAPIClient())
{
// Arrange mocks for result
var webMocks = client.ArrangeWebResponseValidResultsMocks();
// Make client call
client.Geocoding.Geocode(components: new ComponentsFilter()
{
["postal_code"] = "96766"
});
// Assertions
webMocks.WebRequestUtil.AssertGetWasCalledOnceWithUrl(
"https://maps.googleapis.com/maps/api/geocode/json?" +
"components=postal_code%3A96766");
}
}
/// <summary>
/// Test UTF8 request
/// </summary>
[TestMethod]
public void TestUtf8Request()
{
// Get mocked client
using (var client = GetMockedAPIClient())
{
// Arrange mocks for result
var webMocks = client.ArrangeWebResponseValidResultsMocks();
// Make client call
client.Geocoding.Geocode("中国"); // China
// Assertions
webMocks.WebRequestUtil.AssertGetWasCalledOnceWithUrl(
"https://maps.googleapis.com/maps/api/geocode/json?" +
"address=%E4%B8%AD%E5%9B%BD");
}
}
/// <summary>
/// Parse geocode response
/// </summary>
[TestMethod]
public void TestParseGeocodeResponse()
{
// Get mocked client
using (var client = GetMockedAPIClient())
{
// Set response data
var responseData = @"{
""results"" : [
{
""address_components"" : [
{
""long_name"" : ""1600"",
""short_name"" : ""1600"",
""types"" : [ ""street_number"" ]
},
{
""long_name"" : ""Amphitheatre Parkway"",
""short_name"" : ""Amphitheatre Pkwy"",
""types"" : [ ""route"" ]
},
{
""long_name"" : ""Mountain View"",
""short_name"" : ""Mountain View"",
""types"" : [ ""locality"", ""political"" ]
},
{
""long_name"" : ""Santa Clara County"",
""short_name"" : ""Santa Clara County"",
""types"" : [ ""administrative_area_level_2"", ""political"" ]
},
{
""long_name"" : ""California"",
""short_name"" : ""CA"",
""types"" : [ ""administrative_area_level_1"", ""political"" ]
},
{
""long_name"" : ""United States"",
""short_name"" : ""US"",
""types"" : [ ""country"", ""political"" ]
},
{
""long_name"" : ""94043"",
""short_name"" : ""94043"",
""types"" : [ ""postal_code"" ]
}
],
""formatted_address"" : ""1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA"",
""geometry"" : {
""location"" : {
""lat"" : 37.4224504,
""lng"" : -122.0840859
},
""location_type"" : ""ROOFTOP"",
""viewport"" : {
""northeast"" : {
""lat"" : 37.4237993802915,
""lng"" : -122.0827369197085
},
""southwest"" : {
""lat"" : 37.4211014197085,
""lng"" : -122.0854348802915
}
}
},
""place_id"" : ""ChIJ2eUgeAK6j4ARbn5u_wAGqWA"",
""types"" : [ ""street_address"" ]
}
],
""status"" : ""OK""
}";
// Arrange mocks for response data
var webMocks = client.ArrangeWebResponseResultMocks(
new MockResultWebResponseConfig(responseData));
// Make client call
var data = client.Geocoding.Geocode("1600 Amphitheatre Parkway, Mountain View, CA");
// Assertions
webMocks.WebRequestUtil.AssertGetWasCalledOnceWithUrl(
"https://maps.googleapis.com/maps/api/geocode/json?" +
"address=1600+Amphitheatre+Parkway%2C+Mountain+View%2C+CA");
// Data
data.IsValid.Should().BeTrue();
data.HasErrorMessage.Should().BeFalse();
// Results
data.Results.Should().NotBeNullOrEmpty();
data.Results.Count.Should().Be(1);
data.Results[0].AddressComponents.Should().NotBeNullOrEmpty();
data.Results[0].AddressComponents.Count.Should().Be(7);
data.Results[0].AddressComponents[5].LongName.Should().Be("United States");
data.Results[0].AddressComponents[5].ShortName.Should().Be("US");
data.Results[0].AddressComponents[5].Types.Should().NotBeNullOrEmpty();
data.Results[0].AddressComponents[5].Types.Should().Contain(
new[] { PlaceResultTypeEnum.Country, PlaceResultTypeEnum.Political});
data.Results[0].FormattedAddress.Should().StartWith("1600 Amphitheatre Pkwy");
data.Results[0].PlaceId.Should().Be("ChIJ2eUgeAK6j4ARbn5u_wAGqWA");
data.Results[0].Types.Should().NotBeNullOrEmpty();
data.Results[0].Types.Count.Should().Be(1);
data.Results[0].Types.Should().Contain("street_address");
data.Results[0].Geometry.Should().NotBeNull();
data.Results[0].Geometry.Location.Should().NotBeNull();
data.Results[0].Geometry.Location.Latitude.Should().Be(37.4224504);
data.Results[0].Geometry.Location.Longitude.Should().Be(-122.0840859);
data.Results[0].Geometry.LocationType.Should().Be(GeometryLocationType.Rooftop);
data.Results[0].Geometry.Viewport.Should().NotBeNull();
data.Results[0].Geometry.Viewport.NorthEast.Should().NotBeNull();
data.Results[0].Geometry.Viewport.NorthEast.Latitude.Should().Be(37.4237993802915);
data.Results[0].Geometry.Viewport.NorthEast.Longitude.Should().Be(-122.0827369197085);
data.Results[0].Geometry.Viewport.Southwest.Should().NotBeNull();
data.Results[0].Geometry.Viewport.Southwest.Latitude.Should().Be(37.4211014197085);
data.Results[0].Geometry.Viewport.Southwest.Longitude.Should().Be(-122.0854348802915);
}
}
#endregion
}
}