Skip to content

Commit c823943

Browse files
committed
Add mandatory meta_key check to server
* fix all tests by adding param where needed
1 parent a3cf65b commit c823943

21 files changed

+249
-30
lines changed

integrations/acquisition/covid_hosp/facility/test_scenarios.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# standard library
44
import unittest
5-
from unittest.mock import MagicMock
5+
from unittest.mock import MagicMock, patch
66

77
# first party
88
from delphi.epidata.acquisition.covid_hosp.common.database import Database
@@ -19,6 +19,21 @@
1919

2020
NEWLINE="\n"
2121

22+
def _request(params):
23+
"""Request and parse epidata.
24+
25+
We default to GET since it has better caching and logging
26+
capabilities, but fall back to POST if the request is too
27+
long and returns a 414.
28+
"""
29+
params.update({'meta_key': 'meta_secret'})
30+
try:
31+
return Epidata._request_with_retry(params).json()
32+
except Exception as e:
33+
return {'result': 0, 'message': 'error: ' + str(e)}
34+
35+
36+
@patch.object(Epidata, '_request', _request)
2237
class AcquisitionTests(unittest.TestCase):
2338

2439
def setUp(self):

integrations/acquisition/covid_hosp/state_daily/test_scenarios.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,21 @@
2222
__test_target__ = \
2323
'delphi.epidata.acquisition.covid_hosp.state_daily.update'
2424

25+
def _request(params):
26+
"""Request and parse epidata.
2527
28+
We default to GET since it has better caching and logging
29+
capabilities, but fall back to POST if the request is too
30+
long and returns a 414.
31+
"""
32+
params.update({'meta_key': 'meta_secret'})
33+
try:
34+
return Epidata._request_with_retry(params).json()
35+
except Exception as e:
36+
return {'result': 0, 'message': 'error: ' + str(e)}
37+
38+
39+
@patch.object(Epidata, '_request', _request)
2640
class AcquisitionTests(unittest.TestCase):
2741

2842
def setUp(self):

integrations/acquisition/covid_hosp/state_timeseries/test_scenarios.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# standard library
44
import unittest
5-
from unittest.mock import MagicMock
5+
from unittest.mock import MagicMock, patch
66

77
# first party
88
from delphi.epidata.acquisition.covid_hosp.common.database import Database
@@ -18,7 +18,21 @@
1818
__test_target__ = \
1919
'delphi.epidata.acquisition.covid_hosp.state_timeseries.update'
2020

21+
def _request(params):
22+
"""Request and parse epidata.
2123
24+
We default to GET since it has better caching and logging
25+
capabilities, but fall back to POST if the request is too
26+
long and returns a 414.
27+
"""
28+
params.update({'meta_key': 'meta_secret'})
29+
try:
30+
return Epidata._request_with_retry(params).json()
31+
except Exception as e:
32+
return {'result': 0, 'message': 'error: ' + str(e)}
33+
34+
35+
@patch.object(Epidata, '_request', _request)
2236
class AcquisitionTests(unittest.TestCase):
2337

2438
def setUp(self):

integrations/acquisition/covidcast/test_covidcast_meta_caching.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# standard library
44
import json
55
import unittest
6+
from unittest.mock import patch
67

78
# third party
89
import mysql.connector
@@ -24,7 +25,21 @@
2425
# use the local instance of the Epidata API
2526
BASE_URL = 'http://delphi_web_epidata/epidata/api.php'
2627

28+
def _request(params):
29+
"""Request and parse epidata.
2730
31+
We default to GET since it has better caching and logging
32+
capabilities, but fall back to POST if the request is too
33+
long and returns a 414.
34+
"""
35+
params.update({'meta_key': 'meta_secret'})
36+
try:
37+
return Epidata._request_with_retry(params).json()
38+
except Exception as e:
39+
return {'result': 0, 'message': 'error: ' + str(e)}
40+
41+
42+
@patch.object(Epidata, '_request', _request)
2843
class CovidcastMetaCacheTests(unittest.TestCase):
2944
"""Tests covidcast metadata caching."""
3045

@@ -144,7 +159,7 @@ def test_caching(self):
144159
self.cnx.commit()
145160

