Skip to content

Commit

Permalink
fix: tests for new itemset output for selects, add xls2json type hints
Browse files Browse the repository at this point in the history
- yes_or_no_question and xml_tests updated for new itemset output for
  selects. The default_survey_sheet test expected no default_form_name.
- add type hints to workbook_to_json and parse_file_to_json.
  • Loading branch information
lindsay-stevens committed May 5, 2023
1 parent acd7640 commit 2adb257
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 35 deletions.
30 changes: 17 additions & 13 deletions pyxform/xls2json.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import re
import sys
from collections import Counter
from typing import Any, Dict, List
from typing import IO, Any, Dict, List, Optional

from pyxform import aliases, constants
from pyxform.constants import (
Expand Down Expand Up @@ -386,11 +386,11 @@ def add_choices_info_to_question(

def workbook_to_json(
workbook_dict,
form_name=None,
fallback_form_name=None,
default_language=constants.DEFAULT_LANGUAGE_VALUE,
warnings=None,
) -> "Dict[str, Any]":
form_name: Optional[str] = None,
fallback_form_name: Optional[str] = None,
default_language: str = constants.DEFAULT_LANGUAGE_VALUE,
warnings: Optional[List[str]] = None,
) -> Dict[str, Any]:
"""
workbook_dict -- nested dictionaries representing a spreadsheet.
should be similar to those returned by xls_to_dict
Expand Down Expand Up @@ -1500,12 +1500,12 @@ def get_filename(path):


def parse_file_to_json(
path,
default_name="data",
default_language=constants.DEFAULT_LANGUAGE_VALUE,
warnings=None,
file_object=None,
):
path: str,
default_name: str = "data",
default_language: str = constants.DEFAULT_LANGUAGE_VALUE,
warnings: Optional[List[str]] = None,
file_object: Optional[IO] = None,
) -> Dict[str, Any]:
"""
A wrapper for workbook_to_json
"""
Expand All @@ -1514,7 +1514,11 @@ def parse_file_to_json(
workbook_dict = parse_file_to_workbook_dict(path, file_object)
fallback_form_name = str(get_filename(path))
return workbook_to_json(
workbook_dict, default_name, fallback_form_name, default_language, warnings
workbook_dict=workbook_dict,
form_name=default_name,
fallback_form_name=fallback_form_name,
default_language=default_language,
warnings=warnings,
)


Expand Down
19 changes: 18 additions & 1 deletion tests/test_expected_output/yes_or_no_question.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
],
"type": "select one",
"name": "good_day",
"itemset": "yes_or_no",
"list_name": "yes_or_no",
"parameters": {},
"label": {
Expand All @@ -46,5 +47,21 @@
}
]
}
]
],
"choices": {
"yes_or_no": [
{
"label": {
"english": "yes"
},
"name": "yes"
},
{
"label": {
"english": "no"
},
"name": "no"
}
]
}
}
18 changes: 13 additions & 5 deletions tests/xform_test_case/xlsform_spec_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,24 @@
class TestXFormConversion(XFormTestCase):
maxDiff = None

def compare_xform(self, file_name: str):
def compare_xform(self, file_name: str, set_default_name: bool = True):
self.get_file_path(file_name)
expected_output_path = os.path.join(
test_expected_output.PATH, self.root_filename + ".xml"
)
# Do the conversion:
warnings = []
json_survey = xls2json.parse_file_to_json(
self.path_to_excel_file, default_name=self.root_filename, warnings=warnings
)
if set_default_name:
json_survey = xls2json.parse_file_to_json(
self.path_to_excel_file,
default_name=self.root_filename,
warnings=warnings,
)
else:
json_survey = xls2json.parse_file_to_json(
self.path_to_excel_file,
warnings=warnings,
)
survey = builder.create_survey_element_from_dict(json_survey)
survey.print_xform_to_file(self.output_path, warnings=warnings)
# Compare with the expected output:
Expand All @@ -48,7 +56,7 @@ def test_search_and_select(self):
self.compare_xform(file_name="search_and_select.xlsx")

def test_default_survey_sheet(self):
self.compare_xform(file_name="survey_no_name.xlsx")
self.compare_xform(file_name="survey_no_name.xlsx", set_default_name=False)


class TestCalculateWithoutCalculation(TestCase):
Expand Down
39 changes: 23 additions & 16 deletions tests/xform_test_case/xml_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"""
Test XForm XML syntax.
"""
import re
from unittest import TestCase
from xml.dom.minidom import getDOMImplementation

Expand All @@ -13,6 +12,8 @@


class XMLTests(XFormTestCase):
maxDiff = None

def setUp(self):
self.survey = create_survey_from_xls(
path_to_text_fixture("yes_or_no_question.xls"), "yes_or_no_question"
Expand All @@ -35,22 +36,34 @@ def test_to_xml(self):
<text id="/yes_or_no_question/good_day:label">
<value>have you had a good day today?</value>
</text>
<text id="/yes_or_no_question/good_day/no:label">
<value>no</value>
</text>
<text id="/yes_or_no_question/good_day/yes:label">
<text id="yes_or_no-0">
<value>yes</value>
</text>
<text id="yes_or_no-1">
<value>no</value>
</text>
</translation>
</itext>
<instance>
<yes_or_no_question id="yes_or_no_question_2011_04_22">
<yes_or_no_question id="yes_or_no_question">
<good_day/>
<meta>
<instanceID/>
</meta>
</yes_or_no_question>
</instance>
<instance id="yes_or_no">
<root>
<item>
<itextId>yes_or_no-0</itextId>
<name>yes</name>
</item>
<item>
<itextId>yes_or_no-1</itextId>
<name>no</name>
</item>
</root>
</instance>
<bind nodeset="/yes_or_no_question/good_day" type="string"/>
<bind jr:preload="uid"
nodeset="/yes_or_no_question/meta/instanceID"
Expand All @@ -60,20 +73,14 @@ def test_to_xml(self):
<h:body>
<select1 ref="/yes_or_no_question/good_day">
<label ref="jr:itext('/yes_or_no_question/good_day:label')"/>
<item>
<label ref="jr:itext('/yes_or_no_question/good_day/yes:label')"/>
<value>yes</value>
</item>
<item>
<label ref="jr:itext('/yes_or_no_question/good_day/no:label')"/>
<value>no</value>
</item>
<itemset nodeset="instance('yes_or_no')/root/item">
<value ref="name"/>
<label ref="jr:itext(itextId)"/>
</itemset>
</select1>
</h:body>
</h:html>
"""
xml_str = re.sub(r"yes_or_no_question_2011_04_22", self.survey.id_string, xml_str)
self.maxDiff = None
self.assertXFormEqual(xml_str, self.survey.to_xml())


Expand Down

0 comments on commit 2adb257

Please sign in to comment.