|
26 | 26 | import datacommons.utils as utils |
27 | 27 | import json |
28 | 28 | import unittest |
29 | | -import six.moves.urllib |
| 29 | +import six.moves.urllib as urllib |
30 | 30 |
|
31 | 31 |
|
32 | 32 | def request_mock(*args, **kwargs): |
@@ -122,7 +122,14 @@ def read(self): |
122 | 122 | } |
123 | 123 | }) |
124 | 124 | return MockResponse(json.dumps({'payload': res_json})) |
125 | | - if ((data['place'] == ['geoId/05', 'dc/MadDcid'] or |
| 125 | + if (data['place'] == ['geoId/00'] and |
| 126 | + data['stats_var'] == 'dc/0hyp6tkn18vcb'): |
| 127 | + # No data for the request |
| 128 | + res_json = json.dumps({ |
| 129 | + 'geoId/00': None |
| 130 | + }) |
| 131 | + return MockResponse(json.dumps({'payload': res_json})) |
| 132 | + if ((data['place'] == ['geoId/05', 'dc/MadDcid'] or |
126 | 133 | data['place'] == ['geoId/05']) and |
127 | 134 | data['stats_var'] == 'dc/0hyp6tkn18vcb'): |
128 | 135 | # Response ignores dcid that does not exist. |
@@ -355,6 +362,16 @@ def test_no_dcids(self, urlopen): |
355 | 362 | no_dcids = dc.get_stats([], 'dc/0hyp6tkn18vcb') |
356 | 363 | self.assertDictEqual({}, no_dcids) |
357 | 364 |
|
| 365 | + @mock.patch('urllib.request.urlopen', side_effect=request_mock) |
| 366 | + def test_no_data(self, urlopen): |
| 367 | + """ Calling get_stats with for None data. """ |
| 368 | + # Set the API key |
| 369 | + dc.set_api_key('TEST-API-KEY') |
| 370 | + |
| 371 | + # Call get_stats with no dcids. |
| 372 | + result = dc.get_stats(['geoId/00'], 'dc/0hyp6tkn18vcb') |
| 373 | + self.assertDictEqual({}, result) |
| 374 | + |
358 | 375 | @mock.patch('six.moves.urllib.request.urlopen', side_effect=request_mock) |
359 | 376 | def test_batch_request(self, mock_urlopen): |
360 | 377 | """ Make multiple calls to REST API when number of geos exceeds the batch size. """ |
|
0 commit comments