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