|
| 1 | +import pytest |
| 2 | +from unittest import TestCase |
| 3 | + |
| 4 | +from airbyte_cdk.sources.declarative.validators.predicate_validator import PredicateValidator |
| 5 | +from airbyte_cdk.sources.declarative.validators.validation_strategy import ValidationStrategy |
| 6 | + |
| 7 | + |
| 8 | +class MockValidationStrategy(ValidationStrategy): |
| 9 | + def __init__(self, should_fail=False, error_message="Validation failed"): |
| 10 | + self.should_fail = should_fail |
| 11 | + self.error_message = error_message |
| 12 | + self.validate_called = False |
| 13 | + self.validated_value = None |
| 14 | + |
| 15 | + def validate(self, value): |
| 16 | + self.validate_called = True |
| 17 | + self.validated_value = value |
| 18 | + if self.should_fail: |
| 19 | + raise ValueError(self.error_message) |
| 20 | + |
| 21 | + |
| 22 | +class TestPredicateValidator(TestCase): |
| 23 | + def test_given_valid_input_validate_is_successful(self): |
| 24 | + strategy = MockValidationStrategy() |
| 25 | + |
| 26 | + validator = PredicateValidator(value=test_value, strategy=strategy) |
| 27 | + |
| 28 | + validator.validate() |
| 29 | + |
| 30 | + assert strategy.validate_called |
| 31 | + assert strategy.validated_value == test_value |
| 32 | + |
| 33 | + def test_given_invalid_input_when_validate_then_raise_value_error(self): |
| 34 | + error_message = "Invalid email format" |
| 35 | + strategy = MockValidationStrategy(should_fail=True, error_message=error_message) |
| 36 | + test_value = "invalid-email" |
| 37 | + validator = PredicateValidator(value=test_value, strategy=strategy) |
| 38 | + |
| 39 | + with pytest.raises(ValueError) as context: |
| 40 | + validator.validate() |
| 41 | + |
| 42 | + assert error_message in str(context.exception) |
| 43 | + assert strategy.validate_called |
| 44 | + assert strategy.validated_value == test_value |
| 45 | + |
| 46 | + def test_given_complex_object_when_validate_then_successful(self): |
| 47 | + strategy = MockValidationStrategy() |
| 48 | + test_value = { "user": { "email": "[email protected]", "name": "Test User"}} |
| 49 | + validator = PredicateValidator(value=test_value, strategy=strategy) |
| 50 | + |
| 51 | + validator.validate() |
| 52 | + |
| 53 | + assert strategy.validate_called |
| 54 | + assert strategy.validated_value == test_value |
0 commit comments