55# ------------------------------------
66# cSpell:disable
77
8- import pytest
9- from test_base import TestBase , servicePreparer
10- from devtools_testutils import is_live_and_not_recording
8+ from test_base import TestBase , servicePreparer , recorded_by_proxy_httpx
119
1210# from azure.ai.projects.models import ResponsesUserMessageItemParam, ItemContentInputText
1311
1614class TestConversationCrud (TestBase ):
1715
1816 @servicePreparer ()
19- @pytest .mark .skipif (
20- condition = (not is_live_and_not_recording ()),
21- reason = "Skipped because we cannot record network calls with OpenAI client" ,
22- )
17+ @recorded_by_proxy_httpx
2318 def test_conversation_crud (self , ** kwargs ):
2419 """
2520 Test CRUD operations for Conversations.
@@ -39,67 +34,70 @@ def test_conversation_crud(self, **kwargs):
3934 DELETE /openai/conversations/{conversation_id} client.conversations.delete()
4035 """
4136
42- client = self .create_client (operation_group = "agents" , ** kwargs ).get_openai_client ()
43-
44- # Create a conversations with no messages
45- # See https://platform.openai.com/docs/api-reference/conversations/create
46- conversation1 = client .conversations .create ()
47- TestBase ._validate_conversation (conversation1 )
48- print (f"Created conversation 1 (id: { conversation1 .id } )" )
49-
50- # Create a conversation with a short-form text and long form messages as Dict
51- conversation2 = client .conversations .create (
52- items = [
53- {"type" : "message" , "role" : "user" , "content" : "first message" },
54- {"type" : "message" , "role" : "user" , "content" : [{"type" : "input_text" , "text" : "second message" }]},
55- ]
56- )
57- TestBase ._validate_conversation (conversation2 )
58- print (f"Created conversation 2 (id: { conversation2 .id } )" )
59-
60- # Create a conversation with a short-form text message, strongly typed.
61- # NOTE: The assert below will fail if you just use the auto-emitted source code as-is. You need to apply
62- # the fixes in file post-emitter-fixes.cmd to fix the emitted code and make the assert below pass.
63- # conversation3 = client.conversations.create(
64- # items=[
65- # ResponsesUserMessageItemParam(content="third message"),
66- # ResponsesUserMessageItemParam(content=[ItemContentInputText(text="fourth message")]),
67- # ]
68- # )
69- # TestBase._validate_conversation(conversation3)
70- # print(f"Created conversation 3 (id: {conversation3.id})")
71-
72- # Get first conversation
73- conversation = client .conversations .retrieve (conversation_id = conversation1 .id )
74- TestBase ._validate_conversation (conversation1 , expected_id = conversation1 .id )
75- print (f"Got conversation (id: { conversation .id } , metadata: { conversation .metadata } )" )
76-
77- # List conversations
78- # Commented out because OpenAI client does not support listing conversations
79- # for conversation in client.conversations.list():
80- # TestBase._validate_conversation(conversation)
81- # print(f"Listed conversation (id: {conversation.id})")
82-
83- # Update conversation
84- metadata = {"key1" : "value1" , "key2" : "value2" }
85- conversation = client .conversations .update (conversation_id = conversation1 .id , metadata = metadata )
86- TestBase ._validate_conversation (conversation , expected_id = conversation1 .id , expected_metadata = metadata )
87- print (f"Conversation updated" )
88-
89- conversation = client .conversations .retrieve (conversation_id = conversation1 .id )
90- TestBase ._validate_conversation (conversation )
91- print (f"Got updated conversation (id: { conversation .id } , metadata: { conversation .metadata } )" )
92-
93- # Delete conversation
94- # result = client.conversations.delete(conversation_id=conversation3.id)
95- # assert result.id == conversation3.id
96- # assert result.deleted
97- # print(f"Conversation deleted (id: {result.id}, deleted: {result.deleted})")
98- result = client .conversations .delete (conversation_id = conversation2 .id )
99- assert result .id == conversation2 .id
100- assert result .deleted
101- print (f"Conversation 2 deleted (id: { result .id } , deleted: { result .deleted } )" )
102- result = client .conversations .delete (conversation_id = conversation1 .id )
103- assert result .id == conversation1 .id
104- assert result .deleted
105- print (f"Conversation 1 deleted (id: { result .id } , deleted: { result .deleted } )" )
37+ with self .create_client (operation_group = "agents" , ** kwargs ).get_openai_client () as client :
38+
39+ # Create a conversations with no messages
40+ # See https://platform.openai.com/docs/api-reference/conversations/create
41+ conversation1 = client .conversations .create ()
42+ TestBase ._validate_conversation (conversation1 )
43+ print (f"Created conversation 1 (id: { conversation1 .id } )" )
44+
45+ # Create a conversation with a short-form text and long form messages as Dict
46+ conversation2 = client .conversations .create (
47+ items = [
48+ {"type" : "message" , "role" : "user" , "content" : "first message" },
49+ {"type" : "message" , "role" : "user" , "content" : [{"type" : "input_text" , "text" : "second message" }]},
50+ ]
51+ )
52+ TestBase ._validate_conversation (conversation2 )
53+ print (f"Created conversation 2 (id: { conversation2 .id } )" )
54+
55+ # Create a conversation with a short-form text message, strongly typed.
56+ # TODO: OpenAI accepts `items: Iterable[ResponseInputItemParam] | Omit | None = omit,` but our emitted
57+ # code does not have any class named ResponseInputItemParam. This causes typing errors. Make sure this is fixed when
58+ # we emit from TypeSpec that uses the new OpenAI TypeSpec package.
59+ # NOTE: The assert below will fail if you just use the auto-emitted source code as-is. You need to apply
60+ # the fixes in file post-emitter-fixes.cmd to fix the emitted code and make the assert below pass.
61+ # conversation3 = client.conversations.create(
62+ # items=[
63+ # ResponsesUserMessageItemParam(content="third message"),
64+ # ResponsesUserMessageItemParam(content=[ItemContentInputText(text="fourth message")]),
65+ # ]
66+ # )
67+ # TestBase._validate_conversation(conversation3)
68+ # print(f"Created conversation 3 (id: {conversation3.id})")
69+
70+ # Get first conversation
71+ conversation = client .conversations .retrieve (conversation_id = conversation1 .id )
72+ TestBase ._validate_conversation (conversation1 , expected_id = conversation1 .id )
73+ print (f"Got conversation (id: { conversation .id } , metadata: { conversation .metadata } )" )
74+
75+ # List conversations
76+ # Commented out because OpenAI client does not support listing conversations
77+ # for conversation in client.conversations.list():
78+ # TestBase._validate_conversation(conversation)
79+ # print(f"Listed conversation (id: {conversation.id})")
80+
81+ # Update conversation
82+ metadata = {"key1" : "value1" , "key2" : "value2" }
83+ conversation = client .conversations .update (conversation_id = conversation1 .id , metadata = metadata )
84+ TestBase ._validate_conversation (conversation , expected_id = conversation1 .id , expected_metadata = metadata )
85+ print (f"Conversation updated" )
86+
87+ conversation = client .conversations .retrieve (conversation_id = conversation1 .id )
88+ TestBase ._validate_conversation (conversation )
89+ print (f"Got updated conversation (id: { conversation .id } , metadata: { conversation .metadata } )" )
90+
91+ # Delete conversation
92+ # result = client.conversations.delete(conversation_id=conversation3.id)
93+ # assert result.id == conversation3.id
94+ # assert result.deleted
95+ # print(f"Conversation deleted (id: {result.id}, deleted: {result.deleted})")
96+ result = client .conversations .delete (conversation_id = conversation2 .id )
97+ assert result .id == conversation2 .id
98+ assert result .deleted
99+ print (f"Conversation 2 deleted (id: { result .id } , deleted: { result .deleted } )" )
100+ result = client .conversations .delete (conversation_id = conversation1 .id )
101+ assert result .id == conversation1 .id
102+ assert result .deleted
103+ print (f"Conversation 1 deleted (id: { result .id } , deleted: { result .deleted } )" )
0 commit comments