main
1# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
3from __future__ import annotations
4
5import os
6from typing import Any, cast
7
8import pytest
9
10from openai import OpenAI, AsyncOpenAI
11from tests.utils import assert_matches_type
12from openai.types.uploads import UploadPart
13
14base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
15
16
17class 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
63class TestAsyncParts:
64 parametrize = pytest.mark.parametrize(
65 "async_client", [False, True, {"http_client": "aiohttp"}], indirect=True, ids=["loose", "strict", "aiohttp"]
66 )
67
68 @parametrize
69 async def test_method_create(self, async_client: AsyncOpenAI) -> None:
70 part = await async_client.uploads.parts.create(
71 upload_id="upload_abc123",
72 data=b"raw file contents",
73 )
74 assert_matches_type(UploadPart, part, path=["response"])
75
76 @parametrize
77 async def test_raw_response_create(self, async_client: AsyncOpenAI) -> None:
78 response = await async_client.uploads.parts.with_raw_response.create(
79 upload_id="upload_abc123",
80 data=b"raw file contents",
81 )
82
83 assert response.is_closed is True
84 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
85 part = response.parse()
86 assert_matches_type(UploadPart, part, path=["response"])
87
88 @parametrize
89 async def test_streaming_response_create(self, async_client: AsyncOpenAI) -> None:
90 async with async_client.uploads.parts.with_streaming_response.create(
91 upload_id="upload_abc123",
92 data=b"raw file contents",
93 ) as response:
94 assert not response.is_closed
95 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
96
97 part = await response.parse()
98 assert_matches_type(UploadPart, part, path=["response"])
99
100 assert cast(Any, response.is_closed) is True
101
102 @parametrize
103 async def test_path_params_create(self, async_client: AsyncOpenAI) -> None:
104 with pytest.raises(ValueError, match=r"Expected a non-empty value for `upload_id` but received ''"):
105 await async_client.uploads.parts.with_raw_response.create(
106 upload_id="",
107 data=b"raw file contents",
108 )