def test_bespoke_message_for_sum_validation(self): store = AnswerStore() answer_total = Answer(answer_id="total-answer", value=10) store.add_or_update(answer_total) with self.app_request_context(): schema = load_schema_from_name("test_sum_equal_validation_against_total") question_schema = QuestionnaireSchema.get_mutable_deepcopy( schema.get_block("breakdown-block").get("question") ) question_schema["validation"] = { "messages": {"TOTAL_SUM_NOT_EQUALS": "Test Message"} } form_data = MultiDict({"breakdown-1": "3", "breakdown-2": "5"}) form = generate_form( schema, question_schema, store, metadata=None, form_data=form_data ) with patch( "app.questionnaire.questionnaire_schema.QuestionnaireSchema.get_all_questions_for_block", return_value=[question_schema], ): form.validate() self.assertIn( form.question_errors["breakdown-question"], "Test Message" )
def render_placeholder(self, placeholder_data, list_item_id): placeholder_parser = PlaceholderParser( language=self._language, schema=self._schema, answer_store=self._answer_store, metadata=self._metadata, list_item_id=list_item_id, location=self._location, list_store=self._list_store, ) placeholder_data = QuestionnaireSchema.get_mutable_deepcopy( placeholder_data) if "text_plural" in placeholder_data: plural_schema = placeholder_data["text_plural"] count = self.get_plural_count(plural_schema["count"]) plural_form_key = get_plural_form_key(count, self._language) placeholder_data["text"] = plural_schema["forms"][plural_form_key] if "text" not in placeholder_data and "placeholders" not in placeholder_data: raise ValueError("No placeholder found to render") transformed_values = placeholder_parser( placeholder_data["placeholders"]) return placeholder_data["text"].format(**transformed_values)
def __call__(self, placeholder_list: Sequence[Mapping]) -> Mapping: placeholder_list = QuestionnaireSchema.get_mutable_deepcopy( placeholder_list) for placeholder in placeholder_list: if placeholder["placeholder"] not in self._placeholder_map: self._placeholder_map[placeholder[ "placeholder"]] = self._parse_placeholder(placeholder) return self._placeholder_map
def __call__( self, placeholder_list: Sequence[Mapping] ) -> MutableMapping[str, Union[ValueSourceEscapedTypes, ValueSourceTypes]]: placeholder_list = QuestionnaireSchema.get_mutable_deepcopy( placeholder_list) for placeholder in placeholder_list: if placeholder["placeholder"] not in self._placeholder_map: self._placeholder_map[placeholder[ "placeholder"]] = self._parse_placeholder(placeholder) return self._placeholder_map
def render(self, dict_to_render, list_item_id): """ Transform the current schema json to a fully rendered dictionary """ dict_to_render = QuestionnaireSchema.get_mutable_deepcopy( dict_to_render) pointers = find_pointers_containing(dict_to_render, "placeholders") for pointer in pointers: rendered_text = self.render_pointer(dict_to_render, pointer, list_item_id) set_pointer(dict_to_render, pointer, rendered_text) return dict_to_render
def test_invalid_calculation_type(self): store = AnswerStore() answer_total = Answer(answer_id="total-answer", value=10) store.add_or_update(answer_total) with self.app_request_context(): schema = load_schema_from_name("test_sum_equal_validation_against_total") question_schema = QuestionnaireSchema.get_mutable_deepcopy( schema.get_block("breakdown-block").get("question") ) question_schema["calculations"] = [ { "calculation_type": "subtraction", "answer_id": "total-answer", "answers_to_calculate": ["breakdown-1", "breakdown-2"], "conditions": ["equals"], } ] form_data = MultiDict({"breakdown-1": "3", "breakdown-2": "5"}) form = generate_form( schema, question_schema, store, metadata=None, form_data=form_data ) with self.assertRaises(Exception) as ite: with patch( "app.questionnaire.questionnaire_schema.QuestionnaireSchema.get_all_questions_for_block", return_value=[question_schema], ): form.validate() self.assertEqual( "Invalid calculation_type: subtraction", str(ite.exception) )