Skip to content

Commit

Permalink
Add additional tests to capture edge cases and more error conditions (#…
Browse files Browse the repository at this point in the history
…3237)

* Add additional unit tests to capture additional edge cases

* fix formatting issue (pre-commit)
  • Loading branch information
umermansoor authored Jul 30, 2024
1 parent 04be2ee commit a246a79
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
2 changes: 1 addition & 1 deletion autogen/agentchat/conversable_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}'
"""
Expand Down
21 changes: 20 additions & 1 deletion test/agentchat/test_function_call.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"]

Expand All @@ -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():
Expand Down

0 comments on commit a246a79

Please sign in to comment.