diff --git a/onadata/apps/logger/tests/models/test_instance.py b/onadata/apps/logger/tests/models/test_instance.py index 52c309e39a..9231062f50 100644 --- a/onadata/apps/logger/tests/models/test_instance.py +++ b/onadata/apps/logger/tests/models/test_instance.py @@ -2,6 +2,7 @@ """ Test Instance model. """ + import os from datetime import datetime, timedelta from unittest.mock import Mock, patch @@ -1186,3 +1187,26 @@ def test_parse_numbers(self): instance.refresh_from_db() self.assertEqual(instance.json["num_integer"], -1) self.assertEqual(instance.json["num_decimal"], -1.0) + + def test_xml_entity_node_missing(self): + """Entity node missing in submission XML""" + self.project = get_user_default_project(self.user) + xform = self._publish_registration_form(self.user) + xml = ( + '' + '' + "d156a2dce4c34751af57f21ef5c4e6cc" + "-1.286905 36.772845 0 0" + "purpleheart" + "300" + "" + "" + "uuid:9d3f042e-cfec-4d2a-8b5b-212e3b04802b" + "300cm purpleheart" + "" + "" + ) + Instance.objects.create(xml=xml, user=self.user, xform=xform) + + self.assertEqual(Entity.objects.count(), 0) diff --git a/onadata/libs/utils/logger_tools.py b/onadata/libs/utils/logger_tools.py index 0ee6f78c80..b410b76e82 100644 --- a/onadata/libs/utils/logger_tools.py +++ b/onadata/libs/utils/logger_tools.py @@ -1261,11 +1261,11 @@ def create_or_update_entity_from_instance(instance: Instance) -> None: registration_form_qs = RegistrationForm.objects.filter( xform=instance.xform, is_active=True ) + entity_node = get_meta_from_xml(instance.xml, "entity") - if not registration_form_qs.exists(): + if not registration_form_qs.exists() or not entity_node: return - entity_node = get_meta_from_xml(instance.xml, "entity") registration_form = registration_form_qs.first() mutation_success_checks = ["1", "true"] entity_uuid = entity_node.getAttribute("id")