|
| 1 | +"""Tests for the JsonSchema model, including the `examples` field.""" |
| 2 | + |
| 3 | +from utcp.data.tool import JsonSchema, JsonSchemaSerializer |
| 4 | + |
| 5 | + |
| 6 | +def test_jsonschema_examples_field_is_typed(): |
| 7 | + """`examples` is a declared field, not just an extra attribute.""" |
| 8 | + assert "examples" in JsonSchema.model_fields |
| 9 | + |
| 10 | + schema = JsonSchema(type="string", examples=["user123", "user456"]) |
| 11 | + assert schema.examples == ["user123", "user456"] |
| 12 | + |
| 13 | + |
| 14 | +def test_jsonschema_examples_default_none(): |
| 15 | + """`examples` defaults to None when absent.""" |
| 16 | + schema = JsonSchema(type="string") |
| 17 | + assert schema.examples is None |
| 18 | + |
| 19 | + |
| 20 | +def test_jsonschema_examples_roundtrip(): |
| 21 | + """`examples` survives serialize -> validate roundtrip.""" |
| 22 | + serializer = JsonSchemaSerializer() |
| 23 | + schema = JsonSchema( |
| 24 | + type="object", |
| 25 | + examples=[{"id": "user123", "name": "John Doe"}], |
| 26 | + ) |
| 27 | + |
| 28 | + as_dict = serializer.to_dict(schema) |
| 29 | + assert as_dict["examples"] == [{"id": "user123", "name": "John Doe"}] |
| 30 | + |
| 31 | + restored = serializer.validate_dict(as_dict) |
| 32 | + assert restored.examples == schema.examples |
| 33 | + |
| 34 | + |
| 35 | +def test_jsonschema_examples_allows_mixed_json_types(): |
| 36 | + """`examples` accepts any JSON value (string, bool, number, object).""" |
| 37 | + schema = JsonSchema(examples=["a", True, 1, 1.5, None, {"k": "v"}, [1, 2]]) |
| 38 | + assert schema.examples == ["a", True, 1, 1.5, None, {"k": "v"}, [1, 2]] |
0 commit comments