Skip to content

Commit

Permalink
Clean up naming and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinRiese committed Dec 11, 2024
1 parent e27e1b8 commit 2946b37
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
18 changes: 10 additions & 8 deletions corehq/messaging/scheduling/scheduling_partitioned/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,11 +275,13 @@ def _passes_user_data_filter(self, contact):
actual_values_set = self.convert_to_set(user_data.get(key, ""))

if actual_values_set.isdisjoint(allowed_values_set):
return False, f"Filtered out on property {key}: exp: ({','.join(allowed_values_set)}), act: ({','.join(actual_values_set)})"
return False, (f"Filtered out on property {key}: "
f"exp: ({','.join(allowed_values_set)}), "
f"act: ({','.join(actual_values_set)})")

return True, ""

def expand_recipients(self, log_filter=None):
def expand_recipients(self, handle_filtered_recipient=None):
"""
Can be used as a generator to iterate over all individual contacts who
are the recipients of this ScheduleInstance.
Expand All @@ -290,11 +292,11 @@ def expand_recipients(self, log_filter=None):

for member in recipient_list:
for contact in self._expand_recipient(member):
passed, error_msg = self._passes_user_data_filter(contact)
passed, filter_reason = self._passes_user_data_filter(contact)
if passed:
yield contact
elif log_filter is not None:
log_filter(contact, error_msg)
elif handle_filtered_recipient is not None:
handle_filtered_recipient(contact, filter_reason)

def get_content_send_lock(self, recipient):
if is_commcarecase(recipient):
Expand Down Expand Up @@ -334,15 +336,15 @@ def send_current_event_content_to_recipients(self):

logged_event = MessagingEvent.create_from_schedule_instance(self, content)

def log_filter_error(filtered_recipient, msg):
def log_filtered_recipient(filtered_recipient, reason):
sub_event = logged_event.create_subevent_from_contact_and_content(
filtered_recipient,
content,
)
sub_event.error(MessagingEvent.ERROR_FILTER_MISMATCH, msg)
sub_event.error(MessagingEvent.ERROR_FILTER_MISMATCH, reason)

recipient_count = 0
for recipient in self.expand_recipients(log_filter_error):
for recipient in self.expand_recipients(log_filtered_recipient):
recipient_count += 1

# The framework will retry sending a non-processed schedule instance
Expand Down
25 changes: 20 additions & 5 deletions corehq/messaging/scheduling/tests/test_recipients.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,19 @@ def test_fails_with_user_data_filter_because_value_does_not_match(self):
schedule = AlertSchedule()
schedule.use_user_case_for_filter = False
schedule.user_data_filter = {"wants_email": ["no"]}
self.assertFalse(ScheduleInstance(domain=self.domain, schedule=schedule)
._passes_user_data_filter(self.mobile_user))
passed, msg = (ScheduleInstance(domain=self.domain, schedule=schedule).
_passes_user_data_filter(self.mobile_user))
self.assertFalse(passed)
self.assertEqual(msg, "Filtered out on property wants_email: exp: (no), act: (yes)")

def test_fails_with_user_data_filter_because_one_value_does_not_match(self):
schedule = AlertSchedule()
schedule.use_user_case_for_filter = False
schedule.user_data_filter = {"wants_email": ["yes"], "color": ["red"]}
self.assertFalse(ScheduleInstance(domain=self.domain, schedule=schedule)
._passes_user_data_filter(self.mobile_user))
passed, msg = (ScheduleInstance(domain=self.domain, schedule=schedule).
_passes_user_data_filter(self.mobile_user))
self.assertFalse(passed)
self.assertEqual(msg, "Filtered out on property color: exp: (red), act: (green)")

def test_passes_with_user_case_filter(self):
case = create_case_2(self.domain, case_type="thing", case_json={"case_color": "green"})
Expand Down Expand Up @@ -652,10 +656,21 @@ def test_mobile_worker_recipients_with_user_data_filter(self):
recipient_type='Group',
recipient_id=self.group2.get_id
)
message = ""
filtered_count = 0

def handle_filtered_recipient(_, msg):
nonlocal message
nonlocal filtered_count
message = msg
filtered_count += 1

self.assertEqual(
self.user_ids(instance.expand_recipients()),
self.user_ids(instance.expand_recipients(handle_filtered_recipient)),
[self.mobile_user4.get_id, self.mobile_user5.get_id, self.mobile_user6.get_id]
)
self.assertEqual(message, "Filtered out on property role: exp: (nurse), act: (pharmacist)")
self.assertEqual(2, filtered_count)

def test_web_user_recipient_with_user_data_filter(self):
schedule = TimedSchedule.create_simple_daily_schedule(
Expand Down

0 comments on commit 2946b37

Please sign in to comment.