PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/regressiontests/forms/localflavor/utils.py

https://code.google.com/p/mango-py/
Python | 51 lines | 45 code | 4 blank | 2 comment | 0 complexity | 79d26e8bd6204cb882b3cc160a833039 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. from django.core.exceptions import ValidationError
  2. from django.core.validators import EMPTY_VALUES
  3. from django.test.utils import get_warnings_state, restore_warnings_state
  4. from django.utils.unittest import TestCase
  5. class LocalFlavorTestCase(TestCase):
  6. # NOTE: These are copied from the TestCase Django uses for tests which
  7. # access the database
  8. def save_warnings_state(self):
  9. self._warnings_state = get_warnings_state()
  10. def restore_warnings_state(self):
  11. restore_warnings_state(self._warnings_state)
  12. def assertFieldOutput(self, fieldclass, valid, invalid, field_args=[],
  13. field_kwargs={}, empty_value=u''):
  14. """
  15. Asserts that a field behaves correctly with various inputs.
  16. Args:
  17. fieldclass: the class of the field to be tested.
  18. valid: a dictionary mapping valid inputs to their expected
  19. cleaned values.
  20. invalid: a dictionary mapping invalid inputs to one or more
  21. raised error messages.
  22. field_args: the args passed to instantiate the field
  23. field_kwargs: the kwargs passed to instantiate the field
  24. empty_value: the expected clean output for inputs in EMPTY_VALUES
  25. """
  26. required = fieldclass(*field_args, **field_kwargs)
  27. optional = fieldclass(*field_args, **dict(field_kwargs, required=False))
  28. # test valid inputs
  29. for input, output in valid.items():
  30. self.assertEqual(required.clean(input), output)
  31. self.assertEqual(optional.clean(input), output)
  32. # test invalid inputs
  33. for input, errors in invalid.items():
  34. self.assertRaisesRegexp(ValidationError, unicode(errors),
  35. required.clean, input
  36. )
  37. self.assertRaisesRegexp(ValidationError, unicode(errors),
  38. optional.clean, input
  39. )
  40. # test required inputs
  41. error_required = u'This field is required'
  42. for e in EMPTY_VALUES:
  43. self.assertRaisesRegexp(ValidationError, error_required,
  44. required.clean, e)
  45. self.assertEqual(optional.clean(e), empty_value)