|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | + |
| 16 | +from inspect import Parameter |
| 17 | + |
| 18 | +import pytest |
| 19 | + |
| 20 | +from toolbox_core.protocol import ParameterSchema |
| 21 | + |
| 22 | + |
| 23 | +def test_parameter_schema_float(): |
| 24 | + """Tests ParameterSchema with type 'float'.""" |
| 25 | + schema = ParameterSchema(name="price", type="float", description="The item price") |
| 26 | + expected_type = float |
| 27 | + assert schema._ParameterSchema__get_type() == expected_type |
| 28 | + |
| 29 | + param = schema.to_param() |
| 30 | + assert isinstance(param, Parameter) |
| 31 | + assert param.name == "price" |
| 32 | + assert param.annotation == expected_type |
| 33 | + assert param.kind == Parameter.POSITIONAL_OR_KEYWORD |
| 34 | + assert param.default == Parameter.empty |
| 35 | + |
| 36 | + |
| 37 | +def test_parameter_schema_boolean(): |
| 38 | + """Tests ParameterSchema with type 'boolean'.""" |
| 39 | + schema = ParameterSchema( |
| 40 | + name="is_active", type="boolean", description="Activity status" |
| 41 | + ) |
| 42 | + expected_type = bool |
| 43 | + assert schema._ParameterSchema__get_type() == expected_type |
| 44 | + |
| 45 | + param = schema.to_param() |
| 46 | + assert isinstance(param, Parameter) |
| 47 | + assert param.name == "is_active" |
| 48 | + assert param.annotation == expected_type |
| 49 | + assert param.kind == Parameter.POSITIONAL_OR_KEYWORD |
| 50 | + |
| 51 | + |
| 52 | +def test_parameter_schema_array_string(): |
| 53 | + """Tests ParameterSchema with type 'array' containing strings.""" |
| 54 | + item_schema = ParameterSchema( |
| 55 | + name="", type="string", description="" |
| 56 | + ) |
| 57 | + schema = ParameterSchema( |
| 58 | + name="tags", type="array", description="List of tags", items=item_schema |
| 59 | + ) |
| 60 | + |
| 61 | + assert schema._ParameterSchema__get_type() == list[str] |
| 62 | + |
| 63 | + param = schema.to_param() |
| 64 | + assert isinstance(param, Parameter) |
| 65 | + assert param.name == "tags" |
| 66 | + assert param.annotation == list[str] |
| 67 | + assert param.kind == Parameter.POSITIONAL_OR_KEYWORD |
| 68 | + |
| 69 | + |
| 70 | +def test_parameter_schema_array_integer(): |
| 71 | + """Tests ParameterSchema with type 'array' containing integers.""" |
| 72 | + item_schema = ParameterSchema(name="", type="integer", description="") |
| 73 | + schema = ParameterSchema( |
| 74 | + name="scores", type="array", description="List of scores", items=item_schema |
| 75 | + ) |
| 76 | + |
| 77 | + param = schema.to_param() |
| 78 | + assert isinstance(param, Parameter) |
| 79 | + assert param.name == "scores" |
| 80 | + assert param.annotation == list[int] |
| 81 | + assert param.kind == Parameter.POSITIONAL_OR_KEYWORD |
| 82 | + |
| 83 | + |
| 84 | +def test_parameter_schema_array_no_items_error(): |
| 85 | + """Tests that 'array' type raises error if 'items' is None.""" |
| 86 | + schema = ParameterSchema( |
| 87 | + name="bad_list", type="array", description="List without item type" |
| 88 | + ) |
| 89 | + |
| 90 | + expected_error_msg = "Unexpected value: type is 'list' but items is None" |
| 91 | + with pytest.raises(Exception, match=expected_error_msg): |
| 92 | + schema._ParameterSchema__get_type() |
| 93 | + |
| 94 | + with pytest.raises(Exception, match=expected_error_msg): |
| 95 | + schema.to_param() |
| 96 | + |
| 97 | + |
| 98 | +def test_parameter_schema_unsupported_type_error(): |
| 99 | + """Tests that an unsupported type raises ValueError.""" |
| 100 | + unsupported_type = "datetime" |
| 101 | + schema = ParameterSchema( |
| 102 | + name="event_time", type=unsupported_type, description="When it happened" |
| 103 | + ) |
| 104 | + |
| 105 | + expected_error_msg = f"Unsupported schema type: {unsupported_type}" |
| 106 | + with pytest.raises(ValueError, match=expected_error_msg): |
| 107 | + schema._ParameterSchema__get_type() |
| 108 | + |
| 109 | + with pytest.raises(ValueError, match=expected_error_msg): |
| 110 | + schema.to_param() |
0 commit comments