134 lines
4.8 KiB
Python
134 lines
4.8 KiB
Python
"""Tests for schema inference and validation."""
|
|
|
|
from config_converter.validators import SchemaInferrer, SchemaValidator
|
|
from config_converter.validators.schema import SchemaType
|
|
|
|
|
|
class TestSchemaInferrer:
|
|
"""Tests for schema inference."""
|
|
|
|
def test_infer_null(self) -> None:
|
|
"""Test inferring null type."""
|
|
inferrer = SchemaInferrer()
|
|
schema = inferrer.infer(None)
|
|
assert schema.root_type == SchemaType.NULL
|
|
|
|
def test_infer_boolean(self) -> None:
|
|
"""Test inferring boolean type."""
|
|
inferrer = SchemaInferrer()
|
|
schema = inferrer.infer(True)
|
|
assert schema.root_type == SchemaType.BOOLEAN
|
|
|
|
def test_infer_integer(self) -> None:
|
|
"""Test inferring integer type."""
|
|
inferrer = SchemaInferrer()
|
|
schema = inferrer.infer(42)
|
|
assert schema.root_type == SchemaType.INTEGER
|
|
|
|
def test_infer_float(self) -> None:
|
|
"""Test inferring float type."""
|
|
inferrer = SchemaInferrer()
|
|
schema = inferrer.infer(3.14)
|
|
assert schema.root_type == SchemaType.NUMBER
|
|
|
|
def test_infer_string(self) -> None:
|
|
"""Test inferring string type."""
|
|
inferrer = SchemaInferrer()
|
|
schema = inferrer.infer("hello")
|
|
assert schema.root_type == SchemaType.STRING
|
|
|
|
def test_infer_array(self) -> None:
|
|
"""Test inferring array type."""
|
|
inferrer = SchemaInferrer()
|
|
schema = inferrer.infer([1, 2, 3])
|
|
assert schema.root_type == SchemaType.ARRAY
|
|
|
|
def test_infer_object(self) -> None:
|
|
"""Test inferring object type."""
|
|
inferrer = SchemaInferrer()
|
|
schema = inferrer.infer({"key": "value"})
|
|
assert schema.root_type == SchemaType.OBJECT
|
|
assert len(schema.properties) == 1
|
|
assert schema.properties[0].name == "key"
|
|
assert schema.properties[0].type == SchemaType.STRING
|
|
|
|
def test_infer_nested_object(self) -> None:
|
|
"""Test inferring nested object type."""
|
|
inferrer = SchemaInferrer()
|
|
data = {"outer": {"inner": "value"}}
|
|
schema = inferrer.infer(data)
|
|
assert schema.root_type == SchemaType.OBJECT
|
|
outer_prop = schema.properties[0]
|
|
assert outer_prop.name == "outer"
|
|
assert outer_prop.type == SchemaType.OBJECT
|
|
|
|
def test_infer_object_with_multiple_properties(self) -> None:
|
|
"""Test inferring object with multiple properties."""
|
|
inferrer = SchemaInferrer()
|
|
data = {
|
|
"name": "test",
|
|
"age": 25,
|
|
"active": True,
|
|
"score": 95.5,
|
|
}
|
|
schema = inferrer.infer(data)
|
|
assert schema.root_type == SchemaType.OBJECT
|
|
prop_types = {p.name: p.type for p in schema.properties}
|
|
assert prop_types["name"] == SchemaType.STRING
|
|
assert prop_types["age"] == SchemaType.INTEGER
|
|
assert prop_types["active"] == SchemaType.BOOLEAN
|
|
assert prop_types["score"] == SchemaType.NUMBER
|
|
|
|
def test_infer_array_of_objects(self) -> None:
|
|
"""Test inferring array of objects."""
|
|
inferrer = SchemaInferrer()
|
|
data = [{"name": "a"}, {"name": "b"}]
|
|
schema = inferrer.infer(data)
|
|
assert schema.root_type == SchemaType.ARRAY
|
|
assert schema.items is not None
|
|
assert schema.items.type == SchemaType.OBJECT
|
|
|
|
|
|
class TestSchemaValidator:
|
|
"""Tests for schema validation."""
|
|
|
|
def test_validate_valid_object(self) -> None:
|
|
"""Test validating valid object data."""
|
|
inferrer = SchemaInferrer()
|
|
data = {"name": "test", "age": 25}
|
|
schema = inferrer.infer(data)
|
|
validator = SchemaValidator(schema)
|
|
is_valid, errors = validator.validate(data)
|
|
assert is_valid is True
|
|
assert len(errors) == 0
|
|
|
|
def test_validate_invalid_type(self) -> None:
|
|
"""Test validating data with wrong type."""
|
|
inferrer = SchemaInferrer()
|
|
schema = inferrer.infer({"name": "test", "age": 25})
|
|
validator = SchemaValidator(schema)
|
|
is_valid, errors = validator.validate({"name": "test", "age": "not a number"})
|
|
assert is_valid is False
|
|
assert len(errors) > 0
|
|
|
|
def test_validate_missing_required_property(self) -> None:
|
|
"""Test validating with missing required property."""
|
|
inferrer = SchemaInferrer()
|
|
data = {"name": "test"}
|
|
schema = inferrer.infer({"name": "test", "age": 25})
|
|
validator = SchemaValidator(schema)
|
|
is_valid, errors = validator.validate(data)
|
|
assert is_valid is False
|
|
|
|
|
|
class TestInferredSchema:
|
|
"""Tests for InferredSchema class."""
|
|
|
|
def test_to_dict(self) -> None:
|
|
"""Test converting schema to dictionary."""
|
|
inferrer = SchemaInferrer()
|
|
schema = inferrer.infer({"key": "value"})
|
|
schema_dict = schema.to_dict()
|
|
assert "root_type" in schema_dict
|
|
assert "properties" in schema_dict
|