示例#1
0
    def test_year_date_validator_valid(self):
        validator = YearCheck()

        mock_form = Mock()
        mock_form.year.data = '2017'

        mock_field = Mock()

        try:
            validator(mock_form, mock_field)
        except ValidationError:
            self.fail('Valid date raised ValidationError')
示例#2
0
    def test_year_date_validator_none(self):
        validator = YearCheck()

        mock_form = Mock()
        mock_form.year.data = None

        mock_field = Mock()

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

        self.assertEqual(error_messages['INVALID_DATE'], str(ite.exception))
示例#3
0
    def test_year_date_validator_empty_string(self):

        validator = YearCheck()

        mock_form = Mock()
        mock_form.year.data = ''

        mock_field = Mock()

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

        self.assertEqual(error_messages['INVALID_DATE'], str(ite.exception))
示例#4
0
def get_year_form(answer,
                  answer_store,
                  metadata,
                  error_messages,
                  label,
                  guidance,
                  group_instance=0):
    """
    Returns a year form metaclass with appropriate validators. Used in both date and
    date range form creation.

    :param answer: The answer on which to base this form
    :param error_messages: The messages to use upon this form during validation
    :return: The generated MonthYearDateForm metaclass
    """
    class YearDateForm(Form):
        year = None

        @property
        def data(self):
            data = super().data
            if not data['year']:
                return None

            return '{:04d}'.format(int(data['year']))

    validate_with = [OptionalForm()]

    if answer['mandatory'] is True:
        validate_with = validate_mandatory_date(error_messages, answer)

    error_message = get_bespoke_message(answer, 'INVALID_DATE')
    validate_with.append(YearCheck(error_message))

    if 'minimum' in answer or 'maximum' in answer:
        min_max_validation = validate_min_max_date(
            answer,
            answer_store,
            metadata,
            'yyyy',
            group_instance=group_instance)
        validate_with.append(min_max_validation)

    YearDateForm.year = CustomIntegerField(
        label=label,
        validators=validate_with,
        description=guidance,
    )

    return YearDateForm