|
14 | 14 |
|
15 | 15 |
|
16 | 16 | from inspect import Parameter
|
17 |
| -from typing import Optional |
| 17 | +from typing import Any, Optional |
18 | 18 |
|
19 | 19 | import pytest
|
20 | 20 |
|
@@ -170,3 +170,102 @@ def test_parameter_schema_array_optional():
|
170 | 170 | assert param.annotation == expected_type
|
171 | 171 | assert param.kind == Parameter.POSITIONAL_OR_KEYWORD
|
172 | 172 | assert param.default is None
|
| 173 | + |
| 174 | + |
| 175 | +def test_parameter_schema_map_generic(): |
| 176 | + """Tests ParameterSchema with a generic 'map' type.""" |
| 177 | + schema = ParameterSchema(name="metadata", type="map", description="Some metadata") |
| 178 | + expected_type = dict[str, Any] |
| 179 | + assert schema._ParameterSchema__get_type() == expected_type |
| 180 | + |
| 181 | + param = schema.to_param() |
| 182 | + assert isinstance(param, Parameter) |
| 183 | + assert param.name == "metadata" |
| 184 | + assert param.annotation == expected_type |
| 185 | + assert param.kind == Parameter.POSITIONAL_OR_KEYWORD |
| 186 | + |
| 187 | + |
| 188 | +def test_parameter_schema_map_typed_string(): |
| 189 | + """Tests ParameterSchema with a typed 'map' type (string values).""" |
| 190 | + schema = ParameterSchema( |
| 191 | + name="headers", |
| 192 | + type="map", |
| 193 | + description="HTTP headers", |
| 194 | + valueType="string", |
| 195 | + ) |
| 196 | + expected_type = dict[str, str] |
| 197 | + assert schema._ParameterSchema__get_type() == expected_type |
| 198 | + |
| 199 | + param = schema.to_param() |
| 200 | + assert param.annotation == expected_type |
| 201 | + |
| 202 | + |
| 203 | +def test_parameter_schema_map_typed_integer(): |
| 204 | + """Tests ParameterSchema with a typed 'map' type (integer values).""" |
| 205 | + schema = ParameterSchema( |
| 206 | + name="user_scores", |
| 207 | + type="map", |
| 208 | + description="User scores", |
| 209 | + valueType="integer", |
| 210 | + ) |
| 211 | + expected_type = dict[str, int] |
| 212 | + assert schema._ParameterSchema__get_type() == expected_type |
| 213 | + param = schema.to_param() |
| 214 | + assert param.annotation == expected_type |
| 215 | + |
| 216 | + |
| 217 | +def test_parameter_schema_map_typed_float(): |
| 218 | + """Tests ParameterSchema with a typed 'map' type (float values).""" |
| 219 | + schema = ParameterSchema( |
| 220 | + name="item_prices", |
| 221 | + type="map", |
| 222 | + description="Item prices", |
| 223 | + valueType="float", |
| 224 | + ) |
| 225 | + expected_type = dict[str, float] |
| 226 | + assert schema._ParameterSchema__get_type() == expected_type |
| 227 | + param = schema.to_param() |
| 228 | + assert param.annotation == expected_type |
| 229 | + |
| 230 | + |
| 231 | +def test_parameter_schema_map_typed_boolean(): |
| 232 | + """Tests ParameterSchema with a typed 'map' type (boolean values).""" |
| 233 | + schema = ParameterSchema( |
| 234 | + name="feature_flags", |
| 235 | + type="map", |
| 236 | + description="Feature flags", |
| 237 | + valueType="boolean", |
| 238 | + ) |
| 239 | + expected_type = dict[str, bool] |
| 240 | + assert schema._ParameterSchema__get_type() == expected_type |
| 241 | + param = schema.to_param() |
| 242 | + assert param.annotation == expected_type |
| 243 | + |
| 244 | + |
| 245 | +def test_parameter_schema_map_optional(): |
| 246 | + """Tests an optional ParameterSchema with a 'map' type.""" |
| 247 | + schema = ParameterSchema( |
| 248 | + name="optional_metadata", |
| 249 | + type="map", |
| 250 | + description="Optional metadata", |
| 251 | + required=False, |
| 252 | + ) |
| 253 | + expected_type = Optional[dict[str, Any]] |
| 254 | + assert schema._ParameterSchema__get_type() == expected_type |
| 255 | + param = schema.to_param() |
| 256 | + assert param.annotation == expected_type |
| 257 | + assert param.default is None |
| 258 | + |
| 259 | + |
| 260 | +def test_parameter_schema_map_unsupported_value_type_error(): |
| 261 | + """Tests that an unsupported map valueType raises ValueError.""" |
| 262 | + unsupported_type = "custom_object" |
| 263 | + schema = ParameterSchema( |
| 264 | + name="custom_data", |
| 265 | + type="map", |
| 266 | + description="Custom data map", |
| 267 | + valueType=unsupported_type, |
| 268 | + ) |
| 269 | + expected_error_msg = f"Unsupported map value type: {unsupported_type}" |
| 270 | + with pytest.raises(ValueError, match=expected_error_msg): |
| 271 | + schema._ParameterSchema__get_type() |
0 commit comments