tool functions and pytest
This commit is contained in:
89
tests/test_tool_parse_exec.py
Normal file
89
tests/test_tool_parse_exec.py
Normal file
@@ -0,0 +1,89 @@
|
||||
import pytest
|
||||
import tool_helper
|
||||
from unittest import mock
|
||||
import tests.helper as helper
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def test_tool_dummy():
|
||||
with mock.patch("tests.helper.tool_dummy") as mock_dummy:
|
||||
helper.tool_dummy()
|
||||
mock_dummy.assert_called_once() # this will check if the mocked function on the context was called.
|
||||
|
||||
|
||||
def test_tool_parse_no_exec():
|
||||
with mock.patch("tests.helper.tool_dummy") as mock_dummy:
|
||||
tool_helper.parse_and_execute_tool_call("something else", [helper.tool_dummy, helper.tool_dummy2])
|
||||
assert mock_dummy.call_count == 0
|
||||
|
||||
|
||||
def test_match_and_extract_no_match():
|
||||
result = tool_helper._match_and_extract("something else", r"<tool_call>(.*)<\/tool_call>")
|
||||
assert result == None
|
||||
|
||||
|
||||
def test_match_and_extract_matching():
|
||||
result = tool_helper._match_and_extract("asdfsdfas <tool_call>{json content}</tool_call> adfafsd", r"<tool_call>(.*)<\/tool_call>")
|
||||
assert result == "{json content}"
|
||||
|
||||
|
||||
def test_match_and_extract_matching2():
|
||||
result = tool_helper._match_and_extract("<tool_call>{json content}</tool_call>", r"<tool_call>(.*)<\/tool_call>")
|
||||
assert result == "{json content}"
|
||||
|
||||
|
||||
def test_match_and_extract_matching3_with_newline():
|
||||
result = tool_helper._match_and_extract("<tool_call>\n{json content}\n</tool_call>", r"<tool_call>(.*)<\/tool_call>")
|
||||
assert result == "\n{json content}\n"
|
||||
|
||||
|
||||
def test_string_malformed_faulty():
|
||||
with mock.patch("utils.print_error") as print_error_mock:
|
||||
result = tool_helper._execute_tool_call_str("{json_content}", [])
|
||||
assert result == None
|
||||
print_error_mock.assert_called_once() # this will check if the mocked function on the context was called.
|
||||
|
||||
|
||||
def test_tool_call_json_1():
|
||||
with mock.patch("utils.print_error") as print_error_mock:
|
||||
result = tool_helper._execute_tool_call_json({"name": "tool_dummy", "arguments": {"a": 1, "b": "zwei"}}, [helper.tool_dummy, helper.tool_dummy2])
|
||||
assert result == "result_1_zwei"
|
||||
assert print_error_mock.call_count == 0
|
||||
|
||||
|
||||
def test_tool_call_json_2():
|
||||
with mock.patch("utils.print_error") as print_error_mock:
|
||||
result = tool_helper._execute_tool_call_json({"name": "tool_dummy2", "arguments": {"text": "some_text"}}, [helper.tool_dummy, helper.tool_dummy2])
|
||||
assert result == "SOME_TEXT"
|
||||
assert print_error_mock.call_count == 0
|
||||
|
||||
|
||||
def test_tool_call_json_non_existing_call_check():
|
||||
with mock.patch("utils.print_error") as print_error_mock:
|
||||
result = tool_helper._execute_tool_call_json({"name": "tool_dummy_which_is_not_existing", "arguments": {"text": "some_text"}}, [helper.tool_dummy, helper.tool_dummy2])
|
||||
assert result == None
|
||||
assert print_error_mock.call_count == 1 # this will check if the mocked function on the context was called.
|
||||
|
||||
def test_tool_call_json_wrong_arguments_check():
|
||||
with mock.patch("utils.print_error") as print_error_mock:
|
||||
result = tool_helper._execute_tool_call_json({"name": "tool_dummy", "arguments": {"a": "must_be_an_int_but_is_string", "b": "zwei"}}, [helper.tool_dummy, helper.tool_dummy2])
|
||||
assert result == None
|
||||
assert print_error_mock.call_count == 1 # this will check if the mocked function on the context was called.
|
||||
|
||||
|
||||
|
||||
def test_regex_multiline():
|
||||
import re
|
||||
pattern = r"<start>(.*)</end>"
|
||||
|
||||
# The text to search (spanning multiple lines)
|
||||
text = """<start>
|
||||
{json}
|
||||
</end>"""
|
||||
|
||||
# Use re.search with re.DOTALL to match across newlines
|
||||
match = re.search(pattern, text, re.DOTALL)
|
||||
|
||||
assert match.group(1).find("{json}") != -1
|
Reference in New Issue
Block a user