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)
This commit is contained in:
Umer Mansoor 2024-07-30 06:57:32 -07:00 committed by GitHub
parent 04be2ee930
commit a246a79cda
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 2 deletions

View File

@ -2185,7 +2185,7 @@ class ConversableAgent(LLMAgent):
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"}'
"""

View File

@ -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 test_execute_function():
} # 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 test_execute_function():
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():