Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from eligibility_signposting_api.services.calculators.rule_calculator import RuleCalculator

Row = Collection[Mapping[str, Any]]
magic_cohort = "elid_all_people"


@service
Expand Down Expand Up @@ -70,19 +71,22 @@ def get_the_base_eligible_campaigns(self, campaign_group: list[rules.CampaignCon
return base_eligible_campaigns
return []

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

if not iteration:
return set()
return False # pragma: no cover
iteration_cohorts: set[str] = {
cohort.cohort_label for cohort in iteration.iteration_cohorts if cohort.cohort_label
}
if magic_cohort in iteration_cohorts:
return True

cohorts_row: Mapping[str, dict[str, dict[str, dict[str, Any]]]] = next(
(row for row in self.person_data if row.get("ATTRIBUTE_TYPE") == "COHORTS"), {}
)
person_cohorts: set[str] = set(cohorts_row.get("COHORT_MAP", {}).get("cohorts", {}).get("M", {}).keys())
return iteration_cohorts & person_cohorts
return bool(iteration_cohorts & person_cohorts)

def evaluate_eligibility_by_iteration_rules(
self, campaign_group: list[rules.CampaignConfig]
Expand Down
42 changes: 42 additions & 0 deletions tests/unit/services/calculators/test_eligibility_calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,48 @@ def test_not_base_eligible(faker: Faker):
)


@pytest.mark.parametrize(
("cohorts", "test_comment"),
[
(["elid_all_people"], "Only magic cohort present"),
(["elid_all_people", "cohort1"], "Magic cohort with other cohorts"),
],
)
def test_base_eligible_with_when_magic_cohort_is_present(faker: Faker, cohorts, test_comment):
# Given
nhs_number = NHSNumber(faker.nhs_number())
date_of_birth = DateOfBirth(faker.date_of_birth(minimum_age=76, maximum_age=79))

person_rows = person_rows_builder(nhs_number, date_of_birth=date_of_birth, cohorts=["cohort1"])
campaign_configs = [
rule_builder.CampaignConfigFactory.build(
target="RSV",
iterations=[
rule_builder.IterationFactory.build(
iteration_cohorts=[
rule_builder.IterationCohortFactory.build(cohort_label=label) for label in cohorts
],
iteration_rules=[rule_builder.PersonAgeSuppressionRuleFactory.build()],
)
],
)
]

calculator = EligibilityCalculator(person_rows, campaign_configs)

# When
actual = calculator.evaluate_eligibility()

# Then
assert_that(
actual,
is_eligibility_status().with_conditions(
has_item(is_condition().with_condition_name(ConditionName("RSV")).and_status(Status.actionable))
),
test_comment,
)


@freeze_time("2025-04-25")
def test_only_live_campaigns_considered(faker: Faker):
# Given
Expand Down
Loading