From 10d560d857dd04e2f64034953c7a39fee8b47fbe Mon Sep 17 00:00:00 2001 From: Davis Raymond Muro Date: Wed, 29 Jan 2020 10:36:21 +0300 Subject: [PATCH] Return flow data results for Merged Datasets Return flow data results for Merged Datasets and update tests to ensure the data is returned --- .../api/tests/viewsets/test_floip_viewset.py | 53 +++++++++++++++++++ onadata/apps/api/viewsets/floip_viewset.py | 14 ++++- 2 files changed, 65 insertions(+), 2 deletions(-) diff --git a/onadata/apps/api/tests/viewsets/test_floip_viewset.py b/onadata/apps/api/tests/viewsets/test_floip_viewset.py index 45871fc85a..41aad5b3ff 100644 --- a/onadata/apps/api/tests/viewsets/test_floip_viewset.py +++ b/onadata/apps/api/tests/viewsets/test_floip_viewset.py @@ -9,7 +9,16 @@ from onadata.apps.api.tests.viewsets.test_abstract_viewset import \ TestAbstractViewSet from onadata.apps.api.viewsets.floip_viewset import FloipViewSet +from onadata.apps.api.viewsets.merged_xform_viewset import MergedXFormViewSet from onadata.apps.logger.models import Instance, XForm +from onadata.libs.utils.user_auth import get_user_default_project + + +MD = """ +| survey | +| | type | name | label | +| | photo | image1 | Photo | +""" class TestFloipViewSet(TestAbstractViewSet): @@ -196,6 +205,50 @@ def test_publish_number_question_names(self): # pylint: disable=C0103 self.assertIn(u"The name '1448506769745_42' is an invalid xml tag", response.data['text']) + def test_retrieve_responses_merged_dataset(self): + """ + Test retrieving responses from the FLOIP endpoint + """ + merged_dataset_view = MergedXFormViewSet.as_view({ + 'post': 'create', + }) + + self.project = get_user_default_project(self.user) + self._publish_xls_form_to_project() + self._make_submissions() + xform = self._publish_markdown(MD, self.user, id_string='a') + + data = { + 'xforms': [ + f'http://testserver/api/v1/forms/{self.xform.pk}', + f'http://testserver/api/v1/forms/{xform.pk}', + ], + 'name': + 'Merged Dataset', + 'project': + f'http://testserver/api/v1/projects/{self.project.pk}', + } + + request = self.factory.post('/', data=data, **self.extra) + response = merged_dataset_view(request) + self.assertEqual(response.status_code, 201) + dataset_uuid = response.data['uuid'] + + # Assert that it's possible to retrieve the responses + view = FloipViewSet.as_view({'get': 'responses'}) + request = self.factory.get( + f'/flow-results/packages/{dataset_uuid}/responses', + content_type='application/vnd.api+json', **self.extra) + response = view(request, uuid=dataset_uuid) + self.assertEqual(response.status_code, 200) + + # Convert the returned generator object into a list + response.data['attributes']['responses'] = list( + response.data['attributes']['responses']) + # The transportation form(self.xform) contains 11 responses + # Assert that the responses are returned + self.assertEqual(len(response.data['attributes']['responses']), 11) + def test_responses_endpoint_format(self): """ Test that the responses endpoint returns flow results in the correct diff --git a/onadata/apps/api/viewsets/floip_viewset.py b/onadata/apps/api/viewsets/floip_viewset.py index 5722c8c213..54b26df5b6 100644 --- a/onadata/apps/api/viewsets/floip_viewset.py +++ b/onadata/apps/api/viewsets/floip_viewset.py @@ -12,7 +12,7 @@ from rest_framework_json_api.renderers import JSONRenderer from onadata.apps.api.permissions import XFormPermissions -from onadata.apps.logger.models import XForm +from onadata.apps.logger.models import XForm, Instance from onadata.libs import filters from onadata.libs.renderers.renderers import floip_list from onadata.libs.serializers.floip_serializer import ( @@ -105,7 +105,17 @@ def responses(self, request, uuid=None): else: status_code = status.HTTP_201_CREATED else: - queryset = xform.instances.values_list('json', flat=True) + if xform.is_merged_dataset: + xform_pks = xform.mergedxform.xforms.filter( + deleted_at__isnull=True).values_list( + 'pk', flat=True) + queryset = Instance.objects.filter( + xform_id__in=xform_pks, + deleted_at__isnull=True).values_list( + 'json', flat=True) + else: + queryset = xform.instances.values_list('json', flat=True) + paginate_queryset = self.paginate_queryset(queryset) if paginate_queryset: data['attributes']['responses'] = floip_list(paginate_queryset)