|
| 1 | +import pytest |
| 2 | +from unittest import TestCase |
| 3 | + |
| 4 | +from airbyte_cdk.sources.declarative.validators.dpath_validator import DpathValidator |
| 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 TestDpathValidator(TestCase): |
| 23 | + def test_given_valid_path_and_input_validate_is_successful(self): |
| 24 | + strategy = MockValidationStrategy() |
| 25 | + validator = DpathValidator(field_path=["user", "profile", "email"], strategy=strategy) |
| 26 | + |
| 27 | + test_data = { "user": { "profile": { "email": "[email protected]", "name": "Test User"}}} |
| 28 | + |
| 29 | + validator.validate(test_data) |
| 30 | + |
| 31 | + assert strategy.validate_called |
| 32 | + assert strategy.validated_value |
| 33 | + |
| 34 | + def test_given_invalid_path_when_validate_then_raise_key_error(self): |
| 35 | + strategy = MockValidationStrategy() |
| 36 | + validator = DpathValidator(field_path=["user", "profile", "phone"], strategy=strategy) |
| 37 | + |
| 38 | + test_data = { "user": { "profile": { "email": "[email protected]"}}} |
| 39 | + |
| 40 | + with pytest.raises(KeyError) as context: |
| 41 | + validator.validate(test_data) |
| 42 | + assert "Error validating path" in str(context.exception) |
| 43 | + assert not strategy.validate_called |
| 44 | + |
| 45 | + def test_given_strategy_fails_when_validate_then_raise_value_error(self): |
| 46 | + error_message = "Invalid email format" |
| 47 | + strategy = MockValidationStrategy(should_fail=True, error_message=error_message) |
| 48 | + validator = DpathValidator(field_path=["user", "email"], strategy=strategy) |
| 49 | + |
| 50 | + test_data = {"user": {"email": "invalid-email"}} |
| 51 | + |
| 52 | + with pytest.raises(ValueError) as context: |
| 53 | + validator.validate(test_data) |
| 54 | + |
| 55 | + assert "Error validating value" in str(context.exception) |
| 56 | + assert error_message in str(context.exception) |
| 57 | + assert strategy.validate_called |
| 58 | + assert strategy.validated_value == "invalid-email" |
| 59 | + |
| 60 | + def test_given_empty_path_list_when_validate_then_validate_raises_exception(self): |
| 61 | + strategy = MockValidationStrategy() |
| 62 | + validator = DpathValidator(field_path=[], strategy=strategy) |
| 63 | + test_data = {"key": "value"} |
| 64 | + |
| 65 | + with pytest.raises(ValueError): |
| 66 | + validator.validate(test_data) |
| 67 | + |
| 68 | + def test_given_empty_input_data_when_validate_then_validate_raises_exception(self): |
| 69 | + strategy = MockValidationStrategy() |
| 70 | + validator = DpathValidator(field_path=["data", "field"], strategy=strategy) |
| 71 | + |
| 72 | + test_data = {} |
| 73 | + |
| 74 | + with pytest.raises(KeyError): |
| 75 | + validator.validate(test_data) |
| 76 | + |
| 77 | + def test_path_with_wildcard_when_validate_then_validate_is_successful(self): |
| 78 | + strategy = MockValidationStrategy() |
| 79 | + validator = DpathValidator(field_path=["users", "*", "email"], strategy=strategy) |
| 80 | + |
| 81 | + test_data = { |
| 82 | + "users": { |
| 83 | + "user1": { "email": "[email protected]", "name": "User One"}, |
| 84 | + "user2": { "email": "[email protected]", "name": "User Two"}, |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + validator.validate(test_data) |
| 89 | + |
| 90 | + assert strategy.validate_called |
| 91 | + assert strategy. validated_value in [ "[email protected]", "[email protected]"] |
| 92 | + self. assertIn( strategy. validated_value, [ "[email protected]", "[email protected]"]) |
0 commit comments