1
1
"""Integration tests for delphi_epidata.py."""
2
2
3
3
# standard library
4
- import unittest
5
4
import time
6
- from unittest .mock import patch , MagicMock
7
5
from json import JSONDecodeError
6
+ from unittest .mock import MagicMock , patch
8
7
9
- # third party
10
- from aiohttp .client_exceptions import ClientResponseError
11
- import mysql .connector
8
+ # first party
12
9
import pytest
10
+ from aiohttp .client_exceptions import ClientResponseError
13
11
14
- # first party
15
- from delphi_utils import Nans
16
- from delphi .epidata .client .delphi_epidata import Epidata
17
- from delphi .epidata .acquisition .covidcast .database import Database , CovidcastRow
18
- from delphi .epidata .acquisition .covidcast .covidcast_meta_cache_updater import main as update_covidcast_meta_cache
19
- from delphi .epidata .acquisition .covidcast .test_utils import CovidcastBase
12
+ # third party
20
13
import delphi .operations .secrets as secrets
14
+ from delphi .epidata .acquisition .covidcast .covidcast_meta_cache_updater import main as update_covidcast_meta_cache
15
+ from delphi .epidata .acquisition .covidcast .test_utils import CovidcastBase , CovidcastTestRow
16
+ from delphi .epidata .client .delphi_epidata import Epidata
17
+ from delphi_utils import Nans
18
+
21
19
22
20
# py3tester coverage target
23
21
__test_target__ = 'delphi.epidata.client.delphi_epidata'
22
+ # all the Nans we use here are just one value, so this is a shortcut to it:
23
+ nmv = Nans .NOT_MISSING .value
24
24
25
25
def fake_epidata_endpoint (func ):
26
26
"""This can be used as a decorator to enable a bogus Epidata endpoint to return 404 responses."""
@@ -30,9 +30,6 @@ def wrapper(*args):
30
30
Epidata .BASE_URL = 'http://delphi_web_epidata/epidata/api.php'
31
31
return wrapper
32
32
33
- # all the Nans we use here are just one value, so this is a shortcut to it:
34
- nmv = Nans .NOT_MISSING .value
35
-
36
33
class DelphiEpidataPythonClientTests (CovidcastBase ):
37
34
"""Tests the Python client."""
38
35
@@ -54,13 +51,11 @@ def test_covidcast(self):
54
51
55
52
# insert placeholder data: three issues of one signal, one issue of another
56
53
rows = [
57
- self . _make_placeholder_row (issue = self . DEFAULT_ISSUE + i , value = i , lag = i )[ 0 ]
54
+ CovidcastTestRow . make_default_row (issue = 2020_02_02 + i , value = i , lag = i )
58
55
for i in range (3 )
59
56
]
60
57
row_latest_issue = rows [- 1 ]
61
- rows .append (
62
- self ._make_placeholder_row (signal = "sig2" )[0 ]
63
- )
58
+ rows .append (CovidcastTestRow .make_default_row (signal = "sig2" ))
64
59
self ._insert_rows (rows )
65
60
66
61
with self .subTest (name = 'request two signals' ):
@@ -70,10 +65,11 @@ def test_covidcast(self):
70
65
)
71
66
72
67
expected = [
73
- self . expected_from_row ( row_latest_issue ),
74
- self . expected_from_row ( rows [- 1 ])
68
+ row_latest_issue . as_api_compatibility_row_dict ( ),
69
+ rows [- 1 ]. as_api_compatibility_row_dict ( )
75
70
]
76
71
72
+ self .assertEqual (response ['epidata' ], expected )
77
73
# check result
78
74
self .assertEqual (response , {
79
75
'result' : 1 ,
@@ -89,10 +85,10 @@ def test_covidcast(self):
89
85
90
86
expected = [{
91
87
rows [0 ].signal : [
92
- self . expected_from_row ( row_latest_issue , self . DEFAULT_MINUS + ['signal' ]),
88
+ row_latest_issue . as_api_compatibility_row_dict ( ignore_fields = ['signal' ]),
93
89
],
94
90
rows [- 1 ].signal : [
95
- self . expected_from_row ( rows [- 1 ], self . DEFAULT_MINUS + ['signal' ]),
91
+ rows [- 1 ]. as_api_compatibility_row_dict ( ignore_fields = ['signal' ]),
96
92
],
97
93
}]
98
94
@@ -109,12 +105,12 @@ def test_covidcast(self):
109
105
** self .params_from_row (rows [0 ])
110
106
)
111
107
112
- expected = self . expected_from_row ( row_latest_issue )
108
+ expected = [ row_latest_issue . as_api_compatibility_row_dict ()]
113
109
114
110
# check result
115
111
self .assertEqual (response_1 , {
116
112
'result' : 1 ,
117
- 'epidata' : [ expected ] ,
113
+ 'epidata' : expected ,
118
114
'message' : 'success' ,
119
115
})
120
116
@@ -124,13 +120,13 @@ def test_covidcast(self):
124
120
** self .params_from_row (rows [0 ], as_of = rows [1 ].issue )
125
121
)
126
122
127
- expected = self . expected_from_row ( rows [1 ])
123
+ expected = [ rows [1 ]. as_api_compatibility_row_dict ()]
128
124
129
125
# check result
130
126
self .maxDiff = None
131
127
self .assertEqual (response_1a , {
132
128
'result' : 1 ,
133
- 'epidata' : [ expected ] ,
129
+ 'epidata' : expected ,
134
130
'message' : 'success' ,
135
131
})
136
132
@@ -141,8 +137,8 @@ def test_covidcast(self):
141
137
)
142
138
143
139
expected = [
144
- self . expected_from_row ( rows [0 ]),
145
- self . expected_from_row ( rows [1 ])
140
+ rows [0 ]. as_api_compatibility_row_dict ( ),
141
+ rows [1 ]. as_api_compatibility_row_dict ( )
146
142
]
147
143
148
144
# check result
@@ -158,12 +154,12 @@ def test_covidcast(self):
158
154
** self .params_from_row (rows [0 ], lag = 2 )
159
155
)
160
156
161
- expected = self . expected_from_row ( row_latest_issue )
157
+ expected = [ row_latest_issue . as_api_compatibility_row_dict ()]
162
158
163
159
# check result
164
160
self .assertDictEqual (response_3 , {
165
161
'result' : 1 ,
166
- 'epidata' : [ expected ] ,
162
+ 'epidata' : expected ,
167
163
'message' : 'success' ,
168
164
})
169
165
with self .subTest (name = 'long request' ):
@@ -223,16 +219,16 @@ def test_geo_value(self):
223
219
# insert placeholder data: three counties, three MSAs
224
220
N = 3
225
221
rows = [
226
- self . _make_placeholder_row (geo_type = "county" , geo_value = str (i )* 5 , value = i )[ 0 ]
222
+ CovidcastTestRow . make_default_row (geo_type = "county" , geo_value = str (i )* 5 , value = i )
227
223
for i in range (N )
228
224
] + [
229
- self . _make_placeholder_row (geo_type = "msa" , geo_value = str (i )* 5 , value = i * 10 )[ 0 ]
225
+ CovidcastTestRow . make_default_row (geo_type = "msa" , geo_value = str (i )* 5 , value = i * 10 )
230
226
for i in range (N )
231
227
]
232
228
self ._insert_rows (rows )
233
229
234
230
counties = [
235
- self . expected_from_row ( rows [i ]) for i in range (N )
231
+ rows [i ]. as_api_compatibility_row_dict ( ) for i in range (N )
236
232
]
237
233
238
234
def fetch (geo ):
@@ -241,41 +237,48 @@ def fetch(geo):
241
237
)
242
238
243
239
# test fetch all
244
- r = fetch ('*' )
245
- self .assertEqual (r ['message' ], 'success' )
246
- self .assertEqual (r ['epidata' ], counties )
240
+ request = fetch ('*' )
241
+ self .assertEqual (request ['message' ], 'success' )
242
+ self .assertEqual (request ['epidata' ], counties )
247
243
# test fetch a specific region
248
- r = fetch ('11111' )
249
- self .assertEqual (r ['message' ], 'success' )
250
- self .assertEqual (r ['epidata' ], [counties [1 ]])
244
+ request = fetch ('11111' )
245
+ self .assertEqual (request ['message' ], 'success' )
246
+ self .assertEqual (request ['epidata' ], [counties [1 ]])
251
247
# test fetch a specific yet not existing region
252
- r = fetch ('55555' )
253
- self .assertEqual (r ['message' ], 'no results' )
248
+ request = fetch ('55555' )
249
+ self .assertEqual (request ['message' ], 'no results' )
254
250
# test fetch a multiple regions
255
- r = fetch (['11111' , '22222' ])
256
- self .assertEqual (r ['message' ], 'success' )
257
- self .assertEqual (r ['epidata' ], [counties [1 ], counties [2 ]])
251
+ request = fetch (['11111' , '22222' ])
252
+ self .assertEqual (request ['message' ], 'success' )
253
+ self .assertEqual (request ['epidata' ], [counties [1 ], counties [2 ]])
258
254
# test fetch a multiple regions in another variant
259
- r = fetch (['00000' , '22222' ])
260
- self .assertEqual (r ['message' ], 'success' )
261
- self .assertEqual (r ['epidata' ], [counties [0 ], counties [2 ]])
255
+ request = fetch (['00000' , '22222' ])
256
+ self .assertEqual (request ['message' ], 'success' )
257
+ self .assertEqual (request ['epidata' ], [counties [0 ], counties [2 ]])
262
258
# test fetch a multiple regions but one is not existing
263
- r = fetch (['11111' , '55555' ])
264
- self .assertEqual (r ['message' ], 'success' )
265
- self .assertEqual (r ['epidata' ], [counties [1 ]])
259
+ request = fetch (['11111' , '55555' ])
260
+ self .assertEqual (request ['message' ], 'success' )
261
+ self .assertEqual (request ['epidata' ], [counties [1 ]])
266
262
# test fetch a multiple regions but specify no region
267
- r = fetch ([])
268
- self .assertEqual (r ['message' ], 'no results' )
263
+ request = fetch ([])
264
+ self .assertEqual (request ['message' ], 'no results' )
269
265
270
266
def test_covidcast_meta (self ):
271
267
"""Test that the covidcast_meta endpoint returns expected data."""
272
268
269
+ DEFAULT_TIME_VALUE = 2020_02_02
270
+ DEFAULT_ISSUE = 2020_02_02
271
+
273
272
# insert placeholder data: three dates, three issues. values are:
274
273
# 1st issue: 0 10 20
275
274
# 2nd issue: 1 11 21
276
275
# 3rd issue: 2 12 22
277
276
rows = [
278
- self ._make_placeholder_row (time_value = self .DEFAULT_TIME_VALUE + t , issue = self .DEFAULT_ISSUE + i , value = t * 10 + i )[0 ]
277
+ CovidcastTestRow .make_default_row (
278
+ time_value = DEFAULT_TIME_VALUE + t ,
279
+ issue = DEFAULT_ISSUE + i ,
280
+ value = t * 10 + i
281
+ )
279
282
for i in range (3 ) for t in range (3 )
280
283
]
281
284
self ._insert_rows (rows )
@@ -299,14 +302,14 @@ def test_covidcast_meta(self):
299
302
signal = rows [0 ].signal ,
300
303
time_type = rows [0 ].time_type ,
301
304
geo_type = rows [0 ].geo_type ,
302
- min_time = self . DEFAULT_TIME_VALUE ,
303
- max_time = self . DEFAULT_TIME_VALUE + 2 ,
305
+ min_time = DEFAULT_TIME_VALUE ,
306
+ max_time = DEFAULT_TIME_VALUE + 2 ,
304
307
num_locations = 1 ,
305
308
min_value = 2. ,
306
309
mean_value = 12. ,
307
310
max_value = 22. ,
308
311
stdev_value = 8.1649658 , # population stdev, not sample, which is 10.
309
- max_issue = self . DEFAULT_ISSUE + 2 ,
312
+ max_issue = DEFAULT_ISSUE + 2 ,
310
313
min_lag = 0 ,
311
314
max_lag = 0 , # we didn't set lag when inputting data
312
315
)
@@ -322,10 +325,10 @@ def test_async_epidata(self):
322
325
# insert placeholder data: three counties, three MSAs
323
326
N = 3
324
327
rows = [
325
- self . _make_placeholder_row (geo_type = "county" , geo_value = str (i )* 5 , value = i )[ 0 ]
328
+ CovidcastTestRow . make_default_row (geo_type = "county" , geo_value = str (i )* 5 , value = i )
326
329
for i in range (N )
327
330
] + [
328
- self . _make_placeholder_row (geo_type = "msa" , geo_value = str (i )* 5 , value = i * 10 )[ 0 ]
331
+ CovidcastTestRow . make_default_row (geo_type = "msa" , geo_value = str (i )* 5 , value = i * 10 )
329
332
for i in range (N )
330
333
]
331
334
self ._insert_rows (rows )
0 commit comments