-
Notifications
You must be signed in to change notification settings - Fork 16
1972 replace covidcast #2056
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
base: main
Are you sure you want to change the base?
1972 replace covidcast #2056
Changes from 38 commits
4afe0f0
67a312c
e4f2679
c941982
a5628ac
6e22db8
a2a149f
9269621
8e67b6c
bf21d33
d76cd40
fd50d9d
76f1519
157c6c6
23384e7
e5c3b46
33936a4
01a7f66
6eeef4e
1f59e06
7f60275
55150bc
329d340
9d91be7
5ac98ab
b92695a
15ce75f
6ee3e9e
f25605d
2654946
c63f095
79bf550
4c44d3a
8a308c4
b4039c5
4916465
670bf04
2f94d15
57fc591
913c72f
a9cebed
e30aaca
733c85e
f61462a
06fafd0
d3bc895
68e2850
9ff0979
f7fcefc
957af29
9585196
cf4f06d
b5929af
ee64984
fa9143a
2de9d3b
a1aad7a
38f25bb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{"data_source": ["chng", "chng", "chng", | ||
"covid-act-now", | ||
"covid-act-now", | ||
"covid-act-now", | ||
"chng"], | ||
"signal": ["smoothed_outpatient_cli", | ||
"smoothed_outpatient_covid", | ||
"smoothed_outpatient_covid", | ||
"pcr_specimen_positivity_rate", | ||
"pcr_specimen_positivity_rate", | ||
"pcr_specimen_total_tests", | ||
"inactive"], | ||
"geo_type": ["state", "state", "county", | ||
"hrr", "msa", "msa", | ||
"state"], | ||
"min_time": ["20200101", "20200101", "20200101", | ||
"20200101", "20200101", "20200101", | ||
"20200101"], | ||
"max_time": ["20240101", "20240101", "20240101", | ||
"20240101", "20240101", "20240101", | ||
"20240101"], | ||
"last_update": [1711963480, 1711963480, 1711963480, | ||
1711963480, 1711963480, 1711963480, | ||
1711963480], | ||
"time_type": ["day", "day", "day", | ||
"day", "day", "day", | ||
"day"] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{"geo_value": ["1044"], | ||
"stderr": [null], | ||
"value": [3], | ||
"issue": [20200101], | ||
"lag": [7], | ||
"sample_size": [null], | ||
"time_value": [20200101], | ||
"direction": [null] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{"geo_value": ["0888"], | ||
"stderr": [2], | ||
"value": [14], | ||
"issue": [20200101], | ||
"lag": [1], | ||
"sample_size": [100], | ||
"time_value": [20200101], | ||
"direction": [null] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
"""Tests for datafetcher.py.""" | ||
|
||
from datetime import date | ||
from datetime import date, datetime | ||
import mock | ||
import json | ||
from pathlib import Path | ||
import numpy as np | ||
import pandas as pd | ||
import pytest | ||
|
@@ -14,6 +16,7 @@ | |
from delphi_utils.validator.errors import ValidationFailure | ||
|
||
|
||
TEST_DIR = Path(__file__).parent.parent | ||
|
||
class TestDataFetcher: | ||
"""Tests for various data fetching utilities.""" | ||
|
@@ -45,6 +48,27 @@ def raise_for_status(self): | |
{'source': 'covid-act-now', 'db_source': 'covid-act-now'}], 200) | ||
elif "params" in kwargs and kwargs["params"] == {'signal': 'chng:inactive'}: | ||
return MockResponse([{"signals": [{"active": False}]}], 200) | ||
elif args[0] == 'https://api.delphi.cmu.edu/epidata/covidcast_meta/' and \ | ||
'delphi_epidata' in kwargs["headers"]["user-agent"]: | ||
aysim319 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
with open(f"{TEST_DIR}/test_data/sample_epidata_metadata.json") as f: | ||
epidata = json.load(f) | ||
response = {"epidata": epidata, "result": 1, "message": "success"} | ||
return MockResponse(response, 200) | ||
elif args[0] == 'https://api.delphi.cmu.edu/epidata/covidcast/' and \ | ||
'delphi_epidata' in kwargs["headers"]["user-agent"]: | ||
aysim319 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
signal_type = args[1].get("signals") | ||
geo_type = args[1].get("geo_type") | ||
if signal_type == "a": | ||
with open(f"{TEST_DIR}/test_data/sample_epidata_signal_a.json") as f: | ||
epidata = json.load(f) | ||
response = {"epidata": epidata, "result": 1, "message": "success"} | ||
return MockResponse(response, 200) | ||
Comment on lines
+63
to
+66
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. I love that you broke the sample data out into their own .json files! However, for accuracy's sake, the format doesnt match actual usage now; youre handing back "HTTP Responses" where the " AFAICT, the differences come from the old usage of these tests where it was mocking calls to the TL;DR: If youre going to mock HTTP requests (which i think is the right thing to do), make them return something that looks like the real HTTP responses. Otherwise, mock a different layer in the call stack. 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. I compared the output that epidata.covidcast returned and that's how it returns: sample_epidata_covidcast_result.json
|
||
if geo_type == "county": | ||
with open(f"{TEST_DIR}/test_data/sample_epidata_signal_county.json") as f: | ||
epidata = json.load(f) | ||
response = {"epidata": epidata, "result": 1, "message": "success"} | ||
return MockResponse(response, 200) | ||
return MockResponse({"epidata": {}, "result": 1, "message": "success"}, 200) | ||
else: | ||
return MockResponse([{"signals": [{"active": True}]}], 200) | ||
|
||
|
@@ -57,27 +81,9 @@ def test_bad_api_key(self, **kwargs): | |
get_geo_signal_combos("chng", api_key="") | ||
|
||
@mock.patch('requests.get', side_effect=mocked_requests_get) | ||
@mock.patch("covidcast.metadata") | ||
def test_get_geo_signal_combos(self, mock_metadata, mock_get): | ||
def test_get_geo_signal_combos(self, mock_get): | ||
|
||
"""Test that the geo signal combos are correctly pulled from the covidcast metadata.""" | ||
# Need to use actual data_source and signal names since we reference the API | ||
# We let the chng signal "inactive" be an inactive signal | ||
mock_metadata.return_value = pd.DataFrame({"data_source": ["chng", "chng", "chng", | ||
"covid-act-now", | ||
"covid-act-now", | ||
"covid-act-now", | ||
"chng"], | ||
"signal": ["smoothed_outpatient_cli", | ||
"smoothed_outpatient_covid", | ||
"smoothed_outpatient_covid", | ||
"pcr_specimen_positivity_rate", | ||
"pcr_specimen_positivity_rate", | ||
"pcr_specimen_total_tests", | ||
"inactive"], | ||
"geo_type": ["state", "state", "county", | ||
"hrr", "msa", "msa", | ||
"state"] | ||
}) | ||
assert set(get_geo_signal_combos("chng", api_key="")) == set( | ||
[("state", "smoothed_outpatient_cli"), | ||
("state", "smoothed_outpatient_covid"), | ||
|
@@ -87,49 +93,20 @@ def test_get_geo_signal_combos(self, mock_metadata, mock_get): | |
("msa", "pcr_specimen_positivity_rate"), | ||
("msa", "pcr_specimen_total_tests")]) | ||
|
||
@mock.patch("covidcast.signal") | ||
def test_threaded_api_calls(self, mock_signal): | ||
@mock.patch('requests.get', side_effect=mocked_requests_get) | ||
def test_threaded_api_calls(self, mock_get): | ||
"""Test that calls to the covidcast API are made.""" | ||
|
||
signal_data_1 = pd.DataFrame({"geo_value": ["1044"], | ||
"stderr": [None], | ||
"value": [3], | ||
"issue": [10], | ||
"lag": [7], | ||
"sample_size": [None], | ||
"time_value": [10] | ||
}) | ||
signal_data_2 = pd.DataFrame({"geo_value": ["0888"], | ||
"stderr": [2], | ||
"value": [14], | ||
"issue": [10], | ||
"lag": [1], | ||
"sample_size": [100], | ||
"time_value": [8] | ||
}) | ||
|
||
def mock_signal_return_fn(unused_data_source, signal_type, unused_start_date, | ||
unused_end_date, geo_type): | ||
"""Function to return data when covidcast.signal() is called.""" | ||
if signal_type == "a": | ||
return signal_data_1 | ||
if geo_type == "county": | ||
return signal_data_2 | ||
return None | ||
|
||
mock_signal.side_effect = mock_signal_return_fn | ||
|
||
processed_signal_data_1 = pd.DataFrame({"geo_id": ["1044"], | ||
"val": [3], | ||
"se": [np.nan], | ||
"sample_size": [np.nan], | ||
"time_value": [10] | ||
"time_value": [datetime.strptime("20200101", "%Y%m%d")], | ||
}) | ||
processed_signal_data_2 = pd.DataFrame({"geo_id": ["0888"], | ||
"val": [14], | ||
"se": [2], | ||
"sample_size": [100], | ||
"time_value": [8] | ||
"time_value": [datetime.strptime("20200101", "%Y%m%d")], | ||
}) | ||
expected = { | ||
("county", "a"): processed_signal_data_1, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,6 @@ | |
|
||
required = [ | ||
"boto3", | ||
"covidcast", | ||
"darker[isort]~=2.1.1", | ||
"delphi-utils", | ||
"mock", | ||
|
Uh oh!
There was an error while loading. Please reload this page.