Skip to content

Commit

Permalink
Modify Export Builder tests
Browse files Browse the repository at this point in the history
Modify Export Builder tests passing in the new 'default_name' arguement
to PyXForms 'create_survey_from_xls' function and use SELECT_BIND_TYPE
and MULTIPLE_SELECT_TYPE common tags instead of static values. More info:
XLSForm/pyxform#375
  • Loading branch information
DavisRayM committed Mar 13, 2020
1 parent e046a8a commit 45ed424
Showing 1 changed file with 46 additions and 22 deletions.
68 changes: 46 additions & 22 deletions onadata/libs/tests/utils/test_export_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
from onadata.apps.viewer.tests.export_helpers import viewer_fixture_path
from onadata.libs.utils.csv_builder import (CSVDataFrameBuilder,
get_labels_from_columns)
from onadata.libs.utils.common_tags import (SELECT_BIND_TYPE,
MULTIPLE_SELECT_TYPE)
from onadata.libs.utils.export_builder import (
decode_mongo_encoded_section_names,
dict_to_joined_export,
Expand Down Expand Up @@ -238,7 +240,7 @@ class TestExportBuilder(TestBase):
def _create_childrens_survey(self, filename="childrens_survey.xls"):
survey = create_survey_from_xls(_logger_fixture_path(
filename
))
), default_name=filename.split('.')[0])
self.dd = DataDictionary()
self.dd._survey = survey
return survey
Expand Down Expand Up @@ -438,7 +440,8 @@ def test_zipped_csv_export_works_with_unicode(self):
cvs writer doesnt handle unicode we we have to encode to ascii
"""
survey = create_survey_from_xls(_logger_fixture_path(
'childrens_survey_unicode.xls'))
'childrens_survey_unicode.xls'),
default_name='childrens_survey_unicode')
export_builder = ExportBuilder()
export_builder.set_survey(survey)
temp_zip_file = NamedTemporaryFile(suffix='.zip')
Expand Down Expand Up @@ -933,7 +936,8 @@ def test_zipped_sav_export_with_duplicate_column_name(self):

def test_xls_export_works_with_unicode(self):
survey = create_survey_from_xls(_logger_fixture_path(
'childrens_survey_unicode.xls'))
'childrens_survey_unicode.xls'),
default_name='childrenss_survey_unicode')
export_builder = ExportBuilder()
export_builder.set_survey(survey)
temp_xls_file = NamedTemporaryFile(suffix='.xlsx')
Expand All @@ -953,7 +957,9 @@ def test_xls_export_with_hxl_adds_extra_row(self):
xlsform_path = os.path.join(
settings.PROJECT_ROOT, "apps", "main", "tests", "fixtures",
"hxl_test", "hxl_example.xlsx")
survey = create_survey_from_xls(xlsform_path)
survey = create_survey_from_xls(
xlsform_path,
default_name=xlsform_path.split('/')[-1].split('.')[0])
export_builder = ExportBuilder()
export_builder.INCLUDE_HXL = True
export_builder.set_survey(survey)
Expand Down Expand Up @@ -1482,7 +1488,8 @@ def test_get_valid_sheet_name_catches_long_duplicate_names(self):

def test_to_xls_export_generates_valid_sheet_names(self):
survey = create_survey_from_xls(_logger_fixture_path(
'childrens_survey_with_a_very_long_name.xls'))
'childrens_survey_with_a_very_long_name.xls'),
default_name='childrens_survey_with_a_very_long_name')
export_builder = ExportBuilder()
export_builder.set_survey(survey)
xls_file = NamedTemporaryFile(suffix='.xls')
Expand All @@ -1501,7 +1508,8 @@ def test_to_xls_export_generates_valid_sheet_names(self):

def test_child_record_parent_table_is_updated_when_sheet_is_renamed(self):
survey = create_survey_from_xls(_logger_fixture_path(
'childrens_survey_with_a_very_long_name.xls'))
'childrens_survey_with_a_very_long_name.xls'),
default_name='childrens_survey_with_a_very_long_name')
export_builder = ExportBuilder()
export_builder.set_survey(survey)
xls_file = NamedTemporaryFile(suffix='.xlsx')
Expand Down Expand Up @@ -1563,7 +1571,8 @@ def test_type_conversion(self):
}

survey = create_survey_from_xls(viewer_fixture_path(
'test_data_types/test_data_types.xls'))
'test_data_types/test_data_types.xls'),
default_name='test_data_types')
export_builder = ExportBuilder()
export_builder.set_survey(survey)
# format submission 1 for export
Expand All @@ -1588,7 +1597,8 @@ def test_type_conversion(self):

def test_xls_convert_dates_before_1900(self):
survey = create_survey_from_xls(viewer_fixture_path(
'test_data_types/test_data_types.xls'))
'test_data_types/test_data_types.xls'),
default_name='test_data_types')
export_builder = ExportBuilder()
export_builder.set_survey(survey)
data = [
Expand Down Expand Up @@ -1758,7 +1768,8 @@ def test_generate_field_title_truncated_titles_select_multiple(self):

def test_xls_export_remove_group_name(self):
survey = create_survey_from_xls(_logger_fixture_path(
'childrens_survey_unicode.xls'))
'childrens_survey_unicode.xls'),
default_name='childrens_survey_unicode')
export_builder = ExportBuilder()
export_builder.TRUNCATE_GROUP_TITLE = True
export_builder.set_survey(survey)
Expand All @@ -1779,7 +1790,8 @@ def test_zipped_csv_export_remove_group_name(self):
cvs writer doesnt handle unicode we we have to encode to ascii
"""
survey = create_survey_from_xls(_logger_fixture_path(
'childrens_survey_unicode.xls'))
'childrens_survey_unicode.xls'),
default_name='childrens_survey_unicode')
export_builder = ExportBuilder()
export_builder.TRUNCATE_GROUP_TITLE = True
export_builder.set_survey(survey)
Expand Down Expand Up @@ -1824,7 +1836,8 @@ def test_zipped_csv_export_remove_group_name(self):

