Skip to content

Commit

Permalink
Merge pull request #456 from calculate-with-dynamic-default
Browse files Browse the repository at this point in the history
  • Loading branch information
lognaturel authored Jul 23, 2020
2 parents 0d53f40 + d0c5f1c commit c46e853
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
36 changes: 36 additions & 0 deletions pyxform/tests_v1/test_typed_calculates.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,39 @@ def test_row_without_label_or_calculation_throws_error(self):
errored=True,
error__contains="The survey element named 'a' has no label or hint.",
)

def test_calculate_without_calculation_without_default(self):
self.assertPyxformXform(
name="calculate-without-calculation-without-default",
md="""
| survey | | | | | |
| | type | name | label | calculation | default |
| | calculate | a | | | |
""",
errored=True,
error__contains="Missing calculation",
)

def test_calculate_without_calculation_with_default_without_dynamic_default(self):
self.assertPyxformXform(
name="calculate-without-calculation-with-default-without-dynamic-default",
md="""
| survey | | | | | |
| | type | name | label | calculation | default |
| | calculate | a | | | foo |
""",
errored=True,
error__contains="Missing calculation",
)

def test_calculate_without_calculation_with_dynamic_default(self):
self.assertPyxformXform(
name="calculate-without-calculation-with-dynamic-default",
md="""
| survey | | | | | |
| | type | name | label | calculation | default |
| | calculate | a | | | random() |
""",
errored=False,
instance__contains=["<a/>"],
)
5 changes: 4 additions & 1 deletion pyxform/xls2json.py
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,10 @@ def workbook_to_json(

if question_type == "calculate":
calculation = row.get("bind", {}).get("calculate")
if not calculation:
question_default = row.get("default")
if not calculation and not (
question_default and default_is_dynamic(question_default, question_type)
):
raise PyXFormError(
row_format_string % row_number + " Missing calculation."
)
Expand Down

0 comments on commit c46e853

Please sign in to comment.