Skip to content

Commit 5f6ef58

Browse files
Sambit Kumar Mishrasambit-mishra-NHSD
authored andcommitted
Changes for ELI-595
1 parent 61cc4ac commit 5f6ef58

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

src/rules_validation_api/validators/iteration_validator.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,27 @@ def transform_actions_mapper(cls, action_mapper: ActionsMapper) -> ActionsMapper
9797
action_mapper.root = new_root
9898
return action_mapper
9999

100+
@model_validator(mode="after")
101+
def validate_rule_cohort_labels_against_iteration_cohorts(self) -> typing.Self:
102+
allowed_labels = {cohort.cohort_label for cohort in self.iteration_cohorts}
103+
print(allowed_labels)
104+
105+
for idx, rule in enumerate(self.iteration_rules):
106+
if rule.cohort_label is None:
107+
continue
108+
print("parsed cohort labels from rules:")
109+
print(rule.parsed_cohort_labels)
110+
if not all(label in allowed_labels for label in rule.parsed_cohort_labels):
111+
allowed_str = ", ".join(sorted(allowed_labels))
112+
msg = (
113+
f"Invalid cohort_label value: {rule.cohort_label}. "
114+
f"Allowed values: {allowed_str}. "
115+
f"Rule index: {idx}"
116+
)
117+
raise ValueError(msg)
118+
119+
return self
120+
100121
@model_validator(mode="after")
101122
def action_mapper_validation(self) -> typing.Self:
102123
all_errors = []

tests/unit/validation/test_iteration_validator.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,3 +570,19 @@ def test_iteration_full_datetime_validation( # noqa : PLR0913
570570
f"Failed! Input: {iteration_time_input}, Default: {default_time_iteration_input}. "
571571
f"Expected {expected_date_time} but got {result}"
572572
)
573+
574+
def test_iteration_rules_having_invalid_cohort_labels_throws_error(
575+
self, valid_iteration_with_only_mandatory_fields,
576+
valid_iteration_rule_with_only_mandatory_fields,
577+
valid_iteration_cohorts,
578+
):
579+
data = valid_iteration_with_only_mandatory_fields.copy()
580+
data["iteration_rules"] = [valid_iteration_rule_with_only_mandatory_fields]
581+
data["iteration_cohorts"] = [valid_iteration_cohorts]
582+
data["iteration_rules"][0]["CohortLabel"] = "label_2"
583+
IterationValidation(**(data))
584+
# try:
585+
586+
# IterationValidation(**(data))
587+
# except ValidationError as e:
588+
# pytest.fail(f"Unexpected error during model instantiation: {e}")

0 commit comments

Comments
 (0)