openai/openai-python

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.104.1

Branches

Tags

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

Clone

HTTPS

Download ZIP

tests/api_resources/conversations/test_items.py

491lines · modeblame

4e28a424stainless-app[bot]10 months 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.pagination import SyncConversationCursorPage, AsyncConversationCursorPage
13from openai.types.conversations import (
14Conversation,
15ConversationItem,
16ConversationItemList,
17)
18
19base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
20
21
22class TestItems:
23parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"])
24
25@parametrize
26def test_method_create(self, client: OpenAI) -> None:
27item = client.conversations.items.create(
28conversation_id="conv_123",
29items=[
30{
31"content": "string",
32"role": "user",
33}
34],
35)
36assert_matches_type(ConversationItemList, item, path=["response"])
37
38@parametrize
39def test_method_create_with_all_params(self, client: OpenAI) -> None:
40item = client.conversations.items.create(
41conversation_id="conv_123",
42items=[
43{
44"content": "string",
45"role": "user",
46"type": "message",
47}
48],
49include=["code_interpreter_call.outputs"],
50)
51assert_matches_type(ConversationItemList, item, path=["response"])
52
53@parametrize
54def test_raw_response_create(self, client: OpenAI) -> None:
55response = client.conversations.items.with_raw_response.create(
56conversation_id="conv_123",
57items=[
58{
59"content": "string",
60"role": "user",
61}
62],
63)
64
65assert response.is_closed is True
66assert response.http_request.headers.get("X-Stainless-Lang") == "python"
67item = response.parse()
68assert_matches_type(ConversationItemList, item, path=["response"])
69
70@parametrize
71def test_streaming_response_create(self, client: OpenAI) -> None:
72with client.conversations.items.with_streaming_response.create(
73conversation_id="conv_123",
74items=[
75{
76"content": "string",
77"role": "user",
78}
79],
80) as response:
81assert not response.is_closed
82assert response.http_request.headers.get("X-Stainless-Lang") == "python"
83
84item = response.parse()
85assert_matches_type(ConversationItemList, item, path=["response"])
86
87assert cast(Any, response.is_closed) is True
88
89@parametrize
90def test_path_params_create(self, client: OpenAI) -> None:
91with pytest.raises(ValueError, match=r"Expected a non-empty value for `conversation_id` but received ''"):
92client.conversations.items.with_raw_response.create(
93conversation_id="",
94items=[
95{
96"content": "string",
97"role": "user",
98}
99],
100)
101
102@parametrize
103def test_method_retrieve(self, client: OpenAI) -> None:
104item = client.conversations.items.retrieve(
105item_id="msg_abc",
106conversation_id="conv_123",
107)
108assert_matches_type(ConversationItem, item, path=["response"])
109
110@parametrize
111def test_method_retrieve_with_all_params(self, client: OpenAI) -> None:
112item = client.conversations.items.retrieve(
113item_id="msg_abc",
114conversation_id="conv_123",
115include=["code_interpreter_call.outputs"],
116)
117assert_matches_type(ConversationItem, item, path=["response"])
118
119@parametrize
120def test_raw_response_retrieve(self, client: OpenAI) -> None:
121response = client.conversations.items.with_raw_response.retrieve(
122item_id="msg_abc",
123conversation_id="conv_123",
124)
125
126assert response.is_closed is True
127assert response.http_request.headers.get("X-Stainless-Lang") == "python"
128item = response.parse()
129assert_matches_type(ConversationItem, item, path=["response"])
130
131@parametrize
132def test_streaming_response_retrieve(self, client: OpenAI) -> None:
133with client.conversations.items.with_streaming_response.retrieve(
134item_id="msg_abc",
135conversation_id="conv_123",
136) as response:
137assert not response.is_closed
138assert response.http_request.headers.get("X-Stainless-Lang") == "python"
139
140item = response.parse()
141assert_matches_type(ConversationItem, item, path=["response"])
142
143assert cast(Any, response.is_closed) is True
144
145@parametrize
146def test_path_params_retrieve(self, client: OpenAI) -> None:
147with pytest.raises(ValueError, match=r"Expected a non-empty value for `conversation_id` but received ''"):
148client.conversations.items.with_raw_response.retrieve(
149item_id="msg_abc",
150conversation_id="",
151)
152
153with pytest.raises(ValueError, match=r"Expected a non-empty value for `item_id` but received ''"):
154client.conversations.items.with_raw_response.retrieve(
155item_id="",
156conversation_id="conv_123",
157)
158
159@parametrize
160def test_method_list(self, client: OpenAI) -> None:
161item = client.conversations.items.list(
162conversation_id="conv_123",
163)
164assert_matches_type(SyncConversationCursorPage[ConversationItem], item, path=["response"])
165
166@parametrize
167def test_method_list_with_all_params(self, client: OpenAI) -> None:
168item = client.conversations.items.list(
169conversation_id="conv_123",
170after="after",
171include=["code_interpreter_call.outputs"],
172limit=0,
173order="asc",
174)
175assert_matches_type(SyncConversationCursorPage[ConversationItem], item, path=["response"])
176
177@parametrize
178def test_raw_response_list(self, client: OpenAI) -> None:
179response = client.conversations.items.with_raw_response.list(
180conversation_id="conv_123",
181)
182
183assert response.is_closed is True
184assert response.http_request.headers.get("X-Stainless-Lang") == "python"
185item = response.parse()
186assert_matches_type(SyncConversationCursorPage[ConversationItem], item, path=["response"])
187
188@parametrize
189def test_streaming_response_list(self, client: OpenAI) -> None:
190with client.conversations.items.with_streaming_response.list(
191conversation_id="conv_123",
192) as response:
193assert not response.is_closed
194assert response.http_request.headers.get("X-Stainless-Lang") == "python"
195
196item = response.parse()
197assert_matches_type(SyncConversationCursorPage[ConversationItem], item, path=["response"])
198
199assert cast(Any, response.is_closed) is True
200
201@parametrize
202def test_path_params_list(self, client: OpenAI) -> None:
203with pytest.raises(ValueError, match=r"Expected a non-empty value for `conversation_id` but received ''"):
204client.conversations.items.with_raw_response.list(
205conversation_id="",
206)
207
208@parametrize
209def test_method_delete(self, client: OpenAI) -> None:
210item = client.conversations.items.delete(
211item_id="msg_abc",
212conversation_id="conv_123",
213)
214assert_matches_type(Conversation, item, path=["response"])
215
216@parametrize
217def test_raw_response_delete(self, client: OpenAI) -> None:
218response = client.conversations.items.with_raw_response.delete(
219item_id="msg_abc",
220conversation_id="conv_123",
221)
222
223assert response.is_closed is True
224assert response.http_request.headers.get("X-Stainless-Lang") == "python"
225item = response.parse()
226assert_matches_type(Conversation, item, path=["response"])
227
228@parametrize
229def test_streaming_response_delete(self, client: OpenAI) -> None:
230with client.conversations.items.with_streaming_response.delete(
231item_id="msg_abc",
232conversation_id="conv_123",
233) as response:
234assert not response.is_closed
235assert response.http_request.headers.get("X-Stainless-Lang") == "python"
236
237item = response.parse()
238assert_matches_type(Conversation, item, path=["response"])
239
240assert cast(Any, response.is_closed) is True
241
242@parametrize
243def test_path_params_delete(self, client: OpenAI) -> None:
244with pytest.raises(ValueError, match=r"Expected a non-empty value for `conversation_id` but received ''"):
245client.conversations.items.with_raw_response.delete(
246item_id="msg_abc",
247conversation_id="",
248)
249
250with pytest.raises(ValueError, match=r"Expected a non-empty value for `item_id` but received ''"):
251client.conversations.items.with_raw_response.delete(
252item_id="",
253conversation_id="conv_123",
254)
255
256
257class TestAsyncItems:
258parametrize = pytest.mark.parametrize(
259"async_client", [False, True, {"http_client": "aiohttp"}], indirect=True, ids=["loose", "strict", "aiohttp"]
260)
261
262@parametrize
263async def test_method_create(self, async_client: AsyncOpenAI) -> None:
264item = await async_client.conversations.items.create(
265conversation_id="conv_123",
266items=[
267{
268"content": "string",
269"role": "user",
270}
271],
272)
273assert_matches_type(ConversationItemList, item, path=["response"])
274
275@parametrize
276async def test_method_create_with_all_params(self, async_client: AsyncOpenAI) -> None:
277item = await async_client.conversations.items.create(
278conversation_id="conv_123",
279items=[
280{
281"content": "string",
282"role": "user",
283"type": "message",
284}
285],
286include=["code_interpreter_call.outputs"],
287)
288assert_matches_type(ConversationItemList, item, path=["response"])
289
290@parametrize
291async def test_raw_response_create(self, async_client: AsyncOpenAI) -> None:
292response = await async_client.conversations.items.with_raw_response.create(
293conversation_id="conv_123",
294items=[
295{
296"content": "string",
297"role": "user",
298}
299],
300)
301
302assert response.is_closed is True
303assert response.http_request.headers.get("X-Stainless-Lang") == "python"
304item = response.parse()
305assert_matches_type(ConversationItemList, item, path=["response"])
306
307@parametrize
308async def test_streaming_response_create(self, async_client: AsyncOpenAI) -> None:
309async with async_client.conversations.items.with_streaming_response.create(
310conversation_id="conv_123",
311items=[
312{
313"content": "string",
314"role": "user",
315}
316],
317) as response:
318assert not response.is_closed
319assert response.http_request.headers.get("X-Stainless-Lang") == "python"
320
321item = await response.parse()
322assert_matches_type(ConversationItemList, item, path=["response"])
323
324assert cast(Any, response.is_closed) is True
325
326@parametrize
327async def test_path_params_create(self, async_client: AsyncOpenAI) -> None:
328with pytest.raises(ValueError, match=r"Expected a non-empty value for `conversation_id` but received ''"):
329await async_client.conversations.items.with_raw_response.create(
330conversation_id="",
331items=[
332{
333"content": "string",
334"role": "user",
335}
336],
337)
338
339@parametrize
340async def test_method_retrieve(self, async_client: AsyncOpenAI) -> None:
341item = await async_client.conversations.items.retrieve(
342item_id="msg_abc",
343conversation_id="conv_123",
344)
345assert_matches_type(ConversationItem, item, path=["response"])
346
347@parametrize
348async def test_method_retrieve_with_all_params(self, async_client: AsyncOpenAI) -> None:
349item = await async_client.conversations.items.retrieve(
350item_id="msg_abc",
351conversation_id="conv_123",
352include=["code_interpreter_call.outputs"],
353)
354assert_matches_type(ConversationItem, item, path=["response"])
355
356@parametrize
357async def test_raw_response_retrieve(self, async_client: AsyncOpenAI) -> None:
358response = await async_client.conversations.items.with_raw_response.retrieve(
359item_id="msg_abc",
360conversation_id="conv_123",
361)
362
363assert response.is_closed is True
364assert response.http_request.headers.get("X-Stainless-Lang") == "python"
365item = response.parse()
366assert_matches_type(ConversationItem, item, path=["response"])
367
368@parametrize
369async def test_streaming_response_retrieve(self, async_client: AsyncOpenAI) -> None:
370async with async_client.conversations.items.with_streaming_response.retrieve(
371item_id="msg_abc",
372conversation_id="conv_123",
373) as response:
374assert not response.is_closed
375assert response.http_request.headers.get("X-Stainless-Lang") == "python"
376
377item = await response.parse()
378assert_matches_type(ConversationItem, item, path=["response"])
379
380assert cast(Any, response.is_closed) is True
381
382@parametrize
383async def test_path_params_retrieve(self, async_client: AsyncOpenAI) -> None:
384with pytest.raises(ValueError, match=r"Expected a non-empty value for `conversation_id` but received ''"):
385await async_client.conversations.items.with_raw_response.retrieve(
386item_id="msg_abc",
387conversation_id="",
388)
389
390with pytest.raises(ValueError, match=r"Expected a non-empty value for `item_id` but received ''"):
391await async_client.conversations.items.with_raw_response.retrieve(
392item_id="",
393conversation_id="conv_123",
394)
395
396@parametrize
397async def test_method_list(self, async_client: AsyncOpenAI) -> None:
398item = await async_client.conversations.items.list(
399conversation_id="conv_123",
400)
401assert_matches_type(AsyncConversationCursorPage[ConversationItem], item, path=["response"])
402
403@parametrize
404async def test_method_list_with_all_params(self, async_client: AsyncOpenAI) -> None:
405item = await async_client.conversations.items.list(
406conversation_id="conv_123",
407after="after",
408include=["code_interpreter_call.outputs"],
409limit=0,
410order="asc",
411)
412assert_matches_type(AsyncConversationCursorPage[ConversationItem], item, path=["response"])
413
414@parametrize
415async def test_raw_response_list(self, async_client: AsyncOpenAI) -> None:
416response = await async_client.conversations.items.with_raw_response.list(
417conversation_id="conv_123",
418)
419
420assert response.is_closed is True
421assert response.http_request.headers.get("X-Stainless-Lang") == "python"
422item = response.parse()
423assert_matches_type(AsyncConversationCursorPage[ConversationItem], item, path=["response"])
424
425@parametrize
426async def test_streaming_response_list(self, async_client: AsyncOpenAI) -> None:
427async with async_client.conversations.items.with_streaming_response.list(
428conversation_id="conv_123",
429) as response:
430assert not response.is_closed
431assert response.http_request.headers.get("X-Stainless-Lang") == "python"
432
433item = await response.parse()
434assert_matches_type(AsyncConversationCursorPage[ConversationItem], item, path=["response"])
435
436assert cast(Any, response.is_closed) is True
437
438@parametrize
439async def test_path_params_list(self, async_client: AsyncOpenAI) -> None:
440with pytest.raises(ValueError, match=r"Expected a non-empty value for `conversation_id` but received ''"):
441await async_client.conversations.items.with_raw_response.list(
442conversation_id="",
443)
444
445@parametrize
446async def test_method_delete(self, async_client: AsyncOpenAI) -> None:
447item = await async_client.conversations.items.delete(
448item_id="msg_abc",
449conversation_id="conv_123",
450)
451assert_matches_type(Conversation, item, path=["response"])
452
453@parametrize
454async def test_raw_response_delete(self, async_client: AsyncOpenAI) -> None:
455response = await async_client.conversations.items.with_raw_response.delete(
456item_id="msg_abc",
457conversation_id="conv_123",
458)
459
460assert response.is_closed is True
461assert response.http_request.headers.get("X-Stainless-Lang") == "python"
462item = response.parse()
463assert_matches_type(Conversation, item, path=["response"])
464
465@parametrize
466async def test_streaming_response_delete(self, async_client: AsyncOpenAI) -> None:
467async with async_client.conversations.items.with_streaming_response.delete(
468item_id="msg_abc",
469conversation_id="conv_123",
470) as response:
471assert not response.is_closed
472assert response.http_request.headers.get("X-Stainless-Lang") == "python"
473
474item = await response.parse()
475assert_matches_type(Conversation, item, path=["response"])
476
477assert cast(Any, response.is_closed) is True
478
479@parametrize
480async def test_path_params_delete(self, async_client: AsyncOpenAI) -> None:
481with pytest.raises(ValueError, match=r"Expected a non-empty value for `conversation_id` but received ''"):
482await async_client.conversations.items.with_raw_response.delete(
483item_id="msg_abc",
484conversation_id="",
485)
486
487with pytest.raises(ValueError, match=r"Expected a non-empty value for `item_id` but received ''"):
488await async_client.conversations.items.with_raw_response.delete(
489item_id="",
490conversation_id="conv_123",
491)