"""Tests for validator module.""" import os import pytest from jsonschema import ValidationError from dataforge.validator import ( SchemaValidator, validate_data, validate_file, load_schema, ) 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): schema = {"type": "object", "properties": {"name": {"type": "string"}}} validator = SchemaValidator(schema=schema) assert validator.schema == schema def test_validate_valid_data(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) 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 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) 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) 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) assert valid is False assert len(messages) > 0 class TestValidateFile: """Tests for validate_file function.""" def test_validate_valid_file(self): schema_file = os.path.join(FIXTURES_DIR, "valid_schema.json") data_file = os.path.join(FIXTURES_DIR, "sample.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) assert schema["type"] == "object" assert "properties" in schema