Skip to content

Commit 25d2f0d

Browse files
Introducing magic cohort in iteration cohorts (#100)
* test_base_eligible_with_when_magic_cohort_is_present * check_base_eligibility return type changed to boolean * Simplify cohort eligibility check
1 parent cdee154 commit 25d2f0d

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

src/eligibility_signposting_api/services/calculators/eligibility_calculator.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from eligibility_signposting_api.services.calculators.rule_calculator import RuleCalculator
1414

1515
Row = Collection[Mapping[str, Any]]
16+
magic_cohort = "elid_all_people"
1617

1718

1819
@service
@@ -70,19 +71,22 @@ def get_the_base_eligible_campaigns(self, campaign_group: list[rules.CampaignCon
7071
return base_eligible_campaigns
7172
return []
7273

73-
def check_base_eligibility(self, iteration: rules.Iteration | None) -> set[str]:
74+
def check_base_eligibility(self, iteration: rules.Iteration | None) -> bool:
7475
"""Return cohorts for which person is base eligible."""
7576

7677
if not iteration:
77-
return set()
78+
return False # pragma: no cover
7879
iteration_cohorts: set[str] = {
7980
cohort.cohort_label for cohort in iteration.iteration_cohorts if cohort.cohort_label
8081
}
82+
if magic_cohort in iteration_cohorts:
83+
return True
84+
8185
cohorts_row: Mapping[str, dict[str, dict[str, dict[str, Any]]]] = next(
8286
(row for row in self.person_data if row.get("ATTRIBUTE_TYPE") == "COHORTS"), {}
8387
)
8488
person_cohorts: set[str] = set(cohorts_row.get("COHORT_MAP", {}).get("cohorts", {}).get("M", {}).keys())
85-
return iteration_cohorts & person_cohorts
89+
return bool(iteration_cohorts & person_cohorts)
8690

8791
def evaluate_eligibility_by_iteration_rules(
8892
self, campaign_group: list[rules.CampaignConfig]

tests/unit/services/calculators/test_eligibility_calculator.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,48 @@ def test_not_base_eligible(faker: Faker):
4646
)
4747

4848

49+
@pytest.mark.parametrize(
50+
("cohorts", "test_comment"),
51+
[
52+
(["elid_all_people"], "Only magic cohort present"),
53+
(["elid_all_people", "cohort1"], "Magic cohort with other cohorts"),
54+
],
55+
)
56+
def test_base_eligible_with_when_magic_cohort_is_present(faker: Faker, cohorts, test_comment):
57+
# Given
58+
nhs_number = NHSNumber(faker.nhs_number())
59+
date_of_birth = DateOfBirth(faker.date_of_birth(minimum_age=76, maximum_age=79))
60+
61+
person_rows = person_rows_builder(nhs_number, date_of_birth=date_of_birth, cohorts=["cohort1"])
62+
campaign_configs = [
63+
rule_builder.CampaignConfigFactory.build(
64+
target="RSV",
65+
iterations=[
66+
rule_builder.IterationFactory.build(
67+
iteration_cohorts=[
68+
rule_builder.IterationCohortFactory.build(cohort_label=label) for label in cohorts
69+
],
70+
iteration_rules=[rule_builder.PersonAgeSuppressionRuleFactory.build()],
71+
)
72+
],
73+
)
74+
]
75+
76+
calculator = EligibilityCalculator(person_rows, campaign_configs)
77+
78+
# When
79+
actual = calculator.evaluate_eligibility()
80+
81+
# Then
82+
assert_that(
83+
actual,
84+
is_eligibility_status().with_conditions(
85+
has_item(is_condition().with_condition_name(ConditionName("RSV")).and_status(Status.actionable))
86+
),
87+
test_comment,
88+
)
89+
90+
4991
@freeze_time("2025-04-25")
5092
def test_only_live_campaigns_considered(faker: Faker):
5193
# Given

0 commit comments

Comments
 (0)