forked from chadly/Geocoding.net
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGoogleGeocoderTest.cs
More file actions
160 lines (134 loc) · 6.14 KB
/
GoogleGeocoderTest.cs
File metadata and controls
160 lines (134 loc) · 6.14 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
using Geocoding.Google;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Xunit;
namespace Geocoding.Tests
{
[Collection("Settings")]
public class GoogleGeocoderTest : GeocoderTest
{
GoogleGeocoder geocoder;
public GoogleGeocoderTest(SettingsFixture settings)
: base(settings) { }
protected override IGeocoder CreateGeocoder()
{
string apiKey = settings.GoogleApiKey;
if (String.IsNullOrEmpty(apiKey))
{
geocoder = new GoogleGeocoder();
}
else
{
geocoder = new GoogleGeocoder(apiKey);
}
return geocoder;
}
[Theory]
[InlineData("United States", GoogleAddressType.Country)]
[InlineData("Illinois, US", GoogleAddressType.AdministrativeAreaLevel1)]
[InlineData("New York, New York", GoogleAddressType.Locality)]
[InlineData("90210, US", GoogleAddressType.PostalCode)]
[InlineData("1600 pennsylvania ave washington dc", GoogleAddressType.StreetAddress)]
[InlineData("muswellbrook 2 New South Wales Australia", GoogleAddressType.Unknown)]
public async Task CanParseAddressTypes(string address, GoogleAddressType type)
{
GoogleAddress[] addresses = (await geocoder.GeocodeAsync(address)).ToArray();
Assert.Equal(type, addresses[0].Type);
}
[Theory]
[InlineData("United States", GoogleLocationType.Approximate)]
[InlineData("Illinois, US", GoogleLocationType.Approximate)]
[InlineData("Ingalls Corners Road, Canastota, NY 13032, USA", GoogleLocationType.GeometricCenter)]
[InlineData("51 Harry S. Truman Parkway, Annapolis, MD 21401, USA", GoogleLocationType.RangeInterpolated)]
[InlineData("1600 pennsylvania ave washington dc", GoogleLocationType.Rooftop)]
[InlineData("muswellbrook 2 New South Wales Australia", GoogleLocationType.Approximate)]
public async Task CanParseLocationTypes(string address, GoogleLocationType type)
{
GoogleAddress[] addresses = (await geocoder.GeocodeAsync(address)).ToArray();
Assert.Equal(type, addresses[0].LocationType);
}
[Theory]
[InlineData("United States", "fr", "États-Unis")]
[InlineData("Montreal", "en", "Montreal, QC, Canada")]
[InlineData("Montreal", "fr", "Montréal, QC, Canada")]
[InlineData("Montreal", "de", "Montreal, Québec, Kanada")]
public async Task ApplyLanguage(string address, string language, string result)
{
geocoder.Language = language;
GoogleAddress[] addresses = (await geocoder.GeocodeAsync(address)).ToArray();
Assert.Equal(result, addresses[0].FormattedAddress);
}
[Theory]
[InlineData("Toledo", "us", "Toledo, OH, USA", null)]
[InlineData("Toledo", "es", "Toledo, Spain", "Toledo, Toledo, Spain")]
public async Task ApplyRegionBias(string address, string regionBias, string result1, string result2)
{
geocoder.RegionBias = regionBias;
GoogleAddress[] addresses = (await geocoder.GeocodeAsync(address)).ToArray();
Assert.True(result1 == addresses[0].FormattedAddress || result2 == addresses[0].FormattedAddress);
}
[Theory]
[InlineData("Winnetka", 46, -90, 47, -91, "Winnetka, IL, USA")]
[InlineData("Winnetka", 34.172684, -118.604794, 34.236144, -118.500938, "Winnetka, Los Angeles, CA, USA")]
public async Task ApplyBoundsBias(string address, double biasLatitude1, double biasLongitude1, double biasLatitude2, double biasLongitude2, string result)
{
geocoder.BoundsBias = new Bounds(biasLatitude1, biasLongitude1, biasLatitude2, biasLongitude2);
GoogleAddress[] addresses = (await geocoder.GeocodeAsync(address)).ToArray();
Assert.Equal(result, addresses[0].FormattedAddress);
}
[Theory]
[InlineData("Wimbledon")]
[InlineData("Birmingham")]
[InlineData("Manchester")]
[InlineData("York")]
public async Task CanApplyGBCountryComponentFilters(string address)
{
geocoder.ComponentFilters = new List<GoogleComponentFilter>();
geocoder.ComponentFilters.Add(new GoogleComponentFilter(GoogleComponentFilterType.Country, "GB"));
GoogleAddress[] addresses = (await geocoder.GeocodeAsync(address)).ToArray();
Assert.False(addresses.Any(x => x.Components.Any(o => o.ShortName == "US")));
Assert.True(addresses.Any(x => x.Components.Any(o => o.ShortName == "GB")));
}
[Theory]
[InlineData("Wimbledon")]
[InlineData("Birmingham")]
[InlineData("Manchester")]
[InlineData("York")]
public async Task CanApplyUSCountryComponentFilters(string address)
{
geocoder.ComponentFilters = new List<GoogleComponentFilter>();
geocoder.ComponentFilters.Add(new GoogleComponentFilter(GoogleComponentFilterType.Country, "US"));
GoogleAddress[] addresses = (await geocoder.GeocodeAsync(address)).ToArray();
Assert.True(addresses.Any(x => x.Components.Any(o => o.ShortName == "US")));
Assert.False(addresses.Any(x => x.Components.Any(o => o.ShortName == "GB")));
}
[Theory]
[InlineData("Washington")]
[InlineData("Franklin")]
public async Task CanApplyAdministrativeAreaComponentFilters(string address)
{
geocoder.ComponentFilters = new List<GoogleComponentFilter>();
geocoder.ComponentFilters.Add(new GoogleComponentFilter(GoogleComponentFilterType.AdministrativeArea, "KS"));
GoogleAddress[] addresses = (await geocoder.GeocodeAsync(address)).ToArray();
// Assert we only got addresses in Kansas
Assert.True(addresses.Any(x => x.Components.Any(o => o.ShortName == "KS")));
Assert.False(addresses.Any(x => x.Components.Any(o => o.ShortName == "MA")));
Assert.False(addresses.Any(x => x.Components.Any(o => o.ShortName == "LA")));
Assert.False(addresses.Any(x => x.Components.Any(o => o.ShortName == "NJ")));
}
[Theory]
[InlineData("Rothwell")]
public async Task CanApplyPostalCodeComponentFilters(string address)
{
geocoder.ComponentFilters = new List<GoogleComponentFilter>();
geocoder.ComponentFilters.Add(new GoogleComponentFilter(GoogleComponentFilterType.PostalCode, "NN14"));
GoogleAddress[] addresses = (await geocoder.GeocodeAsync(address)).ToArray();
// Assert we only got Rothwell, Northamptonshire
Assert.True(addresses.Any(x => x.Components.Any(o => o.ShortName == "Northamptonshire")));
Assert.False(addresses.Any(x => x.Components.Any(o => o.ShortName == "West Yorkshire")));
Assert.False(addresses.Any(x => x.Components.Any(o => o.ShortName == "Moreton Bay")));
}
}
}