-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make some Python API methods compatible with coming Mixer API changes. #116
Draft
mvashishtha
wants to merge
1
commit into
datacommonsorg:master
Choose a base branch
from
mvashishtha:dict
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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 |
---|---|---|
|
@@ -31,8 +31,10 @@ | |
import urllib | ||
import zlib | ||
|
||
|
||
def request_mock(*args, **kwargs): | ||
# new_mixer_api specifies whether to use the Mixer API | ||
# that gives dictionaries instead of lists for places-in, | ||
# population, and observation functions. | ||
def request_mock_base(*args, **kwargs): | ||
""" A mock urlopen request sent in the requests package. """ | ||
# Create the mock response object. | ||
class MockResponse: | ||
|
@@ -68,20 +70,21 @@ def read(self): | |
and data['pvs'] == constrained_props: | ||
if data['dcids'] == ['geoId/06085', 'geoId/4805000']: | ||
# Response returned when querying for multiple valid dcids. | ||
res_json = json.dumps([ | ||
{ | ||
'dcid': 'geoId/06085', | ||
'population': 'dc/p/crgfn8blpvl35' | ||
}, | ||
{ | ||
'dcid': 'geoId/4805000', | ||
'population': 'dc/p/f3q9whmjwbf36' | ||
} | ||
]) | ||
res_json =json.dumps({ | ||
'geoId/06085': 'dc/p/crgfn8blpvl35', | ||
'geoId/4805000': 'dc/p/f3q9whmjwbf36'}) if kwargs['new_mixer_api'] else json.dumps([ | ||
{ | ||
'dcid': 'geoId/06085', | ||
'population': 'dc/p/crgfn8blpvl35' | ||
}, | ||
{ | ||
'dcid': 'geoId/4805000', | ||
'population': 'dc/p/f3q9whmjwbf36' | ||
}]) | ||
return MockResponse(json.dumps({'payload': res_json})) | ||
if data['dcids'] == ['geoId/06085', 'dc/MadDcid']: | ||
# Response returned when querying for a dcid that does not exist. | ||
res_json = json.dumps([ | ||
res_json = json.dumps({'geoId/06085': 'dc/p/crgfn8blpvl35'}) if kwargs['new_mixer_api'] else json.dumps([ | ||
{ | ||
'dcid': 'geoId/06085', | ||
'population': 'dc/p/crgfn8blpvl35' | ||
|
@@ -91,7 +94,7 @@ def read(self): | |
if data['dcids'] == ['dc/MadDcid', 'dc/MadderDcid'] or data['dcids'] == []: | ||
# Response returned when both given dcids do not exist or no dcids are | ||
# provided to the method. | ||
res_json = json.dumps([]) | ||
res_json = json.dumps({} if kwargs['new_mixer_api'] else []) | ||
return MockResponse(json.dumps({'payload': res_json})) | ||
|
||
# Mock responses for urlopen request to get_observations | ||
|
@@ -103,7 +106,7 @@ def read(self): | |
and data['measurement_method'] == 'BLSSeasonallyAdjusted': | ||
if data['dcids'] == ['dc/p/x6t44d8jd95rd', 'dc/p/lr52m1yr46r44', 'dc/p/fs929fynprzs']: | ||
# Response returned when querying for multiple valid dcids. | ||
res_json = json.dumps([ | ||
res_json = json.dumps({'dc/p/x6t44d8jd95rd': '18704962.000000', 'dc/p/lr52m1yr46r44': '3075662.000000', 'dc/p/fs929fynprzs': '1973955.000000'}) if kwargs['new_mixer_api'] else json.dumps([ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. try to conform to line 80 |
||
{ | ||
'dcid': 'dc/p/x6t44d8jd95rd', | ||
'observation': '18704962.000000' | ||
|
@@ -120,7 +123,7 @@ def read(self): | |
return MockResponse(json.dumps({'payload': res_json})) | ||
if data['dcids'] == ['dc/p/x6t44d8jd95rd', 'dc/MadDcid']: | ||
# Response returned when querying for a dcid that does not exist. | ||
res_json = json.dumps([ | ||
res_json = json.dumps({'dc/p/x6t44d8jd95rd' : '18704962.000000'}) if kwargs['new_mixer_api'] else json.dumps([ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here |
||
{ | ||
'dcid': 'dc/p/x6t44d8jd95rd', | ||
'observation': '18704962.000000' | ||
|
@@ -130,7 +133,7 @@ def read(self): | |
if data['dcids'] == ['dc/MadDcid', 'dc/MadderDcid'] or data['dcids'] == []: | ||
# Response returned when both given dcids do not exist or no dcids are | ||
# provided to the method. | ||
res_json = json.dumps([]) | ||
res_json = json.dumps({} if kwargs['new_mixer_api'] else []) | ||
return MockResponse(json.dumps({'payload': res_json})) | ||
|
||
# Mock responses for urlopen request to get_place_obs | ||
|
@@ -203,6 +206,12 @@ def read(self): | |
# Otherwise, return an empty response and a 404. | ||
return urllib.error.HTTPError(None, 404, None, None, None) | ||
|
||
def request_mock(*args, **kwargs): | ||
return request_mock_base(new_mixer_api=False, *args, **kwargs) | ||
|
||
def request_mock_new_mixer_api(*args, **kwargs): | ||
return request_mock_base(new_mixer_api=True, *args, **kwargs) | ||
|
||
class TestGetPopulations(unittest.TestCase): | ||
""" Unit tests for get_populations. """ | ||
|
||
|
@@ -225,6 +234,19 @@ def test_multiple_dcids(self, urlopen): | |
'geoId/4805000': 'dc/p/f3q9whmjwbf36' | ||
}) | ||
|
||
@mock.patch('urllib.request.urlopen', side_effect=request_mock_new_mixer_api) | ||
def test_multiple_dcids_new_mixer_api(self, urlopen): | ||
""" Calling get_populations with proper dcids returns valid results. """ | ||
# Set the API key | ||
dc.set_api_key('TEST-API-KEY') | ||
|
||
# Call get_populations | ||
populations = dc.get_populations(['geoId/06085', 'geoId/4805000'], 'Person', | ||
constraining_properties=self._constraints) | ||
self.assertDictEqual(populations, { | ||
'geoId/06085': 'dc/p/crgfn8blpvl35', | ||
'geoId/4805000': 'dc/p/f3q9whmjwbf36' | ||
}) | ||
|
||
@mock.patch('urllib.request.urlopen', side_effect=request_mock) | ||
def test_bad_dcids(self, urlopen): | ||
|
@@ -244,6 +266,24 @@ def test_bad_dcids(self, urlopen): | |
self.assertDictEqual(pops_1, {'geoId/06085': 'dc/p/crgfn8blpvl35'}) | ||
self.assertDictEqual(pops_2, {}) | ||
|
||
@mock.patch('urllib.request.urlopen', side_effect=request_mock_new_mixer_api) | ||
def test_bad_dcids_new_mixer_api(self, urlopen): | ||
""" Calling get_populations with dcids that do not exist returns empty | ||
results. | ||
""" | ||
# Set the API key | ||
dc.set_api_key('TEST-API-KEY') | ||
|
||
# Call get_populations | ||
pops_1 = dc.get_populations(['geoId/06085', 'dc/MadDcid'], 'Person', | ||
constraining_properties=self._constraints) | ||
pops_2 = dc.get_populations(['dc/MadDcid', 'dc/MadderDcid'], 'Person', | ||
constraining_properties=self._constraints) | ||
|
||
# Verify the results | ||
self.assertDictEqual(pops_1, {'geoId/06085': 'dc/p/crgfn8blpvl35'}) | ||
self.assertDictEqual(pops_2, {}) | ||
|
||
@mock.patch('urllib.request.urlopen', side_effect=request_mock) | ||
def test_no_dcids(self, urlopen): | ||
""" Calling get_populations with no dcids returns empty results. """ | ||
|
@@ -254,6 +294,16 @@ def test_no_dcids(self, urlopen): | |
[], 'Person', constraining_properties=self._constraints) | ||
self.assertDictEqual(pops, {}) | ||
|
||
@mock.patch('urllib.request.urlopen', side_effect=request_mock_new_mixer_api) | ||
def test_no_dcids_with_new_mixer_api(self, urlopen): | ||
""" Calling get_populations with no dcids returns empty results. """ | ||
# Set the API key | ||
dc.set_api_key('TEST-API-KEY') | ||
|
||
pops = dc.get_populations( | ||
[], 'Person', constraining_properties=self._constraints) | ||
self.assertDictEqual(pops, {}) | ||
|
||
class TestGetObservations(unittest.TestCase): | ||
""" Unit tests for get_observations. """ | ||
|
||
|
@@ -274,6 +324,23 @@ def test_multiple_dcids(self, urlopen): | |
measurement_method='BLSSeasonallyAdjusted') | ||
self.assertDictEqual(actual, expected) | ||
|
||
@mock.patch('urllib.request.urlopen', side_effect=request_mock_new_mixer_api) | ||
def test_multiple_dcids_with_new_mixer_api(self, urlopen): | ||
""" Calling get_observations with proper dcids returns valid results. """ | ||
# Set the API key | ||
dc.set_api_key('TEST-API-KEY') | ||
|
||
dcids = ['dc/p/x6t44d8jd95rd', 'dc/p/lr52m1yr46r44', 'dc/p/fs929fynprzs'] | ||
expected = { | ||
'dc/p/lr52m1yr46r44': 3075662.0, | ||
'dc/p/fs929fynprzs': 1973955.0, | ||
'dc/p/x6t44d8jd95rd': 18704962.0 | ||
} | ||
actual = dc.get_observations(dcids, 'count', 'measuredValue', '2018-12', | ||
observation_period='P1M', | ||
measurement_method='BLSSeasonallyAdjusted') | ||
self.assertDictEqual(actual, expected) | ||
|
||
@mock.patch('urllib.request.urlopen', side_effect=request_mock) | ||
def test_bad_dcids(self, urlopen): | ||
""" Calling get_observations with dcids that do not exist returns empty | ||
|
@@ -298,6 +365,30 @@ def test_bad_dcids(self, urlopen): | |
self.assertDictEqual(actual_1, {'dc/p/x6t44d8jd95rd': 18704962.0}) | ||
self.assertDictEqual(actual_2, {}) | ||
|
||
@mock.patch('urllib.request.urlopen', side_effect=request_mock_new_mixer_api) | ||
def test_bad_dcids_new_mixer_api(self, urlopen): | ||
""" Calling get_observations with dcids that do not exist returns empty | ||
results. | ||
""" | ||
# Set the API key | ||
dc.set_api_key('TEST-API-KEY') | ||
|
||
# Get the input | ||
dcids_1 = ['dc/p/x6t44d8jd95rd', 'dc/MadDcid'] | ||
dcids_2 = ['dc/MadDcid', 'dc/MadderDcid'] | ||
|
||
# Call get_observations | ||
actual_1 = dc.get_observations(dcids_1, 'count', 'measuredValue', '2018-12', | ||
observation_period='P1M', | ||
measurement_method='BLSSeasonallyAdjusted') | ||
actual_2 = dc.get_observations(dcids_2, 'count', 'measuredValue', '2018-12', | ||
observation_period='P1M', | ||
measurement_method='BLSSeasonallyAdjusted') | ||
|
||
# Verify the results | ||
self.assertDictEqual(actual_1, {'dc/p/x6t44d8jd95rd': 18704962.0}) | ||
self.assertDictEqual(actual_2, {}) | ||
|
||
@mock.patch('urllib.request.urlopen', side_effect=request_mock) | ||
def test_no_dcids(self, urlopen): | ||
""" Calling get_observations with no dcids returns empty results. """ | ||
|
@@ -309,6 +400,17 @@ def test_no_dcids(self, urlopen): | |
measurement_method='BLSSeasonallyAdjusted') | ||
self.assertDictEqual(actual, {}) | ||
|
||
@mock.patch('urllib.request.urlopen', side_effect=request_mock_new_mixer_api) | ||
def test_no_dcids_new_mixer_api(self, urlopen): | ||
""" Calling get_observations with no dcids returns empty results. """ | ||
# Set the API key | ||
dc.set_api_key('TEST-API-KEY') | ||
|
||
actual = dc.get_observations([], 'count', 'measuredValue', '2018-12', | ||
observation_period='P1M', | ||
measurement_method='BLSSeasonallyAdjusted') | ||
self.assertDictEqual(actual, {}) | ||
|
||
|
||
class TestGetPopObs(unittest.TestCase): | ||
""" Unit tests for get_pop_obs. """ | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
might use a normal if else outside