def test_xls_export_with_labels(self):
survey = create_survey_from_xls(_logger_fixture_path(
'childrens_survey_unicode.xls'))
'childrens_survey_unicode.xls'),
default_name='childrens_survey_unicode')
export_builder = ExportBuilder()
export_builder.TRUNCATE_GROUP_TITLE = True
export_builder.INCLUDE_LABELS = True
Expand Down Expand Up @@ -1853,7 +1866,8 @@ def test_xls_export_with_labels(self):

def test_xls_export_with_labels_only(self):
survey = create_survey_from_xls(_logger_fixture_path(
'childrens_survey_unicode.xls'))
'childrens_survey_unicode.xls'),
default_name='childrens_survey_unicode')
export_builder = ExportBuilder()
export_builder.TRUNCATE_GROUP_TITLE = True
export_builder.INCLUDE_LABELS_ONLY = True
Expand All @@ -1877,7 +1891,8 @@ def test_zipped_csv_export_with_labels(self):
cvs writer doesnt handle unicode we we have to encode to ascii
"""
survey = create_survey_from_xls(_logger_fixture_path(
'childrens_survey_unicode.xls'))
'childrens_survey_unicode.xls'),
default_name='childrens_survey_unicode')
export_builder = ExportBuilder()
export_builder.TRUNCATE_GROUP_TITLE = True
export_builder.INCLUDE_LABELS = True
Expand Down Expand Up @@ -1943,7 +1958,8 @@ def test_zipped_csv_export_with_labels_only(self):
cvs writer doesnt handle unicode we we have to encode to ascii
"""
survey = create_survey_from_xls(_logger_fixture_path(
'childrens_survey_unicode.xls'))
'childrens_survey_unicode.xls'),
default_name='childrens_survey_unicode')
export_builder = ExportBuilder()
export_builder.TRUNCATE_GROUP_TITLE = True
export_builder.INCLUDE_LABELS_ONLY = True
Expand Down Expand Up @@ -2063,7 +2079,8 @@ def _test_sav_file(section):

