Skip to content

Commit

Permalink
Test That SAV Exports With Duplicate Column Names Do Not Fail
Browse files Browse the repository at this point in the history
- Edit export_builder to allow creation of SAV files with
duplicate columns names.
- Edit test to ascertain that this happens and the expected
columns are created.
- Move test from test_tasks file to test_export_builder.
  • Loading branch information
Wambere committed Feb 7, 2018
1 parent ef86991 commit efdcafc
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 31 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
30 changes: 0 additions & 30 deletions onadata/apps/viewer/tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
from onadata.apps.viewer.tasks import create_async_export
from onadata.apps.viewer.tasks import mark_expired_pending_exports_as_failed
from onadata.apps.viewer.tasks import delete_expired_failed_exports
from onadata.apps.viewer.models import DataDictionary
from onadata.libs.utils.user_auth import get_user_default_project


class TestExportTasks(TestBase):
Expand Down Expand Up @@ -45,34 +43,6 @@ def test_create_async(self):
self.assertIn("username", options)
self.assertEquals(options.get("id_string"), self.xform.id_string)

def test_zipped_sav_export_with_duplicate_column_name(self):
"""
Test that SAV exports with duplicate column names actually fail
"""
md = """
| survey | | | |
| | type | name | label |
| | text | Sport | Which Sport |
| | text | sport | Which sport |
"""
survey = self.md_to_pyxform_survey(md, {'name': 'sports'})

project = get_user_default_project(self.user)
xform = DataDictionary(created_by=self.user, user=self.user,
xml=survey.to_xml(), json=survey.to_json(),
project=project)
xform.save()

export_type = Export.SAV_ZIP_EXPORT

options = {"group_delimiter": "/",
"remove_group_name": False,
"split_select_multiples": True}

result = create_async_export(xform, export_type, None, False, options)
export = result[0]
self.assertEquals(export.internal_status, Export.FAILED)

def test_mark_expired_pending_exports_as_failed(self):
self._publish_transportation_form_and_submit_instance()
over_threshold = settings.EXPORT_TASK_LIFESPAN + 2
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

0 comments on commit efdcafc

Please sign in to comment.