3
3
# standard library
4
4
import json
5
5
import unittest
6
+ # from unittest.mock import MagicMock
7
+ # from unittest.mock import patch
6
8
7
9
# third party
8
10
import mysql .connector
15
17
# use the local instance of the Epidata API
16
18
BASE_URL = 'http://delphi_web_epidata/epidata/api.php'
17
19
20
+ # TODO replace these real geo_values with fake values, and use patch and mock to mock the return values of
21
+ # delphi_utils.geomap.GeoMapper().get_geo_values(geo_type) in parse_geo_sets() of _params.py
22
+
23
+ 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
24
+
25
+ fips = ['04019' , '19143' , '29063' ] # Example list of valid FIPS codes as strings
26
+ msa = ['40660' , '44180' , '48620' ] # Example list of valid MSAs as strings
18
27
19
28
20
29
class CovidcastTests (CovidcastBase ):
@@ -35,53 +44,52 @@ def request_based_on_row(self, row, extract_response=lambda x: x.json(), **kwarg
35
44
return response , expected
36
45
37
46
def _insert_placeholder_set_one (self ):
38
- row , settings = self ._make_placeholder_row ()
47
+ row , settings = self ._make_placeholder_row (geo_type = 'msa' , geo_value = msa [ 0 ] )
39
48
self ._insert_rows ([row ])
40
49
return row
41
50
42
51
def _insert_placeholder_set_two (self ):
43
52
rows = [
44
- self ._make_placeholder_row (geo_type = 'county ' , geo_value = str ( i ) * 5 , value = i * 1. , stderr = i * 10. , sample_size = i * 100. )[0 ]
53
+ self ._make_placeholder_row (geo_type = 'msa ' , geo_value = msa [ i - 1 ] , value = i * 1. , stderr = i * 10. , sample_size = i * 100. )[0 ]
45
54
for i in [1 , 2 , 3 ]
46
55
] + [
47
- # geo value intended to overlap with counties above
48
- self ._make_placeholder_row (geo_type = 'msa' , geo_value = str (i - 3 )* 5 , value = i * 1. , stderr = i * 10. , sample_size = i * 100. )[0 ]
56
+ self ._make_placeholder_row (geo_type = 'fips' , geo_value = fips [i - 4 ], value = i * 1. , stderr = i * 10. , sample_size = i * 100. )[0 ]
49
57
for i in [4 , 5 , 6 ]
50
58
]
51
59
self ._insert_rows (rows )
52
60
return rows
53
61
54
62
def _insert_placeholder_set_three (self ):
55
63
rows = [
56
- self ._make_placeholder_row (geo_type = 'county ' , geo_value = '11111' , time_value = 2000_01_01 + i , value = i * 1. , stderr = i * 10. , sample_size = i * 100. , issue = 2000_01_03 , lag = 2 - i )[0 ]
64
+ self ._make_placeholder_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 )[0 ]
57
65
for i in [1 , 2 , 3 ]
58
66
] + [
59
67
# time value intended to overlap with 11111 above, with disjoint geo values
60
- self ._make_placeholder_row (geo_type = 'county ' , 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 )[0 ]
68
+ self ._make_placeholder_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 )[0 ]
61
69
for i in [4 , 5 , 6 ]
62
70
]
63
71
self ._insert_rows (rows )
64
72
return rows
65
73
66
74
def _insert_placeholder_set_four (self ):
67
75
rows = [
68
- self ._make_placeholder_row (source = 'src1' , signal = str (i )* 5 , value = i * 1. , stderr = i * 10. , sample_size = i * 100. )[0 ]
76
+ self ._make_placeholder_row (geo_type = 'msa' , geo_value = msa [ 0 ], source = 'src1' , signal = str (i )* 5 , value = i * 1. , stderr = i * 10. , sample_size = i * 100. )[0 ]
69
77
for i in [1 , 2 , 3 ]
70
78
] + [
71
79
# signal intended to overlap with the signal above
72
- self ._make_placeholder_row (source = 'src2' , signal = str (i - 3 )* 5 , value = i * 1. , stderr = i * 10. , sample_size = i * 100. )[0 ]
80
+ self ._make_placeholder_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. )[0 ]
73
81
for i in [4 , 5 , 6 ]
74
82
]
75
83
self ._insert_rows (rows )
76
84
return rows
77
85
78
86
def _insert_placeholder_set_five (self ):
79
87
rows = [
80
- self ._make_placeholder_row (time_value = 2000_01_01 , value = i * 1. , stderr = i * 10. , sample_size = i * 100. , issue = 2000_01_03 + i )[0 ]
88
+ self ._make_placeholder_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 )[0 ]
81
89
for i in [1 , 2 , 3 ]
82
90
] + [
83
91
# different time_values, same issues
84
- self ._make_placeholder_row (time_value = 2000_01_01 + i - 3 , value = i * 1. , stderr = i * 10. , sample_size = i * 100. , issue = 2000_01_03 + i - 3 )[0 ]
92
+ self ._make_placeholder_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 )[0 ]
85
93
for i in [4 , 5 , 6 ]
86
94
]
87
95
self ._insert_rows (rows )
@@ -310,7 +318,7 @@ def test_signal_wildcard(self):
310
318
})
311
319
312
320
def test_geo_value (self ):
313
- """test different variants of geo types: single, *, multi. """
321
+ """test whether geo values are valid for specific geo types """
314
322
315
323
# insert placeholder data
316
324
rows = self ._insert_placeholder_set_two ()
@@ -324,28 +332,33 @@ def fetch(geo_value):
324
332
325
333
return response
326
334
335
+ self .maxDiff = None
327
336
# test fetch a specific region
328
- r = fetch ('11111' )
337
+ r = fetch (msa [ 0 ] )
329
338
self .assertEqual (r ['message' ], 'success' )
330
339
self .assertEqual (r ['epidata' ], [expected_counties [0 ]])
340
+
331
341
# test fetch a specific yet not existing region
332
- r = fetch ('55555' )
333
- self .assertEqual (r ['message' ], 'no results' )
342
+ r = fetch ('11111' )
343
+ self .assertEqual (r ['message' ], 'invalid geo_value for the requested geo_type' )
344
+
334
345
# test fetch multiple regions
335
- r = fetch ('11111,22222' )
346
+ r = fetch ('{},{}' . format ( msa [ 0 ], msa [ 1 ]) )
336
347
self .assertEqual (r ['message' ], 'success' )
337
348
self .assertEqual (r ['epidata' ], [expected_counties [0 ], expected_counties [1 ]])
349
+
338
350
# test fetch multiple noncontiguous regions
339
- r = fetch ('11111,33333' )
351
+ r = fetch ('{},{}' . format ( msa [ 0 ], msa [ 2 ]) )
340
352
self .assertEqual (r ['message' ], 'success' )
341
353
self .assertEqual (r ['epidata' ], [expected_counties [0 ], expected_counties [2 ]])
354
+
342
355
# test fetch multiple regions but one is not existing
343
- r = fetch ('11111,55555' )
344
- self .assertEqual (r ['message' ], 'success ' )
345
- self . assertEqual ( r [ 'epidata' ], [ expected_counties [ 0 ]])
356
+ r = fetch ('{},11111' . format ( msa [ 0 ]) )
357
+ self .assertEqual (r ['message' ], 'invalid geo_value for the requested geo_type ' )
358
+
346
359
# test fetch empty region
347
360
r = fetch ('' )
348
- self .assertEqual (r ['message' ], 'no results ' )
361
+ self .assertEqual (r ['message' ], 'invalid geo_value for the requested geo_type ' )
349
362
350
363
def test_location_timeline (self ):
351
364
"""Select a timeline for a particular location."""
@@ -384,7 +397,7 @@ def test_nullable_columns(self):
384
397
"""Missing values should be surfaced as null."""
385
398
386
399
row , _ = self ._make_placeholder_row (
387
- stderr = None , sample_size = None ,
400
+ geo_type = 'msa' , geo_value = msa [ 0 ], stderr = None , sample_size = None ,
388
401
missing_stderr = Nans .OTHER .value , missing_sample_size = Nans .OTHER .value
389
402
)
390
403
self ._insert_rows ([row ])
@@ -405,7 +418,7 @@ def test_temporal_partitioning(self):
405
418
406
419
# insert placeholder data
407
420
rows = [
408
- self ._make_placeholder_row (time_type = tt )[0 ]
421
+ self ._make_placeholder_row (geo_type = 'msa' , geo_value = msa [ 0 ], time_type = tt )[0 ]
409
422
for tt in "hour day week month year" .split ()
410
423
]
411
424
self ._insert_rows (rows )
0 commit comments