-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Increase coverage for existing modules
- Loading branch information
Showing
12 changed files
with
396 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
import httpx | ||
import pytest | ||
|
||
from cozepy import Chat, ChatEvent, ChatEventType, ChatStatus, Coze, TokenAuth | ||
|
||
chat_testdata = Chat( | ||
id="id", | ||
conversation_id="conversation_id", | ||
bot_id="bot_id", | ||
created_at=123, | ||
completed_at=123, | ||
failed_at=123, | ||
meta_data={}, | ||
status=ChatStatus.FAILED, | ||
) | ||
|
||
chat_stream_testdata = """ | ||
event:conversation.chat.created | ||
data:{"id":"7382159487131697202","conversation_id":"7381473525342978089","bot_id":"7379462189365198898","completed_at":1718792949,"last_error":{"code":0,"msg":""},"status":"created","usage":{"token_count":0,"output_count":0,"input_count":0}} | ||
event:conversation.chat.in_progress | ||
data:{"id":"7382159487131697202","conversation_id":"7381473525342978089","bot_id":"7379462189365198898","completed_at":1718792949,"last_error":{"code":0,"msg":""},"status":"in_progress","usage":{"token_count":0,"output_count":0,"input_count":0}} | ||
event:conversation.message.delta | ||
data:{"id":"7382159494123470858","conversation_id":"7381473525342978089","bot_id":"7379462189365198898","role":"assistant","type":"answer","content":"2","content_type":"text","chat_id":"7382159487131697202"} | ||
event:conversation.message.delta | ||
data:{"id":"7382159494123470858","conversation_id":"7381473525342978089","bot_id":"7379462189365198898","role":"assistant","type":"answer","content":"0","content_type":"text","chat_id":"7382159487131697202"} | ||
event:conversation.message.delta | ||
data:{"id":"7382159494123470858","conversation_id":"7381473525342978089","bot_id":"7379462189365198898","role":"assistant","type":"answer","content":"星期三","content_type":"text","chat_id":"7382159487131697202"} | ||
event:conversation.message.delta | ||
data:{"id":"7382159494123470858","conversation_id":"7381473525342978089","bot_id":"7379462189365198898","role":"assistant","type":"answer","content":"。","content_type":"text","chat_id":"7382159487131697202"} | ||
event:conversation.message.completed | ||
data:{"id":"7382159494123470858","conversation_id":"7381473525342978089","bot_id":"7379462189365198898","role":"assistant","type":"answer","content":"2024 年 10 月 1 日是星期三。","content_type":"text","chat_id":"7382159487131697202"} | ||
event:conversation.message.completed | ||
data:{"id":"7382159494123552778","conversation_id":"7381473525342978089","bot_id":"7379462189365198898","role":"assistant","type":"verbose","content":"{\\"msg_type\\":\\"generate_answer_finish\\",\\"data\\":\\"\\",\\"from_module\\":null,\\"from_unit\\":null}","content_type":"text","chat_id":"7382159487131697202"} | ||
event:conversation.chat.completed | ||
data:{"id":"7382159487131697202","conversation_id":"7381473525342978089","bot_id":"7379462189365198898","completed_at":1718792949,"last_error":{"code":0,"msg":""},"status":"completed","usage":{"token_count":633,"output_count":19,"input_count":614}} | ||
event:done | ||
data:"[DONE]" | ||
""" | ||
|
||
|
||
@pytest.mark.respx(base_url="https://api.coze.com") | ||
class TestConversationMessage: | ||
def test_create(self, respx_mock): | ||
coze = Coze(auth=TokenAuth(token="token")) | ||
|
||
respx_mock.post("/v3/chat").mock(httpx.Response(200, json={"data": chat_testdata.model_dump()})) | ||
res = coze.chat.create(bot_id="bot", user_id="user") | ||
|
||
assert res | ||
assert res.conversation_id == chat_testdata.conversation_id | ||
|
||
def test_stream(self, respx_mock): | ||
coze = Coze(auth=TokenAuth(token="token")) | ||
|
||
respx_mock.post("/v3/chat").mock(httpx.Response(200, content=chat_stream_testdata)) | ||
events = list(coze.chat.stream(bot_id="bot", user_id="user")) | ||
|
||
assert events | ||
assert len(events) == 9 | ||
assert events[0] == ChatEvent( | ||
event=ChatEventType.CONVERSATION_CHAT_CREATED, | ||
chat=Chat( | ||
id="7382159487131697202", | ||
conversation_id="7381473525342978089", | ||
bot_id="7379462189365198898", | ||
created_at=None, | ||
completed_at=1718792949, | ||
failed_at=None, | ||
meta_data=None, | ||
status=ChatStatus.CREATED, | ||
), | ||
) | ||
assert events[len(events) - 1].event == ChatEventType.CONVERSATION_CHAT_COMPLETED | ||
|
||
def test_retrieve(self, respx_mock): | ||
coze = Coze(auth=TokenAuth(token="token")) | ||
|
||
respx_mock.post("/v3/chat/retrieve").mock(httpx.Response(200, json={"data": chat_testdata.model_dump()})) | ||
res = coze.chat.retrieve(conversation_id="conversation", chat_id="chat") | ||
|
||
assert res | ||
assert res.conversation_id == chat_testdata.conversation_id | ||
|
||
def test_submit_tool_outputs_not_stream(self, respx_mock): | ||
coze = Coze(auth=TokenAuth(token="token")) | ||
|
||
respx_mock.post("/v3/chat/submit_tool_outputs").mock( | ||
httpx.Response(200, json={"data": chat_testdata.model_dump()}) | ||
) | ||
res = coze.chat.submit_tool_outputs( | ||
conversation_id="conversation", chat_id="chat", tool_outputs=[], stream=False | ||
) | ||
|
||
assert res | ||
assert res.conversation_id == chat_testdata.conversation_id | ||
|
||
def test_submit_tool_outputs_stream(self, respx_mock): | ||
coze = Coze(auth=TokenAuth(token="token")) | ||
|
||
respx_mock.post("/v3/chat/submit_tool_outputs").mock(httpx.Response(200, content=chat_stream_testdata)) | ||
events = list( | ||
coze.chat.submit_tool_outputs(conversation_id="conversation", chat_id="chat", tool_outputs=[], stream=True) | ||
) | ||
|
||
assert events | ||
assert len(events) == 9 | ||
assert events[0] == ChatEvent( | ||
event=ChatEventType.CONVERSATION_CHAT_CREATED, | ||
chat=Chat( | ||
id="7382159487131697202", | ||
conversation_id="7381473525342978089", | ||
bot_id="7379462189365198898", | ||
created_at=None, | ||
completed_at=1718792949, | ||
failed_at=None, | ||
meta_data=None, | ||
status=ChatStatus.CREATED, | ||
), | ||
) | ||
assert events[len(events) - 1].event == ChatEventType.CONVERSATION_CHAT_COMPLETED | ||
|
||
def test_cancel(self, respx_mock): | ||
coze = Coze(auth=TokenAuth(token="token")) | ||
|
||
respx_mock.post("/v3/chat/cancel").mock(httpx.Response(200, json={"data": chat_testdata.model_dump()})) | ||
res = coze.chat.cancel(conversation_id="conversation", chat_id="chat") | ||
|
||
assert res | ||
assert res.conversation_id == chat_testdata.conversation_id |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import httpx | ||
import pytest | ||
|
||
from cozepy import Coze, Message, TokenAuth | ||
|
||
|
||
@pytest.mark.respx(base_url="https://api.coze.com") | ||
class TestChatMessage: | ||
def test_create(self, respx_mock): | ||
coze = Coze(auth=TokenAuth(token="token")) | ||
|
||
msg = Message.user_text_message("hi") | ||
msg2 = Message.user_text_message("hey") | ||
respx_mock.post("/v3/chat/message/list").mock( | ||
httpx.Response( | ||
200, | ||
json={"data": [msg.model_dump(), msg2.model_dump()]}, | ||
) | ||
) | ||
|
||
message_list = coze.chat.messages.list(conversation_id="conversation id", chat_id="chat id") | ||
assert message_list | ||
assert message_list[0].content == msg.content |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import httpx | ||
import pytest | ||
|
||
from cozepy import Conversation, Coze, TokenAuth | ||
|
||
|
||
@pytest.mark.respx(base_url="https://api.coze.com") | ||
class TestConversation: | ||
def test_create(self, respx_mock): | ||
coze = Coze(auth=TokenAuth(token="token")) | ||
|
||
conversation = Conversation(id="id", created_at=1, meta_data={}) | ||
respx_mock.post("/v1/conversation/create").mock(httpx.Response(200, json={"data": conversation.model_dump()})) | ||
|
||
res = coze.conversations.create() | ||
assert res | ||
assert res.id == conversation.id | ||
|
||
def test_retrieve(self, respx_mock): | ||
coze = Coze(auth=TokenAuth(token="token")) | ||
|
||
conversation = Conversation(id="id", created_at=1, meta_data={}) | ||
respx_mock.get("/v1/conversation/retrieve").mock(httpx.Response(200, json={"data": conversation.model_dump()})) | ||
|
||
res = coze.conversations.retrieve(conversation_id="id") | ||
assert res | ||
assert res.id == conversation.id |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import logging | ||
|
||
import pytest | ||
|
||
from cozepy import setup_logging | ||
|
||
|
||
def test_log(): | ||
with pytest.raises(ValueError): | ||
setup_logging(123) | ||
|
||
setup_logging(logging.DEBUG) |
Oops, something went wrong.