Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for recipient filter with FF on #35478

Merged
merged 4 commits into from
Dec 11, 2024
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 @@ -267,7 +267,10 @@ def _passes_user_data_filter(self, contact):

if self.memoized_schedule.use_user_case_for_filter:
user_case = contact.get_usercase_by_domain(self.domain)
user_data = user_case.case_json
if user_case:
user_data = user_case.case_json
else:
return False
else:
user_data = contact.get_user_data(self.domain)
for key, value_or_property_name in self.memoized_schedule.user_data_filter.items():
Expand Down
122 changes: 72 additions & 50 deletions corehq/messaging/scheduling/tests/test_recipients.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
Field,
)
from corehq.apps.domain.shortcuts import create_domain
from corehq.apps.es.tests.utils import es_test
from corehq.apps.es.users import user_adapter
from corehq.apps.es.client import manager
from corehq.apps.groups.models import Group
from corehq.apps.hqcase.utils import update_case
from corehq.apps.locations.models import SQLLocation
Expand Down Expand Up @@ -42,7 +45,7 @@
from corehq.util.test_utils import (
create_test_case,
set_parent_case,
unregistered_django_model,
unregistered_django_model, flag_enabled,
)
from testapps.test_pillowtop.utils import process_pillow_changes

Expand Down Expand Up @@ -147,7 +150,15 @@ def test_empty_string_matches_unset_property_user_case(self):
self.assertTrue(ScheduleInstance(domain=self.domain, schedule=schedule)
._passes_user_data_filter(self.mobile_user))

def test_fails_if_filter_on_case_but_no_case(self):
schedule = AlertSchedule()
schedule.use_user_case_for_filter = True
schedule.user_data_filter = {"wants_email": ["yes"]}
self.assertFalse(ScheduleInstance(schedule=schedule)
._passes_user_data_filter(self.mobile_user))


@es_test(requires=[user_adapter], setup_class=True)
class SchedulingRecipientTest(TestCase):
domain = 'scheduling-recipient-test'

Expand All @@ -161,9 +172,14 @@ def setUpClass(cls):
cls.country_location = make_loc('usa', domain=cls.domain, type='country')
cls.state_location = make_loc('ma', domain=cls.domain, type='state', parent=cls.country_location)
cls.city_location = make_loc('boston', domain=cls.domain, type='city', parent=cls.state_location)
cls.city_location_2 = make_loc('salem', domain=cls.domain, type='city', parent=cls.state_location)

cls.mobile_user = CommCareUser.create(cls.domain, 'mobile', 'abc', None, None)
cls.mobile_user.set_location(cls.city_location)
cls.mobile_user.add_to_assigned_locations(cls.city_location_2)

user_adapter.index(cls.mobile_user, refresh=True)
manager.index_refresh(user_adapter.index_name)

cls.mobile_user2 = CommCareUser.create(cls.domain, 'mobile2', 'abc', None, None)
cls.mobile_user2.set_location(cls.state_location)
Expand Down Expand Up @@ -247,6 +263,23 @@ def tearDown(self):
def user_ids(self, users):
return [user.get_id for user in users]

def _create_schedule(self,
include_descendant_locations=False,
location_type_filter=None,
user_data_filter=None):
schedule = TimedSchedule.create_simple_daily_schedule(
self.domain,
TimedEvent(time=time(9, 0)),
SMSContent(message={'en': 'Hello'})
)
schedule.include_descendant_locations = include_descendant_locations
if location_type_filter:
schedule.location_type_filter = location_type_filter
if user_data_filter:
schedule.user_data_filter = user_data_filter
schedule.save()
return schedule

