Skip to content

Commit

Permalink
refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kelvin-muchiri committed Jan 16, 2025
1 parent ded27b5 commit 7e1b2e6
Showing 1 changed file with 34 additions and 58 deletions.
92 changes: 34 additions & 58 deletions onadata/libs/tests/utils/test_logger_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
from onadata.apps.logger.xform_instance_parser import AttachmentNameError
from onadata.apps.main.models.meta_data import MetaData
from onadata.apps.main.tests.test_base import TestBase
from onadata.apps.viewer.models import DataDictionary
from onadata.libs.test_utils.pyxform_test_case import PyxformTestCase
from onadata.libs.utils.common_tags import MEDIA_ALL_RECEIVED, MEDIA_COUNT, TOTAL_MEDIA
from onadata.libs.utils.logger_tools import (
Expand Down Expand Up @@ -1169,9 +1168,6 @@ def setUp(self):

# Disable signals
post_save.disconnect(sender=Instance, dispatch_uid="register_export_repeats")
post_save.disconnect(
sender=DataDictionary, dispatch_uid="create_export_repeat_register"
)
self.project = get_user_default_project(self.user)
md = """
| survey |
Expand Down Expand Up @@ -1220,69 +1216,56 @@ def setUp(self):
self.instance = Instance.objects.create(
xml=self.xml, user=self.user, xform=self.xform
)
self.register = MetaData.objects.get(data_type="export_repeat_register")

def test_repeat_register_not_found(self):
"""Nothing happens if export repeat register is not found"""
self.register.delete()
register_instance_export_repeats(self.instance)

exists = MetaData.objects.filter(data_type="export_repeat_register").exists()
self.assertFalse(exists)

def test_incoming_repeat_max_greater(self):
"""Repeat count is incremented if incoming repeat count is greater"""
metadata = MetaData.objects.create(
content_type=ContentType.objects.get_for_model(self.xform),
object_id=self.xform.id,
data_type="export_repeat_register",
extra_data={
"hospital_repeat": 1,
"child_repeat": 1,
},
data_value="",
)
self.register.extra_data = {
"hospital_repeat": 1,
"child_repeat": 1,
}
self.register.save()
register_instance_export_repeats(self.instance)

metadata.refresh_from_db()
self.assertEqual(metadata.extra_data.get("hospital_repeat"), 2)
self.assertEqual(metadata.extra_data.get("child_repeat"), 2)
self.register.refresh_from_db()
self.assertEqual(self.register.extra_data.get("hospital_repeat"), 2)
self.assertEqual(self.register.extra_data.get("child_repeat"), 2)

def test_incoming_repeat_max_less(self):
"""Repeat count is unchanged if incoming repeat count is less"""
metadata = MetaData.objects.create(
content_type=ContentType.objects.get_for_model(self.xform),
object_id=self.xform.id,
data_type="export_repeat_register",
extra_data={
"hospital_repeat": 3,
"child_repeat": 3,
},
data_value="",
)
self.register.extra_data = {
"hospital_repeat": 3,
"child_repeat": 3,
}
self.register.save()
register_instance_export_repeats(self.instance)

metadata.refresh_from_db()
self.register.refresh_from_db()
# repeat counts remain unchanged
self.assertEqual(metadata.extra_data.get("hospital_repeat"), 3)
self.assertEqual(metadata.extra_data.get("child_repeat"), 3)
self.assertEqual(self.register.extra_data.get("hospital_repeat"), 3)
self.assertEqual(self.register.extra_data.get("child_repeat"), 3)

def test_incoming_repeat_max_equal(self):
"""Repeat count is unchanged if incoming repeat count is equal"""
metadata = MetaData.objects.create(
content_type=ContentType.objects.get_for_model(self.xform),
object_id=self.xform.id,
data_type="export_repeat_register",
extra_data={
"hospital_repeat": 2,
"child_repeat": 2,
},
data_value="",
)
self.register.extra_data = {
"hospital_repeat": 2,
"child_repeat": 2,
}
self.register.save()
register_instance_export_repeats(self.instance)

metadata.refresh_from_db()
self.register.refresh_from_db()
# repeat counts remain unchanged
self.assertEqual(metadata.extra_data.get("hospital_repeat"), 2)
self.assertEqual(metadata.extra_data.get("child_repeat"), 2)
self.assertEqual(self.register.extra_data.get("hospital_repeat"), 2)
self.assertEqual(self.register.extra_data.get("child_repeat"), 2)

def test_no_repeats(self):
"""No change in register if no repeats are found in the instance"""
Expand All @@ -1309,17 +1292,16 @@ def test_no_repeats(self):
"</data>"
)
instance = Instance.objects.create(xml=xml, user=self.user, xform=xform)
metadata = MetaData.objects.create(
content_type=ContentType.objects.get_for_model(self.xform),
register = MetaData.objects.get(
content_type=ContentType.objects.get_for_model(xform),
object_id=self.xform.id,
data_type="export_repeat_register",
data_value="",
)

register_instance_export_repeats(instance)
metadata.refresh_from_db()
register.refresh_from_db()

self.assertEqual(metadata.extra_data, {})
self.assertEqual(register.extra_data, {})

def test_submission_review_enabled(self):
"""When submission review is enabled, only approved Instance is registered"""
Expand All @@ -1328,17 +1310,11 @@ def test_submission_review_enabled(self):
self.instance = Instance.objects.create(
xml=self.xml, user=self.user, xform=self.xform
)
metadata = MetaData.objects.create(
content_type=ContentType.objects.get_for_model(self.xform),
object_id=self.xform.id,
data_type="export_repeat_register",
data_value="",
)
register_instance_export_repeats(self.instance)

metadata.refresh_from_db()
self.register.refresh_from_db()

self.assertEqual(metadata.extra_data, {})
self.assertEqual(self.register.extra_data, {})

# Approve submission
SubmissionReview.objects.create(
Expand All @@ -1347,9 +1323,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):
Expand Down

0 comments on commit 7e1b2e6

Please sign in to comment.