forked from googlemaps/google-maps-services-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_geocoding.py
283 lines (231 loc) · 12.2 KB
/
test_geocoding.py
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
# This Python file uses the following encoding: utf-8
#
# Copyright 2014 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.
#
"""Tests for the geocoding module."""
import datetime
import responses
import googlemaps
import googlemaps.test as _test
class GeocodingTest(_test.TestCase):
def setUp(self):
self.key = 'AIzaasdf'
self.client = googlemaps.Client(self.key)
@responses.activate
def test_simple_geocode(self):
responses.add(responses.GET,
'https://maps.googleapis.com/maps/api/geocode/json',
body='{"status":"OK","results":[]}',
status=200,
content_type='application/json')
results = self.client.geocode('Sydney')
self.assertEqual(1, len(responses.calls))
self.assertURLEqual('https://maps.googleapis.com/maps/api/geocode/json?'
'key=%s&address=Sydney' % self.key,
responses.calls[0].request.url)
@responses.activate
def test_reverse_geocode(self):
responses.add(responses.GET,
'https://maps.googleapis.com/maps/api/geocode/json',
body='{"status":"OK","results":[]}',
status=200,
content_type='application/json')
results = self.client.reverse_geocode((-33.8674869, 151.2069902))
self.assertEqual(1, len(responses.calls))
self.assertURLEqual('https://maps.googleapis.com/maps/api/geocode/json?'
'latlng=-33.867487%%2C151.20699&key=%s' % self.key,
responses.calls[0].request.url)
@responses.activate
def test_geocoding_the_googleplex(self):
responses.add(responses.GET,
'https://maps.googleapis.com/maps/api/geocode/json',
body='{"status":"OK","results":[]}',
status=200,
content_type='application/json')
results = self.client.geocode('1600 Amphitheatre Parkway, '
'Mountain View, CA')
self.assertEqual(1, len(responses.calls))
self.assertURLEqual('https://maps.googleapis.com/maps/api/geocode/json?'
'key=%s&address=1600+Amphitheatre+Parkway%%2C+Mountain'
'+View%%2C+CA' % self.key,
responses.calls[0].request.url)
@responses.activate
def test_geocode_with_bounds(self):
responses.add(responses.GET,
'https://maps.googleapis.com/maps/api/geocode/json',
body='{"status":"OK","results":[]}',
status=200,
content_type='application/json')
results = self.client.geocode('Winnetka',
bounds={'southwest': (34.172684, -118.604794),
'northeast':(34.236144, -118.500938)})
self.assertEqual(1, len(responses.calls))
self.assertURLEqual('https://maps.googleapis.com/maps/api/geocode/json?'
'bounds=34.172684%%2C-118.604794%%7C34.236144%%2C'
'-118.500938&key=%s&address=Winnetka' % self.key,
responses.calls[0].request.url)
@responses.activate
def test_geocode_with_region_biasing(self):
responses.add(responses.GET,
'https://maps.googleapis.com/maps/api/geocode/json',
body='{"status":"OK","results":[]}',
status=200,
content_type='application/json')
results = self.client.geocode('Toledo', region='es')
self.assertEqual(1, len(responses.calls))
self.assertURLEqual('https://maps.googleapis.com/maps/api/geocode/json?'
'region=es&key=%s&address=Toledo' % self.key,
responses.calls[0].request.url)
@responses.activate
def test_geocode_with_component_filter(self):
responses.add(responses.GET,
'https://maps.googleapis.com/maps/api/geocode/json',
body='{"status":"OK","results":[]}',
status=200,
content_type='application/json')
results = self.client.geocode('santa cruz',
components={'country': 'ES'})
self.assertEqual(1, len(responses.calls))
self.assertURLEqual('https://maps.googleapis.com/maps/api/geocode/json?'
'key=%s&components=country%%3AES&address=santa+cruz' %
self.key,
responses.calls[0].request.url)
@responses.activate
def test_geocode_with_multiple_component_filters(self):
responses.add(responses.GET,
'https://maps.googleapis.com/maps/api/geocode/json',
body='{"status":"OK","results":[]}',
status=200,
content_type='application/json')
results = self.client.geocode('Torun',
components={'administrative_area': 'TX','country': 'US'})
self.assertEqual(1, len(responses.calls))
self.assertURLEqual('https://maps.googleapis.com/maps/api/geocode/json?'
'key=%s&components=administrative_area%%3ATX%%7C'
'country%%3AUS&address=Torun' % self.key,
responses.calls[0].request.url)
@responses.activate
def test_geocode_with_just_components(self):
responses.add(responses.GET,
'https://maps.googleapis.com/maps/api/geocode/json',
body='{"status":"OK","results":[]}',
status=200,
content_type='application/json')
results = self.client.geocode(
components={'route': 'Annegatan',
'administrative_area': 'Helsinki',
'country': 'Finland'})
self.assertEqual(1, len(responses.calls))
self.assertURLEqual('https://maps.googleapis.com/maps/api/geocode/json?'
'key=%s&components=administrative_area%%3AHelsinki'
'%%7Ccountry%%3AFinland%%7Croute%%3AAnnegatan' % self.key,
responses.calls[0].request.url)
@responses.activate
def test_simple_reverse_geocode(self):
responses.add(responses.GET,
'https://maps.googleapis.com/maps/api/geocode/json',
body='{"status":"OK","results":[]}',
status=200,
content_type='application/json')
results = self.client.reverse_geocode((40.714224, -73.961452))
self.assertEqual(1, len(responses.calls))
self.assertURLEqual('https://maps.googleapis.com/maps/api/geocode/json?'
'latlng=40.714224%%2C-73.961452&key=%s' % self.key,
responses.calls[0].request.url)
@responses.activate
def test_reverse_geocode_restricted_by_type(self):
responses.add(responses.GET,
'https://maps.googleapis.com/maps/api/geocode/json',
body='{"status":"OK","results":[]}',
status=200,
content_type='application/json')
results = self.client.reverse_geocode((40.714224, -73.961452),
location_type='ROOFTOP',
result_type='street_address')
self.assertEqual(1, len(responses.calls))
self.assertURLEqual('https://maps.googleapis.com/maps/api/geocode/json?'
'latlng=40.714224%%2C-73.961452&result_type=street_address&'
'key=%s&location_type=ROOFTOP' % self.key,
responses.calls[0].request.url)
@responses.activate
def test_reverse_geocode_multiple_location_types(self):
responses.add(responses.GET,
'https://maps.googleapis.com/maps/api/geocode/json',
body='{"status":"OK","results":[]}',
status=200,
content_type='application/json')
results = self.client.reverse_geocode((40.714224, -73.961452),
location_type=['ROOFTOP',
'RANGE_INTERPOLATED'],
result_type='street_address')
self.assertEqual(1, len(responses.calls))
self.assertURLEqual('https://maps.googleapis.com/maps/api/geocode/json?'
'latlng=40.714224%%2C-73.961452&result_type=street_address&'
'key=%s&location_type=ROOFTOP%%7CRANGE_INTERPOLATED' %
self.key,
responses.calls[0].request.url)
@responses.activate
def test_reverse_geocode_multiple_result_types(self):
responses.add(responses.GET,
'https://maps.googleapis.com/maps/api/geocode/json',
body='{"status":"OK","results":[]}',
status=200,
content_type='application/json')
results = self.client.reverse_geocode((40.714224, -73.961452),
location_type='ROOFTOP',
result_type=['street_address',
'route'])
self.assertEqual(1, len(responses.calls))
self.assertURLEqual('https://maps.googleapis.com/maps/api/geocode/json?'
'latlng=40.714224%%2C-73.961452&result_type=street_address'
'%%7Croute&key=%s&location_type=ROOFTOP' % self.key,
responses.calls[0].request.url)
@responses.activate
def test_partial_match(self):
responses.add(responses.GET,
'https://maps.googleapis.com/maps/api/geocode/json',
body='{"status":"OK","results":[]}',
status=200,
content_type='application/json')
results = self.client.geocode('Pirrama Pyrmont')
self.assertEqual(1, len(responses.calls))
self.assertURLEqual('https://maps.googleapis.com/maps/api/geocode/json?'
'key=%s&address=Pirrama+Pyrmont' % self.key,
responses.calls[0].request.url)
@responses.activate
def test_utf_results(self):
responses.add(responses.GET,
'https://maps.googleapis.com/maps/api/geocode/json',
body='{"status":"OK","results":[]}',
status=200,
content_type='application/json')
results = self.client.geocode(components={'postal_code': '96766'})
self.assertEqual(1, len(responses.calls))
self.assertURLEqual('https://maps.googleapis.com/maps/api/geocode/json?'
'key=%s&components=postal_code%%3A96766' % self.key,
responses.calls[0].request.url)
@responses.activate
def test_utf8_request(self):
responses.add(responses.GET,
'https://maps.googleapis.com/maps/api/geocode/json',
body='{"status":"OK","results":[]}',
status=200,
content_type='application/json')
self.client.geocode(self.u('\\u4e2d\\u56fd')) # China
self.assertURLEqual(
'https://maps.googleapis.com/maps/api/geocode/json?'
'key=%s&address=%s' % (self.key, '%E4%B8%AD%E5%9B%BD'),
responses.calls[0].request.url)