| 1 | from swarm.util import function_to_json |
| 2 | |
| 3 | |
| 4 | def test_basic_function(): |
| 5 | def basic_function(arg1, arg2): |
| 6 | return arg1 + arg2 |
| 7 | |
| 8 | result = function_to_json(basic_function) |
| 9 | assert result == { |
| 10 | "type": "function", |
| 11 | "function": { |
| 12 | "name": "basic_function", |
| 13 | "description": "", |
| 14 | "parameters": { |
| 15 | "type": "object", |
| 16 | "properties": { |
| 17 | "arg1": {"type": "string"}, |
| 18 | "arg2": {"type": "string"}, |
| 19 | }, |
| 20 | "required": ["arg1", "arg2"], |
| 21 | }, |
| 22 | }, |
| 23 | } |
| 24 | |
| 25 | |
| 26 | def test_complex_function(): |
| 27 | def complex_function_with_types_and_descriptions( |
| 28 | arg1: int, arg2: str, arg3: float = 3.14, arg4: bool = False |
| 29 | ): |
| 30 | """This is a complex function with a docstring.""" |
| 31 | pass |
| 32 | |
| 33 | result = function_to_json(complex_function_with_types_and_descriptions) |
| 34 | assert result == { |
| 35 | "type": "function", |
| 36 | "function": { |
| 37 | "name": "complex_function_with_types_and_descriptions", |
| 38 | "description": "This is a complex function with a docstring.", |
| 39 | "parameters": { |
| 40 | "type": "object", |
| 41 | "properties": { |
| 42 | "arg1": {"type": "integer"}, |
| 43 | "arg2": {"type": "string"}, |
| 44 | "arg3": {"type": "number"}, |
| 45 | "arg4": {"type": "boolean"}, |
| 46 | }, |
| 47 | "required": ["arg1", "arg2"], |
| 48 | }, |
| 49 | }, |
| 50 | } |