Skip to content

Commit

Permalink
Merge pull request #35476 from dimagi/riese/conditional_alert_empty_prop
Browse files Browse the repository at this point in the history
Empty strings in recipient filter match unset prop
  • Loading branch information
MartinRiese authored Dec 4, 2024
2 parents 1bb60cf + fb1f4ba commit 103b01c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
5 changes: 1 addition & 4 deletions corehq/messaging/scheduling/scheduling_partitioned/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,11 +271,8 @@ def _passes_user_data_filter(self, contact):
else:
user_data = contact.get_user_data(self.domain)
for key, value_or_property_name in self.memoized_schedule.user_data_filter.items():
if key not in user_data:
return False

allowed_values_set = {self._get_filter_value(v) for v in self.convert_to_set(value_or_property_name)}
actual_values_set = self.convert_to_set(user_data[key])
actual_values_set = self.convert_to_set(user_data.get(key, ""))

if actual_values_set.isdisjoint(allowed_values_set):
return False
Expand Down
23 changes: 19 additions & 4 deletions corehq/messaging/scheduling/tests/test_recipients.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,17 @@ def testIgnoreSpacesBracesReturnProperty(self):

class PassesUserDataFilterTest(TestCase):
domain = 'passes-user-data-filter-test'
mobile_user = None

@classmethod
def setUpClass(cls):
super(PassesUserDataFilterTest, cls).setUpClass()
cls.domain_obj = create_domain(cls.domain)

user_data = {"wants_email": "yes", "color": "green"}
user_data = {"wants_email": "yes", "color": "green", "empty": ""}
cls.mobile_user = CommCareUser.create(cls.domain, 'mobile', 'abc', None, None, user_data=user_data)
create_case_2(cls.domain, case_type=USERCASE_TYPE, external_id=cls.mobile_user.user_id,
case_json=user_data, save=True)

@classmethod
def tearDownClass(cls):
Expand Down Expand Up @@ -122,16 +125,28 @@ def test_fails_with_user_data_filter_because_one_value_does_not_match(self):
._passes_user_data_filter(self.mobile_user))

def test_passes_with_user_case_filter(self):
case = create_case_2(self.domain, case_type="thing", case_json={"case_color": "red"})
create_case_2(self.domain, case_type=USERCASE_TYPE, external_id=self.mobile_user.user_id,
case_json={"wants_email": "yes", "color": "red"}, save=True)
case = create_case_2(self.domain, case_type="thing", case_json={"case_color": "green"})

schedule = AlertSchedule()
schedule.use_user_case_for_filter = True
schedule.user_data_filter = {"wants_email": ["yes"], "color": ["{case_color}"]}
self.assertTrue(ScheduleInstance(case=case, domain=self.domain, schedule=schedule)
._passes_user_data_filter(self.mobile_user))

def test_empty_string_matches_unset_property(self):
schedule = AlertSchedule()
schedule.use_user_case_for_filter = False
schedule.user_data_filter = {"empty": [""], "unset": ["yes", ""]}
self.assertTrue(ScheduleInstance(schedule=schedule)
._passes_user_data_filter(self.mobile_user))

def test_empty_string_matches_unset_property_user_case(self):
schedule = AlertSchedule()
schedule.use_user_case_for_filter = True
schedule.user_data_filter = {"empty": [""], "unset": ["yes", ""]}
self.assertTrue(ScheduleInstance(domain=self.domain, schedule=schedule)
._passes_user_data_filter(self.mobile_user))


class SchedulingRecipientTest(TestCase):
domain = 'scheduling-recipient-test'
Expand Down

0 comments on commit 103b01c

Please sign in to comment.