def test_specific_case_recipient(self):
with create_case(self.domain, 'person') as case:
instance = ScheduleInstance(
Expand Down Expand Up @@ -549,13 +582,7 @@ def test_host_case_owner_location_parent(self):
self.assertIsNone(instance.recipient)

def test_expand_location_recipients_without_descendants(self):
schedule = TimedSchedule.create_simple_daily_schedule(
self.domain,
TimedEvent(time=time(9, 0)),
SMSContent(message={'en': 'Hello'})
)
schedule.include_descendant_locations = False
schedule.save()
schedule = self._create_schedule()

instance = CaseTimedScheduleInstance(
domain=self.domain,
Expand All @@ -580,13 +607,7 @@ def test_expand_location_recipients_without_descendants(self):
)

def test_expand_location_recipients_with_descendants(self):
schedule = TimedSchedule.create_simple_daily_schedule(
self.domain,
TimedEvent(time=time(9, 0)),
SMSContent(message={'en': 'Hello'})
)
schedule.include_descendant_locations = True
schedule.save()
schedule = self._create_schedule(include_descendant_locations=True)

instance = CaseTimedScheduleInstance(
domain=self.domain,
Expand All @@ -600,14 +621,10 @@ def test_expand_location_recipients_with_descendants(self):
)

def test_expand_location_recipients_with_location_type_filter(self):
schedule = TimedSchedule.create_simple_daily_schedule(
self.domain,
TimedEvent(time=time(9, 0)),
SMSContent(message={'en': 'Hello'})
schedule = self._create_schedule(
include_descendant_locations=True,
location_type_filter=[self.city_location.location_type_id]
)
schedule.include_descendant_locations = True
schedule.location_type_filter = [self.city_location.location_type_id]
schedule.save()

instance = CaseTimedScheduleInstance(
domain=self.domain,
Expand All @@ -620,12 +637,37 @@ def test_expand_location_recipients_with_location_type_filter(self):
[self.mobile_user.get_id]
)

def test_expand_group_recipients(self):
schedule = TimedSchedule.create_simple_daily_schedule(
self.domain,
TimedEvent(time=time(9, 0)),
SMSContent(message={'en': 'Hello'})
def test_expand_location_recipients_secondary_does_not_match(self):
schedule = self._create_schedule()

instance = CaseTimedScheduleInstance(
domain=self.domain,
timed_schedule_id=schedule.schedule_id,
recipient_type='Location',
recipient_id=self.city_location_2.location_id
)
self.assertEqual(
list(instance.expand_recipients()),
[]
)

@flag_enabled('INCLUDE_ALL_LOCATIONS')
def test_expand_location_recipients_secondary_matches(self):
schedule = self._create_schedule()

instance = CaseTimedScheduleInstance(
domain=self.domain,
timed_schedule_id=schedule.schedule_id,
recipient_type='Location',
recipient_id=self.city_location_2.location_id
)
self.assertEqual(
[self.mobile_user.get_id],
self.user_ids(instance.expand_recipients()),
)

def test_expand_group_recipients(self):
schedule = self._create_schedule()
instance = CaseTimedScheduleInstance(
domain=self.domain,
timed_schedule_id=schedule.schedule_id,
Expand All @@ -638,14 +680,7 @@ def test_expand_group_recipients(self):
)

def test_mobile_worker_recipients_with_user_data_filter(self):
schedule = TimedSchedule.create_simple_daily_schedule(
self.domain,
TimedEvent(time=time(9, 0)),
SMSContent(message={'en': 'Hello'})
)
schedule.user_data_filter = {'role': ['nurse']}
schedule.save()

schedule = self._create_schedule(user_data_filter={'role': ['nurse']})
instance = CaseTimedScheduleInstance(
domain=self.domain,
timed_schedule_id=schedule.schedule_id,
Expand All @@ -658,13 +693,7 @@ def test_mobile_worker_recipients_with_user_data_filter(self):
)

def test_web_user_recipient_with_user_data_filter(self):
schedule = TimedSchedule.create_simple_daily_schedule(
self.domain,
TimedEvent(time=time(9, 0)),
SMSContent(message={'en': 'Hello'})
)
schedule.user_data_filter = {'role': ['nurse']}
schedule.save()
schedule = self._create_schedule(user_data_filter={'role': ['nurse']})

instance = CaseTimedScheduleInstance(
domain=self.domain,
Expand All @@ -688,14 +717,7 @@ def test_web_user_recipient_with_user_data_filter(self):
def test_case_group_recipient_with_user_data_filter(self):
# The user data filter should have no effect here because all
# the recipients are cases.

schedule = TimedSchedule.create_simple_daily_schedule(
self.domain,
TimedEvent(time=time(9, 0)),
SMSContent(message={'en': 'Hello'})
)
schedule.user_data_filter = {'role': ['nurse']}
schedule.save()
schedule = self._create_schedule(user_data_filter={'role': ['nurse']})

with create_case(self.domain, 'person') as case:
case_group = CommCareCaseGroup(domain=self.domain, cases=[case.case_id])
Expand Down
Loading