diff --git a/corehq/apps/hqcase/case_deletion_utils.py b/corehq/apps/hqcase/case_deletion_utils.py index aca25bdcb908..5b61c0059bdd 100644 --- a/corehq/apps/hqcase/case_deletion_utils.py +++ b/corehq/apps/hqcase/case_deletion_utils.py @@ -108,6 +108,7 @@ def get_deleted_case_name(case): def get_all_cases_from_form(form, domain): + # A more inclusive method of getting cases from a form, including cases whose deleted field is True touched_cases = {} case_updates = get_case_updates(form) for update in case_updates: diff --git a/corehq/apps/hqcase/tests/test_utils.py b/corehq/apps/hqcase/tests/test_utils.py index e44093e5d84e..3fabc1cbe949 100644 --- a/corehq/apps/hqcase/tests/test_utils.py +++ b/corehq/apps/hqcase/tests/test_utils.py @@ -11,7 +11,11 @@ get_deidentified_data, submit_case_blocks, ) -from corehq.apps.hqcase.case_deletion_utils import get_ordered_case_xforms +from corehq.apps.hqcase.case_deletion_utils import ( + get_all_cases_from_form, + get_deleted_case_name, + get_ordered_case_xforms, +) from corehq.apps.reports.tests.test_case_data import _delete_all_cases_and_forms from corehq.form_processor.models import CommCareCase @@ -135,6 +139,31 @@ def test_xform_order(self): for i in range(2): self.assertGreater(xforms[i + 1].received_on, xforms[i].received_on) + def test_get_deleted_case_name(self): + main_case_id = uuid.uuid4().hex + xform, cases = submit_case_blocks([ + CaseBlock(main_case_id, case_name="main_case", create=True).as_text(), + ], DOMAIN) + self.addCleanup(_delete_all_cases_and_forms, DOMAIN) + + xform.archive() + case = CommCareCase.objects.get_case(cases[0].case_id, DOMAIN) + + self.assertFalse(case.name) + self.assertEqual(get_deleted_case_name(case), "main_case") + + def test_get_deleted_cases_from_form(self): + main_case_id = uuid.uuid4().hex + xform, _ = submit_case_blocks([ + CaseBlock(main_case_id, case_name="main_case", create=True).as_text(), + ], DOMAIN) + self.addCleanup(_delete_all_cases_and_forms, DOMAIN) + + xform.archive() + cases = get_all_cases_from_form(xform, DOMAIN) + self.assertEqual(len(cases), 1) + self.assertTrue(cases[main_case_id].case.is_deleted) + @contextmanager def get_case(*args, **kwargs): diff --git a/corehq/apps/reports/tests/test_case_data.py b/corehq/apps/reports/tests/test_case_data.py index 6d96891bde66..23693e11fcf4 100644 --- a/corehq/apps/reports/tests/test_case_data.py +++ b/corehq/apps/reports/tests/test_case_data.py @@ -170,7 +170,7 @@ def make_simple_case(self): return main_case_id - def make_complex_case(self): + def make_complex_case(self, xform_delete=False): main_case_id = uuid.uuid4().hex child_case1_id = uuid.uuid4().hex child_case2_id = uuid.uuid4().hex @@ -180,7 +180,7 @@ def make_complex_case(self): CaseBlock(main_case_id, case_name="main_case", create=True).as_text(), CaseBlock(child_case1_id, case_name="child1", create=True).as_text(), ], self.domain) - submit_case_blocks([ + xform, cases = submit_case_blocks([ CaseBlock(main_case_id, update={}).as_text(), CaseBlock(child_case2_id, case_name="child2", create=True).as_text(), ], self.domain) @@ -194,6 +194,10 @@ def make_complex_case(self): ], self.domain) self.addCleanup(_delete_all_cases_and_forms, self.domain) + if xform_delete: + for case in cases: + if case.case_id != main_case_id: + return xform, case return main_case_id def make_closed_case(self): @@ -272,6 +276,20 @@ def test_case_walk_returns_right_number_of_reopened_cases(self): self.assertEqual(len(case_data['reopened_cases']), 1) + # Testing case data retrieval for form driven case deletion + def test_case_walk_returns_form_delete_case_list(self): + xform, case = self.make_complex_case(xform_delete=True) + case_data = get_case_and_display_data(case, self.domain, xform.form_id) + + self.assertEqual(len(case_data['form_delete_cases']), 1) + self.assertEqual(len(case_data['delete_cases']), 1) + + def test_case_walk_returns_form_delete_affected_list(self): + xform, case = self.make_complex_case(xform_delete=True) + case_data = get_case_and_display_data(case, self.domain, xform.form_id) + + self.assertEqual(len(case_data['form_affected_cases']), 1) + # Testing deletion def test_delete_case(self): """