Some checks failed
CI / test (ubuntu-latest, 3.10) (push) Has been cancelled
CI / test (ubuntu-latest, 3.11) (push) Has been cancelled
CI / test (ubuntu-latest, 3.12) (push) Has been cancelled
CI / test (ubuntu-latest, 3.8) (push) Has been cancelled
CI / test (ubuntu-latest, 3.9) (push) Has been cancelled
CI / test-minimal (push) Has been cancelled
CI / lint (push) Has been cancelled
CI / build (push) Has been cancelled
CI / release (push) Has been cancelled
80 lines
2.6 KiB
Python
80 lines
2.6 KiB
Python
"""Tests for validator module."""
|
|
|
|
import os
|
|
|
|
import pytest
|
|
|
|
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_init_with_dict(self):
|
|
schema = {"type": "object", "properties": {"name": {"type": "string"}}}
|
|
validator = SchemaValidator(schema=schema)
|
|
errors = validator.validate({"name": "test"})
|
|
assert len(errors) == 0
|
|
|
|
def test_init_with_file(self):
|
|
schema_file = os.path.join(FIXTURES_DIR, "valid_schema.json")
|
|
validator = SchemaValidator(schema_file=schema_file)
|
|
errors = validator.validate({"name": "test", "version": "1.0.0"})
|
|
assert len(errors) == 0
|
|
|
|
def test_validate_invalid_data(self):
|
|
schema = {"type": "object", "properties": {"name": {"type": "string"}}}
|
|
validator = SchemaValidator(schema=schema)
|
|
errors = validator.validate({"name": 123})
|
|
assert len(errors) == 1
|
|
|
|
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_data_valid(self):
|
|
schema = {"type": "object", "properties": {"name": {"type": "string"}}}
|
|
valid, messages = validate_data({"name": "test"}, schema)
|
|
assert valid is True
|
|
|
|
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) == 1
|
|
|
|
|
|
class TestValidateFile:
|
|
"""Tests for validate_file function."""
|
|
|
|
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
|
|
|
|
|
|
class TestLoadSchema:
|
|
"""Tests for load_schema function."""
|
|
|
|
def test_load_schema_from_file(self):
|
|
schema = load_schema(os.path.join(FIXTURES_DIR, "valid_schema.json"))
|
|
assert "$schema" in schema
|
|
assert schema["type"] == "object"
|