Skip to content

Use epidata client in covidcast integration tests + fix csv format bug + fix fields param bug #1091

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

Merged
merged 4 commits into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions integrations/server/test_covidcast.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# first party
from delphi_utils import Nans
from delphi.epidata.acquisition.covidcast.test_utils import CovidcastBase, CovidcastTestRow
from delphi.epidata.client.delphi_epidata import Epidata

# use the local instance of the Epidata API
BASE_URL = 'http://delphi_web_epidata/epidata/api.php'
Expand All @@ -22,11 +23,10 @@ def localSetUp(self):
"""Perform per-test setup."""
self._db._cursor.execute('update covidcast_meta_cache set timestamp = 0, epidata = "[]"')

def request_based_on_row(self, row: CovidcastTestRow, extract_response: Callable = lambda x: x.json(), **kwargs):
def request_based_on_row(self, row: CovidcastTestRow, **kwargs):
params = self.params_from_row(row, endpoint='covidcast', **kwargs)
response = requests.get(BASE_URL, params=params)
response.raise_for_status()
response = extract_response(response)
Epidata.BASE_URL = BASE_URL
response = Epidata.covidcast(**params)

return response

Expand Down Expand Up @@ -155,7 +155,6 @@ def test_csv_format(self):
# NB 'format' is a Python reserved word
response = self.request_based_on_row(
row,
extract_response=lambda resp: resp.text,
**{'format':'csv'}
)

Expand Down Expand Up @@ -194,7 +193,7 @@ def test_fields(self):
row = self._insert_placeholder_set_one()

# limit fields
response = self.request_based_on_row(row, fields='time_value,geo_value')
response = self.request_based_on_row(row, **{"fields":"time_value,geo_value"})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: did you ever figure out why the **{} is needed?

it's fine this way, just curious

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both ways work now. I choose to keep it like this to be consistent with how extra arguments are added in other test functions, but having it as fields='time_value,geo_value' also works.


expected = row.as_api_compatibility_row_dict()
expected_all = {
Expand Down
9 changes: 8 additions & 1 deletion src/client/delphi_epidata.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ def _request(params):
long and returns a 414.
"""
try:
return Epidata._request_with_retry(params).json()
result = Epidata._request_with_retry(params)
if params is not None and "format" in params and params["format"]=="csv":
return result.text
else:
return result.json()
except Exception as e:
return {'result': 0, 'message': 'error: ' + str(e)}

Expand Down Expand Up @@ -606,6 +610,9 @@ def covidcast(
if 'format' in kwargs:
params['format'] = kwargs['format']

if 'fields' in kwargs:
params['fields'] = kwargs['fields']

# Make the API call
return Epidata._request(params)

Expand Down