Skip to content

Commit

Permalink
chg: add all choices in the XLSForm to the XForm even if not referenced
Browse files Browse the repository at this point in the history
- allows users to include reference data for expressions via choices
  without needing a hidden dummy question. Includes all choices rather
  than parsing expressions to find references, because it seems unlikely
  that both a XLSForm would include completely irrelevant choices and
  have so many such choices that the XForm document size is a problem.
- test_xls2json_xls.py
  - moved loop test into test_loop.py and modified to use the same style
    as the existing test
- test_loop.py
  - use xls2xform.convert rather than internals directly
  • Loading branch information
lindsay-stevens committed Nov 2, 2024
1 parent 525bfcf commit 88b0328
Show file tree
Hide file tree
Showing 7 changed files with 289 additions and 92 deletions.
15 changes: 3 additions & 12 deletions pyxform/xls2json.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,13 +520,14 @@ def workbook_to_json(
use_double_colons=use_double_colons,
default_language=default_language,
)
combined_lists = group_dictionaries_by_key(
choices = group_dictionaries_by_key(
list_of_dicts=choices_sheet.data, key=constants.LIST_NAME_S
)
if 0 < len(choices):
json_dict[constants.CHOICES] = choices
# To combine the warning into one message, the check for missing choices translation
# columns is run with Survey sheet below.

choices = combined_lists
# Make sure all the options have the required properties:
warnedabout = set()
for list_name, options in choices.items():
Expand Down Expand Up @@ -1242,16 +1243,6 @@ def workbook_to_json(
choice_filter=row.get(constants.CHOICE_FILTER),
file_extension=file_extension,
)
# Add the choice to the survey choices if it's being used in an itemset.
if (
constants.ITEMSET in new_json_dict
and new_json_dict[constants.ITEMSET] == list_name
and list_name in choices
):
# Initialise choices output if none added already.
if constants.CHOICES not in json_dict:
json_dict[constants.CHOICES] = {}
json_dict[constants.CHOICES][list_name] = choices[list_name]

# Code to deal with table_list appearance flags
# (for groups of selects)
Expand Down
21 changes: 21 additions & 0 deletions tests/test_choices_sheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,24 @@ def test_choices_extra_columns_output_order_matches_xlsform(self):
""",
],
)

def test_unreferenced_lists_included_in_output(self):
"""Should find all specified choice lists in the output, even if unreferenced."""
md = """
| survey |
| | type | name | label |
| | select_one choices | a | A |
| choices |
| | list_name | name | label |
| | choices | 1 | Y |
| | choices | 2 | N |
| | choices2 | 1 | Y |
| | choices2 | 2 | N |
"""
self.assertPyxformXform(
md=md,
xml__xpath_match=[
xpc.model_instance_choices_label("choices", (("1", "Y"), ("2", "N"))),
xpc.model_instance_choices_label("choices2", (("1", "Y"), ("2", "N"))),
],
)
100 changes: 98 additions & 2 deletions tests/test_expected_output/repeat_date_test.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@
<h:head>
<h:title>repeat_date_test</h:title>
<model odk:xforms-version="1.0.0">
<itext>
<translation lang="default" default="true()">
<text id="a_b-0">
<value form="image">jr://images/a.jpg</value>
</text>
<text id="a_b-1">
<value form="image">jr://images/b.jpg</value>
</text>
<text id="happy_sad-0">
<value form="image">jr://images/happy.jpg</value>
</text>
<text id="happy_sad-1">
<value form="image">jr://images/sad.jpg</value>
</text>
</translation>
</itext>
<instance>
<repeat_date_test id="repeat_date_test">
<generated_note_name_2/>
Expand All @@ -21,15 +37,95 @@
</meta>
</repeat_date_test>
</instance>
<instance id="list">
<root>
<item>
<name>a</name>
<label>option a</label>
</item>
<item>
<name>b</name>
<label>option b</label>
</item>
<item>
<name>c</name>
<label>option c</label>
</item>
<item>
<name>d</name>
<label>option d</label>
</item>
</root>
</instance>
<instance id="list2">
<root>
<item>
<name>1</name>
<label>option 1</label>
</item>
<item>
<name>2</name>
<label>option 2</label>
</item>
<item>
<name>3</name>
<label>option 3</label>
</item>
<item>
<name>4</name>
<label>option 4</label>
</item>
<item>
<name>5</name>
<label>option 5</label>
</item>
<item>
<name>6</name>
<label>option 6</label>
</item>
<item>
<name>7</name>
<label>option 7</label>
</item>
<item>
<name>8</name>
<label>option 8</label>
</item>
</root>
</instance>
<instance id="yes_no">
<root>
<item>
<label>Yes</label>
<name>yes</name>
<label>Yes</label>
</item>
<item>
<label>No</label>
<name>no</name>
<label>No</label>
</item>
</root>
</instance>
<instance id="a_b">
<root>
<item>
<itextId>a_b-0</itextId>
<name>a</name>
</item>
<item>
<itextId>a_b-1</itextId>
<name>b</name>
</item>
</root>
</instance>
<instance id="happy_sad">
<root>
<item>
<itextId>happy_sad-0</itextId>
<name>happy</name>
</item>
<item>
<itextId>happy_sad-1</itextId>
<name>sad</name>
</item>
</root>
</instance>
Expand Down
57 changes: 0 additions & 57 deletions tests/test_expected_output/simple_loop.json

This file was deleted.

100 changes: 98 additions & 2 deletions tests/test_expected_output/xml_escaping.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@
<h:head>
<h:title>xml_escaping</h:title>
<model odk:xforms-version="1.0.0">
<itext>
<translation lang="default" default="true()">
<text id="a_b-0">
<value form="image">jr://images/a.jpg</value>
</text>
<text id="a_b-1">
<value form="image">jr://images/b.jpg</value>
</text>
<text id="happy_sad-0">
<value form="image">jr://images/happy.jpg</value>
</text>
<text id="happy_sad-1">
<value form="image">jr://images/sad.jpg</value>
</text>
</translation>
</itext>
<instance>
<xml_escaping id="xml_escaping">
<a/>
Expand All @@ -13,15 +29,95 @@
</meta>
</xml_escaping>
</instance>
<instance id="list">
<root>
<item>
<name>a</name>
<label>option a</label>
</item>
<item>
<name>b</name>
<label>option b</label>
</item>
<item>
<name>c</name>
<label>option c</label>
</item>
<item>
<name>d</name>
<label>option d</label>
</item>
</root>
</instance>
<instance id="list2">
<root>
<item>
<name>1</name>
<label>option 1</label>
</item>
<item>
<name>2</name>
<label>option 2</label>
</item>
<item>
<name>3</name>
<label>option 3</label>
</item>
<item>
<name>4</name>
<label>option 4</label>
</item>
<item>
<name>5</name>
<label>option 5</label>
</item>
<item>
<name>6</name>
<label>option 6</label>
</item>
<item>
<name>7</name>
<label>option 7</label>
</item>
<item>
<name>8</name>
<label>option 8</label>
</item>
</root>
</instance>
<instance id="yes_no">
<root>
<item>
<label>Yes</label>
<name>yes</name>
<label>Yes</label>
</item>
<item>
<label>No</label>
<name>no</name>
<label>No</label>
</item>
</root>
</instance>
<instance id="a_b">
<root>
<item>
<itextId>a_b-0</itextId>
<name>a</name>
</item>
<item>
<itextId>a_b-1</itextId>
<name>b</name>
</item>
</root>
</instance>
<instance id="happy_sad">
<root>
<item>
<itextId>happy_sad-0</itextId>
<name>happy</name>
</item>
<item>
<itextId>happy_sad-1</itextId>
<name>sad</name>
</item>
</root>
</instance>
Expand Down
Loading

0 comments on commit 88b0328

Please sign in to comment.