146161
# fetch the cached version (manually)
147-
params = {'endpoint': 'covidcast_meta', 'cached': 'true'}
162+
params = {'endpoint': 'covidcast_meta', 'cached': 'true', 'meta_key': 'meta_secret'}
148163
response = requests.get(BASE_URL, params=params)
149164
response.raise_for_status()
150165
epidata4 = response.json()
@@ -167,7 +182,7 @@ def test_caching(self):
167182
self.cnx.commit()
168183

169184
# fetch the cached version (manually)
170-
params = {'endpoint': 'covidcast_meta', 'cached': 'true'}
185+
params = {'endpoint': 'covidcast_meta', 'cached': 'true', 'meta_key': 'meta_secret'}
171186
response = requests.get(BASE_URL, params=params)
172187
response.raise_for_status()
173188
epidata5 = response.json()

integrations/acquisition/covidcast/test_csv_uploading.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from datetime import date
55
import os
66
import unittest
7-
from unittest.mock import MagicMock
7+
from unittest.mock import MagicMock, patch
88

99
# third party
1010
import mysql.connector
@@ -20,7 +20,21 @@
2020
# py3tester coverage target (equivalent to `import *`)
2121
__test_target__ = 'delphi.epidata.acquisition.covidcast.csv_to_database'
2222

23+
def _request(params):
24+
"""Request and parse epidata.
2325
26+
We default to GET since it has better caching and logging
27+
capabilities, but fall back to POST if the request is too
28+
long and returns a 414.
29+
"""
30+
params.update({'meta_key': 'meta_secret'})
31+
try:
32+
return Epidata._request_with_retry(params).json()
33+
except Exception as e:
34+
return {'result': 0, 'message': 'error: ' + str(e)}
35+
36+
37+
@patch.object(Epidata, '_request', _request)
2438
class CsvUploadingTests(unittest.TestCase):
2539
"""Tests covidcast CSV uploading."""
2640

integrations/acquisition/covidcast/test_delete_batch.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# standard library
44
from collections import namedtuple
55
import unittest
6+
from unittest.mock import patch
67
from os import path
78

89
# third party
@@ -18,6 +19,21 @@
1819

1920
Example = namedtuple("example", "given expected")
2021

22+
def _request(params):
23+
"""Request and parse epidata.
24+
25+
We default to GET since it has better caching and logging
26+
capabilities, but fall back to POST if the request is too
27+
long and returns a 414.
28+
"""
29+
params.update({'meta_key': 'meta_secret'})
30+
try:
31+
return Epidata._request_with_retry(params).json()
32+
except Exception as e:
33+
return {'result': 0, 'message': 'error: ' + str(e)}
34+
35+
36+
@patch.object(Epidata, '_request', _request)
2137
class DeleteBatch(unittest.TestCase):
2238
"""Tests batch deletions"""
2339

integrations/acquisition/covidcast/test_fill_is_latest_issue.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,21 @@
1515
# py3tester coverage target (equivalent to `import *`)
1616
__test_target__ = 'delphi.epidata.acquisition.covidcast.fill_is_latest_issue'
1717

18+
def _request(params):
19+
"""Request and parse epidata.
1820
21+
We default to GET since it has better caching and logging
22+
capabilities, but fall back to POST if the request is too
23+
long and returns a 414.
24+
"""
25+
params.update({'meta_key': 'meta_secret'})
26+
try:
27+
return Epidata._request_with_retry(params).json()
28+
except Exception as e:
29+
return {'result': 0, 'message': 'error: ' + str(e)}
30+
31+
32+
@unittest.mock.patch.object(Epidata, '_request', _request)
1933
class FillIsLatestIssueTests(unittest.TestCase):
2034
"""Tests filling is_latest_issue column"""
2135

integrations/acquisition/covidcast_nowcast/test_csv_uploading.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,21 @@
2626
issue=(date(2020, 4, 21), epi.Week.fromdate(date(2020, 4, 21)))
2727
)
2828

29+
def _request(params):
30+
"""Request and parse epidata.
2931
32+
We default to GET since it has better caching and logging
33+
capabilities, but fall back to POST if the request is too
34+
long and returns a 414.
35+
"""
36+
params.update({'meta_key': 'meta_secret'})
37+
try:
38+
return Epidata._request_with_retry(params).json()
39+
except Exception as e:
40+
return {'result': 0, 'message': 'error: ' + str(e)}
41+
42+
43+
@patch.object(Epidata, '_request', _request)
3044
class CsvUploadingTests(unittest.TestCase):
3145
"""Tests covidcast nowcast CSV uploading."""
3246

