Skip to content

Commit

Permalink
Merge pull request #1523 fix #1511
Browse files Browse the repository at this point in the history
  • Loading branch information
ukanga authored Dec 19, 2018
2 parents fd13342 + 4596c41 commit 98f642a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
37 changes: 37 additions & 0 deletions onadata/apps/api/tests/viewsets/test_merged_xform_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -709,3 +709,40 @@ def test_xform_has_uncommon_reference(self):
"element with this name.")
self.assertIn('xforms', response.data)
self.assertIn(error_message, response.data['xforms'])

def test_merged_datasets_deleted_parent_retrieve(self):
"""Test retrieving a specific merged dataset when the parent is deleted
"""
merged_dataset = self._create_merged_dataset(geo=True)
merged_xform = MergedXForm.objects.get(pk=merged_dataset['id'])

# make submission to form b
form_b = merged_xform.xforms.all()[1]
xml = '<data id="b"><fruit>mango</fruit></data>'
instance = Instance(xform=form_b, xml=xml)
instance.save()
form_b.refresh_from_db()
form_b.last_submission_time = instance.date_created
form_b.save()
view = MergedXFormViewSet.as_view({'get': 'retrieve'})

# status_code is 200 when: pk exists, user is authenticated

request = self.factory.get('/', **self.extra)
response = view(request, pk=merged_dataset['id'])
self.assertEqual(response.status_code, 200)

# delete parents
[parent.delete() for parent in merged_xform.xforms.all()]
merged_xform.refresh_from_db()

# merged dataset should be available at api/forms/[pk] endpoint
request = self.factory.get('/', **self.extra)
view = XFormViewSet.as_view({'get': 'retrieve'})
response = view(request, pk=merged_dataset['id'])
self.assertEqual(response.status_code, 200)
self.assertEqual(merged_dataset['id'], response.data['formid'])
self.assertTrue(response.data['is_merged_dataset'])
self.assertTrue(response.data['instances_with_geopoints'])
# deleted parents, 0 submissions
self.assertEqual(response.data['num_of_submissions'], 0)
2 changes: 1 addition & 1 deletion onadata/apps/logger/models/xform.py
Original file line number Diff line number Diff line change
Expand Up @@ -908,7 +908,7 @@ def submission_count(self, force_update=False):
if self.num_of_submissions == 0 or force_update:
if self.is_merged_dataset:
count = self.mergedxform.xforms.aggregate(
num=Sum('num_of_submissions')).get('num')
num=Sum('num_of_submissions')).get('num') or 0
else:
count = self.instances.filter(deleted_at__isnull=True).count()

Expand Down

0 comments on commit 98f642a

Please sign in to comment.