Skip to content

Commit

Permalink
Add fix to calculations value source validation (#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
petechd authored Jul 28, 2022
1 parent e2d7e97 commit 5b176df
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/validators/questions/calculated_question_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def validate_calculations_value_source_is_numeric(self):
if answer_id := calculation.get("answer_id"):
self._validate_answer_is_numeric(answer_id)

elif value and value.get("source"):
elif isinstance(value, dict) and value.get("source"):
answer_id = value.get("identifier")
# Calculated summary value source is validated elsewhere and must be of a number type

Expand Down
81 changes: 81 additions & 0 deletions tests/schemas/valid/test_calculations_with_numeric_value.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
{
"mime_type": "application/json/ons/eq",
"language": "en",
"schema_version": "0.0.1",
"data_version": "0.0.3",
"survey_id": "0",
"title": "Calculated question with a number value test survey",
"theme": "default",
"description": "A survey that tests validation against a calculated question with a valid number value",
"metadata": [
{
"name": "user_id",
"type": "string"
},
{
"name": "period_id",
"type": "string"
},
{
"name": "ru_name",
"type": "string"
}
],
"questionnaire_flow": {
"type": "Linear",
"options": {
"summary": {
"collapsible": false
}
}
},
"sections": [
{
"id": "default-section",
"groups": [
{
"id": "group",
"title": "Validate sum against value, calculated summary source",
"blocks": [
{
"type": "Question",
"id": "breakdown-block",
"question": {
"id": "breakdown-question",
"title": "Breakdown validated against a number value",
"description": [
"This is a breakdown of the total from a number value."
],
"type": "Calculated",
"calculations": [
{
"calculation_type": "sum",
"value": 100,
"answers_to_calculate": ["breakdown-1", "breakdown-2"],
"conditions": ["equals"]
}
],
"answers": [
{
"id": "breakdown-1",
"label": "Breakdown 1",
"mandatory": false,
"decimal_places": 2,
"type": "Number"
},
{
"id": "breakdown-2",
"label": "Breakdown 2",
"mandatory": false,
"decimal_places": 2,
"type": "Number"
}
]
}
}
]
}
]
}
]
}

0 comments on commit 5b176df

Please sign in to comment.