Skip to content

Commit

Permalink
move test case
Browse files Browse the repository at this point in the history
move onadata.apps.api.tests.viewset.test_xform_submission_viewset.TestXFormSubmissionViewSet.test_create_entity to onadata.apps.logger.tests.models.test_instance.TestInstance.test_create_entity
  • Loading branch information
kelvin-muchiri committed May 30, 2024
1 parent 55b433b commit aa0e019
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 48 deletions.
41 changes: 0 additions & 41 deletions onadata/apps/api/tests/viewsets/test_xform_submission_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion onadata/apps/logger/models/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
64 changes: 59 additions & 5 deletions onadata/apps/logger/tests/models/test_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand All @@ -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(
Expand Down Expand Up @@ -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(),
Expand Down
1 change: 1 addition & 0 deletions onadata/libs/utils/logger_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit aa0e019

Please sign in to comment.