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