openai/openai-python
Publicmirrored fromhttps://github.com/openai/openai-pythonAvailable
tests/api_resources/test_files.py
492lines · modecode
| 1 | # File generated from our OpenAPI spec by Stainless. |
| 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 SyncPage, AsyncPage |
| 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="fine-tune", |
| 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="fine-tune", |
| 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="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 | |
| 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(SyncPage[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 | purpose="string", |
| 107 | ) |
| 108 | assert_matches_type(SyncPage[FileObject], file, path=["response"]) |
| 109 | |
| 110 | @parametrize |
| 111 | def test_raw_response_list(self, client: OpenAI) -> None: |
| 112 | response = client.files.with_raw_response.list() |
| 113 | |
| 114 | assert response.is_closed is True |
| 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 | |
| 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 | |
| 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 | ) |
| 142 | |
| 143 | assert response.is_closed is True |
| 144 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 145 | file = response.parse() |
| 146 | assert_matches_type(FileDeleted, file, path=["response"]) |
| 147 | |
| 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 | |
| 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 | |
| 168 | @parametrize |
| 169 | @pytest.mark.respx(base_url=base_url) |
| 170 | def test_method_content(self, client: OpenAI, respx_mock: MockRouter) -> None: |
| 171 | respx_mock.get("/files/string/content").mock(return_value=httpx.Response(200, json={"foo": "bar"})) |
| 172 | file = client.files.content( |
| 173 | "string", |
| 174 | ) |
| 175 | assert isinstance(file, _legacy_response.HttpxBinaryResponseContent) |
| 176 | assert file.json() == {"foo": "bar"} |
| 177 | |
| 178 | @parametrize |
| 179 | @pytest.mark.respx(base_url=base_url) |
| 180 | def test_raw_response_content(self, client: OpenAI, respx_mock: MockRouter) -> None: |
| 181 | respx_mock.get("/files/string/content").mock(return_value=httpx.Response(200, json={"foo": "bar"})) |
| 182 | |
| 183 | response = client.files.with_raw_response.content( |
| 184 | "string", |
| 185 | ) |
| 186 | |
| 187 | assert response.is_closed is True |
| 188 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 189 | file = response.parse() |
| 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 |
| 206 | |
| 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 | |
| 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 | ) |
| 221 | |
| 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 | ) |
| 230 | |
| 231 | assert response.is_closed is True |
| 232 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 233 | file = response.parse() |
| 234 | assert_matches_type(str, file, path=["response"]) |
| 235 | |
| 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 | |
| 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 | |
| 258 | |
| 259 | class TestAsyncFiles: |
| 260 | parametrize = pytest.mark.parametrize("async_client", [False, True], indirect=True, ids=["loose", "strict"]) |
| 261 | |
| 262 | @parametrize |
| 263 | async def test_method_create(self, async_client: AsyncOpenAI) -> None: |
| 264 | file = await async_client.files.create( |
| 265 | file=b"raw file contents", |
| 266 | purpose="fine-tune", |
| 267 | ) |
| 268 | assert_matches_type(FileObject, file, path=["response"]) |
| 269 | |
| 270 | @parametrize |
| 271 | async def test_raw_response_create(self, async_client: AsyncOpenAI) -> None: |
| 272 | response = await async_client.files.with_raw_response.create( |
| 273 | file=b"raw file contents", |
| 274 | purpose="fine-tune", |
| 275 | ) |
| 276 | |
| 277 | assert response.is_closed is True |
| 278 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 279 | file = response.parse() |
| 280 | assert_matches_type(FileObject, file, path=["response"]) |
| 281 | |
| 282 | @parametrize |
| 283 | async def test_streaming_response_create(self, async_client: AsyncOpenAI) -> None: |
| 284 | async with async_client.files.with_streaming_response.create( |
| 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 | |
| 296 | @parametrize |
| 297 | async def test_method_retrieve(self, async_client: AsyncOpenAI) -> None: |
| 298 | file = await async_client.files.retrieve( |
| 299 | "string", |
| 300 | ) |
| 301 | assert_matches_type(FileObject, file, path=["response"]) |
| 302 | |
| 303 | @parametrize |
| 304 | async def test_raw_response_retrieve(self, async_client: AsyncOpenAI) -> None: |
| 305 | response = await async_client.files.with_raw_response.retrieve( |
| 306 | "string", |
| 307 | ) |
| 308 | |
| 309 | assert response.is_closed is True |
| 310 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 311 | file = response.parse() |
| 312 | assert_matches_type(FileObject, file, path=["response"]) |
| 313 | |
| 314 | @parametrize |
| 315 | async def test_streaming_response_retrieve(self, async_client: AsyncOpenAI) -> None: |
| 316 | async with async_client.files.with_streaming_response.retrieve( |
| 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 | |
| 327 | @parametrize |
| 328 | async def test_path_params_retrieve(self, async_client: AsyncOpenAI) -> None: |
| 329 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `file_id` but received ''"): |
| 330 | await async_client.files.with_raw_response.retrieve( |
| 331 | "", |
| 332 | ) |
| 333 | |
| 334 | @parametrize |
| 335 | async def test_method_list(self, async_client: AsyncOpenAI) -> None: |
| 336 | file = await async_client.files.list() |
| 337 | assert_matches_type(AsyncPage[FileObject], file, path=["response"]) |
| 338 | |
| 339 | @parametrize |
| 340 | async def test_method_list_with_all_params(self, async_client: AsyncOpenAI) -> None: |
| 341 | file = await async_client.files.list( |
| 342 | purpose="string", |
| 343 | ) |
| 344 | assert_matches_type(AsyncPage[FileObject], file, path=["response"]) |
| 345 | |
| 346 | @parametrize |
| 347 | async def test_raw_response_list(self, async_client: AsyncOpenAI) -> None: |
| 348 | response = await async_client.files.with_raw_response.list() |
| 349 | |
| 350 | assert response.is_closed is True |
| 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 | |
| 355 | @parametrize |
| 356 | async def test_streaming_response_list(self, async_client: AsyncOpenAI) -> None: |
| 357 | async with async_client.files.with_streaming_response.list() as response: |
| 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 | |
| 366 | @parametrize |
| 367 | async def test_method_delete(self, async_client: AsyncOpenAI) -> None: |
| 368 | file = await async_client.files.delete( |
| 369 | "string", |
| 370 | ) |
| 371 | assert_matches_type(FileDeleted, file, path=["response"]) |
| 372 | |
| 373 | @parametrize |
| 374 | async def test_raw_response_delete(self, async_client: AsyncOpenAI) -> None: |
| 375 | response = await async_client.files.with_raw_response.delete( |
| 376 | "string", |
| 377 | ) |
| 378 | |
| 379 | assert response.is_closed is True |
| 380 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 381 | file = response.parse() |
| 382 | assert_matches_type(FileDeleted, file, path=["response"]) |
| 383 | |
| 384 | @parametrize |
| 385 | async def test_streaming_response_delete(self, async_client: AsyncOpenAI) -> None: |
| 386 | async with async_client.files.with_streaming_response.delete( |
| 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 | |
| 397 | @parametrize |
| 398 | async def test_path_params_delete(self, async_client: AsyncOpenAI) -> None: |
| 399 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `file_id` but received ''"): |
| 400 | await async_client.files.with_raw_response.delete( |
| 401 | "", |
| 402 | ) |
| 403 | |
| 404 | @parametrize |
| 405 | @pytest.mark.respx(base_url=base_url) |
| 406 | async def test_method_content(self, async_client: AsyncOpenAI, respx_mock: MockRouter) -> None: |
| 407 | respx_mock.get("/files/string/content").mock(return_value=httpx.Response(200, json={"foo": "bar"})) |
| 408 | file = await async_client.files.content( |
| 409 | "string", |
| 410 | ) |
| 411 | assert isinstance(file, _legacy_response.HttpxBinaryResponseContent) |
| 412 | assert file.json() == {"foo": "bar"} |
| 413 | |
| 414 | @parametrize |
| 415 | @pytest.mark.respx(base_url=base_url) |
| 416 | async def test_raw_response_content(self, async_client: AsyncOpenAI, respx_mock: MockRouter) -> None: |
| 417 | respx_mock.get("/files/string/content").mock(return_value=httpx.Response(200, json={"foo": "bar"})) |
| 418 | |
| 419 | response = await async_client.files.with_raw_response.content( |
| 420 | "string", |
| 421 | ) |
| 422 | |
| 423 | assert response.is_closed is True |
| 424 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 425 | file = response.parse() |
| 426 | assert_matches_type(_legacy_response.HttpxBinaryResponseContent, file, path=["response"]) |
| 427 | |
| 428 | @parametrize |
| 429 | @pytest.mark.respx(base_url=base_url) |
| 430 | async def test_streaming_response_content(self, async_client: AsyncOpenAI, respx_mock: MockRouter) -> None: |
| 431 | respx_mock.get("/files/string/content").mock(return_value=httpx.Response(200, json={"foo": "bar"})) |
| 432 | async with async_client.files.with_streaming_response.content( |
| 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 |
| 442 | |
| 443 | @parametrize |
| 444 | @pytest.mark.respx(base_url=base_url) |
| 445 | async def test_path_params_content(self, async_client: AsyncOpenAI) -> None: |
| 446 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `file_id` but received ''"): |
| 447 | await async_client.files.with_raw_response.content( |
| 448 | "", |
| 449 | ) |
| 450 | |
| 451 | @parametrize |
| 452 | async def test_method_retrieve_content(self, async_client: AsyncOpenAI) -> None: |
| 453 | with pytest.warns(DeprecationWarning): |
| 454 | file = await async_client.files.retrieve_content( |
| 455 | "string", |
| 456 | ) |
| 457 | |
| 458 | assert_matches_type(str, file, path=["response"]) |
| 459 | |
| 460 | @parametrize |
| 461 | async def test_raw_response_retrieve_content(self, async_client: AsyncOpenAI) -> None: |
| 462 | with pytest.warns(DeprecationWarning): |
| 463 | response = await async_client.files.with_raw_response.retrieve_content( |
| 464 | "string", |
| 465 | ) |
| 466 | |
| 467 | assert response.is_closed is True |
| 468 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 469 | file = response.parse() |
| 470 | assert_matches_type(str, file, path=["response"]) |
| 471 | |
| 472 | @parametrize |
| 473 | async def test_streaming_response_retrieve_content(self, async_client: AsyncOpenAI) -> None: |
| 474 | with pytest.warns(DeprecationWarning): |
| 475 | async with async_client.files.with_streaming_response.retrieve_content( |
| 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 |
| 485 | |
| 486 | @parametrize |
| 487 | async def test_path_params_retrieve_content(self, async_client: AsyncOpenAI) -> None: |
| 488 | with pytest.warns(DeprecationWarning): |
| 489 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `file_id` but received ''"): |
| 490 | await async_client.files.with_raw_response.retrieve_content( |
| 491 | "", |
| 492 | ) |
| 493 | |