Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rows to parameters column for text type #667

Merged
merged 6 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions pyxform/xls2json.py
Original file line number Diff line number Diff line change
Expand Up @@ -1306,6 +1306,25 @@ def workbook_to_json(
parent_children_array.append(new_dict)
continue

if question_type == "text":
new_dict = row.copy()
parameters_generic.validate(parameters=parameters, allowed=("rows",))

if "rows" in parameters.keys():
grzesiek2010 marked this conversation as resolved.
Show resolved Hide resolved
try:
int(parameters["rows"])
except ValueError:
raise PyXFormError(
(ROW_FORMAT_STRING % row_number)
+ " Parameter rows must have an integer value."
)

new_dict["control"] = new_dict.get("control", {})
new_dict["control"].update({"rows": parameters["rows"]})

parent_children_array.append(new_dict)
continue

if question_type == "photo":
new_dict = row.copy()

Expand Down
66 changes: 66 additions & 0 deletions tests/test_parameters_rows.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# -*- coding: utf-8 -*-
"""
Test text rows parameter.
"""
from tests.pyxform_test_case import PyxformTestCase


class TestParametersRows(PyxformTestCase):
def test_adding_rows_to_the_body_if_set_in_its_own_column(
self,
):
self.assertPyxformXform(
name="data",
md="""
| survey | | | | |
| | type | name | label | body::rows |
| | text | name | Name | 7 |
""",
xml__xpath_match=["/h:html/h:body/x:input[@ref='/data/name' and @rows='7']"],
)

def test_using_the_number_of_rows_specified_in_parameters_if_it_is_set_in_both_its_own_column_and_the_parameters_column(
self,
):
self.assertPyxformXform(
name="data",
md="""
| survey | | | | | |
| | type | name | label | body::rows | parameters |
| | text | name | Name | 7 | rows=8 |
""",
xml__xpath_match=["/h:html/h:body/x:input[@ref='/data/name' and @rows='8']"],
)

def test_adding_rows_to_the_body_if_set_in_parameters(
self,
):
self.assertPyxformXform(
name="data",
md="""
| survey | | | | |
| | type | name | label | parameters |
| | text | name | Name | rows=7 |
""",
xml__xpath_match=["/h:html/h:body/x:input[@ref='/data/name' and @rows='7']"],
)

def test_throwing_error_if_rows_set_in_parameters_but_the_value_is_not_an_integer(
self,
):
parameters = ("rows=", "rows=foo", "rows=7.5")
md = """
| survey | | | | |
| | type | name | label | parameters |
| | text | name | Name | {case} |
"""
for case in parameters:
with self.subTest(msg=case):
self.assertPyxformXform(
name="data",
md=md.format(case=case),
errored=True,
error__contains=[
"[row : 2] Parameter rows must have an integer value."
],
)
Loading