-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathtest_delphi_epidata.py
364 lines (328 loc) · 10.6 KB
/
test_delphi_epidata.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
"""Integration tests for delphi_epidata.py."""
# standard library
import unittest
# third party
import mysql.connector
# first party
from delphi.epidata.client.delphi_epidata import Epidata
from delphi.epidata.acquisition.covidcast.covidcast_meta_cache_updater import main as update_covidcast_meta_cache
import delphi.operations.secrets as secrets
# py3tester coverage target
__test_target__ = 'delphi.epidata.client.delphi_epidata'
class DelphiEpidataPythonClientTests(unittest.TestCase):
"""Tests the Python client."""
def setUp(self):
"""Perform per-test setup."""
# connect to the `epidata` database and clear relevant tables
cnx = mysql.connector.connect(
user='user',
password='pass',
host='delphi_database_epidata',
database='epidata')
cur = cnx.cursor()
cur.execute('truncate table covidcast')
cnx.commit()
cur.close()
# make connection and cursor available to test cases
self.cnx = cnx
self.cur = cnx.cursor()
# use the local instance of the Epidata API
Epidata.BASE_URL = 'http://delphi_web_epidata/epidata/api.php'
# use the local instance of the epidata database
secrets.db.host = 'delphi_database_epidata'
secrets.db.epi = ('user', 'pass')
def tearDown(self):
"""Perform per-test teardown."""
self.cur.close()
self.cnx.close()
def test_covidcast(self):
"""Test that the covidcast endpoint returns expected data."""
# insert dummy data
self.cur.execute('''
insert into covidcast values
(0, 'src', 'sig', 'day', 'county', 20200414, '01234',
123, 1.5, 2.5, 3.5, 456, 4, 20200414, 0, 0, False),
(0, 'src', 'sig2', 'day', 'county', 20200414, '01234',
123, 1.5, 2.5, 3.5, 456, 4, 20200414, 0, 1, False),
(0, 'src', 'sig', 'day', 'county', 20200414, '01234',
456, 5.5, 1.2, 10.5, 789, 0, 20200415, 1, 0, False),
(0, 'src', 'sig', 'day', 'county', 20200414, '01234',
345, 6.5, 2.2, 11.5, 678, 0, 20200416, 2, 1, False)
''')
self.cnx.commit()
with self.subTest(name='request two signals'):
# fetch data
response = Epidata.covidcast(
'src', ['sig', 'sig2'], 'day', 'county', 20200414, '01234')
# check result
self.assertEqual(response, {
'result': 1,
'epidata': [{
'time_value': 20200414,
'geo_value': '01234',
'value': 6.5,
'stderr': 2.2,
'sample_size': 11.5,
'direction': 0,
'issue': 20200416,
'lag': 2,
'signal': 'sig',
}, {
'time_value': 20200414,
'geo_value': '01234',
'value': 1.5,
'stderr': 2.5,
'sample_size': 3.5,
'direction': 4,
'issue': 20200414,
'lag': 0,
'signal': 'sig2',
}],
'message': 'success',
})
with self.subTest(name='request two signals with tree format'):
# fetch data
response = Epidata.covidcast(
'src', ['sig', 'sig2'], 'day', 'county', 20200414, '01234',
format='tree')
# check result
self.assertEqual(response, {
'result': 1,
'epidata': [{
'sig': [{
'time_value': 20200414,
'geo_value': '01234',
'value': 6.5,
'stderr': 2.2,
'sample_size': 11.5,
'direction': 0,
'issue': 20200416,
'lag': 2,
}],
'sig2': [{
'time_value': 20200414,
'geo_value': '01234',
'value': 1.5,
'stderr': 2.5,
'sample_size': 3.5,
'direction': 4,
'issue': 20200414,
'lag': 0,
}],
}],
'message': 'success',
})
with self.subTest(name='request most recent'):
# fetch data, without specifying issue or lag
response_1 = Epidata.covidcast(
'src', 'sig', 'day', 'county', 20200414, '01234')
# check result
self.assertEqual(response_1, {
'result': 1,
'epidata': [{
'time_value': 20200414,
'geo_value': '01234',
'value': 6.5,
'stderr': 2.2,
'sample_size': 11.5,
'direction': 0,
'issue': 20200416,
'lag': 2,
'signal': 'sig',
}],
'message': 'success',
})
with self.subTest(name='request as-of a date'):
# fetch data, specifying as_of
response_1a = Epidata.covidcast(
'src', 'sig', 'day', 'county', 20200414, '01234',
as_of=20200415)
# check result
self.assertEqual(response_1a, {
'result': 1,
'epidata': [{
'time_value': 20200414,
'geo_value': '01234',
'value': 5.5,
'stderr': 1.2,
'sample_size': 10.5,
'direction': 0,
'issue': 20200415,
'lag': 1,
'signal': 'sig',
}],
'message': 'success',
})
with self.subTest(name='request a range of issues'):
# fetch data, specifying issue range, not lag
response_2 = Epidata.covidcast(
'src', 'sig', 'day', 'county', 20200414, '01234',
issues=Epidata.range(20200414, 20200415))
# check result
self.assertDictEqual(response_2, {
'result': 1,
'epidata': [{
'time_value': 20200414,
'geo_value': '01234',
'value': 1.5,
'stderr': 2.5,
'sample_size': 3.5,
'direction': 4,
'issue': 20200414,
'lag': 0,
'signal': 'sig',
}, {
'time_value': 20200414,
'geo_value': '01234',
'value': 5.5,
'stderr': 1.2,
'sample_size': 10.5,
'direction': 0,
'issue': 20200415,
'lag': 1,
'signal': 'sig',
}],
'message': 'success',
})
with self.subTest(name='request at a given lag'):
# fetch data, specifying lag, not issue range
response_3 = Epidata.covidcast(
'src', 'sig', 'day', 'county', 20200414, '01234',
lag=2)
# check result
self.assertDictEqual(response_3, {
'result': 1,
'epidata': [{
'time_value': 20200414,
'geo_value': '01234',
'value': 6.5,
'stderr': 2.2,
'sample_size': 11.5,
'direction': 0,
'issue': 20200416,
'lag': 2,
'signal': 'sig',
}],
'message': 'success',
})
def test_geo_value(self):
"""test different variants of geo types: single, *, multi."""
# insert dummy data
self.cur.execute('''
insert into covidcast values
(0, 'src', 'sig', 'day', 'county', 20200414, '11111',
123, 10, 11, 12, 456, 13, 20200414, 0, 1, False),
(0, 'src', 'sig', 'day', 'county', 20200414, '22222',
123, 20, 21, 22, 456, 23, 20200414, 0, 1, False),
(0, 'src', 'sig', 'day', 'county', 20200414, '33333',
123, 30, 31, 32, 456, 33, 20200414, 0, 1, False),
(0, 'src', 'sig', 'day', 'msa', 20200414, '11111',
123, 40, 41, 42, 456, 43, 20200414, 0, 1, False),
(0, 'src', 'sig', 'day', 'msa', 20200414, '22222',
123, 50, 51, 52, 456, 53, 20200414, 0, 1, False),
(0, 'src', 'sig', 'day', 'msa', 20200414, '33333',
123, 60, 61, 62, 456, 634, 20200414, 0, 1, False)
''')
self.cnx.commit()
def fetch(geo_value):
# make the request
response = Epidata.covidcast(
'src', 'sig', 'day', 'county', 20200414, geo_value)
return response
counties = [{
'time_value': 20200414,
'geo_value': '11111',
'value': 10,
'stderr': 11,
'sample_size': 12,
'direction': 13,
'issue': 20200414,
'lag': 0,
'signal': 'sig',
}, {
'time_value': 20200414,
'geo_value': '22222',
'value': 20,
'stderr': 21,
'sample_size': 22,
'direction': 23,
'issue': 20200414,
'lag': 0,
'signal': 'sig',
}, {
'time_value': 20200414,
'geo_value': '33333',
'value': 30,
'stderr': 31,
'sample_size': 32,
'direction': 33,
'issue': 20200414,
'lag': 0,
'signal': 'sig',
}]
# test fetch all
r = fetch('*')
self.assertEqual(r['message'], 'success')
self.assertEqual(r['epidata'], counties)
# test fetch a specific region
r = fetch('11111')
self.assertEqual(r['message'], 'success')
self.assertEqual(r['epidata'], [counties[0]])
# test fetch a specific yet not existing region
r = fetch('55555')
self.assertEqual(r['message'], 'no results')
# test fetch a multiple regions
r = fetch(['11111', '22222'])
self.assertEqual(r['message'], 'success')
self.assertEqual(r['epidata'], [counties[0], counties[1]])
# test fetch a multiple regions in another variant
r = fetch(['11111', '33333'])
self.assertEqual(r['message'], 'success')
self.assertEqual(r['epidata'], [counties[0], counties[2]])
# test fetch a multiple regions but one is not existing
r = fetch(['11111', '55555'])
self.assertEqual(r['message'], 'success')
self.assertEqual(r['epidata'], [counties[0]])
# test fetch a multiple regions but specify no region
r = fetch([])
self.assertEqual(r['message'], 'no results')
def test_covidcast_meta(self):
"""Test that the covidcast_meta endpoint returns expected data."""
# insert dummy data
self.cur.execute('''
insert into covidcast values
(0, 'src', 'sig', 'day', 'county', 20200414, '01234',
123, 1.5, 2.5, 3.5, 456, 4, 20200414, 0, 0, False),
(0, 'src', 'sig', 'day', 'county', 20200414, '01234',
345, 6.0, 2.2, 11.5, 678, 0, 20200416, 2, 1, False),
(0, 'src', 'sig', 'day', 'county', 20200415, '01234',
345, 7.0, 2.0, 12.5, 678, 0, 20200416, 1, 1, False)
''')
self.cnx.commit()
# cache it
update_covidcast_meta_cache(args=None)
# fetch data
response = Epidata.covidcast_meta()
# check result
self.assertEqual(response, {
'result': 1,
'epidata': [{
'data_source': 'src',
'signal': 'sig',
'time_type': 'day',
'geo_type': 'county',
'min_time': 20200414,
'max_time': 20200415,
'num_locations': 1,
'min_value': 6.0,
'max_value': 7.0,
'mean_value': 6.5,
'stdev_value': 0.5,
'last_update': 345,
'min_issue': 20200414,
'max_issue': 20200416,
'min_lag': 1,
'max_lag': 2,
}],
'message': 'success',
})