Skip to content

Commit

Permalink
Add more tests for form deletion, new case deletion util functions
Browse files Browse the repository at this point in the history
  • Loading branch information
minhaminha committed Jan 25, 2024
1 parent 53852ac commit f40be4b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
1 change: 1 addition & 0 deletions corehq/apps/hqcase/case_deletion_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
31 changes: 30 additions & 1 deletion corehq/apps/hqcase/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand Down
22 changes: 20 additions & 2 deletions corehq/apps/reports/tests/test_case_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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):
Expand Down Expand Up @@ -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):
"""
Expand Down

0 comments on commit f40be4b

Please sign in to comment.