Test a function that has simple typed parameters and defaults.
()
| 57 | |
| 58 | |
| 59 | def test_simple_function(): |
| 60 | """Test a function that has simple typed parameters and defaults.""" |
| 61 | |
| 62 | func_schema = function_schema(simple_function) |
| 63 | # Check that the JSON schema is a dictionary with title, type, etc. |
| 64 | assert isinstance(func_schema.params_json_schema, dict) |
| 65 | assert func_schema.params_json_schema.get("title") == "simple_function_args" |
| 66 | assert ( |
| 67 | func_schema.params_json_schema.get("properties", {}).get("a").get("description") |
| 68 | == "The first argument" |
| 69 | ) |
| 70 | assert ( |
| 71 | func_schema.params_json_schema.get("properties", {}).get("b").get("description") |
| 72 | == "The second argument" |
| 73 | ) |
| 74 | assert not func_schema.takes_context |
| 75 | |
| 76 | # Valid input |
| 77 | valid_input = {"a": 3} |
| 78 | parsed = func_schema.params_pydantic_model(**valid_input) |
| 79 | args_tuple, kwargs_dict = func_schema.to_call_args(parsed) |
| 80 | result = simple_function(*args_tuple, **kwargs_dict) |
| 81 | assert result == 8 # 3 + 5 |
| 82 | |
| 83 | # Another valid input |
| 84 | valid_input2 = {"a": 3, "b": 10} |
| 85 | parsed2 = func_schema.params_pydantic_model(**valid_input2) |
| 86 | args_tuple2, kwargs_dict2 = func_schema.to_call_args(parsed2) |
| 87 | result2 = simple_function(*args_tuple2, **kwargs_dict2) |
| 88 | assert result2 == 13 # 3 + 10 |
| 89 | |
| 90 | # Invalid input: 'a' must be int |
| 91 | with pytest.raises(ValidationError): |
| 92 | func_schema.params_pydantic_model(**{"a": "not an integer"}) |
| 93 | |
| 94 | |
| 95 | def varargs_function(x: int, *numbers: float, flag: bool = False, **kwargs: Any): |
nothing calls this directly
no test coverage detected