diff --git a/onadata/apps/api/tests/viewsets/test_abstract_viewset.py b/onadata/apps/api/tests/viewsets/test_abstract_viewset.py index dc109421f2..845992522f 100644 --- a/onadata/apps/api/tests/viewsets/test_abstract_viewset.py +++ b/onadata/apps/api/tests/viewsets/test_abstract_viewset.py @@ -2,6 +2,7 @@ """ Test base class for API viewset tests. """ + import json import os import re @@ -11,6 +12,7 @@ from django.conf import settings from django.contrib.auth import authenticate, get_user_model from django.contrib.auth.models import Permission +from django.db.models.signals import post_save from django.test import TestCase import requests @@ -42,8 +44,10 @@ from onadata.apps.logger.views import submission from onadata.apps.logger.xform_instance_parser import clean_and_parse_xml from onadata.apps.main import tests as main_tests -from onadata.apps.main.tests.test_base import TestBase from onadata.apps.main.models import MetaData, UserProfile +from onadata.apps.main.tests.test_base import TestBase +from onadata.apps.viewer.models import DataDictionary +from onadata.apps.viewer.models.data_dictionary import create_export_repeat_register from onadata.libs.serializers.project_serializer import ProjectSerializer from onadata.libs.utils.common_tools import merge_dicts @@ -133,6 +137,20 @@ def setUp(self): self.factory = APIRequestFactory() self._login_user_and_profile() self.maxDiff = None + # Disable signals + post_save.disconnect( + sender=DataDictionary, dispatch_uid="create_export_repeat_register" + ) + + def tearDown(self): + # Enable signals + post_save.connect( + sender=DataDictionary, + dispatch_uid="create_export_repeat_register", + receiver=create_export_repeat_register, + ) + + TestCase.tearDown(self) def user_profile_data(self): """Returns the user profile python object.""" diff --git a/onadata/apps/logger/tests/models/test_instance.py b/onadata/apps/logger/tests/models/test_instance.py index c2bfcbeeac..7fafbf61e5 100644 --- a/onadata/apps/logger/tests/models/test_instance.py +++ b/onadata/apps/logger/tests/models/test_instance.py @@ -1228,8 +1228,7 @@ def test_register_repeats(self): | | form_title | form_id | | | | Births | births | | """ - self._publish_markdown(md, self.user, project) - xform = XForm.objects.all().order_by("-pk").first() + xform = self._publish_markdown(md, self.user, project) xml = ( '' '' '' - '' - f"{self.xform.uuid}" - "" - "Aga Khan" - "" - "Zakayo" - "3.3" - "" - "" - "Melania" - "3.5" - "" - "" - "" - "Mama Lucy" - "" - "Winnie" - "3.1" - "" - "" - "" - "Nairobi West" - "" - "Tom" - "3.1" - "" - "" - "" - "uuid:cb5eb8fe-a046-4e75-9c7f-72183b871698" - "" - "" + register = MetaData.objects.get( + content_type=ContentType.objects.get_for_model(xform), + object_id=self.xform.id, + data_type="export_repeat_register", ) - Instance.objects.create(xml=xml, user=self.user, xform=self.xform) - register_instance_export_repeats(self.instance) + register_instance_export_repeats(instance) + register.refresh_from_db() - metadata = MetaData.objects.get(data_type="export_repeat_register") - # The previous submission should be considered since it has the most repeats - # for hospital_repeat - self.assertEqual(metadata.extra_data.get("hospital_repeat"), 3) - self.assertEqual(metadata.extra_data.get("child_repeat"), 2) + self.assertEqual(register.extra_data, {}) def test_submission_review_enabled(self): """When submission review is enabled, only approved Instance is registered""" @@ -1371,9 +1316,9 @@ def test_submission_review_enabled(self): ) register_instance_export_repeats(self.instance) - metadata = MetaData.objects.get(data_type="export_repeat_register") + self.register.refresh_from_db() - self.assertEqual(metadata.extra_data, {}) + self.assertEqual(self.register.extra_data, {}) # Approve submission SubmissionReview.objects.create( @@ -1382,9 +1327,9 @@ def test_submission_review_enabled(self): register_instance_export_repeats(self.instance) - metadata.refresh_from_db() + self.register.refresh_from_db() - self.assertEqual(metadata.extra_data.get("hospital_repeat"), 2) + self.assertEqual(self.register.extra_data.get("hospital_repeat"), 2) class RegisterXFormExportRepeatsTestCase(TestBase): @@ -1393,6 +1338,9 @@ class RegisterXFormExportRepeatsTestCase(TestBase): def setUp(self): super().setUp() + # Disable signals + post_save.disconnect(sender=Instance, dispatch_uid="register_export_repeats") + self.project = get_user_default_project(self.user) md = """ | survey | @@ -1408,8 +1356,7 @@ def setUp(self): | | form_title | form_id | | | | Births | births | | """ - self._publish_markdown(md, self.user, self.project) - self.xform = XForm.objects.all().order_by("-pk").first() + self.xform = self._publish_markdown(md, self.user, self.project) xml = ( '' '