openai/openai-python
Publicmirrored from https://github.com/openai/openai-pythonAvailable
tests/api_resources/test_files.py
492lines · modeblame
5cfb125aStainless Bot2 years ago | 1 | # File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. |
08b8179aDavid Schnurr2 years ago | 2 | |
| 3 | from __future__ import annotations | |
| 4 | | |
| 5 | import os | |
86379b44Stainless Bot2 years ago | 6 | from typing import Any, cast |
08b8179aDavid Schnurr2 years ago | 7 | |
aa681899Stainless Bot2 years ago | 8 | import httpx |
08b8179aDavid Schnurr2 years ago | 9 | import pytest |
aa681899Stainless Bot2 years ago | 10 | from respx import MockRouter |
08b8179aDavid Schnurr2 years ago | 11 | |
86379b44Stainless Bot2 years ago | 12 | import openai._legacy_response as _legacy_response |
08b8179aDavid Schnurr2 years ago | 13 | from openai import OpenAI, AsyncOpenAI |
| 14 | from tests.utils import assert_matches_type | |
4a0f0fa0Stainless Bot2 years ago | 15 | from openai.types import FileObject, FileDeleted |
08b8179aDavid Schnurr2 years ago | 16 | from openai.pagination import SyncPage, AsyncPage |
| 17 | | |
aa681899Stainless Bot2 years ago | 18 | # pyright: reportDeprecated=false |
| 19 | | |
08b8179aDavid Schnurr2 years ago | 20 | base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010") |
| 21 | | |
| 22 | | |
| 23 | class TestFiles: | |
98d779fbStainless Bot2 years ago | 24 | parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"]) |
08b8179aDavid Schnurr2 years ago | 25 | |
| 26 | @parametrize | |
| 27 | def test_method_create(self, client: OpenAI) -> None: | |
| 28 | file = client.files.create( | |
| 29 | file=b"raw file contents", | |
baa9f07fRobert Craigie2 years ago | 30 | purpose="fine-tune", |
08b8179aDavid Schnurr2 years ago | 31 | ) |
| 32 | assert_matches_type(FileObject, file, path=["response"]) | |
| 33 | | |
| 34 | @parametrize | |
| 35 | def test_raw_response_create(self, client: OpenAI) -> None: | |
| 36 | response = client.files.with_raw_response.create( | |
| 37 | file=b"raw file contents", | |
baa9f07fRobert Craigie2 years ago | 38 | purpose="fine-tune", |
08b8179aDavid Schnurr2 years ago | 39 | ) |
86379b44Stainless Bot2 years ago | 40 | |
| 41 | assert response.is_closed is True | |
08b8179aDavid Schnurr2 years ago | 42 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 43 | file = response.parse() | |
| 44 | assert_matches_type(FileObject, file, path=["response"]) | |
| 45 | | |
86379b44Stainless Bot2 years ago | 46 | @parametrize |
| 47 | def test_streaming_response_create(self, client: OpenAI) -> None: | |
| 48 | with client.files.with_streaming_response.create( | |
| 49 | file=b"raw file contents", | |
| 50 | purpose="fine-tune", | |
| 51 | ) as response: | |
| 52 | assert not response.is_closed | |
| 53 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" | |
| 54 | | |
| 55 | file = response.parse() | |
| 56 | assert_matches_type(FileObject, file, path=["response"]) | |
| 57 | | |
| 58 | assert cast(Any, response.is_closed) is True | |
| 59 | | |
08b8179aDavid Schnurr2 years ago | 60 | @parametrize |
| 61 | def test_method_retrieve(self, client: OpenAI) -> None: | |
| 62 | file = client.files.retrieve( | |
| 63 | "string", | |
| 64 | ) | |
| 65 | assert_matches_type(FileObject, file, path=["response"]) | |
| 66 | | |
| 67 | @parametrize | |
| 68 | def test_raw_response_retrieve(self, client: OpenAI) -> None: | |
| 69 | response = client.files.with_raw_response.retrieve( | |
| 70 | "string", | |
| 71 | ) | |
86379b44Stainless Bot2 years ago | 72 | |
| 73 | assert response.is_closed is True | |
08b8179aDavid Schnurr2 years ago | 74 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 75 | file = response.parse() | |
| 76 | assert_matches_type(FileObject, file, path=["response"]) | |
| 77 | | |
86379b44Stainless Bot2 years ago | 78 | @parametrize |
| 79 | def test_streaming_response_retrieve(self, client: OpenAI) -> None: | |
| 80 | with client.files.with_streaming_response.retrieve( | |
| 81 | "string", | |
| 82 | ) as response: | |
| 83 | assert not response.is_closed | |
| 84 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" | |
| 85 | | |
| 86 | file = response.parse() | |
| 87 | assert_matches_type(FileObject, file, path=["response"]) | |
| 88 | | |
| 89 | assert cast(Any, response.is_closed) is True | |
| 90 | | |
023a4e66Stainless Bot2 years ago | 91 | @parametrize |
| 92 | def test_path_params_retrieve(self, client: OpenAI) -> None: | |
| 93 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `file_id` but received ''"): | |
| 94 | client.files.with_raw_response.retrieve( | |
| 95 | "", | |
| 96 | ) | |
| 97 | | |
08b8179aDavid Schnurr2 years ago | 98 | @parametrize |
| 99 | def test_method_list(self, client: OpenAI) -> None: | |
| 100 | file = client.files.list() | |
| 101 | assert_matches_type(SyncPage[FileObject], file, path=["response"]) | |
| 102 | | |
baa9f07fRobert Craigie2 years ago | 103 | @parametrize |
| 104 | def test_method_list_with_all_params(self, client: OpenAI) -> None: | |
| 105 | file = client.files.list( | |
| 106 | purpose="string", | |
| 107 | ) | |
| 108 | assert_matches_type(SyncPage[FileObject], file, path=["response"]) | |
| 109 | | |
08b8179aDavid Schnurr2 years ago | 110 | @parametrize |
| 111 | def test_raw_response_list(self, client: OpenAI) -> None: | |
| 112 | response = client.files.with_raw_response.list() | |
86379b44Stainless Bot2 years ago | 113 | |
| 114 | assert response.is_closed is True | |
08b8179aDavid Schnurr2 years ago | 115 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 116 | file = response.parse() | |
| 117 | assert_matches_type(SyncPage[FileObject], file, path=["response"]) | |
| 118 | | |
86379b44Stainless Bot2 years ago | 119 | @parametrize |
| 120 | def test_streaming_response_list(self, client: OpenAI) -> None: | |
| 121 | with client.files.with_streaming_response.list() as response: | |
| 122 | assert not response.is_closed | |
| 123 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" | |
| 124 | | |
| 125 | file = response.parse() | |
| 126 | assert_matches_type(SyncPage[FileObject], file, path=["response"]) | |
| 127 | | |
| 128 | assert cast(Any, response.is_closed) is True | |
| 129 | | |
08b8179aDavid Schnurr2 years ago | 130 | @parametrize |
| 131 | def test_method_delete(self, client: OpenAI) -> None: | |
| 132 | file = client.files.delete( | |
| 133 | "string", | |
| 134 | ) | |
| 135 | assert_matches_type(FileDeleted, file, path=["response"]) | |
| 136 | | |
| 137 | @parametrize | |
| 138 | def test_raw_response_delete(self, client: OpenAI) -> None: | |
| 139 | response = client.files.with_raw_response.delete( | |
| 140 | "string", | |
| 141 | ) | |
86379b44Stainless Bot2 years ago | 142 | |
| 143 | assert response.is_closed is True | |
08b8179aDavid Schnurr2 years ago | 144 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 145 | file = response.parse() | |
| 146 | assert_matches_type(FileDeleted, file, path=["response"]) | |
| 147 | | |
86379b44Stainless Bot2 years ago | 148 | @parametrize |
| 149 | def test_streaming_response_delete(self, client: OpenAI) -> None: | |
| 150 | with client.files.with_streaming_response.delete( | |
| 151 | "string", | |
| 152 | ) as response: | |
| 153 | assert not response.is_closed | |
| 154 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" | |
| 155 | | |
| 156 | file = response.parse() | |
| 157 | assert_matches_type(FileDeleted, file, path=["response"]) | |
| 158 | | |
| 159 | assert cast(Any, response.is_closed) is True | |
| 160 | | |
023a4e66Stainless Bot2 years ago | 161 | @parametrize |
| 162 | def test_path_params_delete(self, client: OpenAI) -> None: | |
| 163 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `file_id` but received ''"): | |
| 164 | client.files.with_raw_response.delete( | |
| 165 | "", | |
| 166 | ) | |
| 167 | | |
08b8179aDavid Schnurr2 years ago | 168 | @parametrize |
aa681899Stainless Bot2 years ago | 169 | @pytest.mark.respx(base_url=base_url) |
| 170 | def test_method_content(self, client: OpenAI, respx_mock: MockRouter) -> None: | |
b56cf723Stainless Bot2 years ago | 171 | respx_mock.get("/files/string/content").mock(return_value=httpx.Response(200, json={"foo": "bar"})) |
aa681899Stainless Bot2 years ago | 172 | file = client.files.content( |
08b8179aDavid Schnurr2 years ago | 173 | "string", |
| 174 | ) | |
86379b44Stainless Bot2 years ago | 175 | assert isinstance(file, _legacy_response.HttpxBinaryResponseContent) |
aa681899Stainless Bot2 years ago | 176 | assert file.json() == {"foo": "bar"} |
08b8179aDavid Schnurr2 years ago | 177 | |
| 178 | @parametrize | |
aa681899Stainless Bot2 years ago | 179 | @pytest.mark.respx(base_url=base_url) |
| 180 | def test_raw_response_content(self, client: OpenAI, respx_mock: MockRouter) -> None: | |
b56cf723Stainless Bot2 years ago | 181 | respx_mock.get("/files/string/content").mock(return_value=httpx.Response(200, json={"foo": "bar"})) |
86379b44Stainless Bot2 years ago | 182 | |
aa681899Stainless Bot2 years ago | 183 | response = client.files.with_raw_response.content( |
08b8179aDavid Schnurr2 years ago | 184 | "string", |
| 185 | ) | |
86379b44Stainless Bot2 years ago | 186 | |
| 187 | assert response.is_closed is True | |
08b8179aDavid Schnurr2 years ago | 188 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 189 | file = response.parse() | |
86379b44Stainless Bot2 years ago | 190 | assert_matches_type(_legacy_response.HttpxBinaryResponseContent, file, path=["response"]) |
| 191 | | |
| 192 | @parametrize | |
| 193 | @pytest.mark.respx(base_url=base_url) | |
| 194 | def test_streaming_response_content(self, client: OpenAI, respx_mock: MockRouter) -> None: | |
| 195 | respx_mock.get("/files/string/content").mock(return_value=httpx.Response(200, json={"foo": "bar"})) | |
| 196 | with client.files.with_streaming_response.content( | |
| 197 | "string", | |
| 198 | ) as response: | |
| 199 | assert not response.is_closed | |
| 200 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" | |
| 201 | | |
| 202 | file = response.parse() | |
| 203 | assert_matches_type(bytes, file, path=["response"]) | |
| 204 | | |
| 205 | assert cast(Any, response.is_closed) is True | |
aa681899Stainless Bot2 years ago | 206 | |
023a4e66Stainless Bot2 years ago | 207 | @parametrize |
| 208 | @pytest.mark.respx(base_url=base_url) | |
| 209 | def test_path_params_content(self, client: OpenAI) -> None: | |
| 210 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `file_id` but received ''"): | |
| 211 | client.files.with_raw_response.content( | |
| 212 | "", | |
| 213 | ) | |
| 214 | | |
aa681899Stainless Bot2 years ago | 215 | @parametrize |
| 216 | def test_method_retrieve_content(self, client: OpenAI) -> None: | |
| 217 | with pytest.warns(DeprecationWarning): | |
| 218 | file = client.files.retrieve_content( | |
| 219 | "string", | |
| 220 | ) | |
86379b44Stainless Bot2 years ago | 221 | |
aa681899Stainless Bot2 years ago | 222 | assert_matches_type(str, file, path=["response"]) |
| 223 | | |
| 224 | @parametrize | |
| 225 | def test_raw_response_retrieve_content(self, client: OpenAI) -> None: | |
| 226 | with pytest.warns(DeprecationWarning): | |
| 227 | response = client.files.with_raw_response.retrieve_content( | |
| 228 | "string", | |
| 229 | ) | |
86379b44Stainless Bot2 years ago | 230 | |
| 231 | assert response.is_closed is True | |
aa681899Stainless Bot2 years ago | 232 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 233 | file = response.parse() | |
08b8179aDavid Schnurr2 years ago | 234 | assert_matches_type(str, file, path=["response"]) |
| 235 | | |
86379b44Stainless Bot2 years ago | 236 | @parametrize |
| 237 | def test_streaming_response_retrieve_content(self, client: OpenAI) -> None: | |
| 238 | with pytest.warns(DeprecationWarning): | |
| 239 | with client.files.with_streaming_response.retrieve_content( | |
| 240 | "string", | |
| 241 | ) as response: | |
| 242 | assert not response.is_closed | |
| 243 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" | |
| 244 | | |
| 245 | file = response.parse() | |
| 246 | assert_matches_type(str, file, path=["response"]) | |
| 247 | | |
| 248 | assert cast(Any, response.is_closed) is True | |
| 249 | | |
023a4e66Stainless Bot2 years ago | 250 | @parametrize |
| 251 | def test_path_params_retrieve_content(self, client: OpenAI) -> None: | |
| 252 | with pytest.warns(DeprecationWarning): | |
| 253 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `file_id` but received ''"): | |
| 254 | client.files.with_raw_response.retrieve_content( | |
| 255 | "", | |
| 256 | ) | |
| 257 | | |
08b8179aDavid Schnurr2 years ago | 258 | |
| 259 | class TestAsyncFiles: | |
98d779fbStainless Bot2 years ago | 260 | parametrize = pytest.mark.parametrize("async_client", [False, True], indirect=True, ids=["loose", "strict"]) |
08b8179aDavid Schnurr2 years ago | 261 | |
| 262 | @parametrize | |
98d779fbStainless Bot2 years ago | 263 | async def test_method_create(self, async_client: AsyncOpenAI) -> None: |
| 264 | file = await async_client.files.create( | |
08b8179aDavid Schnurr2 years ago | 265 | file=b"raw file contents", |
baa9f07fRobert Craigie2 years ago | 266 | purpose="fine-tune", |
08b8179aDavid Schnurr2 years ago | 267 | ) |
| 268 | assert_matches_type(FileObject, file, path=["response"]) | |
| 269 | | |
| 270 | @parametrize | |
98d779fbStainless Bot2 years ago | 271 | async def test_raw_response_create(self, async_client: AsyncOpenAI) -> None: |
| 272 | response = await async_client.files.with_raw_response.create( | |
08b8179aDavid Schnurr2 years ago | 273 | file=b"raw file contents", |
baa9f07fRobert Craigie2 years ago | 274 | purpose="fine-tune", |
08b8179aDavid Schnurr2 years ago | 275 | ) |
86379b44Stainless Bot2 years ago | 276 | |
| 277 | assert response.is_closed is True | |
08b8179aDavid Schnurr2 years ago | 278 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 279 | file = response.parse() | |
| 280 | assert_matches_type(FileObject, file, path=["response"]) | |
| 281 | | |
86379b44Stainless Bot2 years ago | 282 | @parametrize |
98d779fbStainless Bot2 years ago | 283 | async def test_streaming_response_create(self, async_client: AsyncOpenAI) -> None: |
| 284 | async with async_client.files.with_streaming_response.create( | |
86379b44Stainless Bot2 years ago | 285 | file=b"raw file contents", |
| 286 | purpose="fine-tune", | |
| 287 | ) as response: | |
| 288 | assert not response.is_closed | |
| 289 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" | |
| 290 | | |
| 291 | file = await response.parse() | |
| 292 | assert_matches_type(FileObject, file, path=["response"]) | |
| 293 | | |
| 294 | assert cast(Any, response.is_closed) is True | |
| 295 | | |
08b8179aDavid Schnurr2 years ago | 296 | @parametrize |
98d779fbStainless Bot2 years ago | 297 | async def test_method_retrieve(self, async_client: AsyncOpenAI) -> None: |
| 298 | file = await async_client.files.retrieve( | |
08b8179aDavid Schnurr2 years ago | 299 | "string", |
| 300 | ) | |
| 301 | assert_matches_type(FileObject, file, path=["response"]) | |
| 302 | | |
| 303 | @parametrize | |
98d779fbStainless Bot2 years ago | 304 | async def test_raw_response_retrieve(self, async_client: AsyncOpenAI) -> None: |
| 305 | response = await async_client.files.with_raw_response.retrieve( | |
08b8179aDavid Schnurr2 years ago | 306 | "string", |
| 307 | ) | |
86379b44Stainless Bot2 years ago | 308 | |
| 309 | assert response.is_closed is True | |
08b8179aDavid Schnurr2 years ago | 310 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 311 | file = response.parse() | |
| 312 | assert_matches_type(FileObject, file, path=["response"]) | |
| 313 | | |
86379b44Stainless Bot2 years ago | 314 | @parametrize |
98d779fbStainless Bot2 years ago | 315 | async def test_streaming_response_retrieve(self, async_client: AsyncOpenAI) -> None: |
| 316 | async with async_client.files.with_streaming_response.retrieve( | |
86379b44Stainless Bot2 years ago | 317 | "string", |
| 318 | ) as response: | |
| 319 | assert not response.is_closed | |
| 320 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" | |
| 321 | | |
| 322 | file = await response.parse() | |
| 323 | assert_matches_type(FileObject, file, path=["response"]) | |
| 324 | | |
| 325 | assert cast(Any, response.is_closed) is True | |
| 326 | | |
023a4e66Stainless Bot2 years ago | 327 | @parametrize |
98d779fbStainless Bot2 years ago | 328 | async def test_path_params_retrieve(self, async_client: AsyncOpenAI) -> None: |
023a4e66Stainless Bot2 years ago | 329 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `file_id` but received ''"): |
98d779fbStainless Bot2 years ago | 330 | await async_client.files.with_raw_response.retrieve( |
023a4e66Stainless Bot2 years ago | 331 | "", |
| 332 | ) | |
| 333 | | |
08b8179aDavid Schnurr2 years ago | 334 | @parametrize |
98d779fbStainless Bot2 years ago | 335 | async def test_method_list(self, async_client: AsyncOpenAI) -> None: |
| 336 | file = await async_client.files.list() | |
08b8179aDavid Schnurr2 years ago | 337 | assert_matches_type(AsyncPage[FileObject], file, path=["response"]) |
| 338 | | |
baa9f07fRobert Craigie2 years ago | 339 | @parametrize |
98d779fbStainless Bot2 years ago | 340 | async def test_method_list_with_all_params(self, async_client: AsyncOpenAI) -> None: |
| 341 | file = await async_client.files.list( | |
baa9f07fRobert Craigie2 years ago | 342 | purpose="string", |
| 343 | ) | |
| 344 | assert_matches_type(AsyncPage[FileObject], file, path=["response"]) | |
| 345 | | |
08b8179aDavid Schnurr2 years ago | 346 | @parametrize |
98d779fbStainless Bot2 years ago | 347 | async def test_raw_response_list(self, async_client: AsyncOpenAI) -> None: |
| 348 | response = await async_client.files.with_raw_response.list() | |
86379b44Stainless Bot2 years ago | 349 | |
| 350 | assert response.is_closed is True | |
08b8179aDavid Schnurr2 years ago | 351 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 352 | file = response.parse() | |
| 353 | assert_matches_type(AsyncPage[FileObject], file, path=["response"]) | |
| 354 | | |
86379b44Stainless Bot2 years ago | 355 | @parametrize |
98d779fbStainless Bot2 years ago | 356 | async def test_streaming_response_list(self, async_client: AsyncOpenAI) -> None: |
| 357 | async with async_client.files.with_streaming_response.list() as response: | |
86379b44Stainless Bot2 years ago | 358 | assert not response.is_closed |
| 359 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" | |
| 360 | | |
| 361 | file = await response.parse() | |
| 362 | assert_matches_type(AsyncPage[FileObject], file, path=["response"]) | |
| 363 | | |
| 364 | assert cast(Any, response.is_closed) is True | |
| 365 | | |
08b8179aDavid Schnurr2 years ago | 366 | @parametrize |
98d779fbStainless Bot2 years ago | 367 | async def test_method_delete(self, async_client: AsyncOpenAI) -> None: |
| 368 | file = await async_client.files.delete( | |
08b8179aDavid Schnurr2 years ago | 369 | "string", |
| 370 | ) | |
| 371 | assert_matches_type(FileDeleted, file, path=["response"]) | |
| 372 | | |
| 373 | @parametrize | |
98d779fbStainless Bot2 years ago | 374 | async def test_raw_response_delete(self, async_client: AsyncOpenAI) -> None: |
| 375 | response = await async_client.files.with_raw_response.delete( | |
08b8179aDavid Schnurr2 years ago | 376 | "string", |
| 377 | ) | |
86379b44Stainless Bot2 years ago | 378 | |
| 379 | assert response.is_closed is True | |
08b8179aDavid Schnurr2 years ago | 380 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 381 | file = response.parse() | |
| 382 | assert_matches_type(FileDeleted, file, path=["response"]) | |
| 383 | | |
86379b44Stainless Bot2 years ago | 384 | @parametrize |
98d779fbStainless Bot2 years ago | 385 | async def test_streaming_response_delete(self, async_client: AsyncOpenAI) -> None: |
| 386 | async with async_client.files.with_streaming_response.delete( | |
86379b44Stainless Bot2 years ago | 387 | "string", |
| 388 | ) as response: | |
| 389 | assert not response.is_closed | |
| 390 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" | |
| 391 | | |
| 392 | file = await response.parse() | |
| 393 | assert_matches_type(FileDeleted, file, path=["response"]) | |
| 394 | | |
| 395 | assert cast(Any, response.is_closed) is True | |
| 396 | | |
023a4e66Stainless Bot2 years ago | 397 | @parametrize |
98d779fbStainless Bot2 years ago | 398 | async def test_path_params_delete(self, async_client: AsyncOpenAI) -> None: |
023a4e66Stainless Bot2 years ago | 399 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `file_id` but received ''"): |
98d779fbStainless Bot2 years ago | 400 | await async_client.files.with_raw_response.delete( |
023a4e66Stainless Bot2 years ago | 401 | "", |
| 402 | ) | |
| 403 | | |
08b8179aDavid Schnurr2 years ago | 404 | @parametrize |
aa681899Stainless Bot2 years ago | 405 | @pytest.mark.respx(base_url=base_url) |
98d779fbStainless Bot2 years ago | 406 | async def test_method_content(self, async_client: AsyncOpenAI, respx_mock: MockRouter) -> None: |
b56cf723Stainless Bot2 years ago | 407 | respx_mock.get("/files/string/content").mock(return_value=httpx.Response(200, json={"foo": "bar"})) |
98d779fbStainless Bot2 years ago | 408 | file = await async_client.files.content( |
08b8179aDavid Schnurr2 years ago | 409 | "string", |
| 410 | ) | |
86379b44Stainless Bot2 years ago | 411 | assert isinstance(file, _legacy_response.HttpxBinaryResponseContent) |
aa681899Stainless Bot2 years ago | 412 | assert file.json() == {"foo": "bar"} |
08b8179aDavid Schnurr2 years ago | 413 | |
| 414 | @parametrize | |
aa681899Stainless Bot2 years ago | 415 | @pytest.mark.respx(base_url=base_url) |
98d779fbStainless Bot2 years ago | 416 | async def test_raw_response_content(self, async_client: AsyncOpenAI, respx_mock: MockRouter) -> None: |
b56cf723Stainless Bot2 years ago | 417 | respx_mock.get("/files/string/content").mock(return_value=httpx.Response(200, json={"foo": "bar"})) |
86379b44Stainless Bot2 years ago | 418 | |
98d779fbStainless Bot2 years ago | 419 | response = await async_client.files.with_raw_response.content( |
08b8179aDavid Schnurr2 years ago | 420 | "string", |
| 421 | ) | |
86379b44Stainless Bot2 years ago | 422 | |
| 423 | assert response.is_closed is True | |
08b8179aDavid Schnurr2 years ago | 424 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 425 | file = response.parse() | |
86379b44Stainless Bot2 years ago | 426 | assert_matches_type(_legacy_response.HttpxBinaryResponseContent, file, path=["response"]) |
| 427 | | |
| 428 | @parametrize | |
| 429 | @pytest.mark.respx(base_url=base_url) | |
98d779fbStainless Bot2 years ago | 430 | async def test_streaming_response_content(self, async_client: AsyncOpenAI, respx_mock: MockRouter) -> None: |
86379b44Stainless Bot2 years ago | 431 | respx_mock.get("/files/string/content").mock(return_value=httpx.Response(200, json={"foo": "bar"})) |
98d779fbStainless Bot2 years ago | 432 | async with async_client.files.with_streaming_response.content( |
86379b44Stainless Bot2 years ago | 433 | "string", |
| 434 | ) as response: | |
| 435 | assert not response.is_closed | |
| 436 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" | |
| 437 | | |
| 438 | file = await response.parse() | |
| 439 | assert_matches_type(bytes, file, path=["response"]) | |
| 440 | | |
| 441 | assert cast(Any, response.is_closed) is True | |
aa681899Stainless Bot2 years ago | 442 | |
023a4e66Stainless Bot2 years ago | 443 | @parametrize |
| 444 | @pytest.mark.respx(base_url=base_url) | |
98d779fbStainless Bot2 years ago | 445 | async def test_path_params_content(self, async_client: AsyncOpenAI) -> None: |
023a4e66Stainless Bot2 years ago | 446 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `file_id` but received ''"): |
98d779fbStainless Bot2 years ago | 447 | await async_client.files.with_raw_response.content( |
023a4e66Stainless Bot2 years ago | 448 | "", |
| 449 | ) | |
| 450 | | |
aa681899Stainless Bot2 years ago | 451 | @parametrize |
98d779fbStainless Bot2 years ago | 452 | async def test_method_retrieve_content(self, async_client: AsyncOpenAI) -> None: |
aa681899Stainless Bot2 years ago | 453 | with pytest.warns(DeprecationWarning): |
98d779fbStainless Bot2 years ago | 454 | file = await async_client.files.retrieve_content( |
aa681899Stainless Bot2 years ago | 455 | "string", |
| 456 | ) | |
86379b44Stainless Bot2 years ago | 457 | |
aa681899Stainless Bot2 years ago | 458 | assert_matches_type(str, file, path=["response"]) |
| 459 | | |
| 460 | @parametrize | |
98d779fbStainless Bot2 years ago | 461 | async def test_raw_response_retrieve_content(self, async_client: AsyncOpenAI) -> None: |
aa681899Stainless Bot2 years ago | 462 | with pytest.warns(DeprecationWarning): |
98d779fbStainless Bot2 years ago | 463 | response = await async_client.files.with_raw_response.retrieve_content( |
aa681899Stainless Bot2 years ago | 464 | "string", |
| 465 | ) | |
86379b44Stainless Bot2 years ago | 466 | |
| 467 | assert response.is_closed is True | |
aa681899Stainless Bot2 years ago | 468 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 469 | file = response.parse() | |
08b8179aDavid Schnurr2 years ago | 470 | assert_matches_type(str, file, path=["response"]) |
86379b44Stainless Bot2 years ago | 471 | |
| 472 | @parametrize | |
98d779fbStainless Bot2 years ago | 473 | async def test_streaming_response_retrieve_content(self, async_client: AsyncOpenAI) -> None: |
86379b44Stainless Bot2 years ago | 474 | with pytest.warns(DeprecationWarning): |
98d779fbStainless Bot2 years ago | 475 | async with async_client.files.with_streaming_response.retrieve_content( |
86379b44Stainless Bot2 years ago | 476 | "string", |
| 477 | ) as response: | |
| 478 | assert not response.is_closed | |
| 479 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" | |
| 480 | | |
| 481 | file = await response.parse() | |
| 482 | assert_matches_type(str, file, path=["response"]) | |
| 483 | | |
| 484 | assert cast(Any, response.is_closed) is True | |
023a4e66Stainless Bot2 years ago | 485 | |
| 486 | @parametrize | |
98d779fbStainless Bot2 years ago | 487 | async def test_path_params_retrieve_content(self, async_client: AsyncOpenAI) -> None: |
023a4e66Stainless Bot2 years ago | 488 | with pytest.warns(DeprecationWarning): |
| 489 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `file_id` but received ''"): | |
98d779fbStainless Bot2 years ago | 490 | await async_client.files.with_raw_response.retrieve_content( |
023a4e66Stainless Bot2 years ago | 491 | "", |
| 492 | ) |