Skip to content

Commit

Permalink
fix failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kelvin-muchiri committed Nov 18, 2024
1 parent 4b4877d commit 90d741c
Showing 1 changed file with 10 additions and 54 deletions.
64 changes: 10 additions & 54 deletions onadata/apps/api/tests/viewsets/test_data_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1671,8 +1671,7 @@ def test_data_w_attachment(self):
self.assertIsInstance(response.data, dict)
self.assertDictContainsSubset(data, response.data)

@patch("onadata.apps.api.viewsets.data_viewset.send_message")
def test_delete_submission(self, send_message_mock):
def test_delete_submission(self):
self._make_submissions()
formid = self.xform.pk
dataid = self.xform.instances.all().order_by("id")[0].pk
Expand All @@ -1691,15 +1690,6 @@ def test_delete_submission(self, send_message_mock):
self.assertEqual(response.status_code, 204)
first_xform_instance = self.xform.instances.filter(pk=dataid)
self.assertEqual(first_xform_instance[0].deleted_by, request.user)
# message sent upon delete
self.assertTrue(send_message_mock.called)
send_message_mock.assert_called_with(
instance_id=dataid,
target_id=formid,
target_type=XFORM,
user=request.user,
message_verb=SUBMISSION_DELETED,
)

# second delete of same submission should return 404
request = self.factory.delete("/", **self.extra)
Expand Down Expand Up @@ -1767,8 +1757,8 @@ def test_post_save_signal_on_submission_deletion(self, mock, send_message_mock):
self.assertEqual(mock.call_count, 1)
self.assertTrue(send_message_mock.called)

@patch("onadata.apps.api.viewsets.data_viewset.send_message")
def test_deletion_of_bulk_submissions(self, send_message_mock):
@patch("onadata.apps.api.viewsets.data_viewset.safe_cache_set")
def test_deletion_of_bulk_submissions(self, mock_cache_set):
self._make_submissions()
self.xform.refresh_from_db()
formid = self.xform.pk
Expand Down Expand Up @@ -1800,23 +1790,16 @@ def test_deletion_of_bulk_submissions(self, send_message_mock):
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.assertTrue(send_message_mock.called)
send_message_mock.assert_called_with(
instance_id=[str(i.pk) for i in records_to_be_deleted],
target_id=formid,
target_type=XFORM,
user=request.user,
message_verb=SUBMISSION_DELETED,
)
self.xform.refresh_from_db()
current_count = self.xform.instances.filter(deleted_at=None).count()
self.assertNotEqual(current_count, initial_count)
self.assertEqual(current_count, 2)
self.assertEqual(self.xform.num_of_submissions, 2)
mock_cache_set.assert_called_once_with(
f"xfm-submissions-under-deletion-{formid}",
[str(i.pk) for i in records_to_be_deleted],
3600,
)

@override_settings(ENABLE_SUBMISSION_PERMANENT_DELETE=True)
@patch("onadata.apps.api.viewsets.data_viewset.send_message")
Expand Down Expand Up @@ -1879,8 +1862,7 @@ def test_submissions_permanent_deletion(self, send_message_mock):
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):
def test_permanent_deletions_bulk_submissions(self):
"""
Test that permanent bulk submission deletions work
"""
Expand All @@ -1900,18 +1882,6 @@ def test_permanent_deletions_bulk_submissions(self, send_message_mock):
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.assertTrue(send_message_mock.called)
send_message_mock.assert_called_with(
instance_id=[str(i.pk) for i in records_to_be_deleted],
target_id=formid,
target_type=XFORM,
user=request.user,
message_verb=SUBMISSION_DELETED,
)
self.xform.refresh_from_db()
current_count = self.xform.num_of_submissions
self.assertNotEqual(current_count, initial_count)
Expand Down Expand Up @@ -2016,8 +1986,7 @@ def test_delete_submission_inactive_form(self, send_message_mock):
self.assertEqual(response.status_code, 400)
self.assertTrue(send_message_mock.called)

@patch("onadata.apps.api.viewsets.data_viewset.send_message")
def test_delete_submissions(self, send_message_mock):
def test_delete_submissions(self):
xls_file_path = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"../fixtures/tutorial/tutorial.xlsx",
Expand Down Expand Up @@ -2053,18 +2022,6 @@ def test_delete_submissions(self, send_message_mock):
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),
)
self.assertTrue(send_message_mock.called)
send_message_mock.assert_called_with(
instance_id=[str(i.pk) for i in deleted_instances_subset],
target_id=formid,
target_type=XFORM,
user=request.user,
message_verb=SUBMISSION_DELETED,
)

# Test that num of submissions for the form is successfully updated
self.xform.refresh_from_db()
Expand All @@ -2079,7 +2036,6 @@ def test_delete_submissions(self, send_message_mock):
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

0 comments on commit 90d741c

Please sign in to comment.