In form field does not called any validators with required=False and empty value

31 views
Skip to first unread message

Lagovas Lagovas

unread,
Jan 15, 2014, 2:25:48 PM1/15/14
to django...@googlegroups.com
Example of code:
...
def test_field(self):
    incorrect_data = u""
    correct_data = u"this is correct data"

    field_instantiate_kwargs = {
       'min_length':10,
       'error_messages':{
       'min_length': u"some message",
       'required': u"required message"
       }
    }

    self.assertFieldOutput(
       UpperCaseCharField,
       {correct_data: correct_data.upper()},
       {incorrect_data: [u"some message",]},
       field_kwargs=field_instantiate_kwargs
    )

After that I take this errors:
  File "app/tests.py", line 47, in test_UpperCaseCharField field_kwargs=field_instantiate_kwargs File "app/test cases.py",
line 618, in assertFieldOutput optional.clean(input)
AssertionError: ValidationError not raised

I find in django source code this assertFieldOutput and place when raises error (https://github.com/django/django/blob/master/django/test/testcases.py):
with self.assertRaises(ValidationError) as context_manager:
    optional.clean(input)
optional - it's my field with required=False. django automatic test my field with required=False and wait that my code must raise ValidationError, when in base class Field returns "clean" data if input value are in empty_values before calling any validators (in my case must be called MinLengthValdator).

class Field(object):
...
   def run_validators(self, value):
      if value in self.empty_values:
         return
      errors = []
      for v in self.validators:
          try:
              v(value)
          except ValidationError as e:
              if hasattr(e, 'code') and e.code in self.error_messages:
              e.message = self.error_messages[e.code]
              errors.extend(e.error_list)
          if errors:
              raise ValidationError(errors)

    def clean(self, value):
        value = self.to_python(value)
        self.validate(value)
        self.run_validators(value)
       return value

What I done wrong and how i should use this assert function for testing my field? One of solution is override in custom field self.empty_values i think, but yet not sure.
Reply all
Reply to author
Forward
0 new messages