-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathtest_geocoding_advanced.py
More file actions
309 lines (275 loc) · 11.2 KB
/
Copy pathtest_geocoding_advanced.py
File metadata and controls
309 lines (275 loc) · 11.2 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
#
# Copyright 2024 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
#
"""Advanced / regression tests for the geocoding module.
These tests are specifically aimed at issue #540 ("unexpected keyword argument
``enable_address_descriptor``"). They exhaustively pin down the public
signature of :func:`googlemaps.Client.reverse_geocode` and the on-the-wire
behaviour of the ``enable_address_descriptor`` flag so the regression cannot
silently come back in a future release.
"""
import inspect
from urllib.parse import parse_qs, urlparse
import responses
import googlemaps
from googlemaps import geocoding
from . import TestCase
_GEOCODE_URL = "https://maps.googleapis.com/maps/api/geocode/json"
_DESCRIPTOR_BODY = (
'{"status":"OK","results":[],'
'"address_descriptor":{'
'"landmarks":[{"placeId":"id","display_name":{"text":"Opera House"}}],'
'"areas":[{"placeId":"area1","display_name":{"text":"Sydney"}}]'
'}}'
)
_PLAIN_BODY = '{"status":"OK","results":[]}'
def _query(call):
"""Return the parsed query string for a captured ``responses`` call."""
return parse_qs(urlparse(call.request.url).query)
class ReverseGeocodeSignatureTest(TestCase):
"""Pin the public signature so issue #540 cannot regress."""
def test_module_function_accepts_enable_address_descriptor(self):
sig = inspect.signature(geocoding.reverse_geocode)
self.assertIn("enable_address_descriptor", sig.parameters)
self.assertFalse(
sig.parameters["enable_address_descriptor"].default,
"enable_address_descriptor must default to a falsy value",
)
def test_client_method_accepts_enable_address_descriptor(self):
client = googlemaps.Client(key="AIzaasdf")
sig = inspect.signature(client.reverse_geocode)
self.assertIn(
"enable_address_descriptor",
sig.parameters,
"Client.reverse_geocode must expose enable_address_descriptor "
"(regression for issue #540).",
)
def test_client_method_call_does_not_raise_typeerror(self):
"""The exact failure mode from issue #540 must no longer occur."""
client = googlemaps.Client(key="AIzaasdf")
with responses.RequestsMock() as rsps:
rsps.add(
responses.GET,
_GEOCODE_URL,
body=_DESCRIPTOR_BODY,
status=200,
content_type="application/json",
)
try:
client.reverse_geocode(
(-33.8674869, 151.2069902),
enable_address_descriptor=True,
)
except TypeError as exc: # pragma: no cover - regression guard
self.fail(
"reverse_geocode raised TypeError for "
"enable_address_descriptor (issue #540): %s" % exc
)
class ReverseGeocodeAddressDescriptorTest(TestCase):
def setUp(self):
self.key = "AIzaasdf"
self.client = googlemaps.Client(self.key)
@responses.activate
def test_flag_true_sends_lowercase_true(self):
responses.add(
responses.GET, _GEOCODE_URL,
body=_DESCRIPTOR_BODY, status=200,
content_type="application/json",
)
self.client.reverse_geocode(
(-33.8674869, 151.2069902), enable_address_descriptor=True,
)
q = _query(responses.calls[0])
self.assertEqual(q["enable_address_descriptor"], ["true"])
self.assertEqual(q["latlng"], ["-33.8674869,151.2069902"])
@responses.activate
def test_flag_default_omits_parameter(self):
responses.add(
responses.GET, _GEOCODE_URL,
body=_PLAIN_BODY, status=200,
content_type="application/json",
)
self.client.reverse_geocode((-33.8674869, 151.2069902))
q = _query(responses.calls[0])
self.assertNotIn("enable_address_descriptor", q)
@responses.activate
def test_flag_false_omits_parameter(self):
responses.add(
responses.GET, _GEOCODE_URL,
body=_PLAIN_BODY, status=200,
content_type="application/json",
)
self.client.reverse_geocode(
(-33.8674869, 151.2069902), enable_address_descriptor=False,
)
q = _query(responses.calls[0])
self.assertNotIn("enable_address_descriptor", q)
@responses.activate
def test_falsy_values_omit_parameter(self):
for falsy in (None, 0, "", [], {}):
with self.subTest(value=falsy):
with responses.RequestsMock() as rsps:
rsps.add(
responses.GET, _GEOCODE_URL,
body=_PLAIN_BODY, status=200,
content_type="application/json",
)
self.client.reverse_geocode(
(40.0, -73.0), enable_address_descriptor=falsy,
)
q = _query(rsps.calls[0])
self.assertNotIn("enable_address_descriptor", q)
@responses.activate
def test_truthy_non_bool_values_send_true(self):
for truthy in (1, "yes", ["x"], {"a": 1}):
with self.subTest(value=truthy):
with responses.RequestsMock() as rsps:
rsps.add(
responses.GET, _GEOCODE_URL,
body=_DESCRIPTOR_BODY, status=200,
content_type="application/json",
)
self.client.reverse_geocode(
(40.0, -73.0), enable_address_descriptor=truthy,
)
q = _query(rsps.calls[0])
self.assertEqual(q["enable_address_descriptor"], ["true"])
@responses.activate
def test_combined_with_result_and_location_type(self):
responses.add(
responses.GET, _GEOCODE_URL,
body=_DESCRIPTOR_BODY, status=200,
content_type="application/json",
)
self.client.reverse_geocode(
(40.714224, -73.961452),
result_type=["street_address", "route"],
location_type="ROOFTOP",
language="en",
enable_address_descriptor=True,
)
q = _query(responses.calls[0])
self.assertEqual(q["enable_address_descriptor"], ["true"])
self.assertEqual(q["result_type"], ["street_address|route"])
self.assertEqual(q["location_type"], ["ROOFTOP"])
self.assertEqual(q["language"], ["en"])
self.assertEqual(q["latlng"], ["40.714224,-73.961452"])
@responses.activate
def test_works_with_place_id_string(self):
place_id = "ChIJN1t_tDeuEmsRUsoyG83frY4"
responses.add(
responses.GET, _GEOCODE_URL,
body=_DESCRIPTOR_BODY, status=200,
content_type="application/json",
)
self.client.reverse_geocode(place_id, enable_address_descriptor=True)
q = _query(responses.calls[0])
self.assertEqual(q["place_id"], [place_id])
self.assertNotIn("latlng", q)
self.assertEqual(q["enable_address_descriptor"], ["true"])
@responses.activate
def test_works_with_dict_latlng(self):
responses.add(
responses.GET, _GEOCODE_URL,
body=_DESCRIPTOR_BODY, status=200,
content_type="application/json",
)
self.client.reverse_geocode(
{"lat": -33.8674869, "lng": 151.2069902},
enable_address_descriptor=True,
)
q = _query(responses.calls[0])
self.assertEqual(q["latlng"], ["-33.8674869,151.2069902"])
self.assertEqual(q["enable_address_descriptor"], ["true"])
@responses.activate
def test_works_with_list_latlng(self):
responses.add(
responses.GET, _GEOCODE_URL,
body=_DESCRIPTOR_BODY, status=200,
content_type="application/json",
)
self.client.reverse_geocode(
[-33.8674869, 151.2069902], enable_address_descriptor=True,
)
q = _query(responses.calls[0])
self.assertEqual(q["latlng"], ["-33.8674869,151.2069902"])
@responses.activate
def test_works_with_string_latlng(self):
responses.add(
responses.GET, _GEOCODE_URL,
body=_DESCRIPTOR_BODY, status=200,
content_type="application/json",
)
self.client.reverse_geocode(
"-33.8674869,151.2069902", enable_address_descriptor=True,
)
q = _query(responses.calls[0])
self.assertEqual(q["latlng"], ["-33.8674869,151.2069902"])
self.assertEqual(q["enable_address_descriptor"], ["true"])
@responses.activate
def test_response_exposes_address_descriptor(self):
responses.add(
responses.GET, _GEOCODE_URL,
body=_DESCRIPTOR_BODY, status=200,
content_type="application/json",
)
response = self.client.reverse_geocode(
(-33.8674869, 151.2069902), enable_address_descriptor=True,
)
descriptor = response.get("address_descriptor")
self.assertIsNotNone(descriptor)
self.assertEqual(len(descriptor["landmarks"]), 1)
self.assertEqual(descriptor["landmarks"][0]["placeId"], "id")
self.assertEqual(len(descriptor["areas"]), 1)
@responses.activate
def test_repeated_calls_are_independent(self):
"""A previous call with the flag must not leak into the next."""
responses.add(
responses.GET, _GEOCODE_URL,
body=_DESCRIPTOR_BODY, status=200,
content_type="application/json",
)
responses.add(
responses.GET, _GEOCODE_URL,
body=_PLAIN_BODY, status=200,
content_type="application/json",
)
self.client.reverse_geocode(
(40.0, -73.0), enable_address_descriptor=True,
)
self.client.reverse_geocode((40.0, -73.0))
self.assertEqual(
_query(responses.calls[0])["enable_address_descriptor"], ["true"],
)
self.assertNotIn(
"enable_address_descriptor", _query(responses.calls[1]),
)
@responses.activate
def test_keyword_only_call_via_module_function(self):
"""Calling the underlying module function directly must also work."""
responses.add(
responses.GET, _GEOCODE_URL,
body=_DESCRIPTOR_BODY, status=200,
content_type="application/json",
)
geocoding.reverse_geocode(
self.client,
(-33.8674869, 151.2069902),
enable_address_descriptor=True,
)
q = _query(responses.calls[0])
self.assertEqual(q["enable_address_descriptor"], ["true"])
def test_unknown_kwarg_still_raises_typeerror(self):
"""Sanity check: only the documented kwarg is accepted."""
client = googlemaps.Client(self.key)
with self.assertRaises(TypeError):
client.reverse_geocode(
(-33.8674869, 151.2069902),
enable_address_descriptors=True, # note the typo / plural
)