From 47c3e8d0821daf5ddb5f3765e70872d0153a6f53 Mon Sep 17 00:00:00 2001 From: Grzegorz Orczykowski Date: Tue, 14 Nov 2023 02:59:42 +0100 Subject: [PATCH 1/6] Added tests --- tests/test_rows.py | 51 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 tests/test_rows.py diff --git a/tests/test_rows.py b/tests/test_rows.py new file mode 100644 index 00000000..bdb0e0e2 --- /dev/null +++ b/tests/test_rows.py @@ -0,0 +1,51 @@ +# -*- coding: utf-8 -*- +""" +Test text rows parameter. +""" +from tests.pyxform_test_case import PyxformTestCase + + +class TestRows(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_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=["Parameter rows must have an integer value."], + ) From c47ed44fb73abc9d3e322d10c1433ee513337e2d Mon Sep 17 00:00:00 2001 From: Grzegorz Orczykowski Date: Tue, 14 Nov 2023 03:00:23 +0100 Subject: [PATCH 2/6] Add support for rows in parameters --- pyxform/xls2json.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/pyxform/xls2json.py b/pyxform/xls2json.py index e1fe6cae..9e97b342 100644 --- a/pyxform/xls2json.py +++ b/pyxform/xls2json.py @@ -1306,6 +1306,21 @@ def workbook_to_json( parent_children_array.append(new_dict) continue + if question_type == "text": + new_dict = row.copy() + + if "rows" in parameters.keys(): + try: + int(parameters["rows"]) + except ValueError: + raise PyXFormError("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() From d3eba1174cb9f7b98372f80b7c8439f340ba5a6b Mon Sep 17 00:00:00 2001 From: Grzegorz Orczykowski Date: Thu, 23 Nov 2023 14:51:07 +0100 Subject: [PATCH 3/6] Validate parameters --- pyxform/xls2json.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pyxform/xls2json.py b/pyxform/xls2json.py index 9e97b342..25da4143 100644 --- a/pyxform/xls2json.py +++ b/pyxform/xls2json.py @@ -1308,6 +1308,7 @@ def workbook_to_json( if question_type == "text": new_dict = row.copy() + parameters_generic.validate(parameters=parameters, allowed=("rows",)) if "rows" in parameters.keys(): try: From e56c08b32f5472ef723d2eb64ae8eeb8d30befd2 Mon Sep 17 00:00:00 2001 From: Grzegorz Orczykowski Date: Thu, 23 Nov 2023 15:05:32 +0100 Subject: [PATCH 4/6] Include the row number in the error message --- pyxform/xls2json.py | 5 ++++- tests/test_rows.py | 4 +++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/pyxform/xls2json.py b/pyxform/xls2json.py index 25da4143..d21f3644 100644 --- a/pyxform/xls2json.py +++ b/pyxform/xls2json.py @@ -1314,7 +1314,10 @@ def workbook_to_json( try: int(parameters["rows"]) except ValueError: - raise PyXFormError("Parameter rows must have an integer value.") + 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"]}) diff --git a/tests/test_rows.py b/tests/test_rows.py index bdb0e0e2..f5e231b6 100644 --- a/tests/test_rows.py +++ b/tests/test_rows.py @@ -47,5 +47,7 @@ def test_throwing_error_if_rows_set_in_parameters_but_the_value_is_not_an_intege name="data", md=md.format(case=case), errored=True, - error__contains=["Parameter rows must have an integer value."], + error__contains=[ + "[row : 2] Parameter rows must have an integer value." + ], ) From bb85dd3439b9f4e54259afb9814c98f44f1c5a60 Mon Sep 17 00:00:00 2001 From: Grzegorz Orczykowski Date: Thu, 23 Nov 2023 15:06:44 +0100 Subject: [PATCH 5/6] Improved the test name --- tests/{test_rows.py => test_parameters_rows.py} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename tests/{test_rows.py => test_parameters_rows.py} (97%) diff --git a/tests/test_rows.py b/tests/test_parameters_rows.py similarity index 97% rename from tests/test_rows.py rename to tests/test_parameters_rows.py index f5e231b6..8a2c62fc 100644 --- a/tests/test_rows.py +++ b/tests/test_parameters_rows.py @@ -5,7 +5,7 @@ from tests.pyxform_test_case import PyxformTestCase -class TestRows(PyxformTestCase): +class TestParametersRows(PyxformTestCase): def test_adding_rows_to_the_body_if_set_in_its_own_column( self, ): From 7f3a920474055c62e8385a554ea73fa10823dd71 Mon Sep 17 00:00:00 2001 From: Grzegorz Orczykowski Date: Thu, 23 Nov 2023 15:12:11 +0100 Subject: [PATCH 6/6] Added a test for rows specified in both possible ways --- tests/test_parameters_rows.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/test_parameters_rows.py b/tests/test_parameters_rows.py index 8a2c62fc..d857d83e 100644 --- a/tests/test_parameters_rows.py +++ b/tests/test_parameters_rows.py @@ -19,6 +19,19 @@ def test_adding_rows_to_the_body_if_set_in_its_own_column( 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, ):