diff --git a/app/validators/questions/calculated_question_validator.py b/app/validators/questions/calculated_question_validator.py index 3705bc65..f071c721 100644 --- a/app/validators/questions/calculated_question_validator.py +++ b/app/validators/questions/calculated_question_validator.py @@ -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 diff --git a/tests/schemas/valid/test_calculations_with_numeric_value.json b/tests/schemas/valid/test_calculations_with_numeric_value.json new file mode 100644 index 00000000..09c8b3c0 --- /dev/null +++ b/tests/schemas/valid/test_calculations_with_numeric_value.json @@ -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" + } + ] + } + } + ] + } + ] + } + ] +}