|
| 1 | +from collections.abc import Awaitable, Callable |
| 2 | + |
1 | 3 | import httpx
|
2 | 4 | import pytest
|
3 | 5 | from pydantic import BaseModel
|
4 |
| -from pytest_httpx import HTTPXMock |
| 6 | +from pytest_httpx import HTTPXMock, IteratorStream |
5 | 7 |
|
6 | 8 | from workflowai.core.client._api import APIClient
|
7 | 9 | from workflowai.core.domain.errors import WorkflowAIError
|
@@ -60,26 +62,95 @@ def test_extract_error_with_custom_error(self):
|
60 | 62 | assert e.value.response == response
|
61 | 63 |
|
62 | 64 |
|
63 |
| -async def test_stream_404(httpx_mock: HTTPXMock): |
64 |
| - class TestInputModel(BaseModel): |
65 |
| - test_input: str |
66 |
| - |
67 |
| - class TestOutputModel(BaseModel): |
68 |
| - test_output: str |
69 |
| - |
70 |
| - httpx_mock.add_response(status_code=404) |
71 |
| - |
72 |
| - client = APIClient(endpoint="https://blabla.com", api_key="test_api_key") |
| 65 | +@pytest.fixture |
| 66 | +def client() -> APIClient: |
| 67 | + return APIClient(endpoint="https://blabla.com", api_key="test_api_key") |
| 68 | + |
| 69 | + |
| 70 | +class TestInputModel(BaseModel): |
| 71 | + bla: str = "bla" |
| 72 | + |
| 73 | + |
| 74 | +class TestOutputModel(BaseModel): |
| 75 | + a: str |
| 76 | + |
| 77 | + |
| 78 | +class TestAPIClientStream: |
| 79 | + async def test_stream_404(self, httpx_mock: HTTPXMock, client: APIClient): |
| 80 | + class TestInputModel(BaseModel): |
| 81 | + test_input: str |
| 82 | + |
| 83 | + class TestOutputModel(BaseModel): |
| 84 | + test_output: str |
| 85 | + |
| 86 | + httpx_mock.add_response(status_code=404) |
| 87 | + |
| 88 | + with pytest.raises(WorkflowAIError) as e: # noqa: PT012 |
| 89 | + async for _ in client.stream( |
| 90 | + method="GET", |
| 91 | + path="test_path", |
| 92 | + data=TestInputModel(test_input="test"), |
| 93 | + returns=TestOutputModel, |
| 94 | + ): |
| 95 | + pass |
| 96 | + |
| 97 | + assert e.value.response |
| 98 | + assert e.value.response.status_code == 404 |
| 99 | + assert e.value.response.reason_phrase == "Not Found" |
| 100 | + |
| 101 | + @pytest.fixture |
| 102 | + async def stream_fn(self, client: APIClient): |
| 103 | + async def _stm(): |
| 104 | + return [ |
| 105 | + chunk |
| 106 | + async for chunk in client.stream( |
| 107 | + method="GET", |
| 108 | + path="test_path", |
| 109 | + data=TestInputModel(), |
| 110 | + returns=TestOutputModel, |
| 111 | + ) |
| 112 | + ] |
| 113 | + |
| 114 | + return _stm |
| 115 | + |
| 116 | + async def test_stream_with_single_chunk( |
| 117 | + self, |
| 118 | + stream_fn: Callable[[], Awaitable[list[TestOutputModel]]], |
| 119 | + httpx_mock: HTTPXMock, |
| 120 | + ): |
| 121 | + httpx_mock.add_response( |
| 122 | + stream=IteratorStream( |
| 123 | + [ |
| 124 | + b'data: {"a":"test"}\n\n', |
| 125 | + ], |
| 126 | + ), |
| 127 | + ) |
73 | 128 |
|
74 |
| - try: |
75 |
| - async for _ in client.stream( |
76 |
| - method="GET", |
77 |
| - path="test_path", |
78 |
| - data=TestInputModel(test_input="test"), |
79 |
| - returns=TestOutputModel, |
80 |
| - ): |
81 |
| - pass |
82 |
| - except httpx.HTTPStatusError as e: |
83 |
| - assert isinstance(e, httpx.HTTPStatusError) |
84 |
| - assert e.response.status_code == 404 |
85 |
| - assert e.response.reason_phrase == "Not Found" |
| 129 | + chunks = await stream_fn() |
| 130 | + assert chunks == [TestOutputModel(a="test")] |
| 131 | + |
| 132 | + @pytest.mark.parametrize( |
| 133 | + "streamed_chunks", |
| 134 | + [ |
| 135 | + # 2 perfect chunks([b'data: {"a":"test"}\n\n', b'data: {"a":"test2"}\n\n'],), |
| 136 | + [b'data: {"a":"test"}\n\n', b'data: {"a":"test2"}\n\n'], |
| 137 | + # 2 chunks in one |
| 138 | + [b'data: {"a":"test"}\n\ndata: {"a":"test2"}\n\n'], |
| 139 | + # Split not at the end |
| 140 | + [b'data: {"a":"test"}', b'\n\ndata: {"a":"test2"}\n\n'], |
| 141 | + # Really messy |
| 142 | + [b"dat", b'a: {"a":"', b'test"}', b"\n", b"\ndata", b': {"a":"test2"}\n\n'], |
| 143 | + ], |
| 144 | + ) |
| 145 | + async def test_stream_with_multiple_chunks( |
| 146 | + self, |
| 147 | + stream_fn: Callable[[], Awaitable[list[TestOutputModel]]], |
| 148 | + httpx_mock: HTTPXMock, |
| 149 | + streamed_chunks: list[bytes], |
| 150 | + ): |
| 151 | + assert isinstance(streamed_chunks, list), "sanity check" |
| 152 | + assert all(isinstance(chunk, bytes) for chunk in streamed_chunks), "sanity check" |
| 153 | + |
| 154 | + httpx_mock.add_response(stream=IteratorStream(streamed_chunks)) |
| 155 | + chunks = await stream_fn() |
| 156 | + assert chunks == [TestOutputModel(a="test"), TestOutputModel(a="test2")] |
0 commit comments