Skip to content

Commit

Permalink
Add tests for tableau viewset
Browse files Browse the repository at this point in the history
Break apart the process tableau data function into separate functions
  • Loading branch information
WinnyTroy committed Nov 18, 2020
1 parent 3b2d5e6 commit 7bb5906
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 39 deletions.
60 changes: 29 additions & 31 deletions onadata/apps/api/tests/viewsets/test_tableau_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand All @@ -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',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down
15 changes: 7 additions & 8 deletions onadata/apps/api/viewsets/tableau_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -87,16 +84,18 @@ 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}"

if prefix:
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):
Expand Down

0 comments on commit 7bb5906

Please sign in to comment.