|
| 1 | +import httpx |
| 2 | +import pytest |
| 3 | + |
| 4 | +from enapter.http.api.errors import Error, MultiError, check_error |
| 5 | + |
| 6 | + |
| 7 | +@pytest.mark.asyncio |
| 8 | +async def test_check_error_success(): |
| 9 | + """Test that check_error does nothing when response is successful.""" |
| 10 | + response = httpx.Response(status_code=200, request=httpx.Request("GET", "https://example.com")) |
| 11 | + await check_error(response) |
| 12 | + |
| 13 | + |
| 14 | +@pytest.mark.asyncio |
| 15 | +async def test_check_error_non_json(): |
| 16 | + """Test that check_error calls raise_for_status when response is not JSON.""" |
| 17 | + response = httpx.Response(status_code=500, content=b"Internal Server Error", request=httpx.Request("GET", "https://example.com")) |
| 18 | + with pytest.raises(httpx.HTTPStatusError): |
| 19 | + await check_error(response) |
| 20 | + |
| 21 | + |
| 22 | +@pytest.mark.asyncio |
| 23 | +async def test_check_error_api_error_single(): |
| 24 | + """Test that check_error raises a single Error when one is present in DTO.""" |
| 25 | + dto = { |
| 26 | + "errors": [ |
| 27 | + { |
| 28 | + "message": "Device not found", |
| 29 | + "code": "DEVICE_NOT_FOUND", |
| 30 | + "details": {"device_id": "test_device"}, |
| 31 | + } |
| 32 | + ] |
| 33 | + } |
| 34 | + response = httpx.Response(status_code=404, json=dto, request=httpx.Request("GET", "https://example.com")) |
| 35 | + with pytest.raises(Error) as exc_info: |
| 36 | + await check_error(response) |
| 37 | + |
| 38 | + assert exc_info.value.message == "Device not found" |
| 39 | + assert exc_info.value.code == "DEVICE_NOT_FOUND" |
| 40 | + assert exc_info.value.details == {"device_id": "test_device"} |
| 41 | + |
| 42 | + |
| 43 | +@pytest.mark.asyncio |
| 44 | +async def test_check_error_api_error_multiple(): |
| 45 | + """Test that check_error raises MultiError when multiple are present in DTO.""" |
| 46 | + dto = { |
| 47 | + "errors": [ |
| 48 | + {"message": "Invalid parameter A", "code": "INVALID_PARAM"}, |
| 49 | + {"message": "Invalid parameter B", "code": "INVALID_PARAM"}, |
| 50 | + ] |
| 51 | + } |
| 52 | + response = httpx.Response(status_code=400, json=dto, request=httpx.Request("GET", "https://example.com")) |
| 53 | + with pytest.raises(MultiError) as exc_info: |
| 54 | + await check_error(response) |
| 55 | + |
| 56 | + assert len(exc_info.value.errors) == 2 |
| 57 | + assert exc_info.value.errors[0].message == "Invalid parameter A" |
| 58 | + assert exc_info.value.errors[1].message == "Invalid parameter B" |
| 59 | + |
| 60 | + |
| 61 | +def test_error_from_dto_empty(): |
| 62 | + """Test Error.from_dto with minimal fields.""" |
| 63 | + err = Error.from_dto({}) |
| 64 | + assert err.message == "<no message>" |
| 65 | + assert err.code is None |
| 66 | + assert err.details is None |
| 67 | + |
| 68 | + |
| 69 | +def test_multi_error_from_dto_empty_list(): |
| 70 | + """Test MultiError.from_dto raises ValueError on empty error list.""" |
| 71 | + with pytest.raises(ValueError, match="empty error list"): |
| 72 | + MultiError.from_dto({"errors": []}) |
0 commit comments