Skip to content

Commit

Permalink
Allow no-body calculates of any type
Browse files Browse the repository at this point in the history
  • Loading branch information
lognaturel committed Jun 5, 2020
1 parent d6dbf39 commit 92e989b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
6 changes: 3 additions & 3 deletions pyxform/question.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ def xml_instance(self, **kwargs):
return node(self.name, **attributes)

def xml_control(self):
if ("calculate" in self.bind or self.when) and not (self.label or self.hint):
return None

xml_node = self.build_xml()

if xml_node:
Expand Down Expand Up @@ -67,9 +70,6 @@ class InputQuestion(Question):
"""

def build_xml(self):
if ("calculate" in self.bind or self.trigger) and not (self.label or self.hint):
return None

control_dict = self.control
label_and_hint = self.xml_label_and_hint()
survey = self.get_root()
Expand Down
39 changes: 35 additions & 4 deletions pyxform/tests_v1/test_typed_calculates.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,25 @@ def test_xls_type_calculate_has_type_string(self):
| | calculate| a | | 2 * 2 |
""",
xml__contains=[
'<bind calculate="2 * 2" nodeset="/calculate-type/a" type="string"/>'
'<bind calculate="2 * 2" nodeset="/calculate-type/a" type="string"/>',
],
)

def test_other_xls_type_with_calculation_is_passed_through(self):
def test_xls_type_calculate_with_label_has_no_body(self):
self.assertPyxformXform(
name="calculate-type",
md="""
| survey | | | | |
| | type | name | label | calculation |
| | calculate| a | A | 2 * 2 |
""",
xml__contains=[
'<bind calculate="2 * 2" nodeset="/calculate-type/a" type="string"/>',
'<h:body/>'
],
)

def test_non_calculate_type_with_calculation_is_bind_type(self):
self.assertPyxformXform(
name="non-calculate-type",
md="""
Expand All @@ -34,7 +48,7 @@ def test_other_xls_type_with_calculation_is_passed_through(self):
],
)

def test_other_xls_type_with_calculation_and_no_label_has_no_control(self):
def test_non_calculate_type_with_calculation_and_no_label_has_no_control(self):
self.assertPyxformXform(
name="no-label",
md="""
Expand All @@ -46,6 +60,23 @@ def test_other_xls_type_with_calculation_and_no_label_has_no_control(self):
xml__excludes=["input"],
)

def test_select_type_with_calculation_and_no_label_has_no_control(self):
self.assertPyxformXform(
name="calculate-select",
md="""
| survey | | | | |
| | type | name | label | calculation |
| | select_one yes_no | a | | concat('a', 'b') |
| choices | | | | |
| | list_name | name | label | |
| | yes_no | yes | Yes | |
| | yes_no | no | No | |
""",
xml__contains=['<bind calculate="concat(\'a\', \'b\')" nodeset="/calculate-select/a" type="string"/>'],
instance__contains=["<a/>"],
xml__excludes=["<select1>"],
)

def test_row_without_label_or_calculation_throws_error(self):
self.assertPyxformXform(
name="no-label",
Expand All @@ -56,4 +87,4 @@ def test_row_without_label_or_calculation_throws_error(self):
""",
errored=True,
error__contains="The survey element named 'a' has no label or hint.",
)
)

0 comments on commit 92e989b

Please sign in to comment.