Skip to content

Commit

Permalink
test: add unit tests for wasm_chat
Browse files Browse the repository at this point in the history
Signed-off-by: Xin Liu <[email protected]>
  • Loading branch information
apepkuss committed Dec 18, 2023
1 parent 7340ddc commit 8662853
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"ChatHunyuan",
"GigaChat",
"VolcEngineMaasChat",
"WasmChatService",
]


Expand Down
79 changes: 79 additions & 0 deletions libs/community/tests/unit_tests/chat_models/test_wasmchat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import pytest
from langchain_community.chat_models.wasm_chat import (
WasmChatService,
_convert_dict_to_message,
_convert_message_to_dict,
)
from langchain_core.messages import (
AIMessage,
ChatMessage,
FunctionMessage,
HumanMessage,
SystemMessage,
)


def test__convert_message_to_dict_human() -> None:

Check failure on line 16 in libs/community/tests/unit_tests/chat_models/test_wasmchat.py

View workflow job for this annotation

GitHub Actions / ci (libs/community) / lint / build (3.11)

Ruff (I001)

tests/unit_tests/chat_models/test_wasmchat.py:1:1: I001 Import block is un-sorted or un-formatted
message = HumanMessage(content="foo")
result = _convert_message_to_dict(message)
expected_output = {"role": "user", "content": "foo"}
assert result == expected_output


def test__convert_message_to_dict_ai() -> None:
message = AIMessage(content="foo")
result = _convert_message_to_dict(message)
expected_output = {"role": "assistant", "content": "foo"}
assert result == expected_output


def test__convert_message_to_dict_system() -> None:
message = SystemMessage(content="foo")
result = _convert_message_to_dict(message)
expected_output = {"role": "system", "content": "foo"}
assert result == expected_output


def test__convert_message_to_dict_function() -> None:
message = FunctionMessage(name="foo", content="bar")
with pytest.raises(TypeError) as e:
_convert_message_to_dict(message)
assert "Got unknown type" in str(e)


def test__convert_dict_to_message_human() -> None:
message_dict = {"role": "user", "content": "foo"}
result = _convert_dict_to_message(message_dict)
expected_output = HumanMessage(content="foo")
assert result == expected_output


def test__convert_dict_to_message_ai() -> None:
message_dict = {"role": "assistant", "content": "foo"}
result = _convert_dict_to_message(message_dict)
expected_output = AIMessage(content="foo")
assert result == expected_output


def test__convert_dict_to_message_other_role() -> None:
message_dict = {"role": "system", "content": "foo"}
result = _convert_dict_to_message(message_dict)
expected_output = ChatMessage(role="system", content="foo")
assert result == expected_output


def test_wasm_chat_without_service_url() -> None:
chat = WasmChatService()

# create message sequence
system_message = SystemMessage(content="You are an AI assistant")
user_message = HumanMessage(content="What is the capital of France?")
messages = [system_message, user_message]

with pytest.raises(ValueError) as e:
chat(messages)

assert (
"Error code: 503, reason: The IP address or port of the chat service is incorrect."

Check failure on line 77 in libs/community/tests/unit_tests/chat_models/test_wasmchat.py

View workflow job for this annotation

GitHub Actions / ci (libs/community) / lint / build (3.11)

Ruff (E501)

tests/unit_tests/chat_models/test_wasmchat.py:77:89: E501 Line too long (91 > 88)
in str(e)
)

0 comments on commit 8662853

Please sign in to comment.