24
24
def fake_epidata_endpoint (func ):
25
25
"""This can be used as a decorator to enable a bogus Epidata endpoint to return 404 responses."""
26
26
def wrapper (* args ):
27
- Epidata .BASE_URL = 'http://delphi_web_epidata/epidata/fake_api.php '
27
+ Epidata .BASE_URL = 'http://delphi_web_epidata/fake_epidata '
28
28
func (* args )
29
- Epidata .BASE_URL = 'http://delphi_web_epidata/epidata/api.php '
29
+ Epidata .BASE_URL = 'http://delphi_web_epidata/epidata'
30
30
return wrapper
31
31
32
32
class DelphiEpidataPythonClientTests (CovidcastBase ):
@@ -39,7 +39,7 @@ def localSetUp(self):
39
39
self ._db ._cursor .execute ('update covidcast_meta_cache set timestamp = 0, epidata = "[]"' )
40
40
41
41
# use the local instance of the Epidata API
42
- Epidata .BASE_URL = 'http://delphi_web_epidata/epidata/api.php '
42
+ Epidata .BASE_URL = 'http://delphi_web_epidata/epidata'
43
43
Epidata .auth = ('epidata' , 'key' )
44
44
45
45
# use the local instance of the epidata database
@@ -65,8 +65,8 @@ def test_covidcast(self):
65
65
)
66
66
67
67
expected = [
68
- row_latest_issue .as_api_compatibility_row_dict (),
69
- rows [- 1 ].as_api_compatibility_row_dict ()
68
+ row_latest_issue .as_api_row_dict (),
69
+ rows [- 1 ].as_api_row_dict ()
70
70
]
71
71
72
72
self .assertEqual (response ['epidata' ], expected )
@@ -85,10 +85,10 @@ def test_covidcast(self):
85
85
86
86
expected = [{
87
87
rows [0 ].signal : [
88
- row_latest_issue .as_api_compatibility_row_dict (ignore_fields = ['signal' ]),
88
+ row_latest_issue .as_api_row_dict (ignore_fields = ['signal' ]),
89
89
],
90
90
rows [- 1 ].signal : [
91
- rows [- 1 ].as_api_compatibility_row_dict (ignore_fields = ['signal' ]),
91
+ rows [- 1 ].as_api_row_dict (ignore_fields = ['signal' ]),
92
92
],
93
93
}]
94
94
@@ -105,7 +105,7 @@ def test_covidcast(self):
105
105
** self .params_from_row (rows [0 ])
106
106
)
107
107
108
- expected = [row_latest_issue .as_api_compatibility_row_dict ()]
108
+ expected = [row_latest_issue .as_api_row_dict ()]
109
109
110
110
# check result
111
111
self .assertEqual (response_1 , {
@@ -120,7 +120,7 @@ def test_covidcast(self):
120
120
** self .params_from_row (rows [0 ], as_of = rows [1 ].issue )
121
121
)
122
122
123
- expected = [rows [1 ].as_api_compatibility_row_dict ()]
123
+ expected = [rows [1 ].as_api_row_dict ()]
124
124
125
125
# check result
126
126
self .maxDiff = None
@@ -130,15 +130,23 @@ def test_covidcast(self):
130
130
'message' : 'success' ,
131
131
})
132
132
133
+ with self .subTest (name = 'bad as-of date' ):
134
+ # fetch data, specifying as_of
135
+ as_of_response = Epidata .covidcast (
136
+ ** self .params_from_row (rows [0 ], as_of = "20230101-20230102" )
137
+ )
138
+ self .assertEqual (as_of_response , {"epidata" : [], "message" : "not a valid date: 20230101-20230102" , "result" : - 1 })
139
+
140
+
133
141
with self .subTest (name = 'request a range of issues' ):
134
142
# fetch data, specifying issue range, not lag
135
143
response_2 = Epidata .covidcast (
136
144
** self .params_from_row (rows [0 ], issues = Epidata .range (rows [0 ].issue , rows [1 ].issue ))
137
145
)
138
146
139
147
expected = [
140
- rows [0 ].as_api_compatibility_row_dict (),
141
- rows [1 ].as_api_compatibility_row_dict ()
148
+ rows [0 ].as_api_row_dict (),
149
+ rows [1 ].as_api_row_dict ()
142
150
]
143
151
144
152
# check result
@@ -154,7 +162,7 @@ def test_covidcast(self):
154
162
** self .params_from_row (rows [0 ], lag = 2 )
155
163
)
156
164
157
- expected = [row_latest_issue .as_api_compatibility_row_dict ()]
165
+ expected = [row_latest_issue .as_api_row_dict ()]
158
166
159
167
# check result
160
168
self .assertDictEqual (response_3 , {
@@ -170,7 +178,7 @@ def test_covidcast(self):
170
178
)
171
179
172
180
# check result
173
- self .assertEqual (response_1 , {'message' : 'no results' , 'result' : - 2 })
181
+ self .assertEqual (response_1 , {'epidata' : [], ' message' : 'no results' , 'result' : - 2 })
174
182
175
183
@patch ('requests.post' )
176
184
@patch ('requests.get' )
@@ -196,7 +204,7 @@ def test_retry_request(self, get):
196
204
mock_response = MagicMock ()
197
205
mock_response .status_code = 200
198
206
get .side_effect = [JSONDecodeError ('Expecting value' , "" , 0 ), mock_response ]
199
- response = Epidata ._request (None )
207
+ response = Epidata ._request ("" )
200
208
self .assertEqual (get .call_count , 2 )
201
209
self .assertEqual (response , mock_response .json ())
202
210
@@ -207,7 +215,7 @@ def test_retry_request(self, get):
207
215
get .side_effect = [JSONDecodeError ('Expecting value' , "" , 0 ),
208
216
JSONDecodeError ('Expecting value' , "" , 0 ),
209
217
mock_response ]
210
- response = Epidata ._request (None )
218
+ response = Epidata ._request ("" )
211
219
self .assertEqual (get .call_count , 2 ) # 2 from previous test + 2 from this one
212
220
self .assertEqual (response ,
213
221
{'result' : 0 , 'message' : 'error: Expecting value: line 1 column 1 (char 0)' }
@@ -228,7 +236,7 @@ def test_geo_value(self):
228
236
self ._insert_rows (rows )
229
237
230
238
counties = [
231
- rows [i ].as_api_compatibility_row_dict () for i in range (N )
239
+ rows [i ].as_api_row_dict () for i in range (N )
232
240
]
233
241
234
242
def fetch (geo ):
@@ -339,13 +347,15 @@ def test_async_epidata(self):
339
347
self .params_from_row (rows [0 ], source = 'covidcast' ),
340
348
self .params_from_row (rows [1 ], source = 'covidcast' )
341
349
]* 12 , batch_size = 10 )
342
- responses = [i [0 ] for i in test_output ]
343
- # check response is same as standard covidcast call, using 24 calls to test batch sizing
350
+ responses = [i [0 ]["epidata" ] for i in test_output ]
351
+ # check response is same as standard covidcast call (minus fields omitted by the api.php endpoint),
352
+ # using 24 calls to test batch sizing
353
+ ignore_fields = CovidcastTestRow ._api_row_compatibility_ignore_fields
344
354
self .assertEqual (
345
355
responses ,
346
356
[
347
- Epidata .covidcast (** self .params_from_row (rows [0 ])),
348
- Epidata .covidcast (** self .params_from_row (rows [1 ])),
357
+ [{ k : row [ k ] for k in row . keys () - ignore_fields } for row in Epidata .covidcast (** self .params_from_row (rows [0 ]))[ "epidata" ]] ,
358
+ [{ k : row [ k ] for k in row . keys () - ignore_fields } for row in Epidata .covidcast (** self .params_from_row (rows [1 ]))[ "epidata" ]] ,
349
359
]* 12
350
360
)
351
361
0 commit comments