44import pytest
55from faker import Faker
66from freezegun import freeze_time
7- from hamcrest import assert_that , contains_exactly , has_item , has_items , contains_inanyorder , equal_to
7+ from hamcrest import assert_that , contains_exactly , contains_inanyorder , equal_to , has_item , has_items
88
99from eligibility_signposting_api .model import rules
1010from eligibility_signposting_api .model import rules as rules_model
11- from eligibility_signposting_api .model .eligibility import ConditionName , DateOfBirth , NHSNumber , Postcode , Status , \
12- RuleResult
11+ from eligibility_signposting_api .model .eligibility import (
12+ ConditionName ,
13+ DateOfBirth ,
14+ NHSNumber ,
15+ Postcode ,
16+ RuleResult ,
17+ Status ,
18+ )
1319from eligibility_signposting_api .services .calculators .eligibility_calculator import EligibilityCalculator
1420from tests .fixtures .builders .model import rule as rule_builder
1521from tests .fixtures .builders .repos .person import person_rows_builder
@@ -339,48 +345,48 @@ def test_multiple_rule_types_cause_correct_status(faker: Faker):
339345 ("test_comment" , "rule1" , "rule2" , "expected_status" ),
340346 [
341347 (
342- "two rules, both exclude, same priority, should exclude" ,
343- rule_builder .PersonAgeSuppressionRuleFactory .build (priority = rules_model .RulePriority (5 )),
344- rule_builder .PostcodeSuppressionRuleFactory .build (priority = rules_model .RulePriority (5 )),
345- Status .not_actionable ,
348+ "two rules, both exclude, same priority, should exclude" ,
349+ rule_builder .PersonAgeSuppressionRuleFactory .build (priority = rules_model .RulePriority (5 )),
350+ rule_builder .PostcodeSuppressionRuleFactory .build (priority = rules_model .RulePriority (5 )),
351+ Status .not_actionable ,
346352 ),
347353 (
348- "two rules, rule 1 excludes, same priority, should allow" ,
349- rule_builder .PersonAgeSuppressionRuleFactory .build (priority = rules_model .RulePriority (5 )),
350- rule_builder .PostcodeSuppressionRuleFactory .build (
351- priority = rules_model .RulePriority (5 ), comparator = rules_model .RuleComparator ("NW1" )
352- ),
353- Status .actionable ,
354+ "two rules, rule 1 excludes, same priority, should allow" ,
355+ rule_builder .PersonAgeSuppressionRuleFactory .build (priority = rules_model .RulePriority (5 )),
356+ rule_builder .PostcodeSuppressionRuleFactory .build (
357+ priority = rules_model .RulePriority (5 ), comparator = rules_model .RuleComparator ("NW1" )
358+ ),
359+ Status .actionable ,
354360 ),
355361 (
356- "two rules, rule 2 excludes, same priority, should allow" ,
357- rule_builder .PersonAgeSuppressionRuleFactory .build (
358- priority = rules_model .RulePriority (5 ), comparator = rules_model .RuleComparator ("-65" )
359- ),
360- rule_builder .PostcodeSuppressionRuleFactory .build (priority = rules_model .RulePriority (5 )),
361- Status .actionable ,
362+ "two rules, rule 2 excludes, same priority, should allow" ,
363+ rule_builder .PersonAgeSuppressionRuleFactory .build (
364+ priority = rules_model .RulePriority (5 ), comparator = rules_model .RuleComparator ("-65" )
365+ ),
366+ rule_builder .PostcodeSuppressionRuleFactory .build (priority = rules_model .RulePriority (5 )),
367+ Status .actionable ,
362368 ),
363369 (
364- "two rules, rule 1 excludes, different priority, should exclude" ,
365- rule_builder .PersonAgeSuppressionRuleFactory .build (priority = rules_model .RulePriority (5 )),
366- rule_builder .PostcodeSuppressionRuleFactory .build (
367- priority = rules_model .RulePriority (10 ), comparator = rules_model .RuleComparator ("NW1" )
368- ),
369- Status .not_actionable ,
370+ "two rules, rule 1 excludes, different priority, should exclude" ,
371+ rule_builder .PersonAgeSuppressionRuleFactory .build (priority = rules_model .RulePriority (5 )),
372+ rule_builder .PostcodeSuppressionRuleFactory .build (
373+ priority = rules_model .RulePriority (10 ), comparator = rules_model .RuleComparator ("NW1" )
374+ ),
375+ Status .not_actionable ,
370376 ),
371377 (
372- "two rules, rule 2 excludes, different priority, should exclude" ,
373- rule_builder .PersonAgeSuppressionRuleFactory .build (
374- priority = rules_model .RulePriority (5 ), comparator = rules_model .RuleComparator ("-65" )
375- ),
376- rule_builder .PostcodeSuppressionRuleFactory .build (priority = rules_model .RulePriority (10 )),
377- Status .not_actionable ,
378+ "two rules, rule 2 excludes, different priority, should exclude" ,
379+ rule_builder .PersonAgeSuppressionRuleFactory .build (
380+ priority = rules_model .RulePriority (5 ), comparator = rules_model .RuleComparator ("-65" )
381+ ),
382+ rule_builder .PostcodeSuppressionRuleFactory .build (priority = rules_model .RulePriority (10 )),
383+ Status .not_actionable ,
378384 ),
379385 (
380- "two rules, both excludes, different priority, should exclude" ,
381- rule_builder .PersonAgeSuppressionRuleFactory .build (priority = rules_model .RulePriority (5 )),
382- rule_builder .PostcodeSuppressionRuleFactory .build (priority = rules_model .RulePriority (10 )),
383- Status .not_actionable ,
386+ "two rules, both excludes, different priority, should exclude" ,
387+ rule_builder .PersonAgeSuppressionRuleFactory .build (priority = rules_model .RulePriority (5 )),
388+ rule_builder .PostcodeSuppressionRuleFactory .build (priority = rules_model .RulePriority (10 )),
389+ Status .not_actionable ,
384390 ),
385391 ],
386392)
@@ -527,46 +533,46 @@ def test_multiple_conditions_where_all_give_unique_statuses(faker: Faker):
527533 ("test_comment" , "campaign1" , "campaign2" ),
528534 [
529535 (
530- "1st campaign allows, 2nd excludes" ,
531- rule_builder .CampaignConfigFactory .build (
532- target = "RSV" ,
533- iterations = [
534- rule_builder .IterationFactory .build (
535- iteration_cohorts = [rule_builder .IterationCohortFactory .build (cohort_label = "cohort1" )],
536- iteration_rules = [rule_builder .PersonAgeSuppressionRuleFactory .build ()],
537- )
538- ],
539- ),
540- rule_builder .CampaignConfigFactory .build (
541- target = "RSV" ,
542- iterations = [
543- rule_builder .IterationFactory .build (
544- iteration_cohorts = [rule_builder .IterationCohortFactory .build (cohort_label = "cohort1" )],
545- iteration_rules = [rule_builder .PersonAgeSuppressionRuleFactory .build (comparator = "-85" )],
546- )
547- ],
548- ),
536+ "1st campaign allows, 2nd excludes" ,
537+ rule_builder .CampaignConfigFactory .build (
538+ target = "RSV" ,
539+ iterations = [
540+ rule_builder .IterationFactory .build (
541+ iteration_cohorts = [rule_builder .IterationCohortFactory .build (cohort_label = "cohort1" )],
542+ iteration_rules = [rule_builder .PersonAgeSuppressionRuleFactory .build ()],
543+ )
544+ ],
545+ ),
546+ rule_builder .CampaignConfigFactory .build (
547+ target = "RSV" ,
548+ iterations = [
549+ rule_builder .IterationFactory .build (
550+ iteration_cohorts = [rule_builder .IterationCohortFactory .build (cohort_label = "cohort1" )],
551+ iteration_rules = [rule_builder .PersonAgeSuppressionRuleFactory .build (comparator = "-85" )],
552+ )
553+ ],
554+ ),
549555 ),
550556 (
551- "1st campaign excludes, 2nd allows" ,
552- rule_builder .CampaignConfigFactory .build (
553- target = "RSV" ,
554- iterations = [
555- rule_builder .IterationFactory .build (
556- iteration_cohorts = [rule_builder .IterationCohortFactory .build (cohort_label = "cohort1" )],
557- iteration_rules = [rule_builder .PersonAgeSuppressionRuleFactory .build (comparator = "-85" )],
558- )
559- ],
560- ),
561- rule_builder .CampaignConfigFactory .build (
562- target = "RSV" ,
563- iterations = [
564- rule_builder .IterationFactory .build (
565- iteration_cohorts = [rule_builder .IterationCohortFactory .build (cohort_label = "cohort1" )],
566- iteration_rules = [rule_builder .PersonAgeSuppressionRuleFactory .build ()],
567- )
568- ],
569- ),
557+ "1st campaign excludes, 2nd allows" ,
558+ rule_builder .CampaignConfigFactory .build (
559+ target = "RSV" ,
560+ iterations = [
561+ rule_builder .IterationFactory .build (
562+ iteration_cohorts = [rule_builder .IterationCohortFactory .build (cohort_label = "cohort1" )],
563+ iteration_rules = [rule_builder .PersonAgeSuppressionRuleFactory .build (comparator = "-85" )],
564+ )
565+ ],
566+ ),
567+ rule_builder .CampaignConfigFactory .build (
568+ target = "RSV" ,
569+ iterations = [
570+ rule_builder .IterationFactory .build (
571+ iteration_cohorts = [rule_builder .IterationCohortFactory .build (cohort_label = "cohort1" )],
572+ iteration_rules = [rule_builder .PersonAgeSuppressionRuleFactory .build ()],
573+ )
574+ ],
575+ ),
570576 ),
571577 ],
572578)
@@ -820,28 +826,38 @@ def test_status_if_iteration_rules_contains_cohort_label_field(
820826@pytest .mark .parametrize (
821827 ("rule_stop" , "expected_reason_results" , "test_comment" ), # Changed expected_reasons to expected_reason_results
822828 [
823- (True , [
824- RuleResult (
825- "Rule 'Exclude too young less than 75' ('reason 1') excluding - 'DATE_OF_BIRTH' '-75' was '19980309'" ),
826- RuleResult (
827- "Rule 'Exclude too young less than 75' ('reason 2') excluding - 'DATE_OF_BIRTH' '-75' was '19980309'" )
828- ],
829- "rule_stop is True, last rule should not run"
830- ),
831- (False , [
832- RuleResult (
833- "Rule 'Exclude too young less than 75' ('reason 1') excluding - 'DATE_OF_BIRTH' '-75' was '19980309'" ),
834- RuleResult (
835- "Rule 'Exclude too young less than 75' ('reason 2') excluding - 'DATE_OF_BIRTH' '-75' was '19980309'" ),
836- RuleResult (
837- "Rule 'Exclude too young less than 75' ('reason 3') excluding - 'DATE_OF_BIRTH' '-75' was '19980309'" )
838- ],
839- "rule_stop is False, last rule should run"
840- )
829+ (
830+ True ,
831+ [
832+ RuleResult (
833+ "Rule 'Exclude too young less than 75' ('reason 1') excluding - 'DATE_OF_BIRTH' '-75' was '19980309'"
834+ ),
835+ RuleResult (
836+ "Rule 'Exclude too young less than 75' ('reason 2') excluding - 'DATE_OF_BIRTH' '-75' was '19980309'"
837+ ),
838+ ],
839+ "rule_stop is True, last rule should not run" ,
840+ ),
841+ (
842+ False ,
843+ [
844+ RuleResult (
845+ "Rule 'Exclude too young less than 75' ('reason 1') excluding - 'DATE_OF_BIRTH' '-75' was '19980309'"
846+ ),
847+ RuleResult (
848+ "Rule 'Exclude too young less than 75' ('reason 2') excluding - 'DATE_OF_BIRTH' '-75' was '19980309'"
849+ ),
850+ RuleResult (
851+ "Rule 'Exclude too young less than 75' ('reason 3') excluding - 'DATE_OF_BIRTH' '-75' was '19980309'"
852+ ),
853+ ],
854+ "rule_stop is False, last rule should run" ,
855+ ),
841856 ],
842857)
843- def test_rules_stop_behavior (rule_stop : bool , expected_reason_results : list [RuleResult ], test_comment : str ,
844- faker : Faker ) -> None :
858+ def test_rules_stop_behavior (
859+ rule_stop : bool , expected_reason_results : list [RuleResult ], test_comment : str , faker : Faker
860+ ) -> None :
845861 # Given
846862 nhs_number = NHSNumber (faker .nhs_number ())
847863 date_obj = datetime .datetime .strptime ("19980309" , "%Y%m%d" ).date ()
@@ -853,8 +869,9 @@ def test_rules_stop_behavior(rule_stop: bool, expected_reason_results: list[Rule
853869 iterations = [
854870 rule_builder .IterationFactory .build (
855871 iteration_rules = [
856- rule_builder .PersonAgeSuppressionRuleFactory .build (priority = 10 , description = "reason 1" ,
857- rule_stop = rule_stop ),
872+ rule_builder .PersonAgeSuppressionRuleFactory .build (
873+ priority = 10 , description = "reason 1" , rule_stop = rule_stop
874+ ),
858875 rule_builder .PersonAgeSuppressionRuleFactory .build (priority = 10 , description = "reason 2" ),
859876 rule_builder .PersonAgeSuppressionRuleFactory .build (priority = 15 , description = "reason 3" ),
860877 ],
@@ -871,7 +888,10 @@ def test_rules_stop_behavior(rule_stop: bool, expected_reason_results: list[Rule
871888 actual_reason_results : list [RuleResult ] = []
872889 for condition in actual .conditions :
873890 if condition .condition_name == ConditionName ("RSV" ):
874- actual_reason_results .extend ([reason .rule_result for reason in list (itertools .chain (* condition .reasons ))])
891+ for cohort_result in condition .cohort_results :
892+ actual_reason_results .extend (
893+ [reason .rule_result for reason in list (itertools .chain (* cohort_result .reasons ))]
894+ )
875895
876896 # Then
877897 assert_that (
@@ -950,10 +970,11 @@ def test_eligibility_results_when_multiple_cohorts(
950970 # When
951971 actual = calculator .evaluate_eligibility ()
952972
973+ actual_cohort_labels : list [str ] = []
974+ for condition in actual .conditions :
975+ if condition .condition_name == ConditionName ("RSV" ):
976+ for cohort_result in condition .cohort_results :
977+ actual_cohort_labels .append (cohort_result .cohort .cohort_label )
978+
953979 # Then
954- assert_that (
955- actual ,
956- is_eligibility_status ().with_conditions (
957- has_items (is_condition ().with_condition_name (ConditionName ("RSV" )).and_status (expected_status ))
958- ),
959- )
980+ assert_that (actual_cohort_labels , contains_inanyorder (* expected_cohorts ))
0 commit comments