Skip to content

Commit

Permalink
Test enhancements and cleanup
Browse files Browse the repository at this point in the history
Signed-off-by: Kipchirchir Sigei <[email protected]>
  • Loading branch information
KipSigei committed Jun 21, 2023
1 parent 81abe77 commit 7ef972c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
17 changes: 14 additions & 3 deletions onadata/apps/api/tests/viewsets/test_data_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1755,6 +1755,7 @@ def test_submissions_permanent_deletion(self, send_message_mock):
# test that xform submission count is updated
self.xform.refresh_from_db()
self.assertEqual(self.xform.num_of_submissions, 3)
self.assertEqual(self.xform.instances.count(), 3)

# Test project details updated successfully
self.assertEqual(
Expand Down Expand Up @@ -1784,6 +1785,10 @@ def test_submissions_permanent_deletion(self, send_message_mock):
response = view(request, pk=formid)
self.assertEqual(len(response.data), 3)

# check number of instances and num_of_submissions field
self.assertEqual(self.xform.instances.count(), 3)
self.assertEqual(self.xform.num_of_submissions, 3)

@override_settings(ENABLE_SUBMISSION_PERMANENT_DELETE=True)
@patch("onadata.apps.api.viewsets.data_viewset.send_message")
def test_permanent_deletions_bulk_submissions(self, send_message_mock):
Expand Down Expand Up @@ -1824,6 +1829,9 @@ def test_permanent_deletions_bulk_submissions(self, send_message_mock):
self.assertEqual(current_count, 2)
self.assertEqual(self.xform.num_of_submissions, 2)

# check number of xform instances
self.assertEqual(self.xform.instances.count(), 2)

@override_settings(ENABLE_SUBMISSION_PERMANENT_DELETE=True)
@patch("onadata.apps.api.viewsets.data_viewset.send_message")
def test_permanent_instance_delete_inactive_form(self, send_message_mock):
Expand All @@ -1849,6 +1857,7 @@ def test_permanent_instance_delete_inactive_form(self, send_message_mock):
# test that xform submission count is updated
self.xform.refresh_from_db()
self.assertEqual(self.xform.num_of_submissions, 3)
self.assertEqual(self.xform.instances.count(), 3)

# make form inactive
self.xform.downloadable = False
Expand All @@ -1866,6 +1875,9 @@ def test_permanent_instance_delete_inactive_form(self, send_message_mock):
self.assertEqual(self.xform.num_of_submissions, 2)
self.assertTrue(send_message_mock.called)

# check number of instances and num_of_submissions field
self.assertEqual(self.xform.instances.count(), 2)

@override_settings(ENABLE_SUBMISSION_PERMANENT_DELETE=False)
def test_failed_permanent_deletion(self):
"""
Expand All @@ -1882,9 +1894,8 @@ def test_failed_permanent_deletion(self):
)
response = view(request, pk=formid, dataid=dataid)
self.assertEqual(response.status_code, 400)
self.assertEqual(
response.data, {"error": "Permanent Submission deletion not allowed"}
)
error_msg = "Permanent submission deletion is not enabled for this server."
self.assertEqual(response.data, {"error": error_msg})

@patch("onadata.apps.api.viewsets.data_viewset.send_message")
def test_delete_submission_inactive_form(self, send_message_mock):
Expand Down
17 changes: 6 additions & 11 deletions onadata/apps/api/viewsets/data_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ def enketo(self, request, *args, **kwargs):

return Response(data=data)

# pylint: disable=too-many-branches
# pylint: disable=too-many-branches,too-many-locals
def destroy(self, request, *args, **kwargs):
"""Deletes submissions data."""
instance_ids = request.data.get("instance_ids")
Expand All @@ -347,6 +347,9 @@ def destroy(self, request, *args, **kwargs):
enable_submission_permanent_delete = getattr(
settings, "ENABLE_SUBMISSION_PERMANENT_DELETE", False
)
permanent_delete_disabled_msg = _(
"Permanent submission deletion is not enabled for this server."
)
# pylint: disable=attribute-defined-outside-init
self.object = self.get_object()

Expand Down Expand Up @@ -376,11 +379,7 @@ def destroy(self, request, *args, **kwargs):
if enable_submission_permanent_delete:
instance.delete()
else:
error_msg = {
"error": _(
"Permanent submission deletion is not enabled for this server."
)
}
error_msg = {"error": permanent_delete_disabled_msg}
break
else:
# enable soft deletion
Expand Down Expand Up @@ -420,11 +419,7 @@ def destroy(self, request, *args, **kwargs):
if enable_submission_permanent_delete:
self.object.delete()
else:
error_msg = {
"error": _(
"Permanent submission deletion is not enabled for this server."
)
}
error_msg = {"error": permanent_delete_disabled_msg}
return Response(error_msg, status=status.HTTP_400_BAD_REQUEST)
else:
# enable soft deletion
Expand Down

0 comments on commit 7ef972c

Please sign in to comment.