|
1 | | -from databento.historical.error import BentoClientError, BentoServerError |
2 | | - |
3 | | - |
4 | | -class TestHistoricalHttpError: |
5 | | - def test_client_error_str_and_repr(self) -> None: |
6 | | - # Arrange, Act |
7 | | - error = BentoClientError( |
8 | | - http_status=400, |
9 | | - http_body=None, |
10 | | - message="Bad Request", |
11 | | - ) |
12 | | - |
13 | | - # Assert |
14 | | - assert str(error) == "400 Bad Request" |
15 | | - assert ( |
16 | | - repr(error) |
17 | | - == "BentoClientError(request_id=None, http_status=400, message=Bad Request)" # noqa |
18 | | - ) |
19 | | - |
20 | | - def test_server_error_str_and_repr(self) -> None: |
21 | | - # Arrange, Act |
22 | | - error = BentoServerError( |
23 | | - http_status=500, |
24 | | - http_body=None, |
25 | | - message="Internal Server Error", |
26 | | - ) |
27 | | - |
28 | | - # Assert |
29 | | - assert str(error) == "500 Internal Server Error" |
30 | | - assert ( |
31 | | - repr(error) |
32 | | - == "BentoServerError(request_id=None, http_status=500, message=Internal Server Error)" # noqa |
33 | | - ) |
| 1 | +import sys |
| 2 | +from typing import Type |
| 3 | +from unittest.mock import MagicMock |
| 4 | + |
| 5 | +import aiohttp |
| 6 | +import pytest |
| 7 | +import requests |
| 8 | +from databento.common.error import BentoClientError, BentoServerError |
| 9 | +from databento.historical.http import check_http_error, check_http_error_async |
| 10 | + |
| 11 | + |
| 12 | +@pytest.mark.parametrize( |
| 13 | + "status_code, expected_exception, message", |
| 14 | + [ |
| 15 | + pytest.param(404, BentoClientError, r"", id="404"), |
| 16 | + pytest.param(408, BentoClientError, r"timed out.$", id="408"), |
| 17 | + pytest.param(500, BentoServerError, r"", id="500"), |
| 18 | + pytest.param(504, BentoServerError, r"timed out.$", id="504"), |
| 19 | + ], |
| 20 | +) |
| 21 | +def test_check_http_status( |
| 22 | + status_code: int, |
| 23 | + expected_exception: Type[Exception], |
| 24 | + message: str, |
| 25 | +) -> None: |
| 26 | + """ |
| 27 | + Test that responses with the given status code raise the expected exception. |
| 28 | + """ |
| 29 | + response = requests.Response() |
| 30 | + response.status_code = status_code |
| 31 | + with pytest.raises(expected_exception) as exc: |
| 32 | + check_http_error(response) |
| 33 | + |
| 34 | + exc.match(message) |
| 35 | + |
| 36 | + |
| 37 | +@pytest.mark.asyncio |
| 38 | +@pytest.mark.skipif(sys.version_info < (3, 8), reason="no async support for MagicMock") |
| 39 | +@pytest.mark.parametrize( |
| 40 | + "status_code, expected_exception, message", |
| 41 | + [ |
| 42 | + pytest.param(404, BentoClientError, r"", id="404"), |
| 43 | + pytest.param(408, BentoClientError, r"timed out.$", id="408"), |
| 44 | + pytest.param(500, BentoServerError, r"", id="500"), |
| 45 | + pytest.param(504, BentoServerError, r"timed out.$", id="504"), |
| 46 | + ], |
| 47 | +) |
| 48 | +async def test_check_http_status_async( |
| 49 | + status_code: int, |
| 50 | + expected_exception: Type[Exception], |
| 51 | + message: str, |
| 52 | +) -> None: |
| 53 | + """ |
| 54 | + Test that responses with the given status code raise the expected exception. |
| 55 | + """ |
| 56 | + response = MagicMock(spec=aiohttp.ClientResponse) |
| 57 | + response.status = status_code |
| 58 | + with pytest.raises(expected_exception) as exc: |
| 59 | + await check_http_error_async(response) |
| 60 | + |
| 61 | + exc.match(message) |
| 62 | + |
| 63 | + |
| 64 | +def test_client_error_str_and_repr() -> None: |
| 65 | + # Arrange, Act |
| 66 | + error = BentoClientError( |
| 67 | + http_status=400, |
| 68 | + http_body=None, |
| 69 | + message="Bad Request", |
| 70 | + ) |
| 71 | + |
| 72 | + # Assert |
| 73 | + assert str(error) == "400 Bad Request" |
| 74 | + assert ( |
| 75 | + repr(error) |
| 76 | + == "BentoClientError(request_id=None, http_status=400, message=Bad Request)" # noqa |
| 77 | + ) |
| 78 | + |
| 79 | + |
| 80 | +def test_server_error_str_and_repr() -> None: |
| 81 | + # Arrange, Act |
| 82 | + error = BentoServerError( |
| 83 | + http_status=500, |
| 84 | + http_body=None, |
| 85 | + message="Internal Server Error", |
| 86 | + ) |
| 87 | + |
| 88 | + # Assert |
| 89 | + assert str(error) == "500 Internal Server Error" |
| 90 | + assert ( |
| 91 | + repr(error) |
| 92 | + == "BentoServerError(request_id=None, http_status=500, message=Internal Server Error)" # noqa |
| 93 | + ) |
0 commit comments