-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.py
107 lines (80 loc) · 3.89 KB
/
main_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
"""Tests for the geocoding proxy service Flask server.
Author: Dan Haggerty
Date: Feb. 2, 2018
"""
import geocode_client
import httplib
import json
import main
import mock
import response_mocks
import unittest
import urllib
TEST_API_KEY = 'test_api_key'
TEST_APP_ID = 'test_app_id'
TEST_APP_CODE = 'test_app_code'
CREDENTIALS_OPTIONS = [
'--google-api-key', TEST_API_KEY,
'--here-credentials', TEST_APP_ID, TEST_APP_CODE
]
MOCK_LAT_LNG = (23.01, 144.88)
class TestGeocodingProxyService(unittest.TestCase):
"""Test class for the server as a whole."""
def setUp(self):
"""Creates a test client, initializes the server, and mocks urlopen."""
self.app = main.app.test_client()
self.patcher = mock.patch.object(urllib, 'urlopen')
self.mock_urlopen = self.patcher.start()
main._initialize(TEST_API_KEY, (TEST_APP_ID, TEST_APP_CODE),
main.GOOGLE_GEOCODE_SERVICE)
def test_service_initializes_defaults_properly(self):
"""It should use Google as its primary service."""
first_client = main.clients[0]
self.assertEqual(2, len(main.clients))
self.assertIsInstance(first_client, geocode_client.GoogleGeocodeClient)
self.assertEqual({'api_key': TEST_API_KEY}, first_client.credentials)
def test_service_uses_preferred_service_first(self):
"""It should use Here first if it's set as the preferred service."""
main._initialize(TEST_API_KEY, (TEST_APP_ID, TEST_APP_CODE),
main.HERE_GEOCODE_SERVICE)
first_client = main.clients[0]
self.assertEqual(2, len(main.clients))
self.assertIsInstance(first_client, geocode_client.HereGeocodeClient)
self.assertEqual({'app_id': TEST_APP_ID, 'app_code': TEST_APP_CODE},
first_client.credentials)
def test_address_lookup_api_succeeds_with_valid_address(self):
"""It should return a valid lat long in JSON for a valid address."""
self.mock_urlopen.return_value = response_mocks.MockGoogleResponse(
code=httplib.OK, search_results=[MOCK_LAT_LNG])
response = self.app.get('/api/address-lookup?address=660+king+st')
response_data = json.loads(response.data)
expected_data = {
'lat': MOCK_LAT_LNG[0],
'lng': MOCK_LAT_LNG[1],
'service': 'Google Maps Geocoding API',
}
self.assertEqual(httplib.OK, response.status_code)
self.assertEqual(expected_data, response_data)
self.assertEqual(1, self.mock_urlopen.call_count)
def test_address_lookup_calls_other_services_if_first_service_fails(self):
"""It should make another GET request if the first call fails."""
self.mock_urlopen.return_value = response_mocks.MockGoogleResponse(
code=httplib.NOT_FOUND)
self.app.get('/api/address-lookup?address=660+king+st')
self.assertEqual(2, self.mock_urlopen.call_count)
def test_address_lookup_returns_404_if_all_services_fail(self):
"""It should return a 404 if it can't find any results."""
self.mock_urlopen.return_value = response_mocks.MockGoogleResponse(
code=httplib.NOT_FOUND)
response = self.app.get('/api/address-lookup?address=660+king+st')
expected_data = {'error': 'Search term yielded no results'}
actual_data = json.loads(response.data)
self.assertEqual(httplib.NOT_FOUND, response.status_code)
self.assertEqual(expected_data, actual_data)
def test_address_lookup_returns_400_with_invalid_params(self):
"""It should return 400 without a valid address to look up."""
response = self.app.get('/api/address-lookup?invalid=660+king+st')
expected_data = {'error': 'Bad request'}
actual_data = json.loads(response.data)
self.assertEqual(httplib.BAD_REQUEST, response.status_code)
self.assertEqual(expected_data, actual_data)