openai/openai-python

Public

mirrored from https://github.com/openai/openai-pythonAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.21.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

tests/api_resources/test_batches.py

268lines · modeblame

db069cdaStainless Bot2 years ago1# 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 import Batch
13
14base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
15
16
17class TestBatches:
18parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"])
19
20@parametrize
21def test_method_create(self, client: OpenAI) -> None:
22batch = client.batches.create(
23completion_window="24h",
24endpoint="/v1/chat/completions",
25input_file_id="string",
26)
27assert_matches_type(Batch, batch, path=["response"])
28
29@parametrize
30def test_method_create_with_all_params(self, client: OpenAI) -> None:
31batch = client.batches.create(
32completion_window="24h",
33endpoint="/v1/chat/completions",
34input_file_id="string",
35metadata={"foo": "string"},
36)
37assert_matches_type(Batch, batch, path=["response"])
38
39@parametrize
40def test_raw_response_create(self, client: OpenAI) -> None:
41response = client.batches.with_raw_response.create(
42completion_window="24h",
43endpoint="/v1/chat/completions",
44input_file_id="string",
45)
46
47assert response.is_closed is True
48assert response.http_request.headers.get("X-Stainless-Lang") == "python"
49batch = response.parse()
50assert_matches_type(Batch, batch, path=["response"])
51
52@parametrize
53def test_streaming_response_create(self, client: OpenAI) -> None:
54with client.batches.with_streaming_response.create(
55completion_window="24h",
56endpoint="/v1/chat/completions",
57input_file_id="string",
58) as response:
59assert not response.is_closed
60assert response.http_request.headers.get("X-Stainless-Lang") == "python"
61
62batch = response.parse()
63assert_matches_type(Batch, batch, path=["response"])
64
65assert cast(Any, response.is_closed) is True
66
67@parametrize
68def test_method_retrieve(self, client: OpenAI) -> None:
69batch = client.batches.retrieve(
70"string",
71)
72assert_matches_type(Batch, batch, path=["response"])
73
74@parametrize
75def test_raw_response_retrieve(self, client: OpenAI) -> None:
76response = client.batches.with_raw_response.retrieve(
77"string",
78)
79
80assert response.is_closed is True
81assert response.http_request.headers.get("X-Stainless-Lang") == "python"
82batch = response.parse()
83assert_matches_type(Batch, batch, path=["response"])
84
85@parametrize
86def test_streaming_response_retrieve(self, client: OpenAI) -> None:
87with client.batches.with_streaming_response.retrieve(
88"string",
89) as response:
90assert not response.is_closed
91assert response.http_request.headers.get("X-Stainless-Lang") == "python"
92
93batch = response.parse()
94assert_matches_type(Batch, batch, path=["response"])
95
96assert cast(Any, response.is_closed) is True
97
98@parametrize
99def test_path_params_retrieve(self, client: OpenAI) -> None:
100with pytest.raises(ValueError, match=r"Expected a non-empty value for `batch_id` but received ''"):
101client.batches.with_raw_response.retrieve(
102"",
103)
104
105@parametrize
106def test_method_cancel(self, client: OpenAI) -> None:
107batch = client.batches.cancel(
108"string",
109)
110assert_matches_type(Batch, batch, path=["response"])
111
112@parametrize
113def test_raw_response_cancel(self, client: OpenAI) -> None:
114response = client.batches.with_raw_response.cancel(
115"string",
116)
117
118assert response.is_closed is True
119assert response.http_request.headers.get("X-Stainless-Lang") == "python"
120batch = response.parse()
121assert_matches_type(Batch, batch, path=["response"])
122
123@parametrize
124def test_streaming_response_cancel(self, client: OpenAI) -> None:
125with client.batches.with_streaming_response.cancel(
126"string",
127) as response:
128assert not response.is_closed
129assert response.http_request.headers.get("X-Stainless-Lang") == "python"
130
131batch = response.parse()
132assert_matches_type(Batch, batch, path=["response"])
133
134assert cast(Any, response.is_closed) is True
135
136@parametrize
137def test_path_params_cancel(self, client: OpenAI) -> None:
138with pytest.raises(ValueError, match=r"Expected a non-empty value for `batch_id` but received ''"):
139client.batches.with_raw_response.cancel(
140"",
141)
142
143
144class TestAsyncBatches:
145parametrize = pytest.mark.parametrize("async_client", [False, True], indirect=True, ids=["loose", "strict"])
146
147@parametrize
148async def test_method_create(self, async_client: AsyncOpenAI) -> None:
149batch = await async_client.batches.create(
150completion_window="24h",
151endpoint="/v1/chat/completions",
152input_file_id="string",
153)
154assert_matches_type(Batch, batch, path=["response"])
155
156@parametrize
157async def test_method_create_with_all_params(self, async_client: AsyncOpenAI) -> None:
158batch = await async_client.batches.create(
159completion_window="24h",
160endpoint="/v1/chat/completions",
161input_file_id="string",
162metadata={"foo": "string"},
163)
164assert_matches_type(Batch, batch, path=["response"])
165
166@parametrize
167async def test_raw_response_create(self, async_client: AsyncOpenAI) -> None:
168response = await async_client.batches.with_raw_response.create(
169completion_window="24h",
170endpoint="/v1/chat/completions",
171input_file_id="string",
172)
173
174assert response.is_closed is True
175assert response.http_request.headers.get("X-Stainless-Lang") == "python"
176batch = response.parse()
177assert_matches_type(Batch, batch, path=["response"])
178
179@parametrize
180async def test_streaming_response_create(self, async_client: AsyncOpenAI) -> None:
181async with async_client.batches.with_streaming_response.create(
182completion_window="24h",
183endpoint="/v1/chat/completions",
184input_file_id="string",
185) as response:
186assert not response.is_closed
187assert response.http_request.headers.get("X-Stainless-Lang") == "python"
188
189batch = await response.parse()
190assert_matches_type(Batch, batch, path=["response"])
191
192assert cast(Any, response.is_closed) is True
193
194@parametrize
195async def test_method_retrieve(self, async_client: AsyncOpenAI) -> None:
196batch = await async_client.batches.retrieve(
197"string",
198)
199assert_matches_type(Batch, batch, path=["response"])
200
201@parametrize
202async def test_raw_response_retrieve(self, async_client: AsyncOpenAI) -> None:
203response = await async_client.batches.with_raw_response.retrieve(
204"string",
205)
206
207assert response.is_closed is True
208assert response.http_request.headers.get("X-Stainless-Lang") == "python"
209batch = response.parse()
210assert_matches_type(Batch, batch, path=["response"])
211
212@parametrize
213async def test_streaming_response_retrieve(self, async_client: AsyncOpenAI) -> None:
214async with async_client.batches.with_streaming_response.retrieve(
215"string",
216) as response:
217assert not response.is_closed
218assert response.http_request.headers.get("X-Stainless-Lang") == "python"
219
220batch = await response.parse()
221assert_matches_type(Batch, batch, path=["response"])
222
223assert cast(Any, response.is_closed) is True
224
225@parametrize
226async def test_path_params_retrieve(self, async_client: AsyncOpenAI) -> None:
227with pytest.raises(ValueError, match=r"Expected a non-empty value for `batch_id` but received ''"):
228await async_client.batches.with_raw_response.retrieve(
229"",
230)
231
232@parametrize
233async def test_method_cancel(self, async_client: AsyncOpenAI) -> None:
234batch = await async_client.batches.cancel(
235"string",
236)
237assert_matches_type(Batch, batch, path=["response"])
238
239@parametrize
240async def test_raw_response_cancel(self, async_client: AsyncOpenAI) -> None:
241response = await async_client.batches.with_raw_response.cancel(
242"string",
243)
244
245assert response.is_closed is True
246assert response.http_request.headers.get("X-Stainless-Lang") == "python"
247batch = response.parse()
248assert_matches_type(Batch, batch, path=["response"])
249
250@parametrize
251async def test_streaming_response_cancel(self, async_client: AsyncOpenAI) -> None:
252async with async_client.batches.with_streaming_response.cancel(
253"string",
254) as response:
255assert not response.is_closed
256assert response.http_request.headers.get("X-Stainless-Lang") == "python"
257
258batch = await response.parse()
259assert_matches_type(Batch, batch, path=["response"])
260
261assert cast(Any, response.is_closed) is True
262
263@parametrize
264async def test_path_params_cancel(self, async_client: AsyncOpenAI) -> None:
265with pytest.raises(ValueError, match=r"Expected a non-empty value for `batch_id` but received ''"):
266await async_client.batches.with_raw_response.cancel(
267"",
268)