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