Skip to content

Commit aa4102e

Browse files
fix/magic cohorts
1 parent 4db5d7a commit aa4102e

3 files changed

Lines changed: 27 additions & 24 deletions

File tree

src/eligibility_signposting_api/model/rules.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ class IterationCohort(BaseModel):
9595

9696
model_config = {"populate_by_name": True, "extra": "ignore"}
9797

98+
@cached_property
99+
def is_magic_cohort(self) -> bool:
100+
return self.cohort_label.upper() == MAGIC_COHORT_LABEL.upper()
101+
98102

99103
class IterationRule(BaseModel):
100104
type: RuleType = Field(..., alias="Type")
@@ -144,17 +148,6 @@ def parse_dates(cls, v: str | date) -> date:
144148
def serialize_dates(v: date, _info: SerializationInfo) -> str:
145149
return v.strftime("%Y%m%d")
146150

147-
@cached_property
148-
def has_magic_cohort(self) -> bool:
149-
return next(
150-
(
151-
True
152-
for cc in self.iteration_cohorts
153-
if cc.cohort_label and cc.cohort_label.upper() == MAGIC_COHORT_LABEL.upper()
154-
),
155-
False,
156-
)
157-
158151

159152
class CampaignConfig(BaseModel):
160153
id: CampaignID = Field(..., alias="ID")

src/eligibility_signposting_api/services/calculators/eligibility_calculator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def evaluate_eligibility(self) -> eligibility.EligibilityStatus:
122122

123123
for cohort in sorted(active_iteration.iteration_cohorts, key=attrgetter("priority")):
124124
# Base Eligibility - check
125-
if cohort.cohort_label in self.person_cohorts or active_iteration.has_magic_cohort:
125+
if cohort.cohort_label in self.person_cohorts or cohort.is_magic_cohort:
126126
# Eligibility - check
127127
if self.is_eligible_by_filter_rules(cohort, cohort_results, filter_rules):
128128
# Actionability - evaluation

tests/unit/services/calculators/test_eligibility_calculator.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,10 +1042,19 @@ def test_eligibility_results_when_multiple_cohorts(
10421042
("magic cohort group", "magic negative description"),
10431043
("rsv_age_range", "rsv_age_range negative description"),
10441044
],
1045-
"all the cohorts are not-eligible",
1045+
"rsv_75_rolling is not base-eligible & magic cohort group not eligible by F rules ",
10461046
),
10471047
(
1048-
person_rows_builder(nhs_number="123", cohorts=[], postcode="SW19", de=False, icb="QE1"),
1048+
person_rows_builder(nhs_number="123", cohorts=["rsv_75_rolling"], postcode="AC01", de=True, icb="QE1"),
1049+
Status.not_eligible,
1050+
[
1051+
("magic cohort group", "magic negative description"),
1052+
("rsv_age_range", "rsv_age_range negative description"),
1053+
],
1054+
"all the cohorts are not-eligible by F rules",
1055+
),
1056+
(
1057+
person_rows_builder(nhs_number="123", cohorts=["rsv_75_rolling"], postcode="SW19", de=False, icb="QE1"),
10491058
Status.not_actionable,
10501059
[
10511060
("magic cohort group", "magic positive description"),
@@ -1054,7 +1063,7 @@ def test_eligibility_results_when_multiple_cohorts(
10541063
"all the cohorts are not-actionable",
10551064
),
10561065
(
1057-
person_rows_builder(nhs_number="123", cohorts=[], postcode="AC01", de=False, icb="QE1"),
1066+
person_rows_builder(nhs_number="123", cohorts=["rsv_75_rolling"], postcode="AC01", de=False, icb="QE1"),
10581067
Status.actionable,
10591068
[
10601069
("magic cohort group", "magic positive description"),
@@ -1063,20 +1072,20 @@ def test_eligibility_results_when_multiple_cohorts(
10631072
"all the cohorts are actionable",
10641073
),
10651074
(
1066-
person_rows_builder(nhs_number="123", cohorts=[], postcode="AC01", de=False, icb="NOT_QE1"),
1075+
person_rows_builder(nhs_number="123", cohorts=["rsv_75_rolling"], postcode="AC01", de=False, icb="NOT_QE1"),
10671076
Status.actionable,
10681077
[("magic cohort group", "magic positive description")],
10691078
"magic_cohort is actionable, but not others",
10701079
),
10711080
(
1072-
person_rows_builder(nhs_number="123", cohorts=[], postcode="SW19", de=False, icb="NOT_QE1"),
1081+
person_rows_builder(nhs_number="123", cohorts=["rsv_75_rolling"], postcode="SW19", de=False, icb="NOT_QE1"),
10731082
Status.not_actionable,
10741083
[("magic cohort group", "magic positive description")],
10751084
"magic_cohort is not-actionable, but others are not eligible",
10761085
),
10771086
],
10781087
)
1079-
def test_cohort_groups_and_their_descriptions_when_magic_cohort_is_having_the_best_status(
1088+
def test_cohort_groups_and_their_descriptions_when_magic_cohort_is_present(
10801089
person_rows: list[dict[str, Any]],
10811090
expected_status: str,
10821091
expected_cohort_group_and_description: list[tuple[str, str]],
@@ -1089,19 +1098,20 @@ def test_cohort_groups_and_their_descriptions_when_magic_cohort_is_having_the_be
10891098
iterations=[
10901099
rule_builder.IterationFactory.build(
10911100
iteration_cohorts=[
1092-
rule_builder.MagicCohortFactory.build(),
10931101
rule_builder.Rsv75RollingCohortFactory.build(),
1102+
rule_builder.MagicCohortFactory.build(),
10941103
],
10951104
iteration_rules=[
1096-
# common rules
1105+
# F common rule
10971106
rule_builder.DetainedEstateSuppressionRuleFactory.build(type=rules.RuleType.filter),
1107+
# F rules for rsv_75_rolling
1108+
rule_builder.ICBFilterRuleFactory.build(
1109+
type=rules.RuleType.filter, cohort_label=rules.CohortLabel("rsv_75_rolling")
1110+
),
1111+
# S common rule
10981112
rule_builder.PostcodeSuppressionRuleFactory.build(
10991113
comparator=rules.RuleComparator("SW19"),
11001114
),
1101-
# rules for specific cohorts
1102-
rule_builder.ICBFilterRuleFactory.build(
1103-
cohort_label=rules.CohortLabel("rsv_75_rolling"),
1104-
),
11051115
],
11061116
)
11071117
],

0 commit comments

Comments
 (0)