|
5 | 5 | import os |
6 | 6 | from typing import Any, cast |
7 | 7 |
|
| 8 | +import httpx |
8 | 9 | import pytest |
| 10 | +from respx import MockRouter |
9 | 11 |
|
10 | 12 | from groq import Groq, AsyncGroq |
11 | | -from groq.types import ( |
12 | | - FileInfoResponse, |
13 | | - FileListResponse, |
14 | | - FileCreateResponse, |
15 | | - FileDeleteResponse, |
16 | | -) |
| 13 | +from groq.types import FileInfoResponse, FileListResponse, FileCreateResponse, FileDeleteResponse |
17 | 14 | from tests.utils import assert_matches_type |
| 15 | +from groq._response import ( |
| 16 | + BinaryAPIResponse, |
| 17 | + AsyncBinaryAPIResponse, |
| 18 | + StreamedBinaryAPIResponse, |
| 19 | + AsyncStreamedBinaryAPIResponse, |
| 20 | +) |
18 | 21 |
|
19 | 22 | base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010") |
20 | 23 |
|
@@ -120,37 +123,49 @@ def test_path_params_delete(self, client: Groq) -> None: |
120 | 123 | ) |
121 | 124 |
|
122 | 125 | @parametrize |
123 | | - def test_method_content(self, client: Groq) -> None: |
| 126 | + @pytest.mark.respx(base_url=base_url) |
| 127 | + def test_method_content(self, client: Groq, respx_mock: MockRouter) -> None: |
| 128 | + respx_mock.get("/openai/v1/files/file_id/content").mock(return_value=httpx.Response(200, json={"foo": "bar"})) |
124 | 129 | file = client.files.content( |
125 | 130 | "file_id", |
126 | 131 | ) |
127 | | - assert_matches_type(str, file, path=["response"]) |
| 132 | + assert file.is_closed |
| 133 | + assert file.json() == {"foo": "bar"} |
| 134 | + assert cast(Any, file.is_closed) is True |
| 135 | + assert isinstance(file, BinaryAPIResponse) |
128 | 136 |
|
129 | 137 | @parametrize |
130 | | - def test_raw_response_content(self, client: Groq) -> None: |
131 | | - response = client.files.with_raw_response.content( |
| 138 | + @pytest.mark.respx(base_url=base_url) |
| 139 | + def test_raw_response_content(self, client: Groq, respx_mock: MockRouter) -> None: |
| 140 | + respx_mock.get("/openai/v1/files/file_id/content").mock(return_value=httpx.Response(200, json={"foo": "bar"})) |
| 141 | + |
| 142 | + file = client.files.with_raw_response.content( |
132 | 143 | "file_id", |
133 | 144 | ) |
134 | 145 |
|
135 | | - assert response.is_closed is True |
136 | | - assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
137 | | - file = response.parse() |
138 | | - assert_matches_type(str, file, path=["response"]) |
| 146 | + assert file.is_closed is True |
| 147 | + assert file.http_request.headers.get("X-Stainless-Lang") == "python" |
| 148 | + assert file.json() == {"foo": "bar"} |
| 149 | + assert isinstance(file, BinaryAPIResponse) |
139 | 150 |
|
140 | 151 | @parametrize |
141 | | - def test_streaming_response_content(self, client: Groq) -> None: |
| 152 | + @pytest.mark.respx(base_url=base_url) |
| 153 | + def test_streaming_response_content(self, client: Groq, respx_mock: MockRouter) -> None: |
| 154 | + respx_mock.get("/openai/v1/files/file_id/content").mock(return_value=httpx.Response(200, json={"foo": "bar"})) |
142 | 155 | with client.files.with_streaming_response.content( |
143 | 156 | "file_id", |
144 | | - ) as response: |
145 | | - assert not response.is_closed |
146 | | - assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 157 | + ) as file: |
| 158 | + assert not file.is_closed |
| 159 | + assert file.http_request.headers.get("X-Stainless-Lang") == "python" |
147 | 160 |
|
148 | | - file = response.parse() |
149 | | - assert_matches_type(str, file, path=["response"]) |
| 161 | + assert file.json() == {"foo": "bar"} |
| 162 | + assert cast(Any, file.is_closed) is True |
| 163 | + assert isinstance(file, StreamedBinaryAPIResponse) |
150 | 164 |
|
151 | | - assert cast(Any, response.is_closed) is True |
| 165 | + assert cast(Any, file.is_closed) is True |
152 | 166 |
|
153 | 167 | @parametrize |
| 168 | + @pytest.mark.respx(base_url=base_url) |
154 | 169 | def test_path_params_content(self, client: Groq) -> None: |
155 | 170 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `file_id` but received ''"): |
156 | 171 | client.files.with_raw_response.content( |
@@ -297,37 +312,49 @@ async def test_path_params_delete(self, async_client: AsyncGroq) -> None: |
297 | 312 | ) |
298 | 313 |
|
299 | 314 | @parametrize |
300 | | - async def test_method_content(self, async_client: AsyncGroq) -> None: |
| 315 | + @pytest.mark.respx(base_url=base_url) |
| 316 | + async def test_method_content(self, async_client: AsyncGroq, respx_mock: MockRouter) -> None: |
| 317 | + respx_mock.get("/openai/v1/files/file_id/content").mock(return_value=httpx.Response(200, json={"foo": "bar"})) |
301 | 318 | file = await async_client.files.content( |
302 | 319 | "file_id", |
303 | 320 | ) |
304 | | - assert_matches_type(str, file, path=["response"]) |
| 321 | + assert file.is_closed |
| 322 | + assert await file.json() == {"foo": "bar"} |
| 323 | + assert cast(Any, file.is_closed) is True |
| 324 | + assert isinstance(file, AsyncBinaryAPIResponse) |
305 | 325 |
|
306 | 326 | @parametrize |
307 | | - async def test_raw_response_content(self, async_client: AsyncGroq) -> None: |
308 | | - response = await async_client.files.with_raw_response.content( |
| 327 | + @pytest.mark.respx(base_url=base_url) |
| 328 | + async def test_raw_response_content(self, async_client: AsyncGroq, respx_mock: MockRouter) -> None: |
| 329 | + respx_mock.get("/openai/v1/files/file_id/content").mock(return_value=httpx.Response(200, json={"foo": "bar"})) |
| 330 | + |
| 331 | + file = await async_client.files.with_raw_response.content( |
309 | 332 | "file_id", |
310 | 333 | ) |
311 | 334 |
|
312 | | - assert response.is_closed is True |
313 | | - assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
314 | | - file = await response.parse() |
315 | | - assert_matches_type(str, file, path=["response"]) |
| 335 | + assert file.is_closed is True |
| 336 | + assert file.http_request.headers.get("X-Stainless-Lang") == "python" |
| 337 | + assert await file.json() == {"foo": "bar"} |
| 338 | + assert isinstance(file, AsyncBinaryAPIResponse) |
316 | 339 |
|
317 | 340 | @parametrize |
318 | | - async def test_streaming_response_content(self, async_client: AsyncGroq) -> None: |
| 341 | + @pytest.mark.respx(base_url=base_url) |
| 342 | + async def test_streaming_response_content(self, async_client: AsyncGroq, respx_mock: MockRouter) -> None: |
| 343 | + respx_mock.get("/openai/v1/files/file_id/content").mock(return_value=httpx.Response(200, json={"foo": "bar"})) |
319 | 344 | async with async_client.files.with_streaming_response.content( |
320 | 345 | "file_id", |
321 | | - ) as response: |
322 | | - assert not response.is_closed |
323 | | - assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 346 | + ) as file: |
| 347 | + assert not file.is_closed |
| 348 | + assert file.http_request.headers.get("X-Stainless-Lang") == "python" |
324 | 349 |
|
325 | | - file = await response.parse() |
326 | | - assert_matches_type(str, file, path=["response"]) |
| 350 | + assert await file.json() == {"foo": "bar"} |
| 351 | + assert cast(Any, file.is_closed) is True |
| 352 | + assert isinstance(file, AsyncStreamedBinaryAPIResponse) |
327 | 353 |
|
328 | | - assert cast(Any, response.is_closed) is True |
| 354 | + assert cast(Any, file.is_closed) is True |
329 | 355 |
|
330 | 356 | @parametrize |
| 357 | + @pytest.mark.respx(base_url=base_url) |
331 | 358 | async def test_path_params_content(self, async_client: AsyncGroq) -> None: |
332 | 359 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `file_id` but received ''"): |
333 | 360 | await async_client.files.with_raw_response.content( |
|
0 commit comments