2010-11-20 03:33:07 +08:00
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
from django.core.validators import EMPTY_VALUES
|
|
|
|
from django.utils.unittest import TestCase
|
|
|
|
|
|
|
|
|
|
|
|
class LocalFlavorTestCase(TestCase):
|
2010-12-19 04:30:05 +08:00
|
|
|
def assertFieldOutput(self, fieldclass, valid, invalid, field_args=[],
|
|
|
|
field_kwargs={}, empty_value=u''):
|
2010-11-20 03:33:07 +08:00
|
|
|
"""Asserts that a field behaves correctly with various inputs.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
fieldclass: the class of the field to be tested.
|
|
|
|
valid: a dictionary mapping valid inputs to their expected
|
|
|
|
cleaned values.
|
|
|
|
invalid: a dictionary mapping invalid inputs to one or more
|
|
|
|
raised error messages.
|
2010-12-19 04:30:05 +08:00
|
|
|
fieldargs: the args passed to instantiate the field
|
|
|
|
fieldkwargs: the kwargs passed to instantiate the field
|
|
|
|
emptyvalue: the expected clean output for inputs in EMPTY_VALUES
|
2010-11-20 03:33:07 +08:00
|
|
|
"""
|
2010-12-19 04:30:05 +08:00
|
|
|
required = fieldclass(*field_args, **field_kwargs)
|
|
|
|
optional = fieldclass(*field_args, required=False, **field_kwargs)
|
2010-11-20 03:33:07 +08:00
|
|
|
# test valid inputs
|
|
|
|
for input, output in valid.items():
|
|
|
|
self.assertEqual(required.clean(input), output)
|
|
|
|
self.assertEqual(optional.clean(input), output)
|
|
|
|
# test invalid inputs
|
|
|
|
for input, errors in invalid.items():
|
|
|
|
self.assertRaisesRegexp(ValidationError, unicode(errors),
|
|
|
|
required.clean, input
|
|
|
|
)
|
|
|
|
self.assertRaisesRegexp(ValidationError, unicode(errors),
|
|
|
|
optional.clean, input
|
|
|
|
)
|
|
|
|
# test required inputs
|
|
|
|
error_required = u'This field is required'
|
|
|
|
for e in EMPTY_VALUES:
|
|
|
|
self.assertRaisesRegexp(ValidationError, error_required,
|
2010-12-19 04:30:05 +08:00
|
|
|
required.clean, e)
|
|
|
|
self.assertEqual(optional.clean(e), empty_value)
|