-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathtest_covidcast.py
478 lines (379 loc) · 15.5 KB
/
test_covidcast.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
"""Integration tests for the `covidcast` endpoint."""
# standard library
from typing import Callable
import unittest
# third party
import MySQLdb
# first party
from delphi_utils import Nans
from delphi.epidata.acquisition.covidcast.test_utils import CovidcastBase, CovidcastTestRow, FIPS, MSA
from delphi.epidata.client.delphi_epidata import Epidata
class CovidcastTests(CovidcastBase):
"""Tests the `covidcast` endpoint."""
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, **kwargs):
params = self.params_from_row(row, endpoint='covidcast', **kwargs)
# use the local instance of the Epidata API
Epidata.BASE_URL = 'http://delphi_web_epidata/epidata'
Epidata.auth = ('epidata', 'key')
response = Epidata.covidcast(**params)
return response
def _insert_placeholder_set_one(self):
row = CovidcastTestRow.make_default_row()
self._insert_rows([row])
return row
def _insert_placeholder_set_two(self):
rows = [
CovidcastTestRow.make_default_row(geo_type='msa', geo_value=MSA[i-1], value=i*1., stderr=i*10., sample_size=i*100.)
for i in [1, 2, 3]
] + [
CovidcastTestRow.make_default_row(geo_type='fips', geo_value=FIPS[i-4], value=i*1., stderr=i*10., sample_size=i*100.)
for i in [4, 5, 6]
]
self._insert_rows(rows)
return rows
def _insert_placeholder_set_three(self):
rows = [
CovidcastTestRow.make_default_row(time_value=2000_01_01+i, value=i*1., stderr=i*10., sample_size=i*100., issue=2000_01_03, lag=2-i)
for i in [1, 2, 3]
] + [
# time value intended to overlap with the time values above, with disjoint geo values
CovidcastTestRow.make_default_row(geo_value=MSA[i-3], time_value=2000_01_01+i-3, value=i*1., stderr=i*10., sample_size=i*100., issue=2000_01_03, lag=5-i)
for i in [4, 5, 6]
]
self._insert_rows(rows)
return rows
def _insert_placeholder_set_four(self):
rows = [
CovidcastTestRow.make_default_row(source='src1', signal=str(i)*5, value=i*1., stderr=i*10., sample_size=i*100.)
for i in [1, 2, 3]
] + [
# signal intended to overlap with the signal above
CovidcastTestRow.make_default_row(source='src2', signal=str(i-3)*5, value=i*1., stderr=i*10., sample_size=i*100.)
for i in [4, 5, 6]
]
self._insert_rows(rows)
return rows
def _insert_placeholder_set_five(self):
rows = [
CovidcastTestRow.make_default_row(time_value=2000_01_01, value=i*1., stderr=i*10., sample_size=i*100., issue=2000_01_03+i)
for i in [1, 2, 3]
] + [
# different time_values, same issues
CovidcastTestRow.make_default_row(time_value=2000_01_01+i-3, value=i*1., stderr=i*10., sample_size=i*100., issue=2000_01_03+i-3)
for i in [4, 5, 6]
]
self._insert_rows(rows)
return rows
def _insert_placeholder_set_with_weeks(self):
rows = [
CovidcastTestRow.make_default_row(
time_value=2021_05+i, time_type="week",
source="nchs-mortality", signal="deaths_covid_incidence_num",
geo_type="state", geo_value="il",
value=i*i)
for i in [0, 1, 2]
]
self._insert_rows(rows)
return rows
def test_round_trip(self):
"""Make a simple round-trip with some sample data."""
# insert placeholder data
row = self._insert_placeholder_set_one()
# make the request
response = self.request_based_on_row(row)
expected = [row.as_api_row_dict()]
self.assertEqual(response, {
'result': 1,
'epidata': expected,
'message': 'success',
})
# TODO enable test again when the gunicorn issue https://github.com/benoitc/gunicorn/issues/2487 is resolved
# def test_uri_too_long(self):
# def test_uri_too_long(self):
# """Test that a long request yields a 414 with GET but works with POST."""
# # insert placeholder data
# self.cur.execute(f'''
# INSERT INTO
# `covidcast` (`id`, `source`, `signal`, `time_type`, `geo_type`,
# `time_value`, `geo_value`, `value_updated_timestamp`,
# `value`, `stderr`, `sample_size`, `direction_updated_timestamp`,
# `direction`, `issue`, `lag`, `is_latest_issue`, `missing_value`,
# `missing_stderr`,`missing_sample_size`)
# VALUES
# (0, 'src', 'sig', 'day', 'county', 20200414, '01234',
# 123, 1.5, 2.5, 3.5, 456, 4, 20200414, 0, 1,
# {Nans.NOT_MISSING}, {Nans.NOT_MISSING}, {Nans.NOT_MISSING})
# ''')
# self.cnx.commit()
# # make the request with GET
# response = requests.get(BASE_URL, {
# 'endpoint': 'covidcast',
# 'data_source': 'src'*10000,
# 'signal': 'sig',
# 'time_type': 'day',
# 'geo_type': 'county',
# 'time_values': 20200414,
# 'geo_value': '01234',
# })
# self.assertEqual(response.status_code, 414)
# # make request with POST
# response = requests.post(BASE_URL, {
# 'endpoint': 'covidcast',
# 'data_source': 'src'*10000,
# 'signal': 'sig',
# 'time_type': 'day',
# 'geo_type': 'county',
# 'time_values': 20200414,
# 'geo_value': '01234',
# })
# self.assertEqual(response.status_code, 200)
def test_csv_format(self):
"""Test generate csv data."""
# insert placeholder data
row = self._insert_placeholder_set_one()
# make the request
# NB 'format' is a Python reserved word
response = self.request_based_on_row(
row,
**{'format':'csv'}
)
# This is a hardcoded mess because of the field ordering constructed here:
# https://github.com/cmu-delphi/delphi-epidata/blob/f7da6598a810be8df5374e3a71512c631c3a14f1/src/server/endpoints/covidcast.py#L83-L93
column_order = [
"geo_value", "signal", "source", "geo_type", "time_type",
"time_value", "direction", "issue", "lag", "missing_value",
"missing_stderr", "missing_sample_size", "value", "stderr", "sample_size"
]
expected = (
row.as_api_row_df()
.assign(direction = None)
.to_csv(columns=column_order, index=False)
)
# assert that the right data came back
self.assertEqual(response, expected)
def test_raw_json_format(self):
"""Test generate raw json data."""
# insert placeholder data
row = self._insert_placeholder_set_one()
# make the request
response = self.request_based_on_row(row, **{'format':'json'})
expected = [row.as_api_row_dict()]
# assert that the right data came back
self.assertEqual(response, expected)
def test_fields(self):
"""Test fields parameter"""
# insert placeholder data
row = self._insert_placeholder_set_one()
# limit fields
response = self.request_based_on_row(row, **{"fields":"time_value,geo_value,geo_type,source,time_type"})
expected = row.as_api_row_dict()
expected_all = {
'result': 1,
'epidata': [{
k: expected[k] for k in ['time_value', 'geo_value', 'geo_type', 'source', 'time_type']
}],
'message': 'success',
}
# assert that the right data came back
self.assertEqual(response, expected_all)
# limit using invalid fields
response = self.request_based_on_row(row, fields='time_value,geo_value,geo_type,source,time_type,doesnt_exist')
# assert that the right data came back (only valid fields)
self.assertEqual(response, expected_all)
# limit exclude fields: exclude all except time_value and geo_value
response = self.request_based_on_row(row, fields=(
'-value,-stderr,-sample_size,-direction,-issue,-lag,-signal,' +
'-missing_value,-missing_stderr,-missing_sample_size'
))
# assert that the right data came back
self.assertEqual(response, expected_all)
def test_location_wildcard(self):
"""Select all locations with a wildcard query."""
# insert placeholder data
rows = self._insert_placeholder_set_two()
expected = [row.as_api_row_dict() for row in rows[:3]]
# make the request
response = self.request_based_on_row(rows[0], geo_value="*")
self.maxDiff = None
# assert that the right data came back
self.assertEqual(response, {
'result': 1,
'epidata': expected,
'message': 'success',
})
def test_time_values_wildcard(self):
"""Select all time_values with a wildcard query."""
# insert placeholder data
rows = self._insert_placeholder_set_three()
expected = [row.as_api_row_dict() for row in rows[:3]]
# make the request
response = self.request_based_on_row(rows[0], time_values="*")
self.maxDiff = None
# assert that the right data came back
self.assertEqual(response, {
'result': 1,
'epidata': expected,
'message': 'success',
})
def test_issues_wildcard(self):
"""Select all issues with a wildcard query."""
# insert placeholder data
rows = self._insert_placeholder_set_five()
expected = [row.as_api_row_dict() for row in rows[:3]]
# make the request
response = self.request_based_on_row(rows[0], issues="*")
self.maxDiff = None
# assert that the right data came back
self.assertEqual(response, {
'result': 1,
'epidata': expected,
'message': 'success',
})
def test_signal_wildcard(self):
"""Select all signals with a wildcard query."""
# insert placeholder data
rows = self._insert_placeholder_set_four()
expected_signals = [row.as_api_row_dict() for row in rows[:3]]
# make the request
response = self.request_based_on_row(rows[0], signals="*")
self.maxDiff = None
# assert that the right data came back
self.assertEqual(response, {
'result': 1,
'epidata': expected_signals,
'message': 'success',
})
def test_geo_value(self):
"""test whether geo values are valid for specific geo types"""
# insert placeholder data
rows = self._insert_placeholder_set_two()
expected = [row.as_api_row_dict() for row in rows[:3]]
def fetch(geo_value):
# make the request
response = self.request_based_on_row(rows[0], geo_value=geo_value)
return response
# test fetch a specific region
r = fetch(MSA[0])
self.assertEqual(r['message'], 'success')
self.assertEqual(r['epidata'], expected[0:1])
# test fetch a specific yet not existing region
r = fetch('11111')
self.assertEqual(r['message'], 'Invalid geo_value(s) 11111 for the requested geo_type msa')
# test fetch multiple regions
r = fetch(f'{MSA[0]},{MSA[1]}')
self.assertEqual(r['message'], 'success')
self.assertEqual(r['epidata'], expected[0:2])
# test fetch multiple noncontiguous regions
r = fetch(f'{MSA[0]},{MSA[2]}')
self.assertEqual(r['message'], 'success')
self.assertEqual(r['epidata'], [expected[0], expected[2]])
# test fetch multiple regions but one is not existing
r = fetch(f'{MSA[0]},11111')
self.assertEqual(r['message'], 'Invalid geo_value(s) 11111 for the requested geo_type msa')
# test fetch empty region
r = fetch('')
self.assertEqual(r['message'], 'geo_value is empty for the requested geo_type msa!')
# test a region that has no results
r = fetch(MSA[3])
self.assertEqual(r['message'], 'no results')
def test_location_timeline(self):
"""Select a timeline for a particular location."""
# insert placeholder data
rows = self._insert_placeholder_set_three()
expected_timeseries = [row.as_api_row_dict() for row in rows[:3]]
# make the request
response = self.request_based_on_row(rows[0], time_values='20000101-20000105')
# assert that the right data came back
self.assertEqual(response, {
'result': 1,
'epidata': expected_timeseries,
'message': 'success',
})
@unittest.skip("v4 now uses ON DUPLICATE KEY UPDATE which prevents this key collision. Consider moving this test to a database integration test which runs SQL without the ON DUPLICATE KEY UPDATE clause to verify constraints are set correctly.")
def test_unique_key_constraint(self):
"""Don't allow a row with a key collision to be inserted."""
# insert placeholder data
row = self._insert_placeholder_set_one()
# fail to insert different placeholder data under the same key
with self.assertRaises(MySQLdb.errors.IntegrityError):
self._insert_placeholder_set_one()
# succeed to insert different placeholder data under a different time_type
self._insert_placeholder_set_one(time_type='week')
def test_nullable_columns(self):
"""Missing values should be surfaced as null."""
row = CovidcastTestRow.make_default_row(
stderr=None, sample_size=None,
missing_stderr=Nans.OTHER.value, missing_sample_size=Nans.OTHER.value
)
self._insert_rows([row])
# make the request
response = self.request_based_on_row(row)
expected = row.as_api_row_dict()
# assert that the right data came back
self.assertEqual(response, {
'result': 1,
'epidata': [expected],
'message': 'success',
})
def test_temporal_partitioning(self):
"""Request a signal that's available at multiple temporal resolutions."""
# insert placeholder data
rows = [
CovidcastTestRow.make_default_row(time_type=tt)
for tt in "hour day week month year".split()
]
self._insert_rows(rows)
# make the request
response = self.request_based_on_row(rows[1], time_values="*")
expected = [rows[1].as_api_row_dict()]
# assert that the right data came back
self.assertEqual(response, {
'result': 1,
'epidata': expected,
'message': 'success',
})
def test_date_formats(self):
"""Request a signal using different time formats."""
# insert placeholder data
rows = self._insert_placeholder_set_three()
# make the request
response = self.request_based_on_row(rows[0], time_values="20000102", geo_value="*")
# assert that the right data came back
self.assertEqual(len(response['epidata']), 2)
# make the request
response = self.request_based_on_row(rows[0], time_values="2000-01-02", geo_value="*")
# assert that the right data came back
self.assertEqual(len(response['epidata']), 2)
# make the request
response = self.request_based_on_row(rows[0], time_values="20000102,20000103", geo_value="*")
# assert that the right data came back
self.assertEqual(len(response['epidata']), 2 * 2)
# make the request
response = self.request_based_on_row(rows[0], time_values="2000-01-02,2000-01-03", geo_value="*")
# assert that the right data came back
self.assertEqual(len(response['epidata']), 2 * 2)
# make the request
response = self.request_based_on_row(rows[0], time_values="20000102-20000104", geo_value="*")
# assert that the right data came back
self.assertEqual(len(response['epidata']), 2 * 3)
# make the request
response = self.request_based_on_row(rows[0], time_values="2000-01-02:2000-01-04", geo_value="*")
# assert that the right data came back
self.assertEqual(len(response['epidata']), 2 * 3)
def test_week_formats(self):
"""Test different ways to specify week ranges are accepted."""
rows = self._insert_placeholder_set_with_weeks()
expected = {
'result': 1,
'epidata': [r.as_api_row_dict() for r in rows],
'message': 'success',
}
colond = self.request_based_on_row(rows[0], time_values="202105:202107")
dashed = self.request_based_on_row(rows[0], time_values="202105-202107")
enumed = self.request_based_on_row(rows[0], time_values="202105,202106,202107")
self.assertEqual(expected, colond)
self.assertEqual(expected, dashed)
self.assertEqual(expected, enumed)