|
| 1 | +import pytest |
| 2 | +from pydantic import ValidationError |
| 3 | + |
| 4 | +from eligibility_signposting_api.model.campaign_config import AvailableAction |
| 5 | +from rules_validation_api.validators.actions_mapper_validator import ActionsMapperValidator |
| 6 | + |
| 7 | + |
| 8 | +@pytest.fixture |
| 9 | +def valid_available_action(): |
| 10 | + return { |
| 11 | + "ExternalRoutingCode": "BookNBS", |
| 12 | + "ActionDescription": "", |
| 13 | + "ActionType": "ButtonWithAuthLink", |
| 14 | + "UrlLink": "http://www.nhs.uk/book-rsv", |
| 15 | + "UrlLabel": "Continue to booking", |
| 16 | + } |
| 17 | + |
| 18 | + |
| 19 | +class TestBUCValidations: |
| 20 | + def make_action(self, data: dict) -> AvailableAction: |
| 21 | + return AvailableAction(**data) |
| 22 | + |
| 23 | + def test_valid_actions_mapper(self, valid_available_action): |
| 24 | + data = { |
| 25 | + "action1": self.make_action(valid_available_action), |
| 26 | + "action2": self.make_action({**valid_available_action, "ExternalRoutingCode": "AltCode"}), |
| 27 | + } |
| 28 | + mapper = ActionsMapperValidator(root=data) |
| 29 | + |
| 30 | + expected_action_count = 2 |
| 31 | + assert isinstance(mapper, ActionsMapperValidator) |
| 32 | + assert len(mapper.root) == expected_action_count |
| 33 | + |
| 34 | + def test_invalid_actions_mapper_empty_key(self, valid_available_action): |
| 35 | + data = {"": self.make_action(valid_available_action), "action2": self.make_action(valid_available_action)} |
| 36 | + with pytest.raises(ValidationError) as exc_info: |
| 37 | + ActionsMapperValidator(root=data) |
| 38 | + assert "Invalid keys found in ActionsMapper" in str(exc_info.value) |
| 39 | + assert "['']" in str(exc_info.value) |
| 40 | + |
| 41 | + @pytest.mark.parametrize("bad_key", [""]) |
| 42 | + def test_invalid_keys_parametrized(self, bad_key, valid_available_action): |
| 43 | + data = { |
| 44 | + bad_key: self.make_action(valid_available_action), |
| 45 | + "valid_key": self.make_action(valid_available_action), |
| 46 | + } |
| 47 | + with pytest.raises(ValidationError) as exc_info: |
| 48 | + ActionsMapperValidator(root=data) |
| 49 | + assert "Invalid keys found in ActionsMapper" in str(exc_info.value) |
0 commit comments