integrations/client/test_delphi_epidata.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,21 @@ def wrapper(*args):
2727
Epidata.BASE_URL = 'http://delphi_web_epidata/epidata/api.php'
2828
return wrapper
2929

30+
def _request(params):
31+
"""Request and parse epidata.
3032
33+
We default to GET since it has better caching and logging
34+
capabilities, but fall back to POST if the request is too
35+
long and returns a 414.
36+
"""
37+
params.update({'meta_key': 'meta_secret'})
38+
try:
39+
return Epidata._request_with_retry(params).json()
40+
except Exception as e:
41+
return {'result': 0, 'message': 'error: ' + str(e)}
42+
43+
44+
@patch.object(Epidata, '_request', _request)
3145
class DelphiEpidataPythonClientTests(unittest.TestCase):
3246
"""Tests the Python client."""
3347

@@ -315,7 +329,7 @@ def test_retry_request(self, get):
315329
mock_response = MagicMock()
316330
mock_response.status_code = 200
317331
get.side_effect = [JSONDecodeError('Expecting value', "", 0), mock_response]
318-
response = Epidata._request(None)
332+
response = Epidata._request({})
319333
self.assertEqual(get.call_count, 2)
320334
self.assertEqual(response, mock_response.json())
321335

@@ -326,7 +340,7 @@ def test_retry_request(self, get):
326340
get.side_effect = [JSONDecodeError('Expecting value', "", 0),
327341
JSONDecodeError('Expecting value', "", 0),
328342
mock_response]
329-
response = Epidata._request(None)
343+
response = Epidata._request({})
330344
self.assertEqual(get.call_count, 2) # 2 from previous test + 2 from this one
331345
self.assertEqual(response,
332346
{'result': 0, 'message': 'error: Expecting value: line 1 column 1 (char 0)'}
@@ -615,7 +629,8 @@ def test_async_epidata(self):
615629
'time_type': 'day',
616630
'geo_type': 'county',
617631
'geo_value': '11111',
618-
'time_values': '20200414'
632+
'time_values': '20200414',
633+
'meta_key': 'meta_secret'
619634
},
620635
{
621636
'source': 'covidcast',
@@ -624,7 +639,8 @@ def test_async_epidata(self):
624639
'time_type': 'day',
625640
'geo_type': 'county',
626641
'geo_value': '00000',
627-
'time_values': '20200414'
642+
'time_values': '20200414',
643+
'meta_key': 'meta_secret'
628644
}
629645
]*12, batch_size=10)
630646
responses = [i[0] for i in test_output]
@@ -634,7 +650,7 @@ def test_async_epidata(self):
634650
Epidata.covidcast('src', 'sig', 'day', 'county', 20200414, '00000')]*12
635651
)
636652

637-
@fake_epidata_endpoint
653+
@patch.object(Epidata, "BASE_URL", 'http://delphi_web_epidata/epidata/fake_api.php')
638654
def test_async_epidata_fail(self):
639655
with pytest.raises(ClientResponseError, match="404, message='NOT FOUND'"):
640656
Epidata.async_epidata([
@@ -645,6 +661,7 @@ def test_async_epidata_fail(self):
645661
'time_type': 'day',
646662
'geo_type': 'county',
647663
'geo_value': '11111',
648-
'time_values': '20200414'
664+
'time_values': '20200414',
665+
'meta_key': 'meta_secret'
649666
}
650667
])

integrations/server/test_covid_hosp.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,28 @@
22

33
# standard library
44
import unittest
5+
from unittest.mock import patch
56

67
# first party
78
from delphi.epidata.acquisition.covid_hosp.state_timeseries.database import Database
89
from delphi.epidata.client.delphi_epidata import Epidata
910
import delphi.operations.secrets as secrets
1011

12+
def _request(params):
13+
"""Request and parse epidata.
1114
15+
We default to GET since it has better caching and logging
16+
capabilities, but fall back to POST if the request is too
17+
long and returns a 414.
18+
"""
19+
params.update({'meta_key': 'meta_secret'})
20+
try:
21+
return Epidata._request_with_retry(params).json()
22+
except Exception as e:
23+
return {'result': 0, 'message': 'error: ' + str(e)}
24+
25+
26+
@patch.object(Epidata, '_request', _request)
1227
class ServerTests(unittest.TestCase):
1328
"""Tests the `covid_hosp` endpoint."""
1429

0 commit comments

Comments
 (0)