3
3
# standard library
4
4
from typing import Callable
5
5
import unittest
6
- # from unittest.mock import MagicMock
7
- # from unittest.mock import patch
8
6
9
7
# third party
10
8
import mysql .connector
11
9
import requests
12
10
13
11
# first party
14
12
from delphi_utils import Nans
15
- from delphi .epidata .acquisition .covidcast .test_utils import CovidcastBase , CovidcastTestRow
13
+ from delphi .epidata .acquisition .covidcast .test_utils import CovidcastBase , CovidcastTestRow , FIPS , MSA
16
14
from delphi .epidata .client .delphi_epidata import Epidata
17
15
18
16
# use the local instance of the Epidata API
19
17
BASE_URL = 'http://delphi_web_epidata/epidata/api.php'
20
18
21
- # TODO replace these real geo_values with fake values, and use patch and mock to mock the return values of
22
- # delphi_utils.geomap.GeoMapper().get_geo_values(geo_type) in parse_geo_sets() of _params.py
23
-
24
- global fips , msa # add two global lists of valid geo_values of these types to pass the validation in parse_geo_sets() of _params.py
25
-
26
- fips = ['04019' , '19143' , '29063' ] # Example list of valid FIPS codes as strings
27
- msa = ['40660' , '44180' , '48620' ] # Example list of valid MSAs as strings
28
-
29
-
30
19
class CovidcastTests (CovidcastBase ):
31
20
"""Tests the `covidcast` endpoint."""
32
21
@@ -42,52 +31,52 @@ def request_based_on_row(self, row: CovidcastTestRow, **kwargs):
42
31
return response
43
32
44
33
def _insert_placeholder_set_one (self ):
45
- row = CovidcastTestRow .make_default_row (geo_type = 'msa' , geo_value = msa [ 0 ] )
34
+ row = CovidcastTestRow .make_default_row ()
46
35
self ._insert_rows ([row ])
47
36
return row
48
37
49
38
def _insert_placeholder_set_two (self ):
50
39
rows = [
51
- CovidcastTestRow .make_default_row (geo_type = 'msa' , geo_value = msa [i - 1 ], value = i * 1. , stderr = i * 10. , sample_size = i * 100. )
40
+ CovidcastTestRow .make_default_row (geo_type = 'msa' , geo_value = MSA [i - 1 ], value = i * 1. , stderr = i * 10. , sample_size = i * 100. )
52
41
for i in [1 , 2 , 3 ]
53
42
] + [
54
- CovidcastTestRow .make_default_row (geo_type = 'fips' , geo_value = fips [i - 4 ], value = i * 1. , stderr = i * 10. , sample_size = i * 100. )
43
+ CovidcastTestRow .make_default_row (geo_type = 'fips' , geo_value = FIPS [i - 4 ], value = i * 1. , stderr = i * 10. , sample_size = i * 100. )
55
44
for i in [4 , 5 , 6 ]
56
45
]
57
46
self ._insert_rows (rows )
58
47
return rows
59
48
60
49
def _insert_placeholder_set_three (self ):
61
50
rows = [
62
- CovidcastTestRow .make_default_row (geo_type = 'msa' , geo_value = msa [0 ], time_value = 2000_01_01 + i , value = i * 1. , stderr = i * 10. , sample_size = i * 100. , issue = 2000_01_03 , lag = 2 - i )
51
+ CovidcastTestRow .make_default_row (geo_value = MSA [0 ], time_value = 2000_01_01 + i , value = i * 1. , stderr = i * 10. , sample_size = i * 100. , issue = 2000_01_03 , lag = 2 - i )
63
52
for i in [1 , 2 , 3 ]
64
53
] + [
65
54
# time value intended to overlap with the time values above, with disjoint geo values
66
- CovidcastTestRow .make_default_row (geo_type = 'msa' , geo_value = str (i )* 5 , time_value = 2000_01_01 + i - 3 , value = i * 1. , stderr = i * 10. , sample_size = i * 100. , issue = 2000_01_03 , lag = 5 - i )
55
+ CovidcastTestRow .make_default_row (geo_value = str (i )* 5 , time_value = 2000_01_01 + i - 3 , value = i * 1. , stderr = i * 10. , sample_size = i * 100. , issue = 2000_01_03 , lag = 5 - i )
67
56
for i in [4 , 5 , 6 ]
68
57
]
69
58
self ._insert_rows (rows )
70
59
return rows
71
60
72
61
def _insert_placeholder_set_four (self ):
73
62
rows = [
74
- CovidcastTestRow .make_default_row (geo_type = 'msa' , geo_value = msa [ 0 ], source = 'src1' , signal = str (i )* 5 , value = i * 1. , stderr = i * 10. , sample_size = i * 100. )
63
+ CovidcastTestRow .make_default_row (source = 'src1' , signal = str (i )* 5 , value = i * 1. , stderr = i * 10. , sample_size = i * 100. )
75
64
for i in [1 , 2 , 3 ]
76
65
] + [
77
66
# signal intended to overlap with the signal above
78
- CovidcastTestRow .make_default_row (geo_type = 'msa' , geo_value = msa [ 0 ], source = 'src2' , signal = str (i - 3 )* 5 , value = i * 1. , stderr = i * 10. , sample_size = i * 100. )
67
+ CovidcastTestRow .make_default_row (source = 'src2' , signal = str (i - 3 )* 5 , value = i * 1. , stderr = i * 10. , sample_size = i * 100. )
79
68
for i in [4 , 5 , 6 ]
80
69
]
81
70
self ._insert_rows (rows )
82
71
return rows
83
72
84
73
def _insert_placeholder_set_five (self ):
85
74
rows = [
86
- CovidcastTestRow .make_default_row (geo_type = 'msa' , geo_value = msa [ 0 ], time_value = 2000_01_01 , value = i * 1. , stderr = i * 10. , sample_size = i * 100. , issue = 2000_01_03 + i )
75
+ CovidcastTestRow .make_default_row (time_value = 2000_01_01 , value = i * 1. , stderr = i * 10. , sample_size = i * 100. , issue = 2000_01_03 + i )
87
76
for i in [1 , 2 , 3 ]
88
77
] + [
89
78
# different time_values, same issues
90
- CovidcastTestRow .make_default_row (geo_type = 'msa' , geo_value = msa [ 0 ], time_value = 2000_01_01 + i - 3 , value = i * 1. , stderr = i * 10. , sample_size = i * 100. , issue = 2000_01_03 + i - 3 )
79
+ 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 )
91
80
for i in [4 , 5 , 6 ]
92
81
]
93
82
self ._insert_rows (rows )
@@ -317,30 +306,30 @@ def fetch(geo_value):
317
306
318
307
return response
319
308
320
- self .maxDiff = None
321
309
# test fetch a specific region
322
- r = fetch (msa [0 ])
310
+ r = fetch (MSA [0 ])
323
311
self .assertEqual (r ['message' ], 'success' )
324
312
self .assertEqual (r ['epidata' ], expected [0 :1 ])
325
313
# test fetch a specific yet not existing region
326
314
r = fetch ('11111' )
327
- self .assertEqual (r ['message' ], 'invalid geo_value for the requested geo_type' )
328
-
315
+ self .assertEqual (r ['message' ], 'Invalid geo_value(s) 11111 for the requested geo_type msa' )
329
316
# test fetch multiple regions
330
- r = fetch ('{},{}' .format (msa [0 ], msa [1 ]))
317
+ r = fetch ('{},{}' .format (MSA [0 ], MSA [1 ]))
331
318
self .assertEqual (r ['message' ], 'success' )
332
319
self .assertEqual (r ['epidata' ], expected [0 :2 ])
333
320
# test fetch multiple noncontiguous regions
334
- r = fetch ('{},{}' .format (msa [0 ], msa [2 ]))
321
+ r = fetch ('{},{}' .format (MSA [0 ], MSA [2 ]))
335
322
self .assertEqual (r ['message' ], 'success' )
336
323
self .assertEqual (r ['epidata' ], [expected [0 ], expected [2 ]])
337
324
# test fetch multiple regions but one is not existing
338
- r = fetch ('{},11111' .format (msa [0 ]))
339
- self .assertEqual (r ['message' ], 'invalid geo_value for the requested geo_type' )
340
-
325
+ r = fetch ('{},11111' .format (MSA [0 ]))
326
+ self .assertEqual (r ['message' ], 'Invalid geo_value(s) 11111 for the requested geo_type msa' )
341
327
# test fetch empty region
342
328
r = fetch ('' )
343
- self .assertEqual (r ['message' ], 'invalid geo_value for the requested geo_type' )
329
+ self .assertEqual (r ['message' ], 'geo_value is empty for the requested geo_type msa!' )
330
+ # test a region that has no results
331
+ r = fetch (MSA [3 ])
332
+ self .assertEqual (r ['message' ], 'no results' )
344
333
345
334
def test_location_timeline (self ):
346
335
"""Select a timeline for a particular location."""
@@ -377,7 +366,7 @@ def test_nullable_columns(self):
377
366
"""Missing values should be surfaced as null."""
378
367
379
368
row = CovidcastTestRow .make_default_row (
380
- geo_type = 'msa' , geo_value = msa [ 0 ], stderr = None , sample_size = None ,
369
+ stderr = None , sample_size = None ,
381
370
missing_stderr = Nans .OTHER .value , missing_sample_size = Nans .OTHER .value
382
371
)
383
372
self ._insert_rows ([row ])
@@ -398,7 +387,7 @@ def test_temporal_partitioning(self):
398
387
399
388
# insert placeholder data
400
389
rows = [
401
- CovidcastTestRow .make_default_row (geo_type = 'msa' , geo_value = msa [ 0 ], time_type = tt )
390
+ CovidcastTestRow .make_default_row (time_type = tt )
402
391
for tt in "hour day week month year" .split ()
403
392
]
404
393
self ._insert_rows (rows )
0 commit comments