def test_mandatory_mutually_exclusive_question_raises_error_with_question_text( self, ): with self.app_request_context(): schema = load_schema_from_name("test_question_title_in_error") question_schema = schema.get_block("mutually-exclusive-checkbox").get( "question" ) answer_store = AnswerStore( [{"answer_id": "mandatory-checkbox-answer", "value": ["Tuna"]}] ) renderer = PlaceholderRenderer( language="en", schema=schema, answer_store=answer_store ) rendered_schema = renderer.render(question_schema, None) form = generate_form( schema, rendered_schema, answer_store, metadata=None, form_data=MultiDict(), ) form.validate_mutually_exclusive_question(question_schema) error = form.question_errors["mutually-exclusive-checkbox-question"] assert error == format_message_with_title( error_messages["MANDATORY_CHECKBOX"], "Did you really answer ‘Tuna’ to the previous question?", )
def get_mandatory_validator(self): if self.answer_schema["mandatory"] is True: mandatory_message = self.get_validation_message( self.MANDATORY_MESSAGE_KEY) return ResponseRequired(message=format_message_with_title( mandatory_message, self.question_title)) return validators.Optional()
def validators(self) -> List: validate_with: List = [] if self.answer_schema["mandatory"]: validate_with = [ InputRequired(message=self.get_validation_message( format_message_with_title(self.MANDATORY_MESSAGE_KEY, self.question_title))) ] return validate_with
def test_mutually_exclusive_mandatory_checkbox_raises_ValidationError( answer_permutations, is_mandatory, is_only_checkboxes, error_type): validator = MutuallyExclusiveCheck(question_title="") with pytest.raises(ValidationError) as exc: validator( answer_values=iter(answer_permutations), is_mandatory=is_mandatory, is_only_checkboxes=is_only_checkboxes, ) assert format_message_with_title(error_messages[error_type], "") == str(exc.value)
def test_mutually_exclusive_mandatory_exception(self): answer_permutations = [[[], []], [None, []], ["", []]] for values in answer_permutations: with self.assertRaises(ValidationError) as ite: self.validator( answer_values=iter(values), is_mandatory=True, is_only_checkboxes=False, ) self.assertEqual( format_message_with_title(error_messages["MANDATORY_QUESTION"], self.question_title), str(ite.exception), )
def validators(self): validate_with = [OptionalForm()] if self.answer_schema["mandatory"] is True: validate_with = [ DateRequired(message=self.get_validation_message( format_message_with_title(self.MANDATORY_MESSAGE_KEY, self.question_title))) ] error_message = self.get_validation_message("INVALID_DATE") validate_with.append(DateCheck(error_message)) minimum_date = self.get_date_value("minimum") maximum_date = self.get_date_value("maximum") if minimum_date or maximum_date: min_max_validator = self.get_min_max_validator( minimum_date, maximum_date) validate_with.append(min_max_validator) return validate_with
def test_mandatory_mutually_exclusive_question_raises_error_when_not_answered(self): with self.app_request_context(): schema = load_schema_from_name("test_mutually_exclusive") question_schema = schema.get_block("mutually-exclusive-mandatory-date").get( "question" ) form = generate_form( schema, question_schema, AnswerStore(), metadata=None, form_data=MultiDict(), ) form.validate_mutually_exclusive_question(question_schema) self.assertEqual( form.question_errors["mutually-exclusive-mandatory-date-question"], format_message_with_title( error_messages["MANDATORY_QUESTION"], question_schema.get("title") ), )