From a246a79cdae3e282594b057bb0923932716ec47e Mon Sep 17 00:00:00 2001 From: Umer Mansoor Date: Tue, 30 Jul 2024 06:57:32 -0700 Subject: [PATCH] Add additional tests to capture edge cases and more error conditions (#3237) * Add additional unit tests to capture additional edge cases * fix formatting issue (pre-commit) --- autogen/agentchat/conversable_agent.py | 2 +- test/agentchat/test_function_call.py | 21 ++++++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/autogen/agentchat/conversable_agent.py b/autogen/agentchat/conversable_agent.py index 2d8958f3282d..ca1a538e0802 100644 --- a/autogen/agentchat/conversable_agent.py +++ b/autogen/agentchat/conversable_agent.py @@ -2185,7 +2185,7 @@ def _format_json_str(jstr): Ex 2: "{\n \"location\": \"Boston, MA\"\n}" -> "{"location": "Boston, MA"}" - 2. this function also handles JSON escape sequences inside quotes, + 2. this function also handles JSON escape sequences inside quotes. Ex 1: '{"args": "a\na\na\ta"}' -> '{"args": "a\\na\\na\\ta"}' """ diff --git a/test/agentchat/test_function_call.py b/test/agentchat/test_function_call.py index 7023a709d3c1..7745181ea64a 100755 --- a/test/agentchat/test_function_call.py +++ b/test/agentchat/test_function_call.py @@ -91,6 +91,12 @@ def test_json_extraction(): jstr = '{"code": "a=\\"hello\\""}' assert user._format_json_str(jstr) == '{"code": "a=\\"hello\\""}' + jstr = '{\n"tool": "python",\n"query": "print(\'hello\')\n\tprint(\'world\')"\n}' # mixed newlines and tabs + assert user._format_json_str(jstr) == '{"tool": "python","query": "print(\'hello\')\\n\\tprint(\'world\')"}' + + jstr = "{}" # empty json + assert user._format_json_str(jstr) == "{}" + def test_execute_function(): from autogen.agentchat import UserProxyAgent @@ -117,7 +123,7 @@ def add_num(num_to_be_added): } # should be "given_num" with quotes assert "The argument must be in JSON format." in user.execute_function(func_call=wrong_json_format)[1]["content"] - # function execution error with wrong arguments passed + # function execution error with extra arguments wrong_args = {"name": "add_num", "arguments": '{ "num_to_be_added": 5, "given_num": 10 }'} assert "Error: " in user.execute_function(func_call=wrong_args)[1]["content"] @@ -143,6 +149,19 @@ def get_number(): func_call = {"name": "get_number", "arguments": "{}"} assert user.execute_function(func_call)[1]["content"] == "42" + # 4. test with a non-existent function + user = UserProxyAgent(name="test", function_map={}) + func_call = {"name": "nonexistent_function", "arguments": "{}"} + assert "Error: Function" in user.execute_function(func_call=func_call)[1]["content"] + + # 5. test calling a function that raises an exception + def raise_exception(): + raise ValueError("This is an error") + + user = UserProxyAgent(name="test", function_map={"raise_exception": raise_exception}) + func_call = {"name": "raise_exception", "arguments": "{}"} + assert "Error: " in user.execute_function(func_call=func_call)[1]["content"] + @pytest.mark.asyncio async def test_a_execute_function():