Skip to content

Commit 72207d1

Browse files
wip with failing tests
1 parent 050da51 commit 72207d1

2 files changed

Lines changed: 64 additions & 7 deletions

File tree

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from pydantic import Field, field_validator
1+
from pydantic import Field, field_validator, model_validator, ValidationError
2+
from pydantic_core import PydanticCustomError, InitErrorDetails
23

34
from eligibility_signposting_api.model.campaign_config import CampaignConfig
45
from rules_validation_api.validators.iteration_validator import IterationValidation
@@ -13,9 +14,29 @@ def validate_name(cls, value: str) -> str:
1314
raise ValueError("campaign ID must not be empty")
1415
return value
1516

16-
@field_validator("type")
17-
def validate_type(cls, value: str) -> str:
18-
allowed_values = {"V", "S"}
19-
if value not in allowed_values:
20-
raise ValueError(f"type must be one of {allowed_values}")
21-
return value
17+
@model_validator(mode="before")
18+
def validate_default_comms_routing_in_actions_mapper(cls, values):
19+
iterations = values.get("Iterations") or values.get("iterations")
20+
if not iterations:
21+
return values
22+
23+
error_list = []
24+
25+
for i, iter_data in enumerate(iterations):
26+
default_routing = iter_data.get("DefaultCommsRouting")
27+
actions_mapper = iter_data.get("ActionsMapper", {})
28+
29+
if default_routing and default_routing not in actions_mapper:
30+
error_list.append(
31+
InitErrorDetails(
32+
loc=("Iterations", i, "ActionsMapper"),
33+
msg=f"Missing key '{default_routing}' in ActionsMapper",
34+
input=actions_mapper,
35+
type="value_error.missing_actions_mapper_entry"
36+
)
37+
)
38+
39+
if error_list:
40+
raise ValidationError(error_list, cls)
41+
42+
return values

tests/unit/validation/test_campaign_config_validator.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,3 +233,39 @@ def test_validate_iterations_non_empty(self, valid_campaign_config_with_only_man
233233
CampaignConfigValidation(**data)
234234
errors = error.value.errors()
235235
assert any(e["loc"][-1] == "Iterations" for e in errors), "Expected validation error on 'Iterations'"
236+
237+
def test_actions_mapper_has_default_comms_routing_key(self, valid_campaign_config_with_only_mandatory_fields):
238+
expected_action = {
239+
"ExternalRoutingCode": "BookLocal",
240+
"ActionDescription": "##Getting the vaccine\n"
241+
"You can get an RSV vaccination at your GP surgery.\n"
242+
"Your GP surgery may contact you about getting the RSV vaccine. "
243+
"This may be by letter, text, phone call, email or through the NHS App. "
244+
"You do not need to wait to be contacted before booking your vaccination.",
245+
"ActionType": "InfoText"
246+
}
247+
248+
config = {**valid_campaign_config_with_only_mandatory_fields}
249+
config["Iterations"][0]["DefaultCommsRouting"] = "BOOK_LOCAL"
250+
config["Iterations"][0]["ActionsMapper"] = {
251+
"BOOK_LOCAL": expected_action
252+
}
253+
254+
# Should not raise error
255+
CampaignConfigValidation(**config)
256+
257+
def test_actions_mapper_missing_default_comms_routing_key(self, valid_campaign_config_with_only_mandatory_fields):
258+
config = {**valid_campaign_config_with_only_mandatory_fields}
259+
config["Iterations"][0]["DefaultCommsRouting"] = "BOOK_LOCAL"
260+
config["Iterations"][0]["ActionsMapper"] = {} # Missing BOOK_LOCAL
261+
262+
with pytest.raises(ValidationError) as error:
263+
CampaignConfigValidation(**config)
264+
265+
errors = error.value.errors()
266+
assert any(
267+
e["loc"][-1] == "ActionsMapper" and "BOOK_LOCAL" in str(e["msg"])
268+
for e in errors
269+
), "Expected validation error for missing BOOK_LOCAL entry in ActionsMapper"
270+
271+

0 commit comments

Comments
 (0)