From 7bb59063d2811f7cc0c4d795b83dce1df1c30d9b Mon Sep 17 00:00:00 2001 From: WinnyTroy Date: Mon, 16 Nov 2020 16:23:54 +0300 Subject: [PATCH] Add tests for tableau viewset Break apart the process tableau data function into separate functions --- .../tests/viewsets/test_tableau_viewset.py | 60 +++++++++---------- onadata/apps/api/viewsets/tableau_viewset.py | 15 +++-- 2 files changed, 36 insertions(+), 39 deletions(-) diff --git a/onadata/apps/api/tests/viewsets/test_tableau_viewset.py b/onadata/apps/api/tests/viewsets/test_tableau_viewset.py index a6d7b5d55d..d56e13d857 100644 --- a/onadata/apps/api/tests/viewsets/test_tableau_viewset.py +++ b/onadata/apps/api/tests/viewsets/test_tableau_viewset.py @@ -7,9 +7,7 @@ from django.test import RequestFactory from django.utils.dateparse import parse_datetime -from django.contrib.auth.models import User -from onadata.apps.logger.models import OpenData, Instance from onadata.apps.logger.models.open_data import get_or_create_opendata from onadata.apps.api.viewsets.tableau_viewset import TableauViewSet @@ -44,11 +42,10 @@ def setUp(self): 'delete': 'destroy', 'get': 'data' }) - + def get_open_data_object(self): return get_or_create_opendata(self.xform)[0] - def test_tableau_data_and_fetch(self): # pylint: disable=invalid-name """ Test the schema and data endpoint and data returned by each. @@ -62,7 +59,7 @@ def test_tableau_data_and_fetch(self): # pylint: disable=invalid-name expected_schema = [ { 'table_alias': 'data', - 'connection_name': '1_tutorial_w_repeats', + 'connection_name': f'{self.xform.project_id}_{self.xform.id_string}', # noqa 'column_headers': [ { 'id': '_id', @@ -138,7 +135,7 @@ def test_tableau_data_and_fetch(self): # pylint: disable=invalid-name }, { 'table_alias': 'children', - 'connection_name': '1_tutorial_w_repeats_children', + 'connection_name': f'{self.xform.project_id}_{self.xform.id_string}_children', # noqa 'column_headers': [ { 'id': '_id', @@ -179,11 +176,9 @@ def test_tableau_data_and_fetch(self): # pylint: disable=invalid-name sorted(list(response1.data[0].keys())) ) - connection_name = u"%s_%s" % ( - self.xform.project_id, - self.xform.id_string - ) - self.assertEqual(connection_name, response1.data[0].get('connection_name')) + connection_name = f'{self.xform.project_id}_{self.xform.id_string}' + self.assertEqual( + connection_name, response1.data[0].get('connection_name')) # Test that the table alias field being sent to Tableau # for each schema contains the right table name self.assertEqual( @@ -199,38 +194,41 @@ def test_tableau_data_and_fetch(self): # pylint: disable=invalid-name if a.get('id') == '_id'][0] self.assertEqual(_id_datatype, 'int') + self.view = TableauViewSet.as_view({ + 'get': 'data' + }) + request2 = self.factory.get('/', **self.extra) + response2 = self.view(request2, uuid=uuid) + self.assertEqual(response2.status_code, 200) + + # cast generator response to list for easy manipulation + row_data = streaming_data(response2) expected_data = [ { '_gps_altitude': '0.0', '_gps_latitude': '-1.2625621', '_gps_longitude': '36.7921711', '_gps_precision': '20.0', - '_id': 1, + '_id': self.xform.instances.first().id, 'age': 25, - 'children': [{'__parent_id': 1, - '__parent_table': 'data', - '_id': 4, - 'childs_age': 12, - 'childs_name': 'Tom'}, - {'__parent_id': 1, - '__parent_table': 'data', - '_id': 8, - 'childs_age': 5, - 'childs_name': 'Dick'}], + 'children': [ + { + '__parent_id': self.xform.instances.first().id, + '__parent_table': 'data', + '_id': 2591227, + 'childs_age': 12, + 'childs_name': 'Tom'}, + { + '__parent_id': self.xform.instances.first().id, + '__parent_table': 'data', + '_id': 2593505, + 'childs_age': 5, + 'childs_name': 'Dick'}], 'has_children': '1', 'meta_instanceID': 'uuid:b31c6ac2-b8ca-4180-914f-c844fa10ed3b', 'name': 'Bob' }] - - self.view = TableauViewSet.as_view({ - 'get': 'data' - }) - request2 = self.factory.get('/', **self.extra) - response2 = self.view(request2, uuid=uuid) - self.assertEqual(response2.status_code, 200) - # cast generator response to list for easy manipulation - row_data = streaming_data(response2) # Test to confirm that the repeat tables generated # are related to the main table self.assertEqual( diff --git a/onadata/apps/api/viewsets/tableau_viewset.py b/onadata/apps/api/viewsets/tableau_viewset.py index bcbfbcba47..f89f07cec6 100644 --- a/onadata/apps/api/viewsets/tableau_viewset.py +++ b/onadata/apps/api/viewsets/tableau_viewset.py @@ -67,12 +67,9 @@ def process_tableau_data( choice_names = [ question["name"] for question in qstn["children"]] list_name = qstn.get('list_name') - unpack_select_multiple_data( - picked_choices, - list_name, - choice_names, - prefix, - flat_dict) + select_multiple_data = unpack_select_multiple_data( + picked_choices, list_name, choice_names, prefix) + flat_dict.update(select_multiple_data) elif qstn_type == 'geopoint': gps_parts = unpack_gps_data( value, qstn_name, prefix) @@ -87,6 +84,7 @@ def process_tableau_data( def unpack_select_multiple_data(picked_choices, list_name, choice_names, prefix, flat_dict): + unpacked_data = {} for choice in choice_names: qstn_name = f"{list_name}_{choice}" @@ -94,9 +92,10 @@ def unpack_select_multiple_data(picked_choices, list_name, qstn_name = prefix + '_' + qstn_name if choice in picked_choices: - flat_dict[qstn_name] = "TRUE" + unpacked_data[qstn_name] = "TRUE" else: - flat_dict[qstn_name] = "FALSE" + unpacked_data[qstn_name] = "FALSE" + return unpacked_data def unpack_repeat_data(repeat_data, flat_dict):