Skip to content

Commit

Permalink
Updating default form_name to data instead of using filename. (#375)
Browse files Browse the repository at this point in the history
* Updating parameter for default_name to `data` when converting xls file.

* Removing dead code path.

* Importing unicode function for python 3.5.

* Removing unnecessary code and update on the unit tests.
  • Loading branch information
nribeka authored and ukanga committed Oct 2, 2019
1 parent 7a2cfa5 commit 077972c
Show file tree
Hide file tree
Showing 18 changed files with 1,089 additions and 967 deletions.
4 changes: 2 additions & 2 deletions pyxform/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,8 @@ def create_survey_element_from_json(str_or_path):
return create_survey_element_from_dict(d)


def create_survey_from_xls(path_or_file):
excel_reader = SurveyReader(path_or_file)
def create_survey_from_xls(path_or_file, default_name=None):
excel_reader = SurveyReader(path_or_file, default_name=default_name)
d = excel_reader.to_json_dict()
survey = create_survey_element_from_dict(d)
if not survey.id_string:
Expand Down
2 changes: 1 addition & 1 deletion pyxform/file_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def load_file_to_dict(path):
return name, utils.get_pyobj_from_json(path)
else:
name = _section_name(path)
excel_reader = SurveyReader(path)
excel_reader = SurveyReader(path, name)
return name, excel_reader.to_json_dict()


Expand Down
4 changes: 3 additions & 1 deletion pyxform/tests/attributecolumnstest.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ def runTest(self):
# Do the conversion:
warnings = []
json_survey = pyxform.xls2json.parse_file_to_json(
self.path_to_excel_file, warnings=warnings
self.path_to_excel_file,
default_name="attribute_columns_test",
warnings=warnings,
)
survey = pyxform.create_survey_element_from_dict(json_survey)
survey.print_xform_to_file(self.output_path, warnings=warnings)
Expand Down
40 changes: 26 additions & 14 deletions pyxform/tests/bug_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def runTest(self):
warnings = []
with self.assertRaises(Exception):
json_survey = pyxform.xls2json.parse_file_to_json(
path_to_excel_file, warnings=warnings
path_to_excel_file, default_name="group_name_test", warnings=warnings
)
survey = pyxform.create_survey_element_from_dict(json_survey)
survey.print_xform_to_file(output_path, warnings=warnings)
Expand All @@ -49,7 +49,9 @@ def runTest(self):
warnings = []
with self.assertRaises(Exception):
json_survey = pyxform.xls2json.parse_file_to_json(
path_to_excel_file, warnings=warnings
path_to_excel_file,
default_name="not_closed_group_test",
warnings=warnings,
)
survey = pyxform.create_survey_element_from_dict(json_survey)
survey.print_xform_to_file(output_path, warnings=warnings)
Expand All @@ -68,7 +70,7 @@ def runTest(self):
warnings = []
with self.assertRaises(Exception):
json_survey = pyxform.xls2json.parse_file_to_json(
path_to_excel_file, warnings=warnings
path_to_excel_file, default_name="duplicate_columns", warnings=warnings
)
survey = pyxform.create_survey_element_from_dict(json_survey)
survey.print_xform_to_file(output_path, warnings=warnings)
Expand All @@ -87,7 +89,7 @@ def runTest(self):
# Do the conversion:
warnings = []
json_survey = pyxform.xls2json.parse_file_to_json(
self.path_to_excel_file, warnings=warnings
self.path_to_excel_file, default_name="repeat_date_test", warnings=warnings
)
survey = pyxform.create_survey_element_from_dict(json_survey)
survey.print_xform_to_file(self.output_path, warnings=warnings)
Expand All @@ -111,7 +113,7 @@ def runTest(self):
# Do the conversion:
warnings = []
json_survey = pyxform.xls2json.parse_file_to_json(
self.path_to_excel_file, warnings=warnings
self.path_to_excel_file, default_name="xml_escaping", warnings=warnings
)
survey = pyxform.create_survey_element_from_dict(json_survey)
survey.print_xform_to_file(self.output_path, warnings=warnings)
Expand All @@ -137,7 +139,7 @@ def runTest(self):
# Do the conversion:
warnings = []
json_survey = pyxform.xls2json.parse_file_to_json(
path_to_excel_file, warnings=warnings
path_to_excel_file, default_name="default_time_demo", warnings=warnings
)
survey = pyxform.create_survey_element_from_dict(json_survey)
survey.print_xform_to_file(output_path, warnings=warnings)
Expand All @@ -160,7 +162,7 @@ def runTest(self):
# Do the conversion:
warnings = []
json_survey = pyxform.xls2json.parse_file_to_json(
path_to_excel_file, warnings=warnings
path_to_excel_file, default_name="cascades_old", warnings=warnings
)
survey = pyxform.create_survey_element_from_dict(json_survey)
survey.print_xform_to_file(output_path, warnings=warnings)
Expand All @@ -178,7 +180,7 @@ def runTest(self):
# Do the conversion:
warnings = []
json_survey = pyxform.xls2json.parse_file_to_json(
path_to_excel_file, warnings=warnings
path_to_excel_file, default_name="ODKValidateWarnings", warnings=warnings
)
survey = pyxform.create_survey_element_from_dict(json_survey)
survey.print_xform_to_file(output_path, warnings=warnings)
Expand All @@ -197,7 +199,9 @@ def runTest(self):
warnings = []
with self.assertRaises(PyXFormError):
json_survey = pyxform.xls2json.parse_file_to_json(
path_to_excel_file, warnings=warnings
path_to_excel_file,
default_name="cascades_old_with_no_cascade_sheet",
warnings=warnings,
)
survey = pyxform.create_survey_element_from_dict(json_survey)
survey.print_xform_to_file(output_path, warnings=warnings)
Expand All @@ -218,7 +222,11 @@ def runTest(self):
filename = "spaces_in_choices_header.xls"
path_to_excel_file = os.path.join(DIR, "bug_example_xls", filename)
warnings = []
pyxform.xls2json.parse_file_to_json(path_to_excel_file, warnings=warnings)
pyxform.xls2json.parse_file_to_json(
path_to_excel_file,
default_name="spaces_in_choices_header",
warnings=warnings,
)
self.assertEquals(len(warnings), 3, "Found " + str(len(warnings)) + " warnings")

def test_values_with_spaces_are_cleaned(self):
Expand All @@ -230,7 +238,9 @@ def test_values_with_spaces_are_cleaned(self):
"""
filename = "spaces_in_choices_header.xls"
path_to_excel_file = os.path.join(DIR, "bug_example_xls", filename)
survey_reader = SurveyReader(path_to_excel_file)
survey_reader = SurveyReader(
path_to_excel_file, default_name="spaces_in_choices_header"
)
result = survey_reader.to_json_dict()

self.assertEqual(
Expand All @@ -242,7 +252,9 @@ class TestChoiceNameAsType(unittest.TestCase):
def test_choice_name_as_type(self):
filename = "choice_name_as_type.xls"
path_to_excel_file = os.path.join(DIR, "example_xls", filename)
xls_reader = SurveyReader(path_to_excel_file)
xls_reader = SurveyReader(
path_to_excel_file, default_name="choice_name_as_type"
)
survey_dict = xls_reader.to_json_dict()
self.assertTrue(has_external_choices(survey_dict))

Expand All @@ -251,7 +263,7 @@ class TestBlankSecondRow(unittest.TestCase):
def test_blank_second_row(self):
filename = "blank_second_row.xls"
path_to_excel_file = os.path.join(DIR, "bug_example_xls", filename)
xls_reader = SurveyReader(path_to_excel_file)
xls_reader = SurveyReader(path_to_excel_file, default_name="blank_second_row")
survey_dict = xls_reader.to_json_dict()
self.assertTrue(len(survey_dict) > 0)

Expand All @@ -263,7 +275,7 @@ def test_xl_date_ambigous(self):
"""Test non standard sheet with exception is processed successfully."""
filename = "xl_date_ambiguous.xlsx"
path_to_excel_file = os.path.join(DIR, "bug_example_xls", filename)
xls_reader = SurveyReader(path_to_excel_file)
xls_reader = SurveyReader(path_to_excel_file, default_name="xl_date_ambiguous")
survey_dict = xls_reader.to_json_dict()
self.assertTrue(len(survey_dict) > 0)

Expand Down
4 changes: 2 additions & 2 deletions pyxform/tests/group_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class GroupTests(TestCase):
def test_json(self):
x = SurveyReader(utils.path_to_text_fixture("group.xls"))
x = SurveyReader(utils.path_to_text_fixture("group.xls"), default_name="group")
x_results = x.to_json_dict()
expected_dict = {
"name": "group",
Expand Down Expand Up @@ -61,7 +61,7 @@ def test_json(self):
self.assertEqual(x_results, expected_dict)

def test_equality_of_to_dict(self):
x = SurveyReader(utils.path_to_text_fixture("group.xls"))
x = SurveyReader(utils.path_to_text_fixture("group.xls"), default_name="group")
x_results = x.to_json_dict()

survey = create_survey_element_from_dict(x_results)
Expand Down
2 changes: 1 addition & 1 deletion pyxform/tests/loop_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
class LoopTests(TestCase):
def test_loop(self):
path = utils.path_to_text_fixture("another_loop.xls")
survey = create_survey_from_xls(path)
survey = create_survey_from_xls(path, "another_loop")
self.maxDiff = None
expected_dict = {
"name": "another_loop",
Expand Down
4 changes: 3 additions & 1 deletion pyxform/tests/new_cascading_select_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ def runTest(self):
)

# Do the conversion:
json_survey = pyxform.xls2json.parse_file_to_json(self.path_to_excel_file)
json_survey = pyxform.xls2json.parse_file_to_json(
self.path_to_excel_file, default_name=self.root_filename
)

survey = pyxform.create_survey_element_from_dict(json_survey)

Expand Down
2 changes: 1 addition & 1 deletion pyxform/tests/or_other_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def runTest(self):
# Do the conversion:
warnings = []
json_survey = pyxform.xls2json.parse_file_to_json(
self.path_to_excel_file, warnings=warnings
self.path_to_excel_file, default_name="or_other", warnings=warnings
)
survey = pyxform.create_survey_element_from_dict(json_survey)
survey.print_xform_to_file(self.output_path, warnings=warnings)
Expand Down
4 changes: 3 additions & 1 deletion pyxform/tests/select_one_external_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ def runTest(self):

output_csv = os.path.join(DIR, "test_output", self.root_filename + ".csv")
# Do the conversion:
json_survey = pyxform.xls2json.parse_file_to_json(self.path_to_excel_file)
json_survey = pyxform.xls2json.parse_file_to_json(
self.path_to_excel_file, default_name="select_one_external"
)

self.assertTrue(
sheet_to_csv(self.path_to_excel_file, output_csv, "external_choices")
Expand Down
2 changes: 1 addition & 1 deletion pyxform/tests/settings_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def setUp(self):
self.path = utils.path_to_text_fixture("settings.xls")

def test_survey_reader(self):
survey_reader = SurveyReader(self.path)
survey_reader = SurveyReader(self.path, default_name="settings")
expected_dict = {
u"id_string": u"new_id",
u"sms_keyword": u"new_id",
Expand Down
Loading

0 comments on commit 077972c

Please sign in to comment.