Skip to content

Commit 4d554ee

Browse files
lint fixes
1 parent f9f99ab commit 4d554ee

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1+
from pydantic import model_validator
2+
13
from eligibility_signposting_api.model.campaign_config import ActionsMapper
24

35

46
class ActionsMapperValidator(ActionsMapper):
5-
pass
7+
@model_validator(mode="after")
8+
def validate_keys(self) -> "ActionsMapperValidator":
9+
invalid_keys = [key for key in self.root if key is None or key == ""]
10+
if invalid_keys:
11+
msg = f"Invalid keys found in ActionsMapper: {invalid_keys}"
12+
raise ValueError(msg)
13+
return self
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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

Comments
 (0)