forked from codeforboston/voiceapp311
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring crime incidents, moved former unit test into integration …
…tests Closes codeforboston#194
- Loading branch information
Showing
3 changed files
with
80 additions
and
48 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
mycity/mycity/test/integration_tests/test_crime_incidents_api_utils.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import unittest | ||
from mycity.utilities.crime_incidents_api_utils import get_crime_incident_response | ||
|
||
|
||
class CrimeIncidentsAPIUtilitiesTestCase(unittest.TestCase): | ||
|
||
def test_get_crime_incident_response_returns_success_if_query_is_successful(self): | ||
test_address = "46 Everdean St Boston, MA" | ||
result = get_crime_incident_response(test_address) | ||
self.assertEqual(True, result['success']) |
57 changes: 34 additions & 23 deletions
57
mycity/mycity/test/unit_tests/test_crime_incidents_api_utils.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,35 @@ | ||
import unittest | ||
import unittest.mock as mock | ||
import mycity.test.test_constants as test_constants | ||
import mycity.test.unit_tests.base as base | ||
from mycity.utilities.crime_incidents_api_utils import \ | ||
get_crime_incident_response | ||
|
||
class CrimeIncidentsAPIUtilitiesTestCase(base.BaseTestCase): | ||
|
||
@mock.patch( | ||
'mycity.utilities.gis_utils.geocode_address', | ||
return_value=test_constants.GEOCODE_ADDRESS_MOCK | ||
) | ||
@mock.patch('requests.get') | ||
def test_get_crime_incident_response(self, mock_geocode_address, mock_get): | ||
mock_resp = self._mock_response(status=200, | ||
json_data=test_constants.GET_CRIME_INCIDENTS_API_MOCK) | ||
mock_get.return_value = mock_resp | ||
|
||
test_address = "46 Everdean St Boston, MA" | ||
result = get_crime_incident_response(test_address) | ||
self.assertEqual( | ||
True, | ||
result['success'] | ||
) | ||
import requests | ||
import typing | ||
from mycity.utilities.crime_incidents_api_utils import get_crime_incident_response | ||
|
||
|
||
class ResponseStub: | ||
|
||
def __init__(self, status_code: int, response_data: typing.Dict = {}): | ||
self.status_code = status_code | ||
self.response_data = response_data | ||
|
||
def json(self): | ||
return self.response_data | ||
|
||
|
||
class CrimeIncidentsAPIUtilitiesTestCase(unittest.TestCase): | ||
|
||
def test_get_crime_incident_response_returns_empty_dict_if_request_fails(self): | ||
requests_stub = _build_requests_stub(requests.codes.bad) | ||
to_test = get_crime_incident_response('46 Everdean St.', requests_stub) | ||
self.assertEquals({}, to_test) | ||
|
||
def test_get_crime_incident_response_returns_json_if_request_succeeds(self): | ||
expected = {'test': 'yes, it\'s a test'} | ||
requests_stub = _build_requests_stub(requests.codes.ok, expected) | ||
to_test = get_crime_incident_response('46 Everdean St.', requests_stub) | ||
self.assertEquals(expected, to_test) | ||
|
||
|
||
def _build_requests_stub(status_code: int, data_from_service: typing.Dict = {}): | ||
requests_stub = requests | ||
requests_stub.Session.get = mock.MagicMock(return_value = ResponseStub(status_code, data_from_service)) | ||
return requests_stub |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters