|
3 | 3 | # standard library
|
4 | 4 | import time
|
5 | 5 | from json import JSONDecodeError
|
| 6 | +from requests.models import Response |
6 | 7 | from unittest.mock import MagicMock, patch
|
7 | 8 |
|
8 | 9 | # first party
|
@@ -41,6 +42,8 @@ def localSetUp(self):
|
41 | 42 | # use the local instance of the Epidata API
|
42 | 43 | Epidata.BASE_URL = 'http://delphi_web_epidata/epidata'
|
43 | 44 | Epidata.auth = ('epidata', 'key')
|
| 45 | + Epidata.debug = False |
| 46 | + Epidata.sandbox = False |
44 | 47 |
|
45 | 48 | # use the local instance of the epidata database
|
46 | 49 | secrets.db.host = 'delphi_database_epidata'
|
@@ -221,6 +224,65 @@ def test_retry_request(self, get):
|
221 | 224 | {'result': 0, 'message': 'error: Expecting value: line 1 column 1 (char 0)'}
|
222 | 225 | )
|
223 | 226 |
|
| 227 | + @patch('requests.post') |
| 228 | + @patch('requests.get') |
| 229 | + def test_debug(self, get, post): |
| 230 | + """Test that in debug mode request params are correctly logged.""" |
| 231 | + class MockResponse: |
| 232 | + def __init__(self, content, status_code): |
| 233 | + self.content = content |
| 234 | + self.status_code = status_code |
| 235 | + def raise_for_status(self): pass |
| 236 | + |
| 237 | + Epidata.debug = True |
| 238 | + |
| 239 | + with self.subTest(name='test multiple GET'): |
| 240 | + with self.assertLogs('delphi_epidata_client', level='INFO') as logs: |
| 241 | + get.reset_mock() |
| 242 | + get.return_value = MockResponse(b'{"key": "value"}', 200) |
| 243 | + Epidata._request_with_retry("test_endpoint1", params={"key1": "value1"}) |
| 244 | + Epidata._request_with_retry("test_endpoint2", params={"key2": "value2"}) |
| 245 | + |
| 246 | + output = logs.output |
| 247 | + self.assertEqual(len(output), 4) # [request, response, request, response] |
| 248 | + self.assertIn("Sending GET request to URL: http://delphi_web_epidata/epidata/test_endpoint1/", output[0]) |
| 249 | + self.assertIn("params: {'key1': 'value1'}", output[0]) |
| 250 | + self.assertIn("Received 200 response (16 bytes)", output[1]) |
| 251 | + self.assertIn("Sending GET request to URL: http://delphi_web_epidata/epidata/test_endpoint2/", output[2]) |
| 252 | + self.assertIn("params: {'key2': 'value2'}", output[2]) |
| 253 | + self.assertIn("Received 200 response (16 bytes)", output[3]) |
| 254 | + |
| 255 | + with self.subTest(name='test GET and POST'): |
| 256 | + with self.assertLogs('delphi_epidata_client', level='INFO') as logs: |
| 257 | + get.reset_mock() |
| 258 | + get.return_value = MockResponse(b'{"key": "value"}', 414) |
| 259 | + post.reset_mock() |
| 260 | + post.return_value = MockResponse(b'{"key": "value"}', 200) |
| 261 | + Epidata._request_with_retry("test_endpoint3", params={"key3": "value3"}) |
| 262 | + |
| 263 | + output = logs.output |
| 264 | + self.assertEqual(len(output), 4) # [request, response, request, response] |
| 265 | + self.assertIn("Sending GET request to URL: http://delphi_web_epidata/epidata/test_endpoint3/", output[0]) |
| 266 | + self.assertIn("params: {'key3': 'value3'}", output[0]) |
| 267 | + self.assertIn("Received 414 response (16 bytes)", output[1]) |
| 268 | + self.assertIn("Sending POST request to URL: http://delphi_web_epidata/epidata/test_endpoint3/", output[2]) |
| 269 | + self.assertIn("params: {'key3': 'value3'}", output[2]) |
| 270 | + self.assertIn("Received 200 response (16 bytes)", output[3]) |
| 271 | + |
| 272 | + @patch('requests.post') |
| 273 | + @patch('requests.get') |
| 274 | + def test_sandbox(self, get, post): |
| 275 | + """Test that in debug + sandbox mode request params are correctly logged, but no queries are sent.""" |
| 276 | + Epidata.debug = True |
| 277 | + Epidata.sandbox = True |
| 278 | + with self.assertLogs('delphi_epidata_client', level='INFO') as logs: |
| 279 | + Epidata.covidcast('src', 'sig', 'day', 'county', 20200414, '01234') |
| 280 | + output = logs.output |
| 281 | + self.assertEqual(len(output), 1) |
| 282 | + self.assertIn("Sending GET request to URL: http://delphi_web_epidata/epidata/covidcast/", output[0]) |
| 283 | + get.assert_not_called() |
| 284 | + post.assert_not_called() |
| 285 | + |
224 | 286 | def test_geo_value(self):
|
225 | 287 | """test different variants of geo types: single, *, multi."""
|
226 | 288 |
|
|
0 commit comments