|
26 | 26 | deserialize_stream_record, |
27 | 27 | ) |
28 | 28 | from mpt_api_client.models import DeletionStub, Model |
29 | | -from tests.unit.conftest import API_URL, DummyModel |
| 29 | +from tests.unit.conftest import API_URL, ERROR_BODY_SHAPES, DummyModel |
30 | 30 | from tests.unit.http.conftest import ( |
31 | 31 | JSON_LEGAL_IN_STRING, |
32 | 32 | KEEPALIVE_LINE, |
@@ -786,6 +786,42 @@ async def test_async_stream_raises_when_over_cap(async_streaming_service): |
786 | 786 | assert raised.value.payload == over_cap_problem() |
787 | 787 |
|
788 | 788 |
|
| 789 | +# Every status the streaming reader maps to a typed error reads its diagnostic off the |
| 790 | +# error body, so the mapping has to survive an error body of any shape. |
| 791 | +STREAMING_ERROR_STATUSES = ( |
| 792 | + pytest.param(httpx.codes.NOT_IMPLEMENTED, MPTStreamingNotSupportedError, id="501"), |
| 793 | + pytest.param(httpx.codes.NOT_ACCEPTABLE, MPTStreamingNotAcceptableError, id="406"), |
| 794 | + pytest.param(httpx.codes.REQUEST_ENTITY_TOO_LARGE, MPTStreamingOverCapError, id="413"), |
| 795 | +) |
| 796 | + |
| 797 | + |
| 798 | +@pytest.mark.parametrize("body", ERROR_BODY_SHAPES) |
| 799 | +@pytest.mark.parametrize(("status_code", "error_class"), STREAMING_ERROR_STATUSES) |
| 800 | +@respx.mock |
| 801 | +def test_stream_maps_status_per_body_shape(streaming_service, status_code, error_class, body): |
| 802 | + respx.get(STREAM_URL).mock(return_value=httpx.Response(status_code, content=body)) |
| 803 | + iterator = streaming_service.stream() |
| 804 | + |
| 805 | + with pytest.raises(error_class) as raised: |
| 806 | + next(iterator) |
| 807 | + |
| 808 | + assert raised.value.status_code == status_code |
| 809 | + |
| 810 | + |
| 811 | +@pytest.mark.parametrize("body", ERROR_BODY_SHAPES) |
| 812 | +@respx.mock |
| 813 | +async def test_async_stream_over_cap_body_shape(async_streaming_service, body): |
| 814 | + respx.get(STREAM_URL).mock( |
| 815 | + return_value=httpx.Response(httpx.codes.REQUEST_ENTITY_TOO_LARGE, content=body) |
| 816 | + ) |
| 817 | + iterator = async_streaming_service.stream() |
| 818 | + |
| 819 | + with pytest.raises(MPTStreamingOverCapError) as raised: |
| 820 | + await anext(iterator) |
| 821 | + |
| 822 | + assert raised.value.status_code == httpx.codes.REQUEST_ENTITY_TOO_LARGE |
| 823 | + |
| 824 | + |
789 | 825 | @respx.mock |
790 | 826 | def test_stream_yields_stub_for_deleted_row( |
791 | 827 | nullable_fields_service, deletion_stub_record, data_record |
|
0 commit comments