parts/django/tests/regressiontests/forms/localflavor/utils.py
changeset 307 c6bca38c1cbf
equal deleted inserted replaced
306:5ff1fc726848 307:c6bca38c1cbf
       
     1 from unittest import TestCase
       
     2 
       
     3 from django.core.exceptions import ValidationError
       
     4 from django.core.validators import EMPTY_VALUES
       
     5 
       
     6 
       
     7 class LocalFlavorTestCase(TestCase):
       
     8     def assertFieldOutput(self, fieldclass, valid, invalid, field_args=[],
       
     9         field_kwargs={}, empty_value=u''):
       
    10         """Asserts that a field behaves correctly with various inputs.
       
    11 
       
    12         Args:
       
    13             fieldclass: the class of the field to be tested.
       
    14             valid: a dictionary mapping valid inputs to their expected
       
    15                     cleaned values.
       
    16             invalid: a dictionary mapping invalid inputs to one or more
       
    17                     raised error messages.
       
    18             fieldargs: the args passed to instantiate the field
       
    19             fieldkwargs: the kwargs passed to instantiate the field
       
    20             emptyvalue: the expected clean output for inputs in EMPTY_VALUES
       
    21         """
       
    22         required = fieldclass(*field_args, **field_kwargs)
       
    23         optional = fieldclass(*field_args, **dict(field_kwargs, required=False))
       
    24         # test valid inputs
       
    25         for input, output in valid.items():
       
    26             self.assertEqual(required.clean(input), output)
       
    27             self.assertEqual(optional.clean(input), output)
       
    28         # test invalid inputs
       
    29         for input, errors in invalid.items():
       
    30             try:
       
    31                 required.clean(input)
       
    32             except ValidationError, e:
       
    33                 self.assertEqual(errors, e.messages)
       
    34             else:
       
    35                 self.fail()
       
    36             try:
       
    37                 optional.clean(input)
       
    38             except ValidationError, e:
       
    39                 self.assertEqual(errors, e.messages)
       
    40             else:
       
    41                 self.fail()
       
    42         # test required inputs
       
    43         error_required = [u'This field is required.']
       
    44         for val in EMPTY_VALUES:
       
    45             try:
       
    46                 required.clean(val)
       
    47             except ValidationError, e:
       
    48                 self.assertEqual(error_required, e.messages)
       
    49             else:
       
    50                 self.fail()
       
    51             self.assertEqual(optional.clean(val), empty_value)