def test_date_validator_day_month_year(day_month_year, mocker, mock_form, mock_field): validator = OptionalForm() comp = _get_comparison_date_parts(day_month_year, mocker) mock_form.__iter__ = mocker.MagicMock(return_value=iter(comp)) validator(mock_form, mock_field)
def test_date_validator_day_month_year_invalid_raises_StopValidation( day_month_year, mocker, mock_form, mock_field): validator = OptionalForm() comp = _get_comparison_date_parts(day_month_year, mocker) mock_form.__iter__ = mocker.MagicMock(return_value=iter(comp)) with pytest.raises(StopValidation) as exc: validator(mock_form, mock_field) assert "" == str(exc.value)
def test_month_year_optional_month_empty(self): validator = OptionalForm() mock_form = Mock() mock_month = Mock() mock_year = Mock() mock_month.raw_data = [] mock_year.raw_data = [""] mock_form.__iter__ = Mock(return_value=iter([mock_month, mock_year])) mock_field = Mock() with self.assertRaises(StopValidation) as ite: validator(mock_form, mock_field) self.assertEqual("", str(ite.exception))
def test_month_year_optional_missing_year(self): validator = OptionalForm() mock_form = Mock() mock_month = Mock() mock_year = Mock() mock_month.raw_data = ["01"] mock_year.raw_data = [""] mock_form.__iter__ = Mock(return_value=iter([mock_month, mock_year])) mock_field = Mock() try: validator(mock_form, mock_field) except StopValidation: self.fail("Date that needs checking raised StopValidation")
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