|
| 1 | +import json |
| 2 | +from typing import Dict |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from agno.tools.function import Function, FunctionCall |
| 7 | +from agno.utils.functions import get_function_call |
| 8 | + |
| 9 | + |
| 10 | +@pytest.fixture |
| 11 | +def sample_functions() -> Dict[str, Function]: |
| 12 | + return { |
| 13 | + "test_function": Function( |
| 14 | + name="test_function", |
| 15 | + description="A test function", |
| 16 | + parameters={ |
| 17 | + "type": "object", |
| 18 | + "properties": { |
| 19 | + "param1": {"type": "string"}, |
| 20 | + "param2": {"type": "integer"}, |
| 21 | + "param3": {"type": "boolean"}, |
| 22 | + }, |
| 23 | + }, |
| 24 | + ), |
| 25 | + "test_function_2": Function( |
| 26 | + name="test_function_2", |
| 27 | + description="A test function 2", |
| 28 | + parameters={ |
| 29 | + "type": "object", |
| 30 | + "properties": { |
| 31 | + "code": {"type": "string"}, |
| 32 | + }, |
| 33 | + }, |
| 34 | + ), |
| 35 | + } |
| 36 | + |
| 37 | + |
| 38 | +def test_get_function_call_basic(sample_functions): |
| 39 | + """Test basic function call creation with valid arguments.""" |
| 40 | + arguments = json.dumps({"param1": "test", "param2": 42, "param3": True}) |
| 41 | + call_id = "test-call-123" |
| 42 | + |
| 43 | + result = get_function_call( |
| 44 | + name="test_function", |
| 45 | + arguments=arguments, |
| 46 | + call_id=call_id, |
| 47 | + functions=sample_functions, |
| 48 | + ) |
| 49 | + |
| 50 | + assert result is not None |
| 51 | + assert isinstance(result, FunctionCall) |
| 52 | + assert result.function == sample_functions["test_function"] |
| 53 | + assert result.call_id == call_id |
| 54 | + assert result.arguments == {"param1": "test", "param2": 42, "param3": True} |
| 55 | + assert result.error is None |
| 56 | + |
| 57 | + |
| 58 | +def test_get_function_call_invalid_name(sample_functions): |
| 59 | + """Test function call with non-existent function name.""" |
| 60 | + result = get_function_call( |
| 61 | + name="non_existent_function", |
| 62 | + arguments='{"param1": "test"}', |
| 63 | + functions=sample_functions, |
| 64 | + ) |
| 65 | + |
| 66 | + assert result is None |
| 67 | + |
| 68 | + |
| 69 | +def test_get_function_call_no_functions(): |
| 70 | + """Test function call with no functions dictionary.""" |
| 71 | + result = get_function_call( |
| 72 | + name="test_function", |
| 73 | + arguments='{"param1": "test"}', |
| 74 | + functions=None, |
| 75 | + ) |
| 76 | + |
| 77 | + assert result is None |
| 78 | + |
| 79 | + |
| 80 | +def test_get_function_call_invalid_json(sample_functions): |
| 81 | + """Test function call with invalid JSON arguments.""" |
| 82 | + result = get_function_call( |
| 83 | + name="test_function", |
| 84 | + arguments="invalid json", |
| 85 | + functions=sample_functions, |
| 86 | + ) |
| 87 | + |
| 88 | + assert result is not None |
| 89 | + assert result.error is not None |
| 90 | + assert "Error while decoding function arguments" in result.error |
| 91 | + |
| 92 | + |
| 93 | +def test_get_function_call_non_dict_arguments(sample_functions): |
| 94 | + """Test function call with non-dictionary arguments.""" |
| 95 | + result = get_function_call( |
| 96 | + name="test_function", |
| 97 | + arguments='["not", "a", "dict"]', |
| 98 | + functions=sample_functions, |
| 99 | + ) |
| 100 | + |
| 101 | + assert result is not None |
| 102 | + assert result.error is not None |
| 103 | + assert "Function arguments are not a valid JSON object" in result.error |
| 104 | + |
| 105 | + |
| 106 | +def test_get_function_call_argument(sample_functions): |
| 107 | + """Test argument sanitization for boolean and null values.""" |
| 108 | + arguments = json.dumps( |
| 109 | + { |
| 110 | + "param1": "None", |
| 111 | + "param2": "True", |
| 112 | + "param3": "False", |
| 113 | + "param4": " test ", |
| 114 | + } |
| 115 | + ) |
| 116 | + |
| 117 | + result = get_function_call( |
| 118 | + name="test_function", |
| 119 | + arguments=arguments, |
| 120 | + functions=sample_functions, |
| 121 | + ) |
| 122 | + |
| 123 | + assert result is not None |
| 124 | + assert result.arguments == { |
| 125 | + "param1": None, |
| 126 | + "param2": True, |
| 127 | + "param3": False, |
| 128 | + "param4": "test", |
| 129 | + } |
| 130 | + |
| 131 | + |
| 132 | +def test_get_function_call_argument_advanced(sample_functions): |
| 133 | + """Test function call without argument sanitization.""" |
| 134 | + arguments = '{"param1": None, "param2": True, "param3": False, "param4": "test"}' |
| 135 | + |
| 136 | + result = get_function_call( |
| 137 | + name="test_function", |
| 138 | + arguments=arguments, |
| 139 | + functions=sample_functions, |
| 140 | + ) |
| 141 | + |
| 142 | + assert result is not None |
| 143 | + assert result.arguments == { |
| 144 | + "param1": None, |
| 145 | + "param2": True, |
| 146 | + "param3": False, |
| 147 | + "param4": "test", |
| 148 | + } |
| 149 | + |
| 150 | + arguments = '{"code": "x = True; y = False; z = None;"}' |
| 151 | + |
| 152 | + result = get_function_call( |
| 153 | + name="test_function_2", |
| 154 | + arguments=arguments, |
| 155 | + functions=sample_functions, |
| 156 | + ) |
| 157 | + |
| 158 | + assert result is not None |
| 159 | + assert result.arguments == { |
| 160 | + "code": "x = True; y = False; z = None;", |
| 161 | + } |
| 162 | + |
| 163 | + |
| 164 | +def test_get_function_call_empty_arguments(sample_functions): |
| 165 | + """Test function call with empty arguments.""" |
| 166 | + result = get_function_call( |
| 167 | + name="test_function", |
| 168 | + arguments="", |
| 169 | + functions=sample_functions, |
| 170 | + ) |
| 171 | + |
| 172 | + assert result is not None |
| 173 | + assert result.arguments is None |
| 174 | + assert result.error is None |
| 175 | + |
| 176 | + |
| 177 | +def test_get_function_call_no_arguments(sample_functions): |
| 178 | + """Test function call with no arguments provided.""" |
| 179 | + result = get_function_call( |
| 180 | + name="test_function", |
| 181 | + arguments=None, |
| 182 | + functions=sample_functions, |
| 183 | + ) |
| 184 | + |
| 185 | + assert result is not None |
| 186 | + assert result.arguments is None |
| 187 | + assert result.error is None |
0 commit comments