openai/openai-python

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.63.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

tests/api_resources/test_batches.py

335lines · 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
4a0f0fa0Stainless Bot2 years ago12from openai.types import Batch
2edeeb1fStainless Bot2 years ago13from openai.pagination import SyncCursorPage, AsyncCursorPage
db069cdaStainless Bot2 years ago14
15base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
16
17
18class TestBatches:
19parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"])
20
21@parametrize
22def test_method_create(self, client: OpenAI) -> None:
23batch = client.batches.create(
24completion_window="24h",
25endpoint="/v1/chat/completions",
26input_file_id="string",
27)
28assert_matches_type(Batch, batch, path=["response"])
29
30@parametrize
31def test_method_create_with_all_params(self, client: OpenAI) -> None:
32batch = client.batches.create(
33completion_window="24h",
34endpoint="/v1/chat/completions",
35input_file_id="string",
36metadata={"foo": "string"},
37)
38assert_matches_type(Batch, batch, path=["response"])
39
40@parametrize
41def test_raw_response_create(self, client: OpenAI) -> None:
42response = client.batches.with_raw_response.create(
43completion_window="24h",
44endpoint="/v1/chat/completions",
45input_file_id="string",
46)
47
48assert response.is_closed is True
49assert response.http_request.headers.get("X-Stainless-Lang") == "python"
50batch = response.parse()
51assert_matches_type(Batch, batch, path=["response"])
52
53@parametrize
54def test_streaming_response_create(self, client: OpenAI) -> None:
55with client.batches.with_streaming_response.create(
56completion_window="24h",
57endpoint="/v1/chat/completions",
58input_file_id="string",
59) as response:
60assert not response.is_closed
61assert response.http_request.headers.get("X-Stainless-Lang") == "python"
62
63batch = response.parse()
64assert_matches_type(Batch, batch, path=["response"])
65
66assert cast(Any, response.is_closed) is True
67
68@parametrize
69def test_method_retrieve(self, client: OpenAI) -> None:
70batch = client.batches.retrieve(
71"string",
72)
73assert_matches_type(Batch, batch, path=["response"])
74
75@parametrize
76def test_raw_response_retrieve(self, client: OpenAI) -> None:
77response = client.batches.with_raw_response.retrieve(
78"string",
79)
80
81assert response.is_closed is True
82assert response.http_request.headers.get("X-Stainless-Lang") == "python"
83batch = response.parse()
84assert_matches_type(Batch, batch, path=["response"])
85
86@parametrize
87def test_streaming_response_retrieve(self, client: OpenAI) -> None:
88with client.batches.with_streaming_response.retrieve(
89"string",
90) as response:
91assert not response.is_closed
92assert response.http_request.headers.get("X-Stainless-Lang") == "python"
93
94batch = response.parse()
95assert_matches_type(Batch, batch, path=["response"])
96
97assert cast(Any, response.is_closed) is True
98
99@parametrize
100def test_path_params_retrieve(self, client: OpenAI) -> None:
101with pytest.raises(ValueError, match=r"Expected a non-empty value for `batch_id` but received ''"):
102client.batches.with_raw_response.retrieve(
103"",
104)
105
2edeeb1fStainless Bot2 years ago106@parametrize
107def test_method_list(self, client: OpenAI) -> None:
108batch = client.batches.list()
109assert_matches_type(SyncCursorPage[Batch], batch, path=["response"])
110
111@parametrize
112def test_method_list_with_all_params(self, client: OpenAI) -> None:
113batch = client.batches.list(
114after="string",
115limit=0,
116)
117assert_matches_type(SyncCursorPage[Batch], batch, path=["response"])
118
119@parametrize
120def test_raw_response_list(self, client: OpenAI) -> None:
121response = client.batches.with_raw_response.list()
122
123assert response.is_closed is True
124assert response.http_request.headers.get("X-Stainless-Lang") == "python"
125batch = response.parse()
126assert_matches_type(SyncCursorPage[Batch], batch, path=["response"])
127
128@parametrize
129def test_streaming_response_list(self, client: OpenAI) -> None:
130with client.batches.with_streaming_response.list() as response:
131assert not response.is_closed
132assert response.http_request.headers.get("X-Stainless-Lang") == "python"
133
134batch = response.parse()
135assert_matches_type(SyncCursorPage[Batch], batch, path=["response"])
136
137assert cast(Any, response.is_closed) is True
138
db069cdaStainless Bot2 years ago139@parametrize
140def test_method_cancel(self, client: OpenAI) -> None:
141batch = client.batches.cancel(
142"string",
143)
144assert_matches_type(Batch, batch, path=["response"])
145
146@parametrize
147def test_raw_response_cancel(self, client: OpenAI) -> None:
148response = client.batches.with_raw_response.cancel(
149"string",
150)
151
152assert response.is_closed is True
153assert response.http_request.headers.get("X-Stainless-Lang") == "python"
154batch = response.parse()
155assert_matches_type(Batch, batch, path=["response"])
156
157@parametrize
158def test_streaming_response_cancel(self, client: OpenAI) -> None:
159with client.batches.with_streaming_response.cancel(
160"string",
161) as response:
162assert not response.is_closed
163assert response.http_request.headers.get("X-Stainless-Lang") == "python"
164
165batch = response.parse()
166assert_matches_type(Batch, batch, path=["response"])
167
168assert cast(Any, response.is_closed) is True
169
170@parametrize
171def test_path_params_cancel(self, client: OpenAI) -> None:
172with pytest.raises(ValueError, match=r"Expected a non-empty value for `batch_id` but received ''"):
173client.batches.with_raw_response.cancel(
174"",
175)
176
177
178class TestAsyncBatches:
179parametrize = pytest.mark.parametrize("async_client", [False, True], indirect=True, ids=["loose", "strict"])
180
181@parametrize
182async def test_method_create(self, async_client: AsyncOpenAI) -> None:
183batch = await async_client.batches.create(
184completion_window="24h",
185endpoint="/v1/chat/completions",
186input_file_id="string",
187)
188assert_matches_type(Batch, batch, path=["response"])
189
190@parametrize
191async def test_method_create_with_all_params(self, async_client: AsyncOpenAI) -> None:
192batch = await async_client.batches.create(
193completion_window="24h",
194endpoint="/v1/chat/completions",
195input_file_id="string",
196metadata={"foo": "string"},
197)
198assert_matches_type(Batch, batch, path=["response"])
199
200@parametrize
201async def test_raw_response_create(self, async_client: AsyncOpenAI) -> None:
202response = await async_client.batches.with_raw_response.create(
203completion_window="24h",
204endpoint="/v1/chat/completions",
205input_file_id="string",
206)
207
208assert response.is_closed is True
209assert response.http_request.headers.get("X-Stainless-Lang") == "python"
210batch = response.parse()
211assert_matches_type(Batch, batch, path=["response"])
212
213@parametrize
214async def test_streaming_response_create(self, async_client: AsyncOpenAI) -> None:
215async with async_client.batches.with_streaming_response.create(
216completion_window="24h",
217endpoint="/v1/chat/completions",
218input_file_id="string",
219) as response:
220assert not response.is_closed
221assert response.http_request.headers.get("X-Stainless-Lang") == "python"
222
223batch = await response.parse()
224assert_matches_type(Batch, batch, path=["response"])
225
226assert cast(Any, response.is_closed) is True
227
228@parametrize
229async def test_method_retrieve(self, async_client: AsyncOpenAI) -> None:
230batch = await async_client.batches.retrieve(
231"string",
232)
233assert_matches_type(Batch, batch, path=["response"])
234
235@parametrize
236async def test_raw_response_retrieve(self, async_client: AsyncOpenAI) -> None:
237response = await async_client.batches.with_raw_response.retrieve(
238"string",
239)
240
241assert response.is_closed is True
242assert response.http_request.headers.get("X-Stainless-Lang") == "python"
243batch = response.parse()
244assert_matches_type(Batch, batch, path=["response"])
245
246@parametrize
247async def test_streaming_response_retrieve(self, async_client: AsyncOpenAI) -> None:
248async with async_client.batches.with_streaming_response.retrieve(
249"string",
250) as response:
251assert not response.is_closed
252assert response.http_request.headers.get("X-Stainless-Lang") == "python"
253
254batch = await response.parse()
255assert_matches_type(Batch, batch, path=["response"])
256
257assert cast(Any, response.is_closed) is True
258
259@parametrize
260async def test_path_params_retrieve(self, async_client: AsyncOpenAI) -> None:
261with pytest.raises(ValueError, match=r"Expected a non-empty value for `batch_id` but received ''"):
262await async_client.batches.with_raw_response.retrieve(
263"",
264)
265
2edeeb1fStainless Bot2 years ago266@parametrize
267async def test_method_list(self, async_client: AsyncOpenAI) -> None:
268batch = await async_client.batches.list()
269assert_matches_type(AsyncCursorPage[Batch], batch, path=["response"])
270
271@parametrize
272async def test_method_list_with_all_params(self, async_client: AsyncOpenAI) -> None:
273batch = await async_client.batches.list(
274after="string",
275limit=0,
276)
277assert_matches_type(AsyncCursorPage[Batch], batch, path=["response"])
278
279@parametrize
280async def test_raw_response_list(self, async_client: AsyncOpenAI) -> None:
281response = await async_client.batches.with_raw_response.list()
282
283assert response.is_closed is True
284assert response.http_request.headers.get("X-Stainless-Lang") == "python"
285batch = response.parse()
286assert_matches_type(AsyncCursorPage[Batch], batch, path=["response"])
287
288@parametrize
289async def test_streaming_response_list(self, async_client: AsyncOpenAI) -> None:
290async with async_client.batches.with_streaming_response.list() as response:
291assert not response.is_closed
292assert response.http_request.headers.get("X-Stainless-Lang") == "python"
293
294batch = await response.parse()
295assert_matches_type(AsyncCursorPage[Batch], batch, path=["response"])
296
297assert cast(Any, response.is_closed) is True
298
db069cdaStainless Bot2 years ago299@parametrize
300async def test_method_cancel(self, async_client: AsyncOpenAI) -> None:
301batch = await async_client.batches.cancel(
302"string",
303)
304assert_matches_type(Batch, batch, path=["response"])
305
306@parametrize
307async def test_raw_response_cancel(self, async_client: AsyncOpenAI) -> None:
308response = await async_client.batches.with_raw_response.cancel(
309"string",
310)
311
312assert response.is_closed is True
313assert response.http_request.headers.get("X-Stainless-Lang") == "python"
314batch = response.parse()
315assert_matches_type(Batch, batch, path=["response"])
316
317@parametrize
318async def test_streaming_response_cancel(self, async_client: AsyncOpenAI) -> None:
319async with async_client.batches.with_streaming_response.cancel(
320"string",
321) as response:
322assert not response.is_closed
323assert response.http_request.headers.get("X-Stainless-Lang") == "python"
324
325batch = await response.parse()
326assert_matches_type(Batch, batch, path=["response"])
327
328assert cast(Any, response.is_closed) is True
329
330@parametrize
331async def test_path_params_cancel(self, async_client: AsyncOpenAI) -> None:
332with pytest.raises(ValueError, match=r"Expected a non-empty value for `batch_id` but received ''"):
333await async_client.batches.with_raw_response.cancel(
334"",
335)