forked from googlemaps/google-maps-services-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_roads.py
137 lines (110 loc) · 5.03 KB
/
test_roads.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
#
# Copyright 2015 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 roads module."""
import responses
import googlemaps
import googlemaps.test as _test
class RoadsTest(_test.TestCase):
def setUp(self):
self.key = "AIzaasdf"
self.client = googlemaps.Client(self.key)
@responses.activate
def test_snap(self):
responses.add(responses.GET,
"https://roads.googleapis.com/v1/snapToRoads",
body='{"snappedPoints":["foo"]}',
status=200,
content_type="application/json")
results = self.client.snap_to_roads((40.714728, -73.998672))
self.assertEqual("foo", results[0])
self.assertEqual(1, len(responses.calls))
self.assertURLEqual("https://roads.googleapis.com/v1/snapToRoads?"
"path=40.714728%%2C-73.998672&key=%s" % self.key,
responses.calls[0].request.url)
@responses.activate
def test_nearest_roads(self):
responses.add(responses.GET,
"https://roads.googleapis.com/v1/nearestRoads",
body='{"snappedPoints":["foo"]}',
status=200,
content_type="application/json")
results = self.client.nearest_roads((40.714728, -73.998672))
self.assertEqual("foo", results[0])
self.assertEqual(1, len(responses.calls))
self.assertURLEqual("https://roads.googleapis.com/v1/nearestRoads?"
"points=40.714728%%2C-73.998672&key=%s" % self.key,
responses.calls[0].request.url)
@responses.activate
def test_path(self):
responses.add(responses.GET,
"https://roads.googleapis.com/v1/speedLimits",
body='{"speedLimits":["foo"]}',
status=200,
content_type="application/json")
results = self.client.snapped_speed_limits([(1, 2),(3, 4)])
self.assertEqual("foo", results["speedLimits"][0])
self.assertEqual(1, len(responses.calls))
self.assertURLEqual("https://roads.googleapis.com/v1/speedLimits?"
"path=1%%2C2|3%%2C4"
"&key=%s" % self.key,
responses.calls[0].request.url)
@responses.activate
def test_speedlimits(self):
responses.add(responses.GET,
"https://roads.googleapis.com/v1/speedLimits",
body='{"speedLimits":["foo"]}',
status=200,
content_type="application/json")
results = self.client.speed_limits("id1")
self.assertEqual("foo", results[0])
self.assertEqual("https://roads.googleapis.com/v1/speedLimits?"
"placeId=id1&key=%s" % self.key,
responses.calls[0].request.url)
@responses.activate
def test_speedlimits_multiple(self):
responses.add(responses.GET,
"https://roads.googleapis.com/v1/speedLimits",
body='{"speedLimits":["foo"]}',
status=200,
content_type="application/json")
results = self.client.speed_limits(["id1", "id2", "id3"])
self.assertEqual("foo", results[0])
self.assertEqual("https://roads.googleapis.com/v1/speedLimits?"
"placeId=id1&placeId=id2&placeId=id3"
"&key=%s" % self.key,
responses.calls[0].request.url)
def test_clientid_not_accepted(self):
client = googlemaps.Client(client_id="asdf", client_secret="asdf")
with self.assertRaises(ValueError):
client.speed_limits("foo")
@responses.activate
def test_retry(self):
class request_callback:
def __init__(self):
self.first_req = True
def __call__(self, req):
if self.first_req:
self.first_req = False
return (500, {}, 'Internal Server Error.')
return (200, {}, '{"speedLimits":[]}')
responses.add_callback(responses.GET,
"https://roads.googleapis.com/v1/speedLimits",
content_type="application/json",
callback=request_callback())
self.client.speed_limits([])
self.assertEqual(2, len(responses.calls))
self.assertEqual(responses.calls[0].request.url, responses.calls[1].request.url)