def test_deleted_case_migration(self): parent_case_id = uuid.uuid4().hex child_case_id = uuid.uuid4().hex parent_case = create_and_save_a_case(self.domain_name, case_id=parent_case_id, case_name='test parent') child_case = create_and_save_a_case(self.domain_name, case_id=child_case_id, case_name='test child') set_parent_case(self.domain_name, child_case, parent_case) form_ids = self._get_form_ids() self.assertEqual(3, len(form_ids)) FormAccessors(self.domain.name).soft_delete_forms( form_ids, datetime.utcnow(), 'test-deletion-with-cases' ) CaseAccessors(self.domain.name).soft_delete_cases( [parent_case_id, child_case_id], datetime.utcnow(), 'test-deletion-with-cases' ) self.assertEqual(2, len(get_doc_ids_in_domain_by_type( self.domain_name, "CommCareCase-Deleted", XFormInstance.get_db()) )) self._do_migration_and_assert_flags(self.domain_name) self.assertEqual(2, len(CaseAccessorSQL.get_deleted_case_ids_in_domain(self.domain_name))) self._compare_diffs([]) parent_transactions = CaseAccessorSQL.get_transactions(parent_case_id) self.assertEqual(2, len(parent_transactions)) self.assertTrue(parent_transactions[0].is_case_create) self.assertTrue(parent_transactions[1].is_form_transaction) child_transactions = CaseAccessorSQL.get_transactions(child_case_id) self.assertEqual(2, len(child_transactions)) self.assertTrue(child_transactions[0].is_case_create) self.assertTrue(child_transactions[1].is_case_index)
def test_extension_case_template_params(self): with self.create_child_case( ) as extension_case, self.create_parent_case() as host_case: set_parent_case(self.domain, extension_case, host_case, relationship='extension') extension_case = CaseAccessors(self.domain).get_case( extension_case.case_id) host_case = CaseAccessors(self.domain).get_case(host_case.case_id) extension_expected_result = {'case': extension_case.to_json()} extension_expected_result['case']['host'] = host_case.to_json() extension_expected_result['case'][ 'owner'] = self.get_expected_template_params_for_mobile() extension_expected_result['case'][ 'last_modified_by'] = self.get_expected_template_params_for_mobile( ) self.assertEqual(get_message_template_params(extension_case), extension_expected_result) host_expected_result = {'case': host_case.to_json()} host_expected_result['case'][ 'owner'] = self.get_expected_template_params_for_mobile() host_expected_result['case'][ 'last_modified_by'] = self.get_expected_template_params_for_mobile( ) self.assertEqual(get_message_template_params(host_case), host_expected_result)
def test_host_case_template_params(self): with self.create_child_case() as extension_case, self.create_parent_case() as host_case: set_parent_case(self.domain, extension_case, host_case, relationship='extension') extension_case = CaseAccessors(self.domain).get_case(extension_case.case_id) r = MessagingTemplateRenderer() r.set_context_param('case', CaseMessagingTemplateParam(extension_case)) self.assertEqual( r.render("Extension case prop: {case.child_prop1}"), "Extension case prop: def" ) self.assertEqual( r.render("Host case prop: {case.host.parent_prop1}"), "Host case prop: abc" ) self.assertEqual( r.render("Don't render case: {case.host}"), "Don't render case: (?)" ) self.assertEqual( r.render("No host host case: {case.host.host.name}"), "No host host case: (?)" ) self.assertEqual( r.render("No parent case: {case.parent.name}"), "No parent case: (?)" )
def test_parent_case_template_params(self): with self.create_child_case() as child_case, self.create_parent_case() as parent_case: set_parent_case(self.domain, child_case, parent_case) child_case = CaseAccessors(self.domain).get_case(child_case.case_id) r = MessagingTemplateRenderer() r.set_context_param('case', CaseMessagingTemplateParam(child_case)) self.assertEqual( r.render("Child case prop: {case.child_prop1}"), "Child case prop: def" ) self.assertEqual( r.render("Parent case prop: {case.parent.parent_prop1}"), "Parent case prop: abc" ) self.assertEqual( r.render("Don't render case: {case.parent}"), "Don't render case: (?)" ) self.assertEqual( r.render("No grandparent case: {case.parent.parent.name}"), "No grandparent case: (?)" ) self.assertEqual( r.render("No host case: {case.host.name}"), "No host case: (?)" )
def test_host_case_owner_location(self): with create_test_case(self.domain, 'test-extension-case', 'name') as extension_case: with create_test_case(self.domain, 'test-host-case', 'name') as host_case: update_case(self.domain, host_case.case_id, case_properties={ 'owner_id': self.city_location.location_id }) set_parent_case(self.domain, extension_case, host_case, relationship='extension') # Test the recipient is returned correctly instance = CaseTimedScheduleInstance( domain=self.domain, case_id=extension_case.case_id, recipient_type=CaseScheduleInstanceMixin. RECIPIENT_TYPE_CUSTOM, recipient_id='HOST_CASE_OWNER_LOCATION', ) self.assertIsInstance(instance.recipient, SQLLocation) self.assertEqual(instance.recipient.location_id, self.city_location.location_id) # Test location that does not exist update_case(self.domain, host_case.case_id, case_properties={'owner_id': 'does-not-exist'}) instance = CaseTimedScheduleInstance( domain=self.domain, case_id=extension_case.case_id, recipient_type=CaseScheduleInstanceMixin. RECIPIENT_TYPE_CUSTOM, recipient_id='HOST_CASE_OWNER_LOCATION', ) self.assertIsNone(instance.recipient) # Test on a case that is not an extension case instance = CaseTimedScheduleInstance( domain=self.domain, case_id=host_case.case_id, recipient_type=CaseScheduleInstanceMixin. RECIPIENT_TYPE_CUSTOM, recipient_id='HOST_CASE_OWNER_LOCATION', ) self.assertIsNone(instance.recipient) # Test with case id that doesn't exist instance = CaseTimedScheduleInstance( domain=self.domain, case_id='does-not-exist', recipient_type=CaseScheduleInstanceMixin. RECIPIENT_TYPE_CUSTOM, recipient_id='HOST_CASE_OWNER_LOCATION', ) self.assertIsNone(instance.recipient)
def test_child_case_recipient(self): with create_case(self.domain, 'person') as child_1, \ create_case(self.domain, 'person') as child_2, \ create_case(self.domain, 'person') as parent: instance = CaseTimedScheduleInstance( domain=self.domain, case_id=parent.case_id, recipient_type=CaseScheduleInstanceMixin. RECIPIENT_TYPE_ALL_CHILD_CASES) self.assertIsInstance(instance.recipient, list) self.assertEqual(len(instance.recipient), 0) set_parent_case(self.domain, child_1, parent, relationship='child', identifier='parent') set_parent_case(self.domain, child_2, parent, relationship='child', identifier='parent') instance = CaseTimedScheduleInstance( domain=self.domain, case_id=parent.case_id, recipient_type=CaseScheduleInstanceMixin. RECIPIENT_TYPE_ALL_CHILD_CASES) self.assertIsInstance(instance.recipient, list) self.assertEqual(len(instance.recipient), 2) self.assertItemsEqual([c.case_id for c in instance.recipient], [child_1.case_id, child_2.case_id])
def test_case_template_params(self): with self.create_child_case() as child_case, self.create_parent_case( ) as parent_case: set_parent_case(self.domain, child_case, parent_case) child_case = CaseAccessors(self.domain).get_case( child_case.case_id) parent_case = CaseAccessors(self.domain).get_case( parent_case.case_id) child_expected_result = {'case': child_case.to_json()} child_expected_result['case']['parent'] = parent_case.to_json() child_expected_result['case'][ 'owner'] = self.get_expected_template_params_for_mobile() child_expected_result['case'][ 'last_modified_by'] = self.get_expected_template_params_for_mobile( ) self.assertEqual(get_message_template_params(child_case), child_expected_result) parent_expected_result = {'case': parent_case.to_json()} parent_expected_result['case'][ 'owner'] = self.get_expected_template_params_for_mobile() parent_expected_result['case'][ 'last_modified_by'] = self.get_expected_template_params_for_mobile( ) self.assertEqual(get_message_template_params(parent_case), parent_expected_result)
def test_host_case_owner_location_parent(self): with create_test_case(self.domain, 'test-extension-case', 'name') as extension_case: with create_test_case(self.domain, 'test-host-case', 'name') as host_case: update_case(self.domain, host_case.case_id, case_properties={'owner_id': self.city_location.location_id}) set_parent_case(self.domain, extension_case, host_case, relationship='extension') # Test the recipient is returned correctly instance = CaseTimedScheduleInstance( domain=self.domain, case_id=extension_case.case_id, recipient_type=CaseScheduleInstanceMixin.RECIPIENT_TYPE_CUSTOM, recipient_id='HOST_CASE_OWNER_LOCATION_PARENT', ) self.assertIsInstance(instance.recipient, SQLLocation) self.assertEqual(instance.recipient.location_id, self.state_location.location_id) # Test no parent location update_case(self.domain, host_case.case_id, case_properties={'owner_id': self.country_location.location_id}) instance = CaseTimedScheduleInstance( domain=self.domain, case_id=extension_case.case_id, recipient_type=CaseScheduleInstanceMixin.RECIPIENT_TYPE_CUSTOM, recipient_id='HOST_CASE_OWNER_LOCATION_PARENT', ) self.assertIsNone(instance.recipient) # Test location that does not exist update_case(self.domain, host_case.case_id, case_properties={'owner_id': 'does-not-exist'}) instance = CaseTimedScheduleInstance( domain=self.domain, case_id=extension_case.case_id, recipient_type=CaseScheduleInstanceMixin.RECIPIENT_TYPE_CUSTOM, recipient_id='HOST_CASE_OWNER_LOCATION_PARENT', ) self.assertIsNone(instance.recipient) # Test on a case that is not an extension case instance = CaseTimedScheduleInstance( domain=self.domain, case_id=host_case.case_id, recipient_type=CaseScheduleInstanceMixin.RECIPIENT_TYPE_CUSTOM, recipient_id='HOST_CASE_OWNER_LOCATION_PARENT', ) self.assertIsNone(instance.recipient) # Test with case id that doesn't exist instance = CaseTimedScheduleInstance( domain=self.domain, case_id='does-not-exist', recipient_type=CaseScheduleInstanceMixin.RECIPIENT_TYPE_CUSTOM, recipient_id='HOST_CASE_OWNER_LOCATION_PARENT', ) self.assertIsNone(instance.recipient)
def test_parent_case_recipient(self): with create_case(self.domain, 'person') as child, create_case(self.domain, 'person') as parent: instance = CaseTimedScheduleInstance(domain=self.domain, case_id=child.case_id, recipient_type=CaseScheduleInstanceMixin.RECIPIENT_TYPE_PARENT_CASE) self.assertIsNone(instance.recipient) set_parent_case(self.domain, child, parent, relationship='child', identifier='parent') instance = CaseTimedScheduleInstance(domain=self.domain, case_id=child.case_id, recipient_type=CaseScheduleInstanceMixin.RECIPIENT_TYPE_PARENT_CASE) self.assertEqual(instance.recipient.case_id, parent.case_id)
def test_parent_case_property_criteria(self): reminder = (CaseReminderHandler .create(self.domain, 'test') .set_case_criteria_start_condition('participant', 'parent/status', MATCH_EXACT, 'green') .set_case_criteria_start_date() .set_case_recipient() .set_sms_content_type('en') .set_daily_schedule(fire_time=time(12, 0), message={'en': 'Hello {case.name}, your test result was normal.'}) .set_stop_condition(max_iteration_count=REPEAT_SCHEDULE_INDEFINITELY) .set_advanced_options()) reminder.active = False reminder.save() self.assertEqual(self.get_reminders(), []) with create_test_case(self.domain, 'participant', 'bob', drop_signals=False) as child_case, \ create_test_case(self.domain, 'parent-case', 'jim', drop_signals=False) as parent_case, \ patch('corehq.apps.reminders.models.CaseReminderHandler.get_now') as now_mock: process_handlers_for_case_changed(self.domain, child_case.case_id, [reminder._id]) self.assertEqual(self.get_reminders(), []) set_parent_case(self.domain, child_case, parent_case) process_handlers_for_case_changed(self.domain, child_case.case_id, [reminder._id]) self.assertEqual(self.get_reminders(), []) now_mock.return_value = datetime(2016, 1, 1, 10, 0) update_case(self.domain, child_case.case_id, case_properties={'status': 'green'}) self.assertEqual(self.get_reminders(), []) update_case(self.domain, child_case.case_id, case_properties={'status': ''}) self.assertEqual(self.get_reminders(), []) update_case(self.domain, parent_case.case_id, case_properties={'status': 'green'}) process_handlers_for_case_changed(self.domain, child_case.case_id, [reminder._id]) reminder_instance = self.assertOneReminder() self.assertEqual(reminder_instance.domain, self.domain) self.assertEqual(reminder_instance.handler_id, reminder.get_id) self.assertFalse(reminder_instance.active) self.assertEqual(reminder_instance.start_date, date(2016, 1, 1)) self.assertIsNone(reminder_instance.start_condition_datetime) self.assertEqual(reminder_instance.next_fire, datetime(2016, 1, 1, 12, 0)) self.assertEqual(reminder_instance.case_id, child_case.case_id) self.assertEqual(reminder_instance.schedule_iteration_num, 1) self.assertEqual(reminder_instance.current_event_sequence_num, 0) self.assertEqual(reminder_instance.callback_try_count, 0) update_case(self.domain, parent_case.case_id, case_properties={'status': 'red'}) process_handlers_for_case_changed(self.domain, child_case.case_id, [reminder._id]) self.assertEqual(self.get_reminders(), [])
def test_case_with_indices_migration(self): parent_case_id = uuid.uuid4().hex child_case_id = uuid.uuid4().hex parent_case = create_and_save_a_case(self.domain_name, case_id=parent_case_id, case_name='test parent') child_case = create_and_save_a_case(self.domain_name, case_id=child_case_id, case_name='test child') set_parent_case(self.domain_name, child_case, parent_case) self.assertEqual(2, len(self._get_case_ids())) self._do_migration_and_assert_flags(self.domain_name) self.assertEqual(2, len(self._get_case_ids())) self._compare_diffs([]) indices = CaseAccessorSQL.get_indices(self.domain_name, child_case_id) self.assertEqual(1, len(indices)) self.assertEqual(parent_case_id, indices[0].referenced_id)
def test_case_template_params(self): with self.create_child_case() as child_case, self.create_parent_case() as parent_case: set_parent_case(self.domain, child_case, parent_case) child_case = CaseAccessors(self.domain).get_case(child_case.case_id) parent_case = CaseAccessors(self.domain).get_case(parent_case.case_id) child_expected_result = {"case": child_case.to_json()} child_expected_result["case"]["parent"] = parent_case.to_json() child_expected_result["case"]["owner"] = self.get_expected_template_params_for_mobile() child_expected_result["case"]["last_modified_by"] = self.get_expected_template_params_for_mobile() self.assertEqual(get_message_template_params(child_case), child_expected_result) parent_expected_result = {"case": parent_case.to_json()} parent_expected_result["case"]["owner"] = self.get_expected_template_params_for_mobile() parent_expected_result["case"]["last_modified_by"] = self.get_expected_template_params_for_mobile() self.assertEqual(get_message_template_params(parent_case), parent_expected_result)
def test_parent_case_property_criteria(self): reminder = (CaseReminderHandler .create(self.domain, 'test') .set_case_criteria_start_condition('participant', 'parent/status', MATCH_EXACT, 'green') .set_case_criteria_start_date() .set_case_recipient() .set_sms_content_type('en') .set_daily_schedule(fire_time=time(12, 0), message={'en': 'Hello {case.name}, your test result was normal.'}) .set_stop_condition(max_iteration_count=REPEAT_SCHEDULE_INDEFINITELY) .set_advanced_options()) reminder.save() self.assertEqual(self.get_reminders(), []) with create_test_case(self.domain, 'participant', 'bob', drop_signals=False) as child_case, \ create_test_case(self.domain, 'parent-case', 'jim', drop_signals=False) as parent_case, \ patch('corehq.apps.reminders.models.CaseReminderHandler.get_now') as now_mock: self.assertEqual(self.get_reminders(), []) set_parent_case(self.domain, child_case, parent_case) self.assertEqual(self.get_reminders(), []) now_mock.return_value = datetime(2016, 1, 1, 10, 0) update_case(self.domain, child_case.case_id, case_properties={'status': 'green'}) self.assertEqual(self.get_reminders(), []) update_case(self.domain, child_case.case_id, case_properties={'status': ''}) self.assertEqual(self.get_reminders(), []) update_case(self.domain, parent_case.case_id, case_properties={'status': 'green'}) reminder_instance = self.assertOneReminder() self.assertEqual(reminder_instance.domain, self.domain) self.assertEqual(reminder_instance.handler_id, reminder.get_id) self.assertTrue(reminder_instance.active) self.assertEqual(reminder_instance.start_date, date(2016, 1, 1)) self.assertIsNone(reminder_instance.start_condition_datetime) self.assertEqual(reminder_instance.next_fire, datetime(2016, 1, 1, 12, 0)) self.assertEqual(reminder_instance.case_id, child_case.case_id) self.assertEqual(reminder_instance.schedule_iteration_num, 1) self.assertEqual(reminder_instance.current_event_sequence_num, 0) self.assertEqual(reminder_instance.callback_try_count, 0) update_case(self.domain, parent_case.case_id, case_properties={'status': 'red'}) self.assertEqual(self.get_reminders(), [])
def test_child_case_recipient(self): with create_case(self.domain, 'person') as child_1, \ create_case(self.domain, 'person') as child_2, \ create_case(self.domain, 'person') as parent: instance = CaseTimedScheduleInstance(domain=self.domain, case_id=parent.case_id, recipient_type=CaseScheduleInstanceMixin.RECIPIENT_TYPE_ALL_CHILD_CASES) self.assertIsInstance(instance.recipient, list) self.assertEqual(len(instance.recipient), 0) set_parent_case(self.domain, child_1, parent, relationship='child', identifier='parent') set_parent_case(self.domain, child_2, parent, relationship='child', identifier='parent') instance = CaseTimedScheduleInstance(domain=self.domain, case_id=parent.case_id, recipient_type=CaseScheduleInstanceMixin.RECIPIENT_TYPE_ALL_CHILD_CASES) self.assertIsInstance(instance.recipient, list) self.assertEqual(len(instance.recipient), 2) self.assertItemsEqual([c.case_id for c in instance.recipient], [child_1.case_id, child_2.case_id])
def test_update_parent(self): es = get_es_new() with trap_extra_setup(ConnectionError): initialize_index_and_mapping(es, CASE_SEARCH_INDEX_INFO) self.addCleanup(ensure_index_deleted, CASE_SEARCH_INDEX_INFO.index) duplicates, uniques = self._create_cases(num_cases=2) parent = uniques[0] child = duplicates[0] set_parent_case(self.domain, child, parent) parent_case_property_value = parent.get_case_property('name') new_parent_case_property_value = f'{parent_case_property_value}-someextratext' self.action.set_properties_to_update([ CaseDeduplicationActionDefinition.PropertyDefinition( name='parent/name', value_type=CaseDeduplicationActionDefinition.VALUE_TYPE_EXACT, value=new_parent_case_property_value, ) ]) self.action.save() for case in chain(duplicates, uniques): send_to_elasticsearch( 'case_search', transform_case_for_elasticsearch(case.to_json())) es.indices.refresh(CASE_SEARCH_INDEX_INFO.index) self.rule = AutomaticUpdateRule.objects.get(id=self.rule.id) self.rule.run_actions_when_case_matches(child) updated_parent_case = CommCareCase.objects.get_case( parent.case_id, self.domain) self.assertEqual(updated_parent_case.get_case_property('name'), new_parent_case_property_value)
def test_host_case_owner_location(self): parent_location = SQLLocation.objects.create( domain=self.domain, name='parent test', site_code='parent', location_type=self.parent_location_type) self.addCleanup(parent_location.delete) child_location = SQLLocation.objects.create( domain=self.domain, name='child test', site_code='child', location_type=self.child_location_type, parent=parent_location) self.addCleanup(child_location.delete) with create_test_case(self.domain, 'test-extension-case', 'name') as extension_case: with create_test_case(self.domain, 'test-host-case', 'name') as host_case: update_case( self.domain, host_case.case_id, case_properties={'owner_id': child_location.location_id}) set_parent_case(self.domain, extension_case, host_case, relationship='extension') handler1 = CaseReminderHandler( domain=self.domain, recipient='HOST_CASE_OWNER_LOCATION') handler2 = CaseReminderHandler( domain=self.domain, recipient='HOST_CASE_OWNER_LOCATION_PARENT') reminder = CaseReminder(domain=self.domain, case_id=extension_case.case_id) # Test the recipients are returned correctly with patch('corehq.apps.reminders.models.CaseReminder.handler', new=handler1): self.assertEqual(reminder.recipient, [child_location]) with patch('corehq.apps.reminders.models.CaseReminder.handler', new=handler2): self.assertEqual(reminder.recipient, [parent_location]) # Remove parent location reference child_location.parent = None child_location.save() with patch('corehq.apps.reminders.models.CaseReminder.handler', new=handler2): self.assertIsNone(reminder.recipient) # Test location that does not exist update_case(self.domain, host_case.case_id, case_properties={'owner_id': 'does-not-exist'}) with patch('corehq.apps.reminders.models.CaseReminder.handler', new=handler1): self.assertIsNone(reminder.recipient) # Test on a case that is not an extension case reminder.case_id = host_case.case_id with patch('corehq.apps.reminders.models.CaseReminder.handler', new=handler1): self.assertIsNone(reminder.recipient) # Test on a blank case id reminder.case_id = None with patch('corehq.apps.reminders.models.CaseReminder.handler', new=handler1): self.assertIsNone(reminder.recipient)