Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kelvin-muchiri committed Nov 19, 2024
1 parent 300f067 commit ed3e249
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
9 changes: 9 additions & 0 deletions onadata/apps/api/tests/viewsets/test_data_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1791,6 +1791,10 @@ def test_deletion_of_bulk_submissions(self, mock_cache_set):
response = view(request, pk=formid)

self.assertEqual(response.status_code, 200)
self.assertEqual(
response.data.get("message"),
"%d records were deleted" % len(records_to_be_deleted),
)
self.xform.refresh_from_db()
current_count = self.xform.instances.filter(deleted_at=None).count()
self.assertNotEqual(current_count, initial_count)
Expand Down Expand Up @@ -2023,6 +2027,10 @@ def test_delete_submissions(self):
response = view(request, pk=formid)

self.assertEqual(response.status_code, 200)
self.assertEqual(
response.data.get("message"),
"%d records were deleted" % len(deleted_instances_subset),
)

# Test that num of submissions for the form is successfully updated
self.xform.refresh_from_db()
Expand All @@ -2037,6 +2045,7 @@ def test_delete_submissions(self):
response = view(request, pk=formid)

self.assertEqual(response.status_code, 200)
self.assertEqual(response.data.get("message"), "3 records were deleted")

# Test project details updated successfully
self.assertEqual(
Expand Down
6 changes: 5 additions & 1 deletion onadata/apps/api/viewsets/data_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ def destroy(self, request, *args, **kwargs):
else:
instance_ids = None

initial_num_of_submissions = self.object.num_of_submissions
delete_xform_submissions_async.delay(
self.object.id,
instance_ids,
Expand All @@ -395,9 +396,12 @@ def destroy(self, request, *args, **kwargs):
instance_ids,
XFORM_SUBMISSIONS_DELETING_TTL,
)
number_of_records_deleted = (
len(instance_ids) if instance_ids else initial_num_of_submissions
)

return Response(
data={"message": f"{len(instance_ids)} records were deleted"},
data={"message": f"{number_of_records_deleted} records were deleted"},
status=status.HTTP_200_OK,
)

Expand Down

0 comments on commit ed3e249

Please sign in to comment.