Wednesday, January 15, 2014

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

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.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f1fcf499-3746-457b-ae9a-a142dd40d8d4%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate