-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeocode_client_test.py
138 lines (103 loc) · 5.35 KB
/
geocode_client_test.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
"""Unit tests for GeocodeClient based classes.
Author: Dan Haggerty
Date: Feb. 2, 2018
"""
import httplib
import mock
import unittest
import urllib
import geocode_client
import response_mocks
class TestGoogleGeocodeClient(unittest.TestCase):
"""Unit tests for GoogleGeocodeClient."""
def setUp(self):
"""Sets up API key and client, mocks urlopen."""
self.api_key = 'test_key'
self.patcher = mock.patch.object(urllib, 'urlopen')
self.mock_urlopen = self.patcher.start()
self.client = geocode_client.GoogleGeocodeClient(api_key=self.api_key)
def test_get_lat_lng_succeeds(self):
"""Getting lat/long returns first search result from Google."""
expected_lat_lng = (42.33, -122.45)
other_lat_lng = (22.55, 55.77)
self.mock_urlopen.return_value = response_mocks.MockGoogleResponse(
code=200, search_results=[expected_lat_lng, other_lat_lng])
results = self.client.get_lat_lng_from_address('660 King St')
self.assertEqual(expected_lat_lng, results)
def test_get_lat_lng_makes_proper_call_to_google_service(self):
"""Getting lat/long forms a proper request to Here API."""
self.mock_urlopen.return_value = response_mocks.MockGoogleResponse(
code=200, search_results=[(42.33, -122.45)])
address = '660 King St'
self.client.get_lat_lng_from_address(address)
expected_params = urllib.urlencode({
'address': address,
'api_key': self.api_key,
})
expected_url = ('https://maps.googleapis.com/maps/api/geocode/json'
'?{}').format(expected_params)
actual_url = self.mock_urlopen.call_args[0][0]
self.assertEqual(expected_url, actual_url)
def test_get_lat_lng_raises_upon_non_success_response(self):
"""Raises a GeocodeServiceSearchError if there's a failure."""
error_response_code = httplib.BAD_REQUEST
self.mock_urlopen.return_value = response_mocks.MockGoogleResponse(
code=error_response_code)
with self.assertRaises(geocode_client.GeocodeServiceSearchError) as e:
self.client.get_lat_lng_from_address('600 King St')
self.assertIn(str(error_response_code), e.exception.message)
def test_get_lat_lng_raises_upon_when_result_set_is_empty(self):
"""Raises a GeocodeServiceSearchError if no results are found."""
self.mock_urlopen.return_value = response_mocks.MockGoogleResponse(
code=200, search_results=[])
with self.assertRaises(geocode_client.GeocodeServiceSearchError) as e:
self.client.get_lat_lng_from_address('600 King St')
self.assertIn('found no results', e.exception.message)
class TestHereGeocodeClient(unittest.TestCase):
"""Unit tests for HereGeocodeClient."""
def setUp(self):
"""Sets up API key and client, mocks urlopen."""
self.app_id = 'test_id'
self.app_code = 'test_code'
self.patcher = mock.patch.object(urllib, 'urlopen')
self.mock_urlopen = self.patcher.start()
self.client = geocode_client.HereGeocodeClient(app_id=self.app_id,
app_code=self.app_code)
def test_get_lat_lng_succeeds(self):
"""Getting lat/long returns first search result from Google."""
expected_lat_lng = (42.33, -122.45)
other_lat_lng = (22.55, 55.77)
self.mock_urlopen.return_value = response_mocks.MockHereResponse(
code=200, search_results=[expected_lat_lng, other_lat_lng])
results = self.client.get_lat_lng_from_address('660 King St')
self.assertEqual(expected_lat_lng, results)
def test_get_lat_lng_makes_proper_call_to_here_service(self):
"""Getting lat/long forms a proper request to Here API."""
self.mock_urlopen.return_value = response_mocks.MockHereResponse(
code=200, search_results=[(42.33, -122.45)])
address = '660 King St'
self.client.get_lat_lng_from_address(address)
expected_params = urllib.urlencode({
'searchtext': address,
'app_id': self.app_id,
'app_code': self.app_code,
})
expected_url = ('https://geocoder.cit.api.here.com/6.2/geocode.json'
'?{}').format(expected_params)
actual_url = self.mock_urlopen.call_args[0][0]
self.assertEqual(expected_url, actual_url)
def test_get_lat_lng_raises_upon_non_success_response(self):
"""Raises a GeocodeServiceSearchError if there's a failure."""
error_response_code = httplib.BAD_REQUEST
self.mock_urlopen.return_value = response_mocks.MockHereResponse(
code=error_response_code)
with self.assertRaises(geocode_client.GeocodeServiceSearchError) as e:
self.client.get_lat_lng_from_address('600 King St')
self.assertIn(str(error_response_code), e.exception.message)
def test_get_lat_lng_raises_upon_when_result_set_is_empty(self):
"""Raises a GeocodeServiceSearchError if no results are found."""
self.mock_urlopen.return_value = response_mocks.MockHereResponse(
code=200, search_results=[])
with self.assertRaises(geocode_client.GeocodeServiceSearchError) as e:
self.client.get_lat_lng_from_address('600 King St')
self.assertIn('found no results', e.exception.message)