openai/openai-python
Publicmirrored fromhttps://github.com/openai/openai-pythonAvailable
tests/api_resources/uploads/test_parts.py
106lines · 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 pytest |
| 9 | |
| 10 | from openai import OpenAI, AsyncOpenAI |
| 11 | from tests.utils import assert_matches_type |
| 12 | from openai.types.uploads import UploadPart |
| 13 | |
| 14 | base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010") |
| 15 | |
| 16 | |
| 17 | class TestParts: |
| 18 | parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"]) |
| 19 | |
| 20 | @parametrize |
| 21 | def test_method_create(self, client: OpenAI) -> None: |
| 22 | part = client.uploads.parts.create( |
| 23 | upload_id="upload_abc123", |
| 24 | data=b"raw file contents", |
| 25 | ) |
| 26 | assert_matches_type(UploadPart, part, path=["response"]) |
| 27 | |
| 28 | @parametrize |
| 29 | def test_raw_response_create(self, client: OpenAI) -> None: |
| 30 | response = client.uploads.parts.with_raw_response.create( |
| 31 | upload_id="upload_abc123", |
| 32 | data=b"raw file contents", |
| 33 | ) |
| 34 | |
| 35 | assert response.is_closed is True |
| 36 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 37 | part = response.parse() |
| 38 | assert_matches_type(UploadPart, part, path=["response"]) |
| 39 | |
| 40 | @parametrize |
| 41 | def test_streaming_response_create(self, client: OpenAI) -> None: |
| 42 | with client.uploads.parts.with_streaming_response.create( |
| 43 | upload_id="upload_abc123", |
| 44 | data=b"raw file contents", |
| 45 | ) as response: |
| 46 | assert not response.is_closed |
| 47 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 48 | |
| 49 | part = response.parse() |
| 50 | assert_matches_type(UploadPart, part, path=["response"]) |
| 51 | |
| 52 | assert cast(Any, response.is_closed) is True |
| 53 | |
| 54 | @parametrize |
| 55 | def test_path_params_create(self, client: OpenAI) -> None: |
| 56 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `upload_id` but received ''"): |
| 57 | client.uploads.parts.with_raw_response.create( |
| 58 | upload_id="", |
| 59 | data=b"raw file contents", |
| 60 | ) |
| 61 | |
| 62 | |
| 63 | class TestAsyncParts: |
| 64 | parametrize = pytest.mark.parametrize("async_client", [False, True], indirect=True, ids=["loose", "strict"]) |
| 65 | |
| 66 | @parametrize |
| 67 | async def test_method_create(self, async_client: AsyncOpenAI) -> None: |
| 68 | part = await async_client.uploads.parts.create( |
| 69 | upload_id="upload_abc123", |
| 70 | data=b"raw file contents", |
| 71 | ) |
| 72 | assert_matches_type(UploadPart, part, path=["response"]) |
| 73 | |
| 74 | @parametrize |
| 75 | async def test_raw_response_create(self, async_client: AsyncOpenAI) -> None: |
| 76 | response = await async_client.uploads.parts.with_raw_response.create( |
| 77 | upload_id="upload_abc123", |
| 78 | data=b"raw file contents", |
| 79 | ) |
| 80 | |
| 81 | assert response.is_closed is True |
| 82 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 83 | part = response.parse() |
| 84 | assert_matches_type(UploadPart, part, path=["response"]) |
| 85 | |
| 86 | @parametrize |
| 87 | async def test_streaming_response_create(self, async_client: AsyncOpenAI) -> None: |
| 88 | async with async_client.uploads.parts.with_streaming_response.create( |
| 89 | upload_id="upload_abc123", |
| 90 | data=b"raw file contents", |
| 91 | ) as response: |
| 92 | assert not response.is_closed |
| 93 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 94 | |
| 95 | part = await response.parse() |
| 96 | assert_matches_type(UploadPart, part, path=["response"]) |
| 97 | |
| 98 | assert cast(Any, response.is_closed) is True |
| 99 | |
| 100 | @parametrize |
| 101 | async def test_path_params_create(self, async_client: AsyncOpenAI) -> None: |
| 102 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `upload_id` but received ''"): |
| 103 | await async_client.uploads.parts.with_raw_response.create( |
| 104 | upload_id="", |
| 105 | data=b"raw file contents", |
| 106 | ) |