diff --git a/onadata/apps/api/tests/viewsets/test_xform_submission_viewset.py b/onadata/apps/api/tests/viewsets/test_xform_submission_viewset.py index bb5500f1a6..f38c180f7a 100644 --- a/onadata/apps/api/tests/viewsets/test_xform_submission_viewset.py +++ b/onadata/apps/api/tests/viewsets/test_xform_submission_viewset.py @@ -1321,47 +1321,6 @@ def test_edit_submission_sent_to_rapidpro(self, mock_send): instance = Instance.objects.get(uuid=new_uuid) mock_send.assert_called_once_with(rest_service.service_url, instance) - def test_create_entity(self): - """An Entity is created for if the form is a RegistrationForm""" - self.xform = self._publish_registration_form(self.user) - submission_path = os.path.join( - self.main_directory, - "fixtures", - "entities", - "instances", - "trees_registration.xml", - ) - - with open(submission_path, "rb") as sf: - data = {"xml_submission_file": sf} - request = self.factory.post("/submission", data) - response = self.view(request) - self.assertEqual(response.status_code, 401) - auth = DigestAuth("bob", "bobbob") - request.META.update(auth(request.META, response)) - response = self.view(request, username=self.user.username) - self.assertContains(response, "Successful submission", status_code=201) - self.assertEqual(Instance.objects.count(), 1) - self.assertEqual(Entity.objects.count(), 1) - instance = Instance.objects.first() - entity = Entity.objects.first() - self.assertEqual(entity.registration_form.xform, self.xform) - self.assertEqual(entity.xml, instance.xml) - expected_json = { - "formhub/uuid": "d156a2dce4c34751af57f21ef5c4e6cc", - "geometry": "-1.286905 36.772845 0 0", - "species": "purpleheart", - "circumference_cm": 300, - "meta/instanceID": "uuid:9d3f042e-cfec-4d2a-8b5b-212e3b04802b", - "meta/instanceName": "300cm purpleheart", - "meta/entity/label": "300cm purpleheart", - "_xform_id_string": "trees_registration", - "_version": "2022110901", - "_id": entity.pk, - } - self.assertEqual(entity.json, expected_json) - self.assertEqual(entity.uuid, "dbee4c32-a922-451c-9df7-42f40bf78f48") - def test_registration_form_inactive(self): """When the RegistrationForm is inactive, Entity should not be created""" self.xform = self._publish_registration_form(self.user) diff --git a/onadata/apps/logger/migrations/0018_entityhistory_and_more.py b/onadata/apps/logger/migrations/0018_entityhistory_and_more.py index 3b09d336c1..8380fd84bf 100644 --- a/onadata/apps/logger/migrations/0018_entityhistory_and_more.py +++ b/onadata/apps/logger/migrations/0018_entityhistory_and_more.py @@ -27,7 +27,7 @@ class Migration(migrations.Migration): ), ("date_created", models.DateTimeField(auto_now_add=True)), ("date_modified", models.DateTimeField(auto_now=True)), - ("xml", models.TextField(blank=True, default="", null=True)), + ("xml", models.TextField(blank=True, null=True)), ("json", models.JSONField(default=dict)), ( "form_version", diff --git a/onadata/apps/logger/models/entity.py b/onadata/apps/logger/models/entity.py index f17ead8ca5..0026f3acde 100644 --- a/onadata/apps/logger/models/entity.py +++ b/onadata/apps/logger/models/entity.py @@ -58,7 +58,7 @@ class Meta(BaseModel.Meta): null=True, blank=True, ) - xml = models.TextField(default="", blank=True, null=True) + xml = models.TextField(blank=True, null=True) json = models.JSONField(default=dict) form_version = models.CharField(max_length=255, null=True, blank=True) created_by = models.ForeignKey(User, null=True, on_delete=models.SET_NULL) diff --git a/onadata/apps/logger/tests/models/test_instance.py b/onadata/apps/logger/tests/models/test_instance.py index deaf004603..6b081802f5 100644 --- a/onadata/apps/logger/tests/models/test_instance.py +++ b/onadata/apps/logger/tests/models/test_instance.py @@ -426,8 +426,62 @@ def test_light_tasks_synchronous(self, mock_json_async): }, ) + 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", + ) + + 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", + ) + + self.assertEqual(Entity.objects.count(), 1) + + entity = Entity.objects.first() + entity_list = EntityList.objects.get(name="trees") + + self.assertEqual(entity.entity_list, entity_list) + + expected_json = { + "id": entity.pk, + "species": "purpleheart", + "geometry": "-1.286905 36.772845 0 0", + "circumference_cm": 300, + "meta/entity/label": "300cm purpleheart", + } + + self.assertEqual(entity.json, expected_json) + self.assertEqual(entity.uuid, "dbee4c32-a922-451c-9df7-42f40bf78f48") + self.assertEqual(entity.history.count(), 1) + + entity_history = entity.history.first() + registration_form = RegistrationForm.objects.get(xform=xform) + + 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.json, expected_json) + self.assertEqual(entity_history.form_version, xform.version) + self.assertEqual(entity_history.created_by, instance.user) + def test_update_entity(self): - """An Entity is updated correctly""" + """An Entity is updated from a submission""" # Simulate existing Entity self.project = get_user_default_project(self.user) entity_list = EntityList.objects.create( @@ -459,13 +513,13 @@ def test_update_entity(self): with open(submission_path, "rb") as file: xml = file.read() Instance.objects.create( - xml=xml, + xml=xml.decode("utf-8"), user=self.user, xform=xform, checksum=sha256(xml).hexdigest(), uuid="45d27780-48fd-4035-8655-9332649385bd", ) - + # Update XForm is a RegistrationForm self.assertEqual(RegistrationForm.objects.filter(xform=xform).count(), 1) # No new Entity created self.assertEqual(Entity.objects.count(), 1) @@ -485,7 +539,7 @@ def test_update_entity(self): ) def test_update_entity_label(self): - """An Entity label is updated""" + """An Entity label is updated from a submission""" # Simulate existing Entity self.project = get_user_default_project(self.user) entity_list = EntityList.objects.create( @@ -536,7 +590,7 @@ def test_update_entity_label(self): with open(submission_path, "rb") as file: xml = file.read() Instance.objects.create( - xml=xml, + xml=xml.decode("utf-8"), user=self.user, xform=updating_xform, checksum=sha256(xml).hexdigest(), diff --git a/onadata/libs/utils/logger_tools.py b/onadata/libs/utils/logger_tools.py index 0ad07a89e9..33485c4585 100644 --- a/onadata/libs/utils/logger_tools.py +++ b/onadata/libs/utils/logger_tools.py @@ -1059,6 +1059,7 @@ def create_entity(instance: Instance, registration_form: RegistrationForm) -> En instance=instance, form_version=registration_form.xform.version, json=json, + created_by=instance.user, ) entity_list.last_entity_update_time = entity.date_modified entity_list.num_entities = F("num_entities") + 1