2011-05-31 14:02:22 +00:00
|
|
|
from __future__ import with_statement
|
|
|
|
|
2010-11-19 19:33:07 +00:00
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
from django.core.validators import EMPTY_VALUES
|
2011-05-03 11:52:04 +00:00
|
|
|
from django.forms.fields import CharField
|
2010-12-18 20:31:00 +00:00
|
|
|
from django.test.utils import get_warnings_state, restore_warnings_state
|
2010-11-19 19:33:07 +00:00
|
|
|
from django.utils.unittest import TestCase
|
|
|
|
|
|
|
|
|
|
|
|
class LocalFlavorTestCase(TestCase):
|
2010-12-18 20:31:00 +00:00
|
|
|
# NOTE: These are copied from the TestCase Django uses for tests which
|
|
|
|
# access the database
|
|
|
|
def save_warnings_state(self):
|
|
|
|
self._warnings_state = get_warnings_state()
|
|
|
|
|
|
|
|
def restore_warnings_state(self):
|
|
|
|
restore_warnings_state(self._warnings_state)
|
|
|
|
|
2011-05-03 11:52:04 +00:00
|
|
|
def assertFieldOutput(self, fieldclass, valid, invalid, field_args=None,
|
|
|
|
field_kwargs=None, empty_value=u''):
|
2010-12-26 11:44:37 +00:00
|
|
|
"""
|
|
|
|
Asserts that a field behaves correctly with various inputs.
|
2010-11-19 19:33:07 +00:00
|
|
|
|
|
|
|
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-26 11:44:37 +00:00
|
|
|
field_args: the args passed to instantiate the field
|
|
|
|
field_kwargs: the kwargs passed to instantiate the field
|
|
|
|
empty_value: the expected clean output for inputs in EMPTY_VALUES
|
|
|
|
|
2010-11-19 19:33:07 +00:00
|
|
|
"""
|
2011-05-03 11:52:04 +00:00
|
|
|
if field_args is None:
|
|
|
|
field_args = []
|
|
|
|
if field_kwargs is None:
|
|
|
|
field_kwargs = {}
|
2010-12-18 20:30:05 +00:00
|
|
|
required = fieldclass(*field_args, **field_kwargs)
|
2010-12-19 02:00:34 +00:00
|
|
|
optional = fieldclass(*field_args, **dict(field_kwargs, required=False))
|
2010-11-19 19:33:07 +00: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():
|
2011-05-31 14:02:22 +00:00
|
|
|
with self.assertRaises(ValidationError) as context_manager:
|
|
|
|
required.clean(input)
|
|
|
|
self.assertEqual(context_manager.exception.messages, errors)
|
|
|
|
|
|
|
|
with self.assertRaises(ValidationError) as context_manager:
|
|
|
|
optional.clean(input)
|
|
|
|
self.assertEqual(context_manager.exception.messages, errors)
|
2010-11-19 19:33:07 +00:00
|
|
|
# test required inputs
|
2011-05-31 14:02:22 +00:00
|
|
|
error_required = [u'This field is required.']
|
2010-11-19 19:33:07 +00:00
|
|
|
for e in EMPTY_VALUES:
|
2011-05-31 14:02:22 +00:00
|
|
|
with self.assertRaises(ValidationError) as context_manager:
|
|
|
|
required.clean(e)
|
|
|
|
self.assertEqual(context_manager.exception.messages, error_required)
|
2010-12-18 20:30:05 +00:00
|
|
|
self.assertEqual(optional.clean(e), empty_value)
|
2011-05-03 11:52:04 +00:00
|
|
|
# test that max_length and min_length are always accepted
|
|
|
|
if issubclass(fieldclass, CharField):
|
|
|
|
field_kwargs.update({'min_length':2, 'max_length':20})
|
|
|
|
self.assertTrue(isinstance(fieldclass(*field_args, **field_kwargs), fieldclass))
|