@@ -16,29 +16,25 @@ def campaign_evaluator():
1616@pytest .mark .parametrize (
1717 ("campaign_target" , "campaign_type" , "conditions_filter" , "category_filter" , "expected_result" ),
1818 [
19- ("RSV" , "V" , ["RSV" ], "VACCINATIONS" , ("RSV" , "V" )),
20- ("RSV" , "V" , ["COVID" ], "VACCINATIONS" , None ),
21- ("RSV" , "S" , ["RSV" ], "ALL" , ("RSV" , "S" )),
22- ("RSV" , "S" , ["ALL" ], "ALL" , ("RSV" , "S" )),
23- ("RSV" , "S" , ["RSV" ], "VACCINATIONS" , None ),
24- ("RSV" , "V" , ["RSV" ], "ALL" , ("RSV" , "V" )),
25- ("FLU" , "V" , ["COVID" , "RSV" ], "ALL" , None ),
26- ("FLU" , "S" , ["ALL" ], "ALL" , ("FLU" , "S" )),
27- ("COVID" , "V" , ["UNKNOWN" ], "VACCINATIONS" , None ),
28- ("FLU" , "V" , ["COVID" , "FLU" ], "VACCINATIONS" , ("FLU" , "V" )),
19+ ("RSV" , "V" , ["RSV" ], "VACCINATIONS" , [ ("RSV" , "V" )] ),
20+ ("RSV" , "V" , ["COVID" ], "VACCINATIONS" , [] ),
21+ ("RSV" , "S" , ["RSV" ], "ALL" , [ ("RSV" , "S" )] ),
22+ ("RSV" , "S" , ["ALL" ], "ALL" , [ ("RSV" , "S" )] ),
23+ ("RSV" , "S" , ["RSV" ], "VACCINATIONS" , [] ),
24+ ("RSV" , "V" , ["RSV" ], "ALL" , [ ("RSV" , "V" )] ),
25+ ("FLU" , "V" , ["COVID" , "RSV" ], "ALL" , [] ),
26+ ("FLU" , "S" , ["ALL" ], "ALL" , [ ("FLU" , "S" )] ),
27+ ("COVID" , "V" , ["UNKNOWN" ], "VACCINATIONS" , [] ),
28+ ("FLU" , "V" , ["COVID" , "FLU" ], "VACCINATIONS" , [ ("FLU" , "V" )] ),
2929 ],
3030)
3131def test_campaigns_grouped_by_condition_name_filters_correctly ( # noqa: PLR0913
3232 campaign_evaluator , campaign_target , campaign_type , conditions_filter , category_filter , expected_result
3333):
3434 campaign = rule .CampaignConfigFactory .build (target = campaign_target , type = campaign_type )
3535
36- result = campaign_evaluator .get_campaign_with_latest_active_iteration_per_target (
37- [campaign ], conditions_filter , category_filter
38- )
39-
40- actual = next (((str (name ), camp .type ) for name , camp in result if camp is not None ), None )
41- assert actual == expected_result
36+ result = campaign_evaluator .get_campaign_with_latest_active_iteration_per_target ([campaign ], conditions_filter , category_filter )
37+ assert_that ([(str (name ), group [0 ].type ) for name , group in result ], is_ (expected_result ))
4238
4339
4440def test_campaigns_grouped_by_condition_name_with_no_campaigns (campaign_evaluator ):
@@ -51,9 +47,7 @@ def test_campaigns_grouped_by_condition_name_with_no_active_campaigns(campaign_e
5147 target = "RSV" , type = "V" , start_date = datetime .date (2025 , 4 , 20 ), end_date = datetime .date (2025 , 4 , 21 )
5248 )
5349
54- result = campaign_evaluator .get_campaign_with_latest_active_iteration_per_target (
55- [campaign ], ["RSV" ], "VACCINATIONS"
56- )
50+ result = campaign_evaluator .get_campaign_with_latest_active_iteration_per_target ([campaign ], ["RSV" ], "VACCINATIONS" )
5751 assert_that (list (result ), is_ ([]))
5852
5953
@@ -69,81 +63,55 @@ def test_campaigns_grouped_by_condition_name_with_various_categories(
6963 campaign_evaluator , category_filter , campaign_type , expected_count
7064):
7165 campaign = rule .CampaignConfigFactory .build (target = "COVID" , type = campaign_type )
72- result = list (
73- campaign_evaluator .get_campaign_with_latest_active_iteration_per_target ([campaign ], ["COVID" ], category_filter )
74- )
66+ result = list (campaign_evaluator .get_campaign_with_latest_active_iteration_per_target ([campaign ], ["COVID" ], category_filter ))
7567 assert_that (len (result ), is_ (expected_count ))
7668 if expected_count > 0 :
7769 assert_that (str (result [0 ][0 ]), is_ ("COVID" ))
7870
7971
8072def test_campaigns_grouped_by_condition_name_with_empty_conditions_filter (campaign_evaluator ):
8173 campaign = rule .CampaignConfigFactory .build (target = "RSV" , type = "V" )
82- result = list (
83- campaign_evaluator .get_campaign_with_latest_active_iteration_per_target ([campaign ], [], "VACCINATIONS" )
84- )
85-
86- assert_that (result , is_ ([]))
74+ result = campaign_evaluator .get_campaign_with_latest_active_iteration_per_target ([campaign ], [], "VACCINATIONS" )
75+ assert_that (list (result ), is_ ([]))
8776
8877
8978def test_campaigns_grouped_by_condition_name_groups_multiple_campaigns_for_same_target (campaign_evaluator ):
90- # providing the start_date here, because CampaignConfigFactory used it for iteration_date
91- campaign1 = rule .CampaignConfigFactory .build (
92- target = "COVID" ,
93- type = "V" ,
94- id = "C1" ,
95- start_date = datetime .datetime .now (datetime .UTC ).date () - datetime .timedelta (days = 1 ),
96- iterations = [rule .IterationFactory .build ()],
97- )
98- campaign2 = rule .CampaignConfigFactory .build (
99- target = "COVID" ,
100- type = "V" ,
101- id = "C2" ,
102- start_date = datetime .datetime .now (datetime .UTC ).date (),
103- iterations = [rule .IterationFactory .build ()],
104- )
79+ campaign1 = rule .CampaignConfigFactory .build (target = "COVID" , type = "V" , id = "C1" )
80+ campaign2 = rule .CampaignConfigFactory .build (target = "COVID" , type = "V" , id = "C2" )
10581 campaign3 = rule .CampaignConfigFactory .build (target = "FLU" , type = "V" , id = "F1" )
10682 inactive_campaign = rule .CampaignConfigFactory .build (
10783 target = "COVID" , type = "V" , id = "C3" , start_date = datetime .date (2025 , 4 , 20 ), end_date = datetime .date (2025 , 4 , 21 )
10884 )
10985
11086 all_campaigns = [campaign1 , campaign2 , campaign3 , inactive_campaign ]
111- result = list (
112- campaign_evaluator .get_campaign_with_latest_active_iteration_per_target (
113- all_campaigns , ["COVID" , "FLU" ], "VACCINATIONS"
114- )
115- )
87+ result = list (campaign_evaluator .get_campaign_with_latest_active_iteration_per_target (all_campaigns , ["COVID" , "FLU" ], "VACCINATIONS" ))
11688
11789 assert_that (len (result ), is_ (2 ))
11890
119- result_dict = {str (name ): campaign for name , campaign in result }
120-
91+ result_dict = {str (name ): campaigns for name , campaigns in result }
12192 assert_that ("COVID" in result_dict )
12293 assert_that ("FLU" in result_dict )
12394
124- assert_that (result_dict ["COVID" ].id , is_ (CampaignID ("C2" )))
125- assert_that (result_dict ["FLU" ].id , is_ (CampaignID ("F1" )))
95+ assert_that (len (result_dict ["COVID" ]), is_ (2 ))
96+ assert_that ({c .id for c in result_dict ["COVID" ]}, is_ ({CampaignID ("C1" ), CampaignID ("C2" )}))
97+
98+ assert_that (len (result_dict ["FLU" ]), is_ (1 ))
99+ assert_that (result_dict ["FLU" ][0 ].id , is_ (CampaignID ("F1" )))
126100
127101
128- def test_campaign_grouping_is_not_affected_by_order_for_mixed_types (campaign_evaluator ):
102+ def test_campaign_grouping_is_affected_by_order_for_mixed_types (campaign_evaluator ):
129103 campaign_v = rule .CampaignConfigFactory .build (target = "RSV" , type = "V" )
130104 campaign_s = rule .CampaignConfigFactory .build (target = "RSV" , type = "S" )
131105
132- # Order: S then V
106+ evaluator_s_first = campaign_evaluator
133107 result_s_first = list (
134- campaign_evaluator .get_campaign_with_latest_active_iteration_per_target (
135- [campaign_s , campaign_v ], ["RSV" ], "VACCINATIONS"
136- )
108+ evaluator_s_first .get_campaign_with_latest_active_iteration_per_target ([campaign_s , campaign_v ], ["RSV" ], "VACCINATIONS" )
137109 )
138- # Even if S is first, it is filtered out by 'allowed_types'
139- assert_that (len (result_s_first ), is_ (1 ))
140- assert_that (result_s_first [0 ][1 ].type , is_ ("V" ))
110+ assert_that (result_s_first , is_ ([]))
141111
142- # Order: V then S
112+ evaluator_v_first = campaign_evaluator
143113 result_v_first = list (
144- campaign_evaluator .get_campaign_with_latest_active_iteration_per_target (
145- [campaign_v , campaign_s ], ["RSV" ], "VACCINATIONS"
146- )
114+ evaluator_v_first .get_campaign_with_latest_active_iteration_per_target ([campaign_v , campaign_s ], ["RSV" ], "VACCINATIONS" )
147115 )
148116 assert_that (len (result_v_first ), is_ (1 ))
149- assert_that (result_v_first [0 ][1 ]. type , is_ ("V" ))
117+ assert_that (len ( result_v_first [0 ][1 ]) , is_ (2 ))
0 commit comments