Skip to content

Commit 59734d1

Browse files
Sambit Kumar Mishrasambit-mishra-NHSD
authored andcommitted
Resolving copilot code smells
1 parent 0bb1fb6 commit 59734d1

2 files changed

Lines changed: 25 additions & 8 deletions

File tree

src/rules_validation_api/validators/iteration_validator.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,26 @@ def transform_actions_mapper(cls, action_mapper: ActionsMapper) -> ActionsMapper
100100
@model_validator(mode="after")
101101
def validate_rule_cohort_labels_against_iteration_cohorts(self) -> typing.Self:
102102
allowed_labels = {cohort.cohort_label for cohort in self.iteration_cohorts}
103+
line_errors: list[InitErrorDetails] = []
103104

104105
for idx, rule in enumerate(self.iteration_rules):
105106
if rule.cohort_label is None:
106107
continue
107-
if not all(label in allowed_labels for label in rule.parsed_cohort_labels):
108-
allowed_str = ", ".join(sorted(allowed_labels))
109-
msg = (
110-
f"Invalid cohort_label value: {rule.cohort_label}. Allowed values: {allowed_str}. Rule index: {idx}"
111-
)
112-
raise ValueError(msg)
108+
109+
for label in rule.parsed_cohort_labels:
110+
if label not in allowed_labels:
111+
allowed_str = ", ".join(sorted(allowed_labels))
112+
error = InitErrorDetails(
113+
type="value_error",
114+
loc=("iteration_rules", idx, "cohort_label"),
115+
input=rule.cohort_label,
116+
ctx={
117+
"error": f"Invalid cohort_label value '{label}'. Allowed values: {allowed_str}."
118+
},
119+
)
120+
line_errors.append(error)
121+
if line_errors:
122+
raise ValidationError.from_exception_data(title="IterationValidation", line_errors=line_errors)
113123

114124
return self
115125

tests/unit/validation/test_iteration_validator.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -579,5 +579,12 @@ def test_iteration_rules_having_invalid_cohort_labels_throws_error(
579579
data["IterationCohorts"] = [valid_iteration_cohorts()]
580580
data["IterationRules"][0]["CohortLabel"] = "label_2"
581581

582-
with pytest.raises(ValidationError):
583-
IterationValidation(**(data))
582+
with pytest.raises(ValidationError) as exc_info:
583+
IterationValidation(**data)
584+
585+
errors = exc_info.value.errors()
586+
# Ensure at least one error is specifically about the invalid CohortLabel in IterationRules[0]
587+
assert any(
588+
err.get("loc", [])[:3] == ("iteration_rules", 0, "cohort_label")
589+
for err in errors
590+
)

0 commit comments

Comments
 (0)