Skip to content

Commit

Permalink
Merge pull request #1232
Browse files Browse the repository at this point in the history
KJ | JW:  Handle same name different case columns in SAV exports
  • Loading branch information
ukanga authored Feb 8, 2018
2 parents 4aaf561 + efdcafc commit 878aaba
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
40 changes: 40 additions & 0 deletions onadata/apps/viewer/tests/test_export_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,46 @@ def test_zipped_sav_export_with_duplicate_name_in_choice_list(self):
os.path.exists(
os.path.join(temp_dir, "exp.sav")))

def test_zipped_sav_export_with_duplicate_column_name(self):
"""
Test that SAV exports with duplicate column names
"""
md = """
| survey | | | |
| | type | name | label |
| | text | Sport | Which sport |
| | text | sport | Which fun sports|
"""
survey = self.md_to_pyxform_survey(md, {'name': 'sports'})
data = [{"Sport": "Basketball", "sport": "Soccer",
'_submission_time': u'2016-11-21T03:43:43.000-08:00'}]

export_builder = ExportBuilder()
export_builder.set_survey(survey)
temp_zip_file = NamedTemporaryFile(suffix='.zip')
export_builder.to_zipped_sav(temp_zip_file.name, data)
temp_zip_file.seek(0)
temp_dir = tempfile.mkdtemp()
zip_file = zipfile.ZipFile(temp_zip_file.name, "r")
zip_file.extractall(temp_dir)
zip_file.close()
temp_zip_file.close()
# check that the children's file (which has the unicode header) exists
self.assertTrue(
os.path.exists(
os.path.join(temp_dir, "sports.sav")))
# check file's contents

with SavReader(os.path.join(temp_dir, "sports.sav"),
returnHeader=True) as reader:
rows = [r for r in reader]

# Check that columns are present
self.assertIn("Sport", rows[0])
# Check for sport in first 5 characters
# because rows contains 'sport@d4b6'
self.assertIn("sport", [x[0:5] for x in rows[0]])

def test_xls_export_works_with_unicode(self):
survey = create_survey_from_xls(_logger_fixture_path(
'childrens_survey_unicode.xls'))
Expand Down
2 changes: 1 addition & 1 deletion onadata/libs/utils/export_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,7 @@ def _check_sav_column(self, column, columns):
col_len_diff = len(column) - 64
column = column[:-col_len_diff]

if column in columns:
if column.lower() in (t.lower() for t in columns):
if len(column) > 59:
column = column[:-5]
column = column + "@" + str(uuid.uuid4()).split("-")[1]
Expand Down
5 changes: 3 additions & 2 deletions onadata/libs/utils/export_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import math
import os
import re
import sys
import time
from datetime import datetime
from datetime import timedelta
Expand All @@ -29,7 +30,7 @@
from onadata.libs.exceptions import J2XException, NoRecordsFoundError
from onadata.libs.utils.common_tags import (DATAVIEW_EXPORT,
GROUPNAME_REMOVED_FLAG)
from onadata.libs.utils.common_tools import str_to_bool
from onadata.libs.utils.common_tools import str_to_bool, report_exception
from onadata.libs.utils.export_builder import ExportBuilder
from onadata.libs.utils.model_tools import (get_columns_with_hxl,
queryset_iterator)
Expand Down Expand Up @@ -233,7 +234,7 @@ def generate_export(export_type, xform, export_id=None, options=None,
export.error_message = str(e)
export.internal_status = Export.FAILED
export.save()

report_exception("SAV Export Failure", e, sys.exc_info())
return export

# generate filename
Expand Down

0 comments on commit 878aaba

Please sign in to comment.