示例#1
0
    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=mandatory_message)

        return validators.Optional()
    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 test_response_invalid_raises_StopValidation(raw_data, message, mock_form,
                                                mock_field):
    validator = ResponseRequired(message)
    mock_field.raw_data = raw_data
    mock_field.errors = []

    with pytest.raises(StopValidation) as exc:
        validator(mock_form, mock_field)

    assert message == str(exc.value)
    def test_response_empty_invalid(self):
        message = "test_response_empty_invalid"
        validator = ResponseRequired(message)

        mock_form = Mock()

        mock_field = Mock()
        mock_field.raw_data = [""]
        mock_field.errors = []

        with self.assertRaises(StopValidation) as ite:
            validator(mock_form, mock_field)

        self.assertEqual(message, str(ite.exception))
    def test_response_blank_valid_when_whitespace_on(self):
        message = "test_response_blank_valid_when_whitespace_on"
        validator = ResponseRequired(message=message, strip_whitespace=False)

        mock_form = Mock()

        mock_field = Mock()
        mock_field.raw_data = ["                      "]
        mock_field.errors = []

        try:
            validator(mock_form, mock_field)
        except StopValidation:
            self.fail(
                "Response that needs further validation raised StopValidation")
    def test_required_empty(self):

        message = "test_required_empty"
        validator = ResponseRequired(message)

        mock_form = Mock()

        mock_field = Mock()
        mock_field.raw_data = ["Here is some valid input"]

        try:
            validator(mock_form, mock_field)
        except StopValidation:
            self.fail(
                "Response that needs further validation raised StopValidation")
def test_response(raw_data, message, strip_whitespace, mock_form, mock_field):
    validator = ResponseRequired(message, strip_whitespace=strip_whitespace)
    mock_field.raw_data = raw_data
    mock_field.errors = []

    validator(mock_form, mock_field)