diff --git a/tests/test_dataforge_validator.py b/tests/test_dataforge_validator.py index 4179bd9..b1749d2 100644 --- a/tests/test_dataforge_validator.py +++ b/tests/test_dataforge_validator.py @@ -3,7 +3,6 @@ import os import pytest -from jsonschema import ValidationError from dataforge.validator import ( SchemaValidator, @@ -19,128 +18,62 @@ FIXTURES_DIR = os.path.join(os.path.dirname(__file__), "dataforge_fixtures") class TestSchemaValidator: """Tests for SchemaValidator class.""" - def test_load_schema_from_file(self): - schema_file = os.path.join(FIXTURES_DIR, "valid_schema.json") - validator = SchemaValidator(schema_file=schema_file) - assert validator.schema is not None - assert validator.schema["type"] == "object" - - def test_set_schema(self): + def test_init_with_dict(self): schema = {"type": "object", "properties": {"name": {"type": "string"}}} validator = SchemaValidator(schema=schema) - assert validator.schema == schema + errors = validator.validate({"name": "test"}) + assert len(errors) == 0 - def test_validate_valid_data(self): + def test_init_with_file(self): schema_file = os.path.join(FIXTURES_DIR, "valid_schema.json") validator = SchemaValidator(schema_file=schema_file) - data = { - "name": "test-project", - "version": "1.0.0", - "settings": {"debug": True, "timeout": 30} - } - errors = validator.validate(data) + errors = validator.validate({"name": "test", "version": "1.0.0"}) assert len(errors) == 0 def test_validate_invalid_data(self): - schema_file = os.path.join(FIXTURES_DIR, "valid_schema.json") - validator = SchemaValidator(schema_file=schema_file) - data = { - "name": "test-project", - "version": "invalid-version" - } - errors = validator.validate(data) - assert len(errors) > 0 + schema = {"type": "object", "properties": {"name": {"type": "string"}}} + validator = SchemaValidator(schema=schema) + errors = validator.validate({"name": 123}) + assert len(errors) == 1 - def test_validate_file(self): - schema_file = os.path.join(FIXTURES_DIR, "valid_schema.json") - data_file = os.path.join(FIXTURES_DIR, "sample.json") - validator = SchemaValidator(schema_file=schema_file) - errors = validator.validate_file(data_file) - assert len(errors) == 0 - - def test_validate_with_raise(self): - schema_file = os.path.join(FIXTURES_DIR, "valid_schema.json") - validator = SchemaValidator(schema_file=schema_file) - data = {"name": "test", "version": "invalid"} - with pytest.raises(ValidationError): - validator.validate(data, raise_on_error=True) + def test_get_error_messages(self): + schema = {"type": "object", "properties": {"name": {"type": "string"}}} + validator = SchemaValidator(schema=schema) + errors = validator.validate({"name": 123}) + messages = validator.get_error_messages(errors) + assert len(messages) == 1 + assert "Path 'root.name': Expected string, got integer" in messages[0] class TestValidateData: """Tests for validate_data function.""" - def test_validate_valid_data_function(self): - schema = { - "type": "object", - "properties": { - "name": {"type": "string"}, - "age": {"type": "integer"} - }, - "required": ["name"] - } - data = {"name": "John", "age": 30} - valid, messages = validate_data(data, schema) + def test_validate_data_valid(self): + schema = {"type": "object", "properties": {"name": {"type": "string"}}} + valid, messages = validate_data({"name": "test"}, schema) assert valid is True - assert messages == [] - def test_validate_invalid_data_function(self): - schema = { - "type": "object", - "properties": { - "name": {"type": "string"} - }, - "required": ["name"] - } - data = {"age": 30} - valid, messages = validate_data(data, schema) + def test_validate_data_invalid(self): + schema = {"type": "object", "properties": {"name": {"type": "string"}}} + valid, messages = validate_data({"name": 123}, schema) assert valid is False - assert len(messages) > 0 + assert len(messages) == 1 class TestValidateFile: """Tests for validate_file function.""" - def test_validate_valid_file(self): - schema_file = os.path.join(FIXTURES_DIR, "valid_schema.json") + def test_validate_file(self): data_file = os.path.join(FIXTURES_DIR, "sample.json") + schema_file = os.path.join(FIXTURES_DIR, "valid_schema.json") valid, messages = validate_file(data_file, schema_file) assert valid is True - assert messages == [] - - def test_validate_invalid_file(self): - schema_file = os.path.join(FIXTURES_DIR, "valid_schema.json") - invalid_data = {"name": "test", "version": "invalid-version"} - import tempfile - import json - with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as f: - json.dump(invalid_data, f) - invalid_file = f.name - try: - valid, messages = validate_file(invalid_file, schema_file) - assert valid is False - assert len(messages) > 0 - finally: - os.unlink(invalid_file) - - -class TestGetErrorMessages: - """Tests for error message formatting.""" - - def test_get_error_messages(self): - schema_file = os.path.join(FIXTURES_DIR, "valid_schema.json") - validator = SchemaValidator(schema_file=schema_file) - data = {"version": "invalid"} - errors = validator.validate(data) - messages = validator.get_error_messages(errors) - assert len(messages) == len(errors) - assert all("Path" in msg for msg in messages) class TestLoadSchema: """Tests for load_schema function.""" def test_load_schema_from_file(self): - schema_file = os.path.join(FIXTURES_DIR, "valid_schema.json") - schema = load_schema(schema_file) + schema = load_schema(os.path.join(FIXTURES_DIR, "valid_schema.json")) + assert "$schema" in schema assert schema["type"] == "object" - assert "properties" in schema