def test_xls_export_with_english_labels(self):
survey = create_survey_from_xls(_logger_fixture_path(
'childrens_survey_en.xls'))
'childrens_survey_en.xls'),
default_name='childrens_survey_en')
# no default_language is not set
self.assertEqual(
survey.to_json_dict().get('default_language'), 'default'
Expand Down Expand Up @@ -2091,7 +2108,8 @@ def test_xls_export_with_english_labels(self):

def test_xls_export_with_swahili_labels(self):
survey = create_survey_from_xls(_logger_fixture_path(
'childrens_survey_sw.xls'))
'childrens_survey_sw.xls'),
default_name='childrens_survey_sw')
# default_language is set to swahili
self.assertEqual(
survey.to_json_dict().get('default_language'), 'swahili'
Expand Down Expand Up @@ -2119,7 +2137,8 @@ def test_xls_export_with_swahili_labels(self):

def test_csv_export_with_swahili_labels(self):
survey = create_survey_from_xls(_logger_fixture_path(
'childrens_survey_sw.xls'))
'childrens_survey_sw.xls'),
default_name='childrens_survey_sw')
# default_language is set to swahili
self.assertEqual(
survey.to_json_dict().get('default_language'), 'swahili'
Expand All @@ -2136,15 +2155,17 @@ def test_csv_export_with_swahili_labels(self):

def test_select_multiples_choices(self):
survey = create_survey_from_xls(_logger_fixture_path(
'childrens_survey_sw.xls'))
'childrens_survey_sw.xls'),
default_name='childrens_survey_sw')
dd = DataDictionary()
dd._survey = survey
export_builder = ExportBuilder()
export_builder.TRUNCATE_GROUP_TITLE = True
export_builder.INCLUDE_LABELS = True
export_builder.set_survey(survey)
child = [e for e in dd.get_survey_elements_with_choices()
if e.bind.get('type') == 'select'][0]
if e.bind.get('type') == SELECT_BIND_TYPE
and e.type == MULTIPLE_SELECT_TYPE][0]
self.assertNotEqual(child.children, [])
choices = export_builder._get_select_mulitples_choices(
child, dd, ExportBuilder.GROUP_DELIMITER,
Expand Down Expand Up @@ -2190,15 +2211,16 @@ def test_select_multiples_choices(self):
def test_select_multiples_choices_with_choice_filter(self):
survey = create_survey_from_xls(_logger_fixture_path(
'choice_filter.xlsx'
))
), default_name='choice_filter')
dd = DataDictionary()
dd._survey = survey
export_builder = ExportBuilder()
export_builder.TRUNCATE_GROUP_TITLE = True
export_builder.INCLUDE_LABELS = True
export_builder.set_survey(survey)
child = [e for e in dd.get_survey_elements_with_choices()
if e.bind.get('type') == 'select'][0]
if e.bind.get('type') == SELECT_BIND_TYPE
and e.type == MULTIPLE_SELECT_TYPE][0]
choices = export_builder._get_select_mulitples_choices(
child, dd, ExportBuilder.GROUP_DELIMITER,
ExportBuilder.TRUNCATE_GROUP_TITLE
Expand Down Expand Up @@ -2282,7 +2304,9 @@ def _create_osm_survey(self):
for filename in filenames]
submission_path = os.path.join(osm_fixtures_dir, 'instance_a.xml')
self._make_submission_w_attachment(submission_path, paths)
survey = create_survey_from_xls(xlsform_path)
survey = create_survey_from_xls(
xlsform_path,
default_name=xlsform_path.split('/')[-1].split('.')[0])
return survey

def test_zip_csv_export_has_comment_and_status_field(self):
Expand Down

0 comments on commit 45ed424

Please sign in to comment.