-
Notifications
You must be signed in to change notification settings - Fork 143
/
Copy pathGeocodingTests.cs
121 lines (95 loc) · 4.77 KB
/
GeocodingTests.cs
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
using System;
using System.Linq;
using System.Security.Authentication;
using System.Threading;
using System.Threading.Tasks;
using GoogleMapsApi.Entities.Common;
using GoogleMapsApi.Entities.Geocoding.Request;
using NUnit.Framework;
using Status = GoogleMapsApi.Entities.Geocoding.Response.Status;
namespace GoogleMapsApi.Test.IntegrationTests
{
[TestFixture]
public class GeocodingTests : BaseTestIntegration
{
[Test]
public void Geocoding_ReturnsCorrectLocation()
{
var request = new GeocodingRequest
{
ApiKey = ApiKey,
Address = "285 Bedford Ave, Brooklyn, NY 11211, USA"
};
var result = GoogleMaps.Geocode.Query(request);
if (result.Status == Status.OVER_QUERY_LIMIT)
Assert.Inconclusive("Cannot run test since you have exceeded your Google API query limit.");
Assert.AreEqual(Status.OK, result.Status);
// 40.{*}, -73.{*}
StringAssert.IsMatch("40\\.\\d*,-73\\.\\d*", result.Results.First().Geometry.Location.LocationString);
}
[Test]
public void GeocodingAsync_ReturnsCorrectLocation()
{
var request = new GeocodingRequest { Address = "285 Bedford Ave, Brooklyn, NY 11211, USA" };
var result = GoogleMaps.Geocode.QueryAsync(request).Result;
if (result.Status == Status.OVER_QUERY_LIMIT)
Assert.Inconclusive("Cannot run test since you have exceeded your Google API query limit.");
Assert.AreEqual(Status.OK, result.Status);
// 40.{*}, -73.{*}
StringAssert.IsMatch("40\\.\\d*,-73\\.\\d*", result.Results.First().Geometry.Location.LocationString);
}
[Test]
public void Geocoding_InvalidClientCredentials_Throws()
{
var request = new GeocodingRequest { Address = "285 Bedford Ave, Brooklyn, NY 11211, USA", ClientID = "gme-ThisIsAUnitTest", SigningKey = "AAECAwQFBgcICQoLDA0ODxAREhM=" };
Assert.Throws<AuthenticationException>(() => GoogleMaps.Geocode.Query(request));
}
[Test]
public void GeocodingAsync_InvalidClientCredentials_Throws()
{
var request = new GeocodingRequest { Address = "285 Bedford Ave, Brooklyn, NY 11211, USA", ClientID = "gme-ThisIsAUnitTest", SigningKey = "AAECAwQFBgcICQoLDA0ODxAREhM=" };
Assert.Throws(Is.TypeOf<AggregateException>().And.InnerException.TypeOf<AuthenticationException>(),
() => GoogleMaps.Geocode.QueryAsync(request).Wait());
}
[Test]
public void GeocodingAsync_Cancel_Throws()
{
var request = new GeocodingRequest { Address = "285 Bedford Ave, Brooklyn, NY 11211, USA" };
var tokeSource = new CancellationTokenSource();
var task = GoogleMaps.Geocode.QueryAsync(request, tokeSource.Token);
tokeSource.Cancel();
Assert.Throws(Is.TypeOf<AggregateException>().And.InnerException.TypeOf<TaskCanceledException>(),
() => task.Wait());
}
[Test]
public void GeocodingAsync_WithPreCanceledToken_Cancels()
{
var request = new GeocodingRequest { Address = "285 Bedford Ave, Brooklyn, NY 11211, USA" };
var cts = new CancellationTokenSource();
cts.Cancel();
var task = GoogleMaps.Geocode.QueryAsync(request, cts.Token);
Assert.Throws(Is.TypeOf<AggregateException>().And.InnerException.TypeOf<TaskCanceledException>(),
() => task.Wait());
}
[Test]
public void ReverseGeocoding_ReturnsCorrectAddress()
{
var request = new GeocodingRequest { Location = new Location(40.7141289, -73.9614074) };
var result = GoogleMaps.Geocode.Query(request);
if (result.Status == Status.OVER_QUERY_LIMIT)
Assert.Inconclusive("Cannot run test since you have exceeded your Google API query limit.");
Assert.AreEqual(Status.OK, result.Status);
StringAssert.Contains("Bedford Ave, Brooklyn, NY 11211, USA", result.Results.First().FormattedAddress);
}
[Test]
public void ReverseGeocodingAsync_ReturnsCorrectAddress()
{
var request = new GeocodingRequest { Location = new Location(40.7141289, -73.9614074) };
var result = GoogleMaps.Geocode.QueryAsync(request).Result;
if (result.Status == Status.OVER_QUERY_LIMIT)
Assert.Inconclusive("Cannot run test since you have exceeded your Google API query limit.");
Assert.AreEqual(Status.OK, result.Status);
StringAssert.Contains("Bedford Ave, Brooklyn, NY 11211, USA", result.Results.First().FormattedAddress);
}
}
}