Skip to content

Commit 53603db

Browse files
authored
Fix bug with null check in get_stats and get_property_values api (#127)
1 parent cc00c9c commit 53603db

File tree

5 files changed

+44
-9
lines changed

5 files changed

+44
-9
lines changed

datacommons/core.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,12 @@ def get_property_values(dcids,
178178
for dcid in dcids:
179179
# Get the list of nodes based on the direction given.
180180
nodes = []
181-
if dcid in payload and out:
182-
nodes = payload[dcid]['out']
183-
elif dcid in payload and not out:
184-
nodes = payload[dcid]['in']
181+
if out:
182+
if dcid in payload and 'out' in payload[dcid]:
183+
nodes = payload[dcid]['out']
184+
else:
185+
if dcid in payload and 'in' in payload[dcid]:
186+
nodes = payload[dcid]['in']
185187

186188
# Add nodes to unique_results if it is not empty
187189
for node in nodes:

datacommons/examples/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
def main():
2626
# Set the dcid to be that of Santa Clara County.
27-
dcids = ['geoId/06085']
27+
dcids = ['geoId/06085', 'dc/p/zsb968m3v1f97']
2828

2929
# Print all incoming and outgoing properties from Santa Clara County.
3030
print('Property Labels for Santa Clara County')

datacommons/places.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def get_places_in(dcids, place_type):
7171
# Create the results and format it appropriately
7272
result = utils._format_expand_payload(payload, 'place', must_exist=dcids)
7373
return result
74-
74+
7575
def get_stats(dcids, stats_var, obs_dates='latest'):
7676
""" Returns :obj:`TimeSeries` for :code:`dcids` \
7777
based on the :code:`stats_var`.
@@ -119,7 +119,7 @@ def get_stats(dcids, stats_var, obs_dates='latest'):
119119
},
120120
'geoId/05': {
121121
'place_name': 'California'
122-
'data': {
122+
'data': {
123123
'2011':316667,
124124
'2012':324116,
125125
'2013':331853,
@@ -146,6 +146,8 @@ def get_stats(dcids, stats_var, obs_dates='latest'):
146146
res.update(payload)
147147
elif obs_dates == 'latest':
148148
for geo, stats in payload.items():
149+
if not stats:
150+
continue
149151
time_series = stats.get('data')
150152
if not time_series: continue
151153
max_date = max(time_series)
@@ -156,6 +158,8 @@ def get_stats(dcids, stats_var, obs_dates='latest'):
156158
elif obs_dates:
157159
obs_dates = set(obs_dates)
158160
for geo, stats in payload.items():
161+
if not stats:
162+
continue
159163
time_series = stats.get('data')
160164
if not time_series: continue
161165
for date in list(time_series):

datacommons/test/core_test.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,12 @@ def read(self):
155155
}
156156
})
157157
return MockResponse(json.dumps({'payload': res_json}))
158+
if data['dcids'] == ['dc/p/1234'] and data['property'] == 'name':
159+
# Response for sending a request for the name with no data
160+
res_json = json.dumps({
161+
'dc/p/1234': {}
162+
})
163+
return MockResponse(json.dumps({'payload': res_json}))
158164
if data['dcids'] == ['geoId/06085', 'geoId/24031']\
159165
and data['property'] == 'madProperty':
160166
# Response for sending a request with a property that does not exist.
@@ -427,6 +433,12 @@ def test_multiple_dcids(self, urlopen_mock):
427433
'geoId/24031': ['Montgomery County']
428434
})
429435

436+
# Return empty result when there is no data.
437+
names = dc.get_property_values(['dc/p/1234'], 'name')
438+
self.assertDictEqual(names, {
439+
'dc/p/1234': []
440+
})
441+
430442
@mock.patch('urllib.request.urlopen', side_effect=request_mock)
431443
def test_bad_dcids(self, urlopen_mock):
432444
""" Calling get_property_values with dcids that do not exist returns empty

datacommons/test/places_test.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import datacommons.utils as utils
2727
import json
2828
import unittest
29-
import six.moves.urllib
29+
import six.moves.urllib as urllib
3030

3131

3232
def request_mock(*args, **kwargs):
@@ -122,7 +122,14 @@ def read(self):
122122
}
123123
})
124124
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
126133
data['place'] == ['geoId/05']) and
127134
data['stats_var'] == 'dc/0hyp6tkn18vcb'):
128135
# Response ignores dcid that does not exist.
@@ -355,6 +362,16 @@ def test_no_dcids(self, urlopen):
355362
no_dcids = dc.get_stats([], 'dc/0hyp6tkn18vcb')
356363
self.assertDictEqual({}, no_dcids)
357364

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+
358375
@mock.patch('six.moves.urllib.request.urlopen', side_effect=request_mock)
359376
def test_batch_request(self, mock_urlopen):
360377
""" Make multiple calls to REST API when number of geos exceeds the batch size. """

0 commit comments

Comments
 (0)