@@ -49,6 +49,12 @@ def localSetUp(self):
49
49
secrets .db .host = 'delphi_database_epidata'
50
50
secrets .db .epi = ('user' , 'pass' )
51
51
52
+ @pytest .fixture (autouse = True )
53
+ def capsys (self , capsys ):
54
+ """Hook capsys (stdout and stderr) into this test class."""
55
+
56
+ self .capsys = capsys
57
+
52
58
def test_covidcast (self ):
53
59
"""Test that the covidcast endpoint returns expected data."""
54
60
@@ -238,46 +244,46 @@ def raise_for_status(self): pass
238
244
239
245
try :
240
246
with self .subTest (name = 'test multiple GET' ):
241
- with self .assertLogs ('delphi_epidata_client' , level = 'INFO' ) as logs :
242
- get .reset_mock ()
243
- get .return_value = MockResponse (b'{"key": "value"}' , 200 )
244
- Epidata ._request_with_retry ("test_endpoint1" , params = {"key1" : "value1" })
245
- Epidata ._request_with_retry ("test_endpoint2" , params = {"key2" : "value2" })
247
+ get .reset_mock ()
248
+ get .return_value = MockResponse (b'{"key": "value"}' , 200 )
249
+ Epidata ._request_with_retry ("test_endpoint1" , params = {"key1" : "value1" })
250
+ Epidata ._request_with_retry ("test_endpoint2" , params = {"key2" : "value2" })
246
251
247
- output = logs .output
252
+ captured = self .capsys .readouterr ()
253
+ output = captured .err .splitlines ()
248
254
self .assertEqual (len (output ), 4 ) # [request, response, request, response]
249
255
self .assertIn ("Sending GET request" , output [0 ])
250
- self .assertIn ("\" url\" : \" http://delphi_web_epidata/epidata/test_endpoint1/\" " , output [0 ])
251
- self .assertIn ("\" params\" : {\" key1\" : \" value1\" }" , output [0 ])
256
+ self .assertIn ("\' url\' : \' http://delphi_web_epidata/epidata/test_endpoint1/\' " , output [0 ])
257
+ self .assertIn ("\' params\' : {\' key1\' : \' value1\' }" , output [0 ])
252
258
self .assertIn ("Received response" , output [1 ])
253
- self .assertIn ("\" status_code\" : 200" , output [1 ])
254
- self .assertIn ("\" len\" : 16" , output [1 ])
259
+ self .assertIn ("\' status_code\' : 200" , output [1 ])
260
+ self .assertIn ("\' len\' : 16" , output [1 ])
255
261
self .assertIn ("Sending GET request" , output [2 ])
256
- self .assertIn ("\" url\" : \" http://delphi_web_epidata/epidata/test_endpoint2/\" " , output [2 ])
257
- self .assertIn ("\" params\" : {\" key2\" : \" value2\" }" , output [2 ])
262
+ self .assertIn ("\' url\' : \' http://delphi_web_epidata/epidata/test_endpoint2/\' " , output [2 ])
263
+ self .assertIn ("\' params\' : {\' key2\' : \' value2\' }" , output [2 ])
258
264
self .assertIn ("Received response" , output [3 ])
259
- self .assertIn ("\" status_code\" : 200" , output [3 ])
260
- self .assertIn ("\" len\" : 16" , output [3 ])
265
+ self .assertIn ("\' status_code\' : 200" , output [3 ])
266
+ self .assertIn ("\' len\' : 16" , output [3 ])
261
267
262
268
with self .subTest (name = 'test GET and POST' ):
263
- with self . assertLogs ( 'delphi_epidata_client' , level = 'INFO' ) as logs :
264
- get .reset_mock ( )
265
- get . return_value = MockResponse ( b'{"key": "value"}' , 414 )
266
- post .reset_mock ( )
267
- post . return_value = MockResponse ( b'{"key ": "value"}' , 200 )
268
- Epidata . _request_with_retry ( "test_endpoint3" , params = { "key3" : "value3" })
269
-
270
- output = logs . output
271
- self .assertEqual (len (output ), 3 ) # [request, response, request , response]
269
+ get . reset_mock ()
270
+ get .return_value = MockResponse ( b'{"key": "value"}' , 414 )
271
+ post . reset_mock ( )
272
+ post .return_value = MockResponse ( b'{"key": "value"}' , 200 )
273
+ Epidata . _request_with_retry ( "test_endpoint3" , params = { "key3 " : "value3" } )
274
+
275
+ captured = self . capsys . readouterr ()
276
+ output = captured . err . splitlines ()
277
+ self .assertEqual (len (output ), 3 ) # [request, retry , response]
272
278
self .assertIn ("Sending GET request" , output [0 ])
273
- self .assertIn ("\" url\" : \" http://delphi_web_epidata/epidata/test_endpoint3/\" " , output [0 ])
274
- self .assertIn ("\" params\" : {\" key3\" : \" value3\" }" , output [0 ])
279
+ self .assertIn ("\' url\' : \' http://delphi_web_epidata/epidata/test_endpoint3/\' " , output [0 ])
280
+ self .assertIn ("\' params\' : {\' key3\' : \' value3\' }" , output [0 ])
275
281
self .assertIn ("Received 414 response, retrying as POST request" , output [1 ])
276
- self .assertIn ("\" url\" : \" http://delphi_web_epidata/epidata/test_endpoint3/\" " , output [1 ])
277
- self .assertIn ("\" params\" : {\" key3\" : \" value3\" }" , output [1 ])
282
+ self .assertIn ("\' url\' : \' http://delphi_web_epidata/epidata/test_endpoint3/\' " , output [1 ])
283
+ self .assertIn ("\' params\' : {\' key3\' : \' value3\' }" , output [1 ])
278
284
self .assertIn ("Received response" , output [2 ])
279
- self .assertIn ("\" status_code\" : 200" , output [2 ])
280
- self .assertIn ("\" len\" : 16" , output [2 ])
285
+ self .assertIn ("\' status_code\' : 200" , output [2 ])
286
+ self .assertIn ("\' len\' : 16" , output [2 ])
281
287
finally : # make sure this global is always reset
282
288
Epidata .debug = False
283
289
@@ -288,12 +294,12 @@ def test_sandbox(self, get, post):
288
294
Epidata .debug = True
289
295
Epidata .sandbox = True
290
296
try :
291
- with self . assertLogs ( 'delphi_epidata_client ' , level = 'INFO' ) as logs :
292
- Epidata . covidcast ( 'src' , 'sig' , 'day' , 'county' , 20200414 , '01234' )
293
- output = logs . output
297
+ Epidata . covidcast ( 'src ' , 'sig' , 'day' , 'county' , 20200414 , '01234' )
298
+ captured = self . capsys . readouterr ( )
299
+ output = captured . err . splitlines ()
294
300
self .assertEqual (len (output ), 1 )
295
301
self .assertIn ("Sending GET request" , output [0 ])
296
- self .assertIn ("\" url\" : \" http://delphi_web_epidata/epidata/covidcast/\" " , output [0 ])
302
+ self .assertIn ("\' url\' : \' http://delphi_web_epidata/epidata/covidcast/\' " , output [0 ])
297
303
get .assert_not_called ()
298
304
post .assert_not_called ()
299
305
finally : # make sure these globals are always reset
0 commit comments