|
21 | 21 | from __future__ import division |
22 | 22 | from __future__ import print_function |
23 | 23 |
|
| 24 | +import base64 |
24 | 25 | from pandas.util.testing import assert_series_equal |
25 | 26 | from unittest import mock |
26 | 27 |
|
|
30 | 31 |
|
31 | 32 | import json |
32 | 33 | import unittest |
| 34 | +import zlib |
33 | 35 |
|
34 | 36 |
|
35 | 37 | def post_request_mock(*args, **kwargs): |
@@ -134,6 +136,64 @@ def json(self): |
134 | 136 | return MockResponse({}, 404) |
135 | 137 |
|
136 | 138 |
|
| 139 | +def get_request_mock(*args, **kwargs): |
| 140 | + """ A mock GET requests sent in the requests package. """ |
| 141 | + # Create the mock response object. |
| 142 | + class MockResponse: |
| 143 | + def __init__(self, json_data, status_code): |
| 144 | + self.json_data = json_data |
| 145 | + self.status_code = status_code |
| 146 | + |
| 147 | + def json(self): |
| 148 | + return self.json_data |
| 149 | + |
| 150 | + headers = kwargs['headers'] |
| 151 | + |
| 152 | + # If the API key does not match, then return 403 Forbidden |
| 153 | + if 'x-api-key' not in headers or headers['x-api-key'] != 'TEST-API-KEY': |
| 154 | + return MockResponse({}, 403) |
| 155 | + |
| 156 | + # Mock responses for get requests to get_pop_obs. |
| 157 | + if args[0] == utils._API_ROOT + utils._API_ENDPOINTS['get_pop_obs'] + '?dcid=geoId/06085': |
| 158 | + # Response returned when querying for a city in the graph. |
| 159 | + res_json = json.dumps({ |
| 160 | + 'name': 'Mountain View', |
| 161 | + 'placeType': 'City', |
| 162 | + 'populations': { |
| 163 | + 'dc/p/013ldrstf6lnf': { |
| 164 | + 'numConstraints': 6, |
| 165 | + 'observations': [ |
| 166 | + { |
| 167 | + 'marginOfError': 119, |
| 168 | + 'measuredProp': 'count', |
| 169 | + 'measuredValue': 225, |
| 170 | + 'measurementMethod': 'CensusACS5yrSurvey', |
| 171 | + 'observationDate': '2014' |
| 172 | + }, { |
| 173 | + 'marginOfError': 108, |
| 174 | + 'measuredProp': 'count', |
| 175 | + 'measuredValue': 180, |
| 176 | + 'measurementMethod': 'CensusACS5yrSurvey', |
| 177 | + 'observationDate': '2012' |
| 178 | + } |
| 179 | + ], |
| 180 | + 'popType': 'Person', |
| 181 | + 'propertyValues': { |
| 182 | + 'age': 'Years16Onwards', |
| 183 | + 'gender': 'Male', |
| 184 | + 'income': 'USDollar30000To34999', |
| 185 | + 'incomeStatus': 'WithIncome', |
| 186 | + 'race': 'USC_HispanicOrLatinoRace', |
| 187 | + 'workExperience': 'USC_NotWorkedFullTime' |
| 188 | + } |
| 189 | + } |
| 190 | + } |
| 191 | + }) |
| 192 | + return MockResponse({'payload': base64.b64encode(zlib.compress(res_json.encode('utf-8')))}, 200) |
| 193 | + |
| 194 | + # Otherwise, return an empty response and a 404. |
| 195 | + return MockResponse({}, 404) |
| 196 | + |
137 | 197 | class TestGetPopulations(unittest.TestCase): |
138 | 198 | """ Unit tests for get_populations. """ |
139 | 199 |
|
@@ -355,6 +415,51 @@ def test_series_no_dcids(self, post_mock): |
355 | 415 | measurement_method='BLSSeasonallyAdjusted') |
356 | 416 | assert_series_equal(actual, expected) |
357 | 417 |
|
| 418 | +class TestGetPopObs(unittest.TestCase): |
| 419 | + """ Unit stests for get_pop_Obs. """ |
| 420 | + |
| 421 | + @mock.patch('requests.get', side_effect=get_request_mock) |
| 422 | + def test_valid_dcid(self, get_mock): |
| 423 | + """ Calling get_pop_obs with valid dcid returns valid results. """ |
| 424 | + # Set the API key |
| 425 | + dc.set_api_key('TEST-API-KEY') |
| 426 | + |
| 427 | + # Call get_places_in |
| 428 | + popobs = dc.get_pop_obs('geoId/06085') |
| 429 | + self.assertDictEqual(popobs, { |
| 430 | + 'name': 'Mountain View', |
| 431 | + 'placeType': 'City', |
| 432 | + 'populations': { |
| 433 | + 'dc/p/013ldrstf6lnf': { |
| 434 | + 'numConstraints': 6, |
| 435 | + 'observations': [ |
| 436 | + { |
| 437 | + 'marginOfError': 119, |
| 438 | + 'measuredProp': 'count', |
| 439 | + 'measuredValue': 225, |
| 440 | + 'measurementMethod': 'CensusACS5yrSurvey', |
| 441 | + 'observationDate': '2014' |
| 442 | + }, { |
| 443 | + 'marginOfError': 108, |
| 444 | + 'measuredProp': 'count', |
| 445 | + 'measuredValue': 180, |
| 446 | + 'measurementMethod': 'CensusACS5yrSurvey', |
| 447 | + 'observationDate': '2012' |
| 448 | + } |
| 449 | + ], |
| 450 | + 'popType': 'Person', |
| 451 | + 'propertyValues': { |
| 452 | + 'age': 'Years16Onwards', |
| 453 | + 'gender': 'Male', |
| 454 | + 'income': 'USDollar30000To34999', |
| 455 | + 'incomeStatus': 'WithIncome', |
| 456 | + 'race': 'USC_HispanicOrLatinoRace', |
| 457 | + 'workExperience': 'USC_NotWorkedFullTime' |
| 458 | + } |
| 459 | + } |
| 460 | + } |
| 461 | + }) |
| 462 | + |
358 | 463 |
|
359 | 464 | if __name__ == '__main__': |
360 | 465 | unittest.main() |
0 commit comments