From d57996b511b403afd6f63fbab6a418b94c2782d2 Mon Sep 17 00:00:00 2001 From: Kelvin Muchiri Date: Fri, 7 Jun 2024 10:18:16 +0300 Subject: [PATCH] refactor tests --- .../apps/logger/tests/models/test_entity.py | 2 +- .../apps/logger/tests/models/test_instance.py | 341 ++++++++---------- .../instances/trees_create_update_true.xml | 18 - .../instances/trees_registration_false.xml | 28 -- .../instances/trees_registration_true.xml | 28 -- .../entities/instances/trees_update.xml | 16 - .../entities/instances/trees_update_false.xml | 16 - .../entities/instances/trees_update_label.xml | 18 - .../entities/instances/trees_update_true.xml | 16 - 9 files changed, 161 insertions(+), 322 deletions(-) delete mode 100644 onadata/apps/main/tests/fixtures/entities/instances/trees_create_update_true.xml delete mode 100644 onadata/apps/main/tests/fixtures/entities/instances/trees_registration_false.xml delete mode 100644 onadata/apps/main/tests/fixtures/entities/instances/trees_registration_true.xml delete mode 100644 onadata/apps/main/tests/fixtures/entities/instances/trees_update.xml delete mode 100644 onadata/apps/main/tests/fixtures/entities/instances/trees_update_false.xml delete mode 100644 onadata/apps/main/tests/fixtures/entities/instances/trees_update_label.xml delete mode 100644 onadata/apps/main/tests/fixtures/entities/instances/trees_update_true.xml diff --git a/onadata/apps/logger/tests/models/test_entity.py b/onadata/apps/logger/tests/models/test_entity.py index e4c4a947e9..7ecf10d80d 100644 --- a/onadata/apps/logger/tests/models/test_entity.py +++ b/onadata/apps/logger/tests/models/test_entity.py @@ -60,7 +60,7 @@ def setUp(self): self.xml = ( '' '' + '"http://openrosa.org/xforms" id="trees_registration" version="2022110901">' "d156a2dce4c34751af57f21ef5c4e6cc" "-1.286905 36.772845 0 0" "purpleheart" diff --git a/onadata/apps/logger/tests/models/test_instance.py b/onadata/apps/logger/tests/models/test_instance.py index 3c4ea908da..ebf7a60fd0 100644 --- a/onadata/apps/logger/tests/models/test_instance.py +++ b/onadata/apps/logger/tests/models/test_instance.py @@ -4,10 +4,8 @@ """ import os from datetime import datetime, timedelta -from hashlib import sha256 from unittest.mock import Mock, patch -from django.conf import settings from django.http.request import HttpRequest from django.test import override_settings from django.utils.timezone import utc @@ -430,26 +428,25 @@ def test_create_entity(self): """An Entity is created from a submission""" self.project = get_user_default_project(self.user) xform = self._publish_registration_form(self.user) - submission_path = os.path.join( - settings.PROJECT_ROOT, - "apps", - "main", - "tests", - "fixtures", - "entities", - "instances", - "trees_registration.xml", + xml = ( + '' + '' + "d156a2dce4c34751af57f21ef5c4e6cc" + "-1.286905 36.772845 0 0" + "purpleheart" + "300" + "" + "" + "uuid:9d3f042e-cfec-4d2a-8b5b-212e3b04802b" + "300cm purpleheart" + '' + "" + "" + "" + "" ) - - with open(submission_path, "rb") as file: - xml = file.read() - instance = Instance.objects.create( - xml=xml.decode("utf-8"), - user=self.user, - xform=xform, - checksum=sha256(xml).hexdigest(), - uuid="9d3f042e-cfec-4d2a-8b5b-212e3b04802b", - ) + instance = Instance.objects.create(xml=xml, user=self.user, xform=xform) self.assertEqual(Entity.objects.count(), 1) @@ -510,26 +507,25 @@ def test_create_entity_false(self): title="Trees registration", ) xform = XForm.objects.all().order_by("-pk").first() - submission_path = os.path.join( - settings.PROJECT_ROOT, - "apps", - "main", - "tests", - "fixtures", - "entities", - "instances", - "trees_registration_false.xml", + xml = ( + '' + '' + "d156a2dce4c34751af57f21ef5c4e6cc" + "-1.286905 36.772845 0 0" + "purpleheart" + "300" + "" + "" + "uuid:9d3f042e-cfec-4d2a-8b5b-212e3b04802b" + "300cm purpleheart" + '' + "" + "" + "" + "" ) - - with open(submission_path, "rb") as file: - xml = file.read() - Instance.objects.create( - xml=xml.decode("utf-8"), - user=self.user, - xform=xform, - checksum=sha256(xml).hexdigest(), - uuid="9d3f042e-cfec-4d2a-8b5b-212e3b04802b", - ) + Instance.objects.create(xml=xml, user=self.user, xform=xform) self.assertEqual(Entity.objects.count(), 0) @@ -563,26 +559,25 @@ def test_create_entity_true(self): title="Trees registration", ) xform = XForm.objects.all().order_by("-pk").first() - submission_path = os.path.join( - settings.PROJECT_ROOT, - "apps", - "main", - "tests", - "fixtures", - "entities", - "instances", - "trees_registration_true.xml", + xml = ( + '' + '' + "d156a2dce4c34751af57f21ef5c4e6cc" + "-1.286905 36.772845 0 0" + "purpleheart" + "300" + "" + "" + "uuid:9d3f042e-cfec-4d2a-8b5b-212e3b04802b" + "300cm purpleheart" + '' + "" + "" + "" + "" ) - - with open(submission_path, "rb") as file: - xml = file.read() - Instance.objects.create( - xml=xml.decode("utf-8"), - user=self.user, - xform=xform, - checksum=sha256(xml).hexdigest(), - uuid="9d3f042e-cfec-4d2a-8b5b-212e3b04802b", - ) + Instance.objects.create(xml=xml, user=self.user, xform=xform) self.assertEqual(Entity.objects.count(), 1) @@ -593,26 +588,25 @@ def test_registration_form_inactive(self): # Deactivate registration form registration_form.is_active = False registration_form.save() - submission_path = os.path.join( - settings.PROJECT_ROOT, - "apps", - "main", - "tests", - "fixtures", - "entities", - "instances", - "trees_registration.xml", + xml = ( + '' + '' + "d156a2dce4c34751af57f21ef5c4e6cc" + "-1.286905 36.772845 0 0" + "purpleheart" + "300" + "" + "" + "uuid:9d3f042e-cfec-4d2a-8b5b-212e3b04802b" + "300cm purpleheart" + '' + "" + "" + "" + "" ) - - with open(submission_path, "rb") as file: - xml = file.read() - Instance.objects.create( - xml=xml.decode("utf-8"), - user=self.user, - xform=xform, - checksum=sha256(xml).hexdigest(), - uuid="9d3f042e-cfec-4d2a-8b5b-212e3b04802b", - ) + Instance.objects.create(xml=xml, user=self.user, xform=xform) self.assertEqual(Entity.objects.count(), 0) @@ -640,26 +634,22 @@ def test_update_entity(self): self._simulate_existing_entity() # Update Entity via submission xform = self._publish_entity_update_form(self.user) - submission_path = os.path.join( - settings.PROJECT_ROOT, - "apps", - "main", - "tests", - "fixtures", - "entities", - "instances", - "trees_update.xml", + xml = ( + '' + '' + "a9caf13e366b44a68f173bbb6746e3d4" + "dbee4c32-a922-451c-9df7-42f40bf78f48" + "30" + "2024-05-28" + "" + "uuid:45d27780-48fd-4035-8655-9332649385bd" + "30cm dbee4c32-a922-451c-9df7-42f40bf78f48" + '' + "" + "" ) - - with open(submission_path, "rb") as file: - xml = file.read() - instance = Instance.objects.create( - xml=xml.decode("utf-8"), - user=self.user, - xform=xform, - checksum=sha256(xml).hexdigest(), - uuid="45d27780-48fd-4035-8655-9332649385bd", - ) + instance = Instance.objects.create(xml=xml, user=self.user, xform=xform) # Update XForm is a RegistrationForm self.assertEqual(RegistrationForm.objects.filter(xform=xform).count(), 1) # No new Entity created @@ -682,7 +672,7 @@ def test_update_entity(self): self.assertEqual(entity_history.registration_form, registration_form) self.assertEqual(entity_history.instance, instance) - self.assertEqual(entity_history.xml, instance.xml) + self.assertEqual(entity_history.xml, xml) self.assertEqual(entity_history.json, expected_json) self.assertEqual(entity_history.form_version, xform.version) self.assertEqual(entity_history.created_by, instance.user) @@ -713,27 +703,25 @@ def test_update_entity_label(self): id_string="trees_update", title="Trees update", ) - submission_path = os.path.join( - settings.PROJECT_ROOT, - "apps", - "main", - "tests", - "fixtures", - "entities", - "instances", - "trees_update_label.xml", - ) updating_xform = XForm.objects.all().order_by("-pk").first() - - with open(submission_path, "rb") as file: - xml = file.read() - Instance.objects.create( - xml=xml.decode("utf-8"), - user=self.user, - xform=updating_xform, - checksum=sha256(xml).hexdigest(), - uuid="45d27780-48fd-4035-8655-9332649385bd", - ) + xml = ( + '' + '' + "a9caf13e366b44a68f173bbb6746e3d4" + "dbee4c32-a922-451c-9df7-42f40bf78f48" + "30" + "2024-05-28" + "" + "uuid:45d27780-48fd-4035-8655-9332649385bd" + "30cm dbee4c32-a922-451c-9df7-42f40bf78f48" + '' + "" + "" + "" + "" + ) + Instance.objects.create(xml=xml, user=self.user, xform=updating_xform) self.entity.refresh_from_db() @@ -773,27 +761,23 @@ def test_update_entity_false(self): id_string="trees_update", title="Trees update", ) - submission_path = os.path.join( - settings.PROJECT_ROOT, - "apps", - "main", - "tests", - "fixtures", - "entities", - "instances", - "trees_update_false.xml", - ) updating_xform = XForm.objects.all().order_by("-pk").first() - - with open(submission_path, "rb") as file: - xml = file.read() - Instance.objects.create( - xml=xml.decode("utf-8"), - user=self.user, - xform=updating_xform, - checksum=sha256(xml).hexdigest(), - uuid="45d27780-48fd-4035-8655-9332649385bd", - ) + xml = ( + '' + '' + "a9caf13e366b44a68f173bbb6746e3d4" + "dbee4c32-a922-451c-9df7-42f40bf78f48" + "30" + "2024-05-28" + "" + "uuid:45d27780-48fd-4035-8655-9332649385bd" + "30cm dbee4c32-a922-451c-9df7-42f40bf78f48" + '' + "" + "" + ) + Instance.objects.create(xml=xml, user=self.user, xform=updating_xform) expected_json = self.entity.json self.entity.refresh_from_db() @@ -821,27 +805,24 @@ def test_update_entity_true(self): id_string="trees_update", title="Trees update", ) - submission_path = os.path.join( - settings.PROJECT_ROOT, - "apps", - "main", - "tests", - "fixtures", - "entities", - "instances", - "trees_update_true.xml", - ) updating_xform = XForm.objects.all().order_by("-pk").first() - - with open(submission_path, "rb") as file: - xml = file.read() - Instance.objects.create( - xml=xml.decode("utf-8"), - user=self.user, - xform=updating_xform, - checksum=sha256(xml).hexdigest(), - uuid="45d27780-48fd-4035-8655-9332649385bd", - ) + xml = ( + '' + '' + "a9caf13e366b44a68f173bbb6746e3d4" + "dbee4c32-a922-451c-9df7-42f40bf78f48" + "30" + "2024-05-28" + "" + "uuid:45d27780-48fd-4035-8655-9332649385bd" + "30cm dbee4c32-a922-451c-9df7-42f40bf78f48" + '' + "" + "" + ) + Instance.objects.create(xml=xml, user=self.user, xform=updating_xform) expected_json = { "id": self.entity.pk, "species": "purpleheart", @@ -877,32 +858,30 @@ def test_entity_create_update_true(self): title="Trees update", ) xform = XForm.objects.all().order_by("-pk").first() + xml = ( + '' + '' + "a9caf13e366b44a68f173bbb6746e3d4" + "dbee4c32-a922-451c-9df7-42f40bf78f48" + "30" + "2024-05-28" + "" + "uuid:45d27780-48fd-4035-8655-9332649385bd" + "30cm dbee4c32-a922-451c-9df7-42f40bf78f48" + '' + "" + "" + "" + "" + ) # If Entity, does not exist, we create one - submission_path = os.path.join( - settings.PROJECT_ROOT, - "apps", - "main", - "tests", - "fixtures", - "entities", - "instances", - "trees_create_update_true.xml", - ) + Instance.objects.create(xml=xml, user=self.user, xform=xform) - def _update_entity(): - with open(submission_path, "rb") as file: - xml = file.read() - Instance.objects.create( - xml=xml.decode("utf-8"), - user=self.user, - xform=xform, - checksum=sha256(xml).hexdigest(), - uuid="9d3f042e-cfec-4d2a-8b5b-212e3b04802b", - ) - - _update_entity() self.assertEqual(Entity.objects.count(), 1) + entity = Entity.objects.first() expected_json = { "id": entity.pk, @@ -917,7 +896,7 @@ def _update_entity(): Entity.objects.all().delete() # Simulate existsing Entity self._simulate_existing_entity() - _update_entity() + Instance.objects.create(xml=xml, user=self.user, xform=xform) expected_json = { "id": self.entity.pk, "species": "purpleheart", diff --git a/onadata/apps/main/tests/fixtures/entities/instances/trees_create_update_true.xml b/onadata/apps/main/tests/fixtures/entities/instances/trees_create_update_true.xml deleted file mode 100644 index c6cb7fb8d9..0000000000 --- a/onadata/apps/main/tests/fixtures/entities/instances/trees_create_update_true.xml +++ /dev/null @@ -1,18 +0,0 @@ - - - - a9caf13e366b44a68f173bbb6746e3d4 - - dbee4c32-a922-451c-9df7-42f40bf78f48 - 30 - 2024-05-28 - - uuid:45d27780-48fd-4035-8655-9332649385bd - 30cm dbee4c32-a922-451c-9df7-42f40bf78f48 - - - - - \ No newline at end of file diff --git a/onadata/apps/main/tests/fixtures/entities/instances/trees_registration_false.xml b/onadata/apps/main/tests/fixtures/entities/instances/trees_registration_false.xml deleted file mode 100644 index bd6ada0e7d..0000000000 --- a/onadata/apps/main/tests/fixtures/entities/instances/trees_registration_false.xml +++ /dev/null @@ -1,28 +0,0 @@ - - - \n - - \n - d156a2dce4c34751af57f21ef5c4e6cc - \n - - \n - -1.286905 36.772845 0 0 - \n - purpleheart - \n - 300 - \n - - \n - - \n - uuid:9d3f042e-cfec-4d2a-8b5b-212e3b04802b - \n - 300cm purpleheart - - - - - \n - \ No newline at end of file diff --git a/onadata/apps/main/tests/fixtures/entities/instances/trees_registration_true.xml b/onadata/apps/main/tests/fixtures/entities/instances/trees_registration_true.xml deleted file mode 100644 index 6c46ddfe92..0000000000 --- a/onadata/apps/main/tests/fixtures/entities/instances/trees_registration_true.xml +++ /dev/null @@ -1,28 +0,0 @@ - - - \n - - \n - d156a2dce4c34751af57f21ef5c4e6cc - \n - - \n - -1.286905 36.772845 0 0 - \n - purpleheart - \n - 300 - \n - - \n - - \n - uuid:9d3f042e-cfec-4d2a-8b5b-212e3b04802b - \n - 300cm purpleheart - - - - - \n - \ No newline at end of file diff --git a/onadata/apps/main/tests/fixtures/entities/instances/trees_update.xml b/onadata/apps/main/tests/fixtures/entities/instances/trees_update.xml deleted file mode 100644 index 135437d902..0000000000 --- a/onadata/apps/main/tests/fixtures/entities/instances/trees_update.xml +++ /dev/null @@ -1,16 +0,0 @@ - - - - a9caf13e366b44a68f173bbb6746e3d4 - - dbee4c32-a922-451c-9df7-42f40bf78f48 - 30 - 2024-05-28 - - uuid:45d27780-48fd-4035-8655-9332649385bd - 30cm dbee4c32-a922-451c-9df7-42f40bf78f48 - - - \ No newline at end of file diff --git a/onadata/apps/main/tests/fixtures/entities/instances/trees_update_false.xml b/onadata/apps/main/tests/fixtures/entities/instances/trees_update_false.xml deleted file mode 100644 index e30c573474..0000000000 --- a/onadata/apps/main/tests/fixtures/entities/instances/trees_update_false.xml +++ /dev/null @@ -1,16 +0,0 @@ - - - - a9caf13e366b44a68f173bbb6746e3d4 - - dbee4c32-a922-451c-9df7-42f40bf78f48 - 30 - 2024-05-28 - - uuid:45d27780-48fd-4035-8655-9332649385bd - 30cm dbee4c32-a922-451c-9df7-42f40bf78f48 - - - \ No newline at end of file diff --git a/onadata/apps/main/tests/fixtures/entities/instances/trees_update_label.xml b/onadata/apps/main/tests/fixtures/entities/instances/trees_update_label.xml deleted file mode 100644 index e210571b31..0000000000 --- a/onadata/apps/main/tests/fixtures/entities/instances/trees_update_label.xml +++ /dev/null @@ -1,18 +0,0 @@ - - - - a9caf13e366b44a68f173bbb6746e3d4 - - dbee4c32-a922-451c-9df7-42f40bf78f48 - 30 - 2024-05-28 - - uuid:45d27780-48fd-4035-8655-9332649385bd - 30cm dbee4c32-a922-451c-9df7-42f40bf78f48 - - - - - \ No newline at end of file diff --git a/onadata/apps/main/tests/fixtures/entities/instances/trees_update_true.xml b/onadata/apps/main/tests/fixtures/entities/instances/trees_update_true.xml deleted file mode 100644 index 560488a260..0000000000 --- a/onadata/apps/main/tests/fixtures/entities/instances/trees_update_true.xml +++ /dev/null @@ -1,16 +0,0 @@ - - - - a9caf13e366b44a68f173bbb6746e3d4 - - dbee4c32-a922-451c-9df7-42f40bf78f48 - 30 - 2024-05-28 - - uuid:45d27780-48fd-4035-8655-9332649385bd - 30cm dbee4c32-a922-451c-9df7-42f40bf78f48 - - - \ No newline at end of file