|
| 1 | +import copy |
| 2 | + |
| 3 | +import pytest |
| 4 | +from pydantic import ValidationError |
| 5 | + |
| 6 | +from rules_validation_api.validators.available_action_validator import AvailableActionValidation |
| 7 | + |
| 8 | + |
| 9 | +# 🔍 Mandatory Fields |
| 10 | +class TestMandatoryFieldsSchemaValidations: |
| 11 | + |
| 12 | + def test_valid_minimal_input(self, valid_available_action): |
| 13 | + data = copy.deepcopy(valid_available_action) |
| 14 | + data.pop("ActionDescription") |
| 15 | + data.pop("UrlLink") |
| 16 | + data.pop("UrlLabel") |
| 17 | + action = AvailableActionValidation(**data) |
| 18 | + assert action.action_type == "ButtonWithAuthLink" |
| 19 | + assert action.action_code == "BookNBS" |
| 20 | + assert action.action_description is None |
| 21 | + assert action.url_link is None |
| 22 | + assert action.url_label is None |
| 23 | + |
| 24 | + def test_missing_required_fields(self, valid_available_action): |
| 25 | + data = copy.deepcopy(valid_available_action) |
| 26 | + data.pop("ActionType") |
| 27 | + data.pop("ExternalRoutingCode") |
| 28 | + with pytest.raises(ValidationError) as exc_info: |
| 29 | + AvailableActionValidation(**data) |
| 30 | + error_msg = str(exc_info.value) |
| 31 | + assert "ActionType" in error_msg |
| 32 | + assert "ExternalRoutingCode" in error_msg |
| 33 | + |
| 34 | + |
| 35 | +# 🔍 Optional Fields |
| 36 | +class TestOptionalFieldsSchemaValidations: |
| 37 | + |
| 38 | + def test_valid_full_input(self, valid_available_action): |
| 39 | + action = AvailableActionValidation(**valid_available_action) |
| 40 | + assert action.action_type == "ButtonWithAuthLink" |
| 41 | + assert action.action_code == "BookNBS" |
| 42 | + assert action.action_description == "" |
| 43 | + assert str(action.url_link) == "http://www.nhs.uk/book-rsv" |
| 44 | + assert action.url_label == "Continue to booking" |
| 45 | + |
| 46 | + def test_empty_string_is_valid_for_optional_fields(self, valid_available_action): |
| 47 | + action = AvailableActionValidation(**valid_available_action) |
| 48 | + assert action.action_description == "" |
| 49 | + assert action.url_label == "Continue to booking" |
| 50 | + |
| 51 | + @pytest.mark.parametrize("bad_url", ["not-a-url", "ftp://bad", "123"]) |
| 52 | + def test_invalid_url_raises_validation_error(self, valid_available_action, bad_url): |
| 53 | + data = copy.deepcopy(valid_available_action) |
| 54 | + data["UrlLink"] = bad_url |
| 55 | + with pytest.raises(ValidationError) as exc_info: |
| 56 | + AvailableActionValidation(**data) |
| 57 | + assert "UrlLink" in str(exc_info.value) |
0 commit comments