Skip to content

Commit

Permalink
Merge pull request #384 from nribeka/pyxform-296
Browse files Browse the repository at this point in the history
Defaulting sheet name to survey if there is only single sheet.
  • Loading branch information
ukanga authored Oct 9, 2019
2 parents 077972c + eb40aab commit 894f31f
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 1 deletion.
Binary file added pyxform/tests/example_xls/survey_no_name.xlsx
Binary file not shown.
23 changes: 23 additions & 0 deletions pyxform/tests/test_expected_output/survey_no_name.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0"?>
<h:html xmlns="http://www.w3.org/2002/xforms" xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:h="http://www.w3.org/1999/xhtml" xmlns:jr="http://openrosa.org/javarosa" xmlns:odk="http://www.opendatakit.org/xforms" xmlns:orx="http://openrosa.org/xforms" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<h:head>
<h:title>data</h:title>
<model>
<instance>
<data id="data">
<state/>
<meta>
<instanceID/>
</meta>
</data>
</instance>
<bind nodeset="/data/state" type="string"/>
<bind jr:preload="uid" nodeset="/data/meta/instanceID" readonly="true()" type="string"/>
</model>
</h:head>
<h:body>
<input ref="/data/state">
<label>The State</label>
</input>
</h:body>
</h:html>
10 changes: 10 additions & 0 deletions pyxform/tests/xls2json_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,13 @@ def test_a_unicode_csv_works(self):
utf_csv_path = utils.path_to_text_fixture("utf_csv.csv")
dict_value = csv_to_dict(utf_csv_path)
self.assertTrue("\\ud83c" in json.dumps(dict_value))


class DefaultToSurveyTest(TestCase):
def test_default_sheet_name_to_survey(self):
xls_path = utils.path_to_text_fixture("survey_no_name.xlsx")
dict_value = xls_to_dict(xls_path)
print (json.dumps(dict_value))
self.assertTrue("survey" in json.dumps(dict_value))
self.assertTrue("state" in json.dumps(dict_value))
self.assertTrue("The State" in json.dumps(dict_value))
22 changes: 22 additions & 0 deletions pyxform/tests/xlsform_spec_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,5 +171,27 @@ def runTest(self):
os.remove(self.output_path)


class DefaultSurveySheetTest(XFormTestCase):
maxDiff = None

def runTest(self):
filename = "survey_no_name.xlsx"
self.get_file_path(filename)
expected_output_path = os.path.join(
DIR, "test_expected_output", self.root_filename + ".xml"
)

warnings = []
json_survey = pyxform.xls2json.parse_file_to_json(
self.path_to_excel_file, warnings=warnings
)
survey = pyxform.create_survey_element_from_dict(json_survey)
survey.print_xform_to_file(self.output_path, warnings=warnings)

with codecs.open(expected_output_path, "rb", encoding="utf-8") as expected_file:
with codecs.open(self.output_path, "rb", encoding="utf-8") as actual_file:
self.assertXFormEqual(expected_file.read(), actual_file.read())


if __name__ == "__main__":
unittest.main()
7 changes: 6 additions & 1 deletion pyxform/xls2json_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,12 @@ def slugify(s):
for sheet in workbook.sheets():
# Do not process sheets that have nothing to do with XLSForm.
if sheet.name not in constants.SUPPORTED_SHEET_NAMES:
continue
if len(workbook.sheets()) == 1:
result[constants.SURVEY], result[
"%s_header" % constants.SURVEY
] = xls_to_dict_normal_sheet(sheet)
else:
continue
if sheet.name == constants.CASCADING_CHOICES:
result[sheet.name] = _xls_to_dict_cascade_sheet(sheet)
else:
Expand Down

0 comments on commit 894f31f

Please sign in to comment.