Cover validate_text assertions, fixture factories, marker registration, and pytest integration using pytester for subprocess testing.
89 lines
2.9 KiB
Python
89 lines
2.9 KiB
Python
"""Tests for the pytest plugin fixtures."""
|
|
|
|
from veritext.core.types import ValidationContext
|
|
from veritext.pytest_plugin.fixtures import ValidatorFactory
|
|
from veritext.validators import bleu, length
|
|
|
|
|
|
class TestValidatorFactory:
|
|
"""Test the ValidatorFactory class."""
|
|
|
|
def test_creates_validator_from_checks(self) -> None:
|
|
"""Test that factory creates a callable validator."""
|
|
factory = ValidatorFactory()
|
|
validate = factory(checks=[length(min_chars=5)])
|
|
|
|
result = validate("Hello, World!")
|
|
assert result.passed
|
|
|
|
def test_validator_uses_provided_reference(self) -> None:
|
|
"""Test that factory passes reference to context."""
|
|
factory = ValidatorFactory()
|
|
reference = "The quick brown fox."
|
|
validate = factory(
|
|
checks=[bleu(min_score=0.5)],
|
|
reference=reference,
|
|
)
|
|
|
|
# Exact match should pass
|
|
result = validate("The quick brown fox.")
|
|
assert result.passed
|
|
|
|
def test_validator_returns_validation_result(self) -> None:
|
|
"""Test that validator returns a ValidationResult."""
|
|
factory = ValidatorFactory()
|
|
validate = factory(checks=[length(min_chars=100)])
|
|
|
|
result = validate("Short")
|
|
assert not result.passed
|
|
assert len(result.checks) == 1
|
|
assert result.checks[0].name == "length"
|
|
|
|
|
|
class TestTextValidatorFixture:
|
|
"""Test the text_validator fixture."""
|
|
|
|
def test_fixture_returns_factory(self, text_validator: ValidatorFactory) -> None:
|
|
"""Test that fixture provides a ValidatorFactory."""
|
|
assert isinstance(text_validator, ValidatorFactory)
|
|
|
|
def test_fixture_can_create_validators(
|
|
self,
|
|
text_validator: ValidatorFactory,
|
|
) -> None:
|
|
"""Test that fixture can be used to create validators."""
|
|
validate = text_validator(checks=[length(min_chars=5, max_chars=50)])
|
|
|
|
assert validate("Hello, World!").passed
|
|
assert not validate("Hi").passed
|
|
|
|
|
|
class TestValidationContextFixture:
|
|
"""Test the validation_context fixture."""
|
|
|
|
def test_fixture_creates_context(
|
|
self,
|
|
validation_context: type,
|
|
) -> None:
|
|
"""Test that fixture creates ValidationContext."""
|
|
ctx = validation_context(reference="Test reference")
|
|
assert isinstance(ctx, ValidationContext)
|
|
assert ctx.reference == "Test reference"
|
|
|
|
def test_fixture_accepts_metadata(
|
|
self,
|
|
validation_context: type,
|
|
) -> None:
|
|
"""Test that fixture passes metadata to context."""
|
|
ctx = validation_context(reference="Test", source="unit_test", version=1)
|
|
assert ctx.metadata["source"] == "unit_test"
|
|
assert ctx.metadata["version"] == 1
|
|
|
|
def test_fixture_allows_no_reference(
|
|
self,
|
|
validation_context: type,
|
|
) -> None:
|
|
"""Test that fixture allows creating context without reference."""
|
|
ctx = validation_context()
|
|
assert ctx.reference is None
|