diff --git a/onadata/apps/logger/models/instance.py b/onadata/apps/logger/models/instance.py index 5cb57c34fb..840d5e6aaf 100644 --- a/onadata/apps/logger/models/instance.py +++ b/onadata/apps/logger/models/instance.py @@ -829,9 +829,19 @@ def post_save_submission(sender, instance=None, created=False, **kwargs): _update_xform_submission_count_delete(instance) if ASYNC_POST_SUBMISSION_PROCESSING_ENABLED: - update_xform_submission_count_async.apply_async(args=[instance.pk, created]) - save_full_json_async.apply_async(args=[instance.pk, created]) - update_project_date_modified_async.apply_async(args=[instance.pk, created]) + transaction.on_commit( + lambda: update_xform_submission_count_async.apply_async( + args=[instance.pk, created] + ) + ) + transaction.on_commit( + lambda: save_full_json_async.apply_async(args=[instance.pk, created]) + ) + transaction.on_commit( + lambda: update_project_date_modified_async.apply_async( + args=[instance.pk, created] + ) + ) else: update_xform_submission_count(instance.pk, created) diff --git a/onadata/apps/logger/tests/test_form_submission.py b/onadata/apps/logger/tests/test_form_submission.py index cfc06d6fe5..4ab3d651dc 100644 --- a/onadata/apps/logger/tests/test_form_submission.py +++ b/onadata/apps/logger/tests/test_form_submission.py @@ -10,6 +10,7 @@ from django.http import UnreadablePostError from django_digest.test import DigestAuth from django_digest.test import Client as DigestClient +from django.test.utils import override_settings from guardian.shortcuts import assign_perm from mock import patch, Mock, ANY from nose import SkipTest @@ -633,3 +634,30 @@ def test_form_submission_with_infinity_values(self): self._make_submission(path=xml_submission_file_path) self.assertEqual(400, self.response.status_code) self.assertIn("invalid input syntax for type json", str(self.response.message)) + + @override_settings(CELERY_TASK_ALWAYS_EAGER=True) + @override_settings(ASYNC_POST_SUBMISSION_PROCESSING_ENABLED=True) + @patch("onadata.apps.viewer.signals._post_process_submissions") + def test_post_save_submission_count_update(self, mock): + """Test that submission count is updated asyncronously""" + # initial count should be 0 + self.assertEqual(0, self.xform.instances.count()) + # publish submission + xml_submission_file_path = os.path.join( + os.path.dirname(os.path.abspath(__file__)), + "../fixtures/tutorial/instances/tutorial_2012-06-27_11-27-53.xml", + ) + + self._make_submission(xml_submission_file_path) + self.assertEqual(self.response.status_code, 201) + + # test that post_save signal is executed + instance = self.xform.instances.first() + self.assertTrue(mock.called) + mock.assert_called_once_with(instance) + self.assertEqual(mock.call_count, 1) + self.xform.refresh_from_db() + + # test submission count + self.assertEqual(1, self.xform.instances.count()) + self.assertEqual(1, self.xform.num_of_submissions)