|
12 | 12 | import pytest
|
13 | 13 | from respx import MockRouter
|
14 | 14 |
|
15 |
| -from finch import Finch, AsyncFinch |
| 15 | +from finch import Finch, AsyncFinch, APIResponseValidationError |
16 | 16 | from finch._types import Omit
|
17 | 17 | from finch._models import BaseModel, FinalRequestOptions
|
18 | 18 | from finch._base_client import BaseClient, make_request_options
|
@@ -385,6 +385,23 @@ def test_client_context_manager(self) -> None:
|
385 | 385 | assert not client.is_closed()
|
386 | 386 | assert client.is_closed()
|
387 | 387 |
|
| 388 | + @pytest.mark.respx(base_url=base_url) |
| 389 | + def test_received_text_for_expected_json(self, respx_mock: MockRouter) -> None: |
| 390 | + class Model(BaseModel): |
| 391 | + name: str |
| 392 | + |
| 393 | + respx_mock.get("/foo").mock(return_value=httpx.Response(200, text="my-custom-format")) |
| 394 | + |
| 395 | + strict_client = Finch(base_url=base_url, access_token=access_token, _strict_response_validation=True) |
| 396 | + |
| 397 | + with pytest.raises(APIResponseValidationError): |
| 398 | + strict_client.get("/foo", cast_to=Model) |
| 399 | + |
| 400 | + client = Finch(base_url=base_url, access_token=access_token, _strict_response_validation=False) |
| 401 | + |
| 402 | + response = client.get("/foo", cast_to=Model) |
| 403 | + assert isinstance(response, str) # type: ignore[unreachable] |
| 404 | + |
388 | 405 |
|
389 | 406 | class TestAsyncFinch:
|
390 | 407 | client = AsyncFinch(base_url=base_url, access_token=access_token, _strict_response_validation=True)
|
@@ -744,3 +761,21 @@ async def test_client_context_manager(self) -> None:
|
744 | 761 | assert not c2.is_closed()
|
745 | 762 | assert not client.is_closed()
|
746 | 763 | assert client.is_closed()
|
| 764 | + |
| 765 | + @pytest.mark.respx(base_url=base_url) |
| 766 | + @pytest.mark.asyncio |
| 767 | + async def test_received_text_for_expected_json(self, respx_mock: MockRouter) -> None: |
| 768 | + class Model(BaseModel): |
| 769 | + name: str |
| 770 | + |
| 771 | + respx_mock.get("/foo").mock(return_value=httpx.Response(200, text="my-custom-format")) |
| 772 | + |
| 773 | + strict_client = AsyncFinch(base_url=base_url, access_token=access_token, _strict_response_validation=True) |
| 774 | + |
| 775 | + with pytest.raises(APIResponseValidationError): |
| 776 | + await strict_client.get("/foo", cast_to=Model) |
| 777 | + |
| 778 | + client = AsyncFinch(base_url=base_url, access_token=access_token, _strict_response_validation=False) |
| 779 | + |
| 780 | + response = await client.get("/foo", cast_to=Model) |
| 781 | + assert isinstance(response, str) # type: ignore[unreachable] |
0 commit comments