|
| 1 | +import enum |
| 2 | +from typing import Optional, Annotated |
| 3 | +from function_schema.core import get_function_schema |
| 4 | + |
| 5 | + |
| 6 | +def test_simple_function(): |
| 7 | + """Test a simple function""" |
| 8 | + |
| 9 | + def func1(): |
| 10 | + """My function""" |
| 11 | + ... |
| 12 | + |
| 13 | + schema = get_function_schema(func1) |
| 14 | + |
| 15 | + assert schema["name"] == "func1", "Function name should be func1" |
| 16 | + assert ( |
| 17 | + schema["description"] == "My function" |
| 18 | + ), "Function description should be My function" |
| 19 | + assert ( |
| 20 | + schema["parameters"]["properties"] == {} |
| 21 | + ), "Function parameters should be empty" |
| 22 | + assert schema["parameters"]["required"] == [], "required parameters should be empty" |
| 23 | + |
| 24 | + |
| 25 | +def test_function_with_args(): |
| 26 | + """Test a function with args""" |
| 27 | + |
| 28 | + def func1(a: int, b: str, c: float = 1.0): |
| 29 | + """My function""" |
| 30 | + ... |
| 31 | + |
| 32 | + schema = get_function_schema(func1) |
| 33 | + |
| 34 | + assert schema["name"] == "func1", "Function name should be func1" |
| 35 | + assert ( |
| 36 | + schema["description"] == "My function" |
| 37 | + ), "Function description should be there" |
| 38 | + assert ( |
| 39 | + schema["parameters"]["properties"]["a"]["type"] == "integer" |
| 40 | + ), "parameter a should be an integer" |
| 41 | + assert ( |
| 42 | + schema["parameters"]["properties"]["b"]["type"] == "string" |
| 43 | + ), "parameter b should be a string" |
| 44 | + assert ( |
| 45 | + schema["parameters"]["properties"]["c"]["type"] == "number" |
| 46 | + ), "parameter c should be a number" |
| 47 | + assert ( |
| 48 | + schema["parameters"]["properties"]["c"]["default"] == 1.0 |
| 49 | + ), "c should have a default value of 1.0" |
| 50 | + assert schema["parameters"]["required"] == [ |
| 51 | + "a", |
| 52 | + "b", |
| 53 | + ], "parameters with no default value should be required" |
| 54 | + |
| 55 | + |
| 56 | +def test_annotated_function(): |
| 57 | + """Test a function with annotations""" |
| 58 | + |
| 59 | + def func1( |
| 60 | + a: Annotated[int, "An integer parameter"], |
| 61 | + b: Annotated[str, "A string parameter"], |
| 62 | + ): |
| 63 | + """My function""" |
| 64 | + ... |
| 65 | + |
| 66 | + schema = get_function_schema(func1) |
| 67 | + |
| 68 | + assert ( |
| 69 | + schema["parameters"]["properties"]["a"]["type"] == "integer" |
| 70 | + ), "parameter a should be an integer" |
| 71 | + assert ( |
| 72 | + schema["parameters"]["properties"]["a"]["description"] == "An integer parameter" |
| 73 | + ), "parameter a should have a description" |
| 74 | + assert ( |
| 75 | + schema["parameters"]["properties"]["b"]["type"] == "string" |
| 76 | + ), "parameter b should be a string" |
| 77 | + assert ( |
| 78 | + schema["parameters"]["properties"]["b"]["description"] == "A string parameter" |
| 79 | + ), "parameter b should have a description" |
| 80 | + |
| 81 | + assert schema["parameters"]["required"] == [ |
| 82 | + "a", |
| 83 | + "b", |
| 84 | + ], "parameters with no default value should be required" |
| 85 | + |
| 86 | + |
| 87 | +def test_annotated_function_with_enum(): |
| 88 | + """Test a function with annotations and enum""" |
| 89 | + def func1( |
| 90 | + animal: Annotated[ |
| 91 | + str, |
| 92 | + "The animal you want to pet", |
| 93 | + enum.Enum("Animal", "Cat Dog"), |
| 94 | + ], |
| 95 | + ): |
| 96 | + """My function""" |
| 97 | + ... |
| 98 | + |
| 99 | + schema = get_function_schema(func1) |
| 100 | + print(schema) |
| 101 | + assert ( |
| 102 | + schema["parameters"]["properties"]["animal"]["type"] == "string" |
| 103 | + ), "parameter animal should be a string" |
| 104 | + assert ( |
| 105 | + schema["parameters"]["properties"]["animal"]["enum"] == ["Cat", "Dog"] |
| 106 | + ), "parameter animal should have an enum" |
0 commit comments