def test_date_type_validator_valid_leap_year():
        validator = DateCheck()

        # 2016 WAS a leap year
        mock_form = Mock()
        mock_form.data = "2016-02-29"
        mock_form.year.data = "2016"

        mock_field = Mock()
        validator(mock_form, mock_field)
    def test_date_type_validator_missing_year(self):
        validator = DateCheck()

        mock_form = Mock()
        mock_form.data = "-12-03"

        mock_field = Mock()

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

        self.assertEqual(error_messages["INVALID_DATE"], str(ite.exception))
    def test_date_type_validator_invalid_month_digits(self):
        validator = DateCheck()

        mock_form = Mock()
        mock_form.data = "2020--2-20"

        mock_field = Mock()

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

        self.assertEqual(error_messages["INVALID_DATE"], str(ite.exception))
    def test_date_type_validator_invalid_leap_year(self):

        validator = DateCheck()

        # 2015 was not a leap year
        mock_form = Mock()
        mock_form.data = "2015-02-29"
        mock_form.year.data = "2015"

        mock_field = Mock()

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

        self.assertEqual(error_messages["INVALID_DATE"], str(ite.exception))
    def validators(self):
        validate_with = [OptionalForm()]

        if self.answer_schema["mandatory"] is True:
            validate_with = [
                DateRequired(message=self.get_validation_message(
                    self.MANDATORY_MESSAGE_KEY))
            ]

        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_date_type_validator_valid_dates():
        validator = DateCheck()

        mock_form = Mock()
        mock_form.data = "2016-01-29"
        mock_form.year.data = "2016"

        mock_field = Mock()
        validator(mock_form, mock_field)

        mock_form = Mock()
        mock_form.data = "2016-12-01"
        mock_form.year.data = "2016"

        mock_field = Mock()
        validator(mock_form, mock_field)

        mock_form = Mock()
        mock_form.data = "2016-03-03"
        mock_form.year.data = "2016"

        mock_field = Mock()
        validator(mock_form, mock_field)
def date_check():
    return DateCheck()