def test_assign_add_property(self): self._make_tree() update = {'reassigned': 'yes'} assign_case(self.primary.case_id, self.primary_user._id, include_subcases=True, include_parent_cases=True, update=update) self._check_state(new_owner_id=self.primary_user._id, expected_changed=self.all, update=update)
def test_assign_to_unknown_user(self): case = self._new_case() try: assign_case(case.case_id, 'noonehere') self.fail('reassigning to nonexistent user should fail') except CaseAssignmentError: pass
def test_assign_all_related(self): self._make_tree() assign_case(self.primary.case_id, self.primary_user._id, include_subcases=True, include_parent_cases=True) self._check_state(new_owner_id=self.primary_user._id, expected_changed=self.all)
def test_assign_exclusion(self): self._make_tree() exclude_fn = lambda case: case._id in (self.grandfather._id, self.primary._id, self.grandson._id) assign_case(self.primary.case_id, self.primary_user._id, include_subcases=True, include_parent_cases=True, exclude_function=exclude_fn) self._check_state(new_owner_id=self.primary_user._id, expected_changed=[ self.grandmother, self.parent, self.son, self.daughter,self.granddaughter, self.grandson2 ])
def test_assign_to_user_in_different_domain(self): other_user = CommCareUser.create('bad-domain', format_username('bad-domain-user', self.domain), "****") case = self._new_case() try: assign_case(case.case_id, other_user._id) self.fail('reassigning to user in wrong domain should fail') except CaseAssignmentError: pass
def test_assign_parent_cases(self): self._make_tree() assign_case(self.primary.case_id, self.primary_user._id, include_subcases=False, include_parent_cases=True) self._check_state(new_owner_id=self.primary_user._id, expected_changed=[ self.grandfather, self.grandmother, self.parent, self.primary ])
def test_assign_subcases(self): self._make_tree() assign_case(self.primary.case_id, self.primary_user._id, include_subcases=True, include_parent_cases=False) self._check_state(new_owner_id=self.primary_user._id, expected_changed=[ self.primary, self.son, self.daughter, self.grandson, self.granddaughter, self.grandson2 ])
def test_assign_to_group(self): group = Group(users=[], name='case-assignment-group', domain=self.domain) group.save() self._make_tree() assign_case(self.primary.case_id, group._id, include_subcases=True, include_parent_cases=True) self._check_state(new_owner_id=group._id, expected_changed=self.all)
def test_assign_noop(self): self._make_tree() num_forms = len(FormAccessors(self.domain).get_forms_by_type('XFormInstance', limit=1000)) self.assertTrue(num_forms < 1000) res = assign_case(self.primary.case_id, self.original_owner._id, include_subcases=True, include_parent_cases=True) self.assertEqual(0, len(res)) new_num_forms = len(FormAccessors(self.domain).get_forms_by_type('XFormInstance', limit=1000)) self.assertEqual(new_num_forms, num_forms)
def test_assign_bad_index_ref(self): # the case has to exist to create the index, but if we delete it later the assignment # shouldn't fail case = self._new_case() case_with_bad_ref = self._new_case(index={'parent': ('person', case._id)}) case.soft_delete() # this call previously failed res = assign_case(case_with_bad_ref.case_id, self.primary_user._id, include_subcases=True, include_parent_cases=True) self.assertEqual(2, len(res)) self.assertIn(case_with_bad_ref.case_id, res) self.assertIn(case._id, res)
def bihar_reassignment(sender, xform, cases, **kwargs): if hasattr(xform, 'domain') and xform.domain in BIHAR_DOMAINS and xform.metadata and xform.metadata.userID != SYSTEM_USERID: owner_ids = set(c.owner_id for c in cases) if len(owner_ids) != 1: logging.warning('form {form} had mismatched case owner ids'.format(form=xform._id)) else: [owner_id] = owner_ids if owner_id not in DEMO_OWNER_IDS: # don't attempt to reassign the cases included in this form cases_not_to_touch = set(c._id for c in cases) def bihar_exclude(case): return case._id in cases_not_to_touch or case.type in REASSIGN_BLACKLIST for case in cases: if case.type in ('cc_bihar_pregnancy', 'cc_bihar_newborn'): reassigned = assign_case( case, owner_id, BiharMockUser(), include_subcases=True, include_parent_cases=True, exclude_function=bihar_exclude, update={'reassignment_form_id': xform._id} ) # update the list of cases not to touch so we don't reassign the same # cases multiple times in the same form cases_not_to_touch = cases_not_to_touch | set(reassigned or [])
self._check_state(new_owner_id=self.primary_user._id, expected_changed=[ self.grandmother, self.parent, self.son, self.daughter, self.granddaughter, self.grandson2 ]) def test_assign_bad_index_ref(self): # the case has to exist to create the index, but if we delete it later the assignment # shouldn't fail case = self._new_case() case_with_bad_ref = self._new_case( index={'parent': ('person', case._id)}) case.soft_delete() # this call previously failed res = assign_case(case_with_bad_ref.case_id, self.primary_user._id, include_subcases=True, include_parent_cases=True) self.assertEqual(2, len(res)) self.assertIn(case_with_bad_ref.case_id, res) self.assertIn(case._id, res) def _make_tree(self): # create a tree that looks like this: # grandmother grandfather # parent # primary # son daughter # grandson granddaughter grandson2 self.grandmother = self._new_case() self.grandfather = self._new_case() self.parent = self._new_case(
def test_assign_parent_cases(self): self._make_tree() assign_case(self.primary.case_id, self.primary_user._id, include_subcases=False, include_parent_cases=True) self._check_state(new_owner_id=self.primary_user._id, expected_changed=[self.grandfather, self.grandmother, self.parent, self.primary])
def test_assign_subcases(self): self._make_tree() assign_case(self.primary.case_id, self.primary_user._id, include_subcases=True, include_parent_cases=False) self._check_state(new_owner_id=self.primary_user._id, expected_changed=[self.primary, self.son, self.daughter, self.grandson, self.granddaughter, self.grandson2])