Skip to content

Commit

Permalink
Return flow data results for Merged Datasets
Browse files Browse the repository at this point in the history
Return flow data results for Merged Datasets and update tests to ensure
the data is returned
  • Loading branch information
DavisRayM committed Jan 29, 2020
1 parent 95f4701 commit 57c4c86
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 3 deletions.
55 changes: 55 additions & 0 deletions onadata/apps/api/tests/viewsets/test_floip_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,21 @@
import os
from builtins import open

from django.utils import timezone

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):
Expand Down Expand Up @@ -196,6 +207,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
Expand Down
16 changes: 13 additions & 3 deletions onadata/apps/api/viewsets/floip_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
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.renderers.renderers import floip_list, floip_rows_list
from onadata.libs.serializers.floip_serializer import (
FloipListSerializer, FloipSerializer, FlowResultsResponseSerializer)

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 57c4c86

Please sign in to comment.