From 90d741c190b0f66250af29ea85cb790d7c923a21 Mon Sep 17 00:00:00 2001 From: Kelvin Muchiri Date: Mon, 18 Nov 2024 09:34:34 +0300 Subject: [PATCH] fix failing tests --- .../api/tests/viewsets/test_data_viewset.py | 64 +++---------------- 1 file changed, 10 insertions(+), 54 deletions(-) diff --git a/onadata/apps/api/tests/viewsets/test_data_viewset.py b/onadata/apps/api/tests/viewsets/test_data_viewset.py index cadce3b8da..f3c4ff33fb 100644 --- a/onadata/apps/api/tests/viewsets/test_data_viewset.py +++ b/onadata/apps/api/tests/viewsets/test_data_viewset.py @@ -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 @@ -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) @@ -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 @@ -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") @@ -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 """ @@ -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) @@ -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", @@ -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() @@ -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(