PageRenderTime 50ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/django-1.2/tests/regressiontests/forms/tests/error_messages.py

https://github.com/theosp/google_appengine
Python | 253 lines | 249 code | 3 blank | 1 comment | 2 complexity | 8dc92e352ed43d0c1b767e346bf9d92a MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. import unittest
  3. from django.core.files.uploadedfile import SimpleUploadedFile
  4. from django.forms import *
  5. from django.test import TestCase
  6. from django.utils.safestring import mark_safe
  7. class AssertFormErrorsMixin(object):
  8. def assertFormErrors(self, expected, the_callable, *args, **kwargs):
  9. try:
  10. the_callable(*args, **kwargs)
  11. self.fail("Testing the 'clean' method on %s failed to raise a ValidationError.")
  12. except ValidationError, e:
  13. self.assertEqual(e.messages, expected)
  14. class FormsErrorMessagesTestCase(unittest.TestCase, AssertFormErrorsMixin):
  15. def test_charfield(self):
  16. e = {
  17. 'required': 'REQUIRED',
  18. 'min_length': 'LENGTH %(show_value)s, MIN LENGTH %(limit_value)s',
  19. 'max_length': 'LENGTH %(show_value)s, MAX LENGTH %(limit_value)s',
  20. }
  21. f = CharField(min_length=5, max_length=10, error_messages=e)
  22. self.assertFormErrors([u'REQUIRED'], f.clean, '')
  23. self.assertFormErrors([u'LENGTH 4, MIN LENGTH 5'], f.clean, '1234')
  24. self.assertFormErrors([u'LENGTH 11, MAX LENGTH 10'], f.clean, '12345678901')
  25. def test_integerfield(self):
  26. e = {
  27. 'required': 'REQUIRED',
  28. 'invalid': 'INVALID',
  29. 'min_value': 'MIN VALUE IS %(limit_value)s',
  30. 'max_value': 'MAX VALUE IS %(limit_value)s',
  31. }
  32. f = IntegerField(min_value=5, max_value=10, error_messages=e)
  33. self.assertFormErrors([u'REQUIRED'], f.clean, '')
  34. self.assertFormErrors([u'INVALID'], f.clean, 'abc')
  35. self.assertFormErrors([u'MIN VALUE IS 5'], f.clean, '4')
  36. self.assertFormErrors([u'MAX VALUE IS 10'], f.clean, '11')
  37. def test_floatfield(self):
  38. e = {
  39. 'required': 'REQUIRED',
  40. 'invalid': 'INVALID',
  41. 'min_value': 'MIN VALUE IS %(limit_value)s',
  42. 'max_value': 'MAX VALUE IS %(limit_value)s',
  43. }
  44. f = FloatField(min_value=5, max_value=10, error_messages=e)
  45. self.assertFormErrors([u'REQUIRED'], f.clean, '')
  46. self.assertFormErrors([u'INVALID'], f.clean, 'abc')
  47. self.assertFormErrors([u'MIN VALUE IS 5'], f.clean, '4')
  48. self.assertFormErrors([u'MAX VALUE IS 10'], f.clean, '11')
  49. def test_decimalfield(self):
  50. e = {
  51. 'required': 'REQUIRED',
  52. 'invalid': 'INVALID',
  53. 'min_value': 'MIN VALUE IS %(limit_value)s',
  54. 'max_value': 'MAX VALUE IS %(limit_value)s',
  55. 'max_digits': 'MAX DIGITS IS %s',
  56. 'max_decimal_places': 'MAX DP IS %s',
  57. 'max_whole_digits': 'MAX DIGITS BEFORE DP IS %s',
  58. }
  59. f = DecimalField(min_value=5, max_value=10, error_messages=e)
  60. self.assertFormErrors([u'REQUIRED'], f.clean, '')
  61. self.assertFormErrors([u'INVALID'], f.clean, 'abc')
  62. self.assertFormErrors([u'MIN VALUE IS 5'], f.clean, '4')
  63. self.assertFormErrors([u'MAX VALUE IS 10'], f.clean, '11')
  64. f2 = DecimalField(max_digits=4, decimal_places=2, error_messages=e)
  65. self.assertFormErrors([u'MAX DIGITS IS 4'], f2.clean, '123.45')
  66. self.assertFormErrors([u'MAX DP IS 2'], f2.clean, '1.234')
  67. self.assertFormErrors([u'MAX DIGITS BEFORE DP IS 2'], f2.clean, '123.4')
  68. def test_datefield(self):
  69. e = {
  70. 'required': 'REQUIRED',
  71. 'invalid': 'INVALID',
  72. }
  73. f = DateField(error_messages=e)
  74. self.assertFormErrors([u'REQUIRED'], f.clean, '')
  75. self.assertFormErrors([u'INVALID'], f.clean, 'abc')
  76. def test_timefield(self):
  77. e = {
  78. 'required': 'REQUIRED',
  79. 'invalid': 'INVALID',
  80. }
  81. f = TimeField(error_messages=e)
  82. self.assertFormErrors([u'REQUIRED'], f.clean, '')
  83. self.assertFormErrors([u'INVALID'], f.clean, 'abc')
  84. def test_datetimefield(self):
  85. e = {
  86. 'required': 'REQUIRED',
  87. 'invalid': 'INVALID',
  88. }
  89. f = DateTimeField(error_messages=e)
  90. self.assertFormErrors([u'REQUIRED'], f.clean, '')
  91. self.assertFormErrors([u'INVALID'], f.clean, 'abc')
  92. def test_regexfield(self):
  93. e = {
  94. 'required': 'REQUIRED',
  95. 'invalid': 'INVALID',
  96. 'min_length': 'LENGTH %(show_value)s, MIN LENGTH %(limit_value)s',
  97. 'max_length': 'LENGTH %(show_value)s, MAX LENGTH %(limit_value)s',
  98. }
  99. f = RegexField(r'^\d+$', min_length=5, max_length=10, error_messages=e)
  100. self.assertFormErrors([u'REQUIRED'], f.clean, '')
  101. self.assertFormErrors([u'INVALID'], f.clean, 'abcde')
  102. self.assertFormErrors([u'LENGTH 4, MIN LENGTH 5'], f.clean, '1234')
  103. self.assertFormErrors([u'LENGTH 11, MAX LENGTH 10'], f.clean, '12345678901')
  104. def test_emailfield(self):
  105. e = {
  106. 'required': 'REQUIRED',
  107. 'invalid': 'INVALID',
  108. 'min_length': 'LENGTH %(show_value)s, MIN LENGTH %(limit_value)s',
  109. 'max_length': 'LENGTH %(show_value)s, MAX LENGTH %(limit_value)s',
  110. }
  111. f = EmailField(min_length=8, max_length=10, error_messages=e)
  112. self.assertFormErrors([u'REQUIRED'], f.clean, '')
  113. self.assertFormErrors([u'INVALID'], f.clean, 'abcdefgh')
  114. self.assertFormErrors([u'LENGTH 7, MIN LENGTH 8'], f.clean, 'a@b.com')
  115. self.assertFormErrors([u'LENGTH 11, MAX LENGTH 10'], f.clean, 'aye@bee.com')
  116. def test_filefield(self):
  117. e = {
  118. 'required': 'REQUIRED',
  119. 'invalid': 'INVALID',
  120. 'missing': 'MISSING',
  121. 'empty': 'EMPTY FILE',
  122. }
  123. f = FileField(error_messages=e)
  124. self.assertFormErrors([u'REQUIRED'], f.clean, '')
  125. self.assertFormErrors([u'INVALID'], f.clean, 'abc')
  126. self.assertFormErrors([u'EMPTY FILE'], f.clean, SimpleUploadedFile('name', None))
  127. self.assertFormErrors([u'EMPTY FILE'], f.clean, SimpleUploadedFile('name', ''))
  128. def test_urlfield(self):
  129. e = {
  130. 'required': 'REQUIRED',
  131. 'invalid': 'INVALID',
  132. 'invalid_link': 'INVALID LINK',
  133. }
  134. f = URLField(verify_exists=True, error_messages=e)
  135. self.assertFormErrors([u'REQUIRED'], f.clean, '')
  136. self.assertFormErrors([u'INVALID'], f.clean, 'abc.c')
  137. self.assertFormErrors([u'INVALID LINK'], f.clean, 'http://www.broken.djangoproject.com')
  138. def test_booleanfield(self):
  139. e = {
  140. 'required': 'REQUIRED',
  141. }
  142. f = BooleanField(error_messages=e)
  143. self.assertFormErrors([u'REQUIRED'], f.clean, '')
  144. def test_choicefield(self):
  145. e = {
  146. 'required': 'REQUIRED',
  147. 'invalid_choice': '%(value)s IS INVALID CHOICE',
  148. }
  149. f = ChoiceField(choices=[('a', 'aye')], error_messages=e)
  150. self.assertFormErrors([u'REQUIRED'], f.clean, '')
  151. self.assertFormErrors([u'b IS INVALID CHOICE'], f.clean, 'b')
  152. def test_multiplechoicefield(self):
  153. e = {
  154. 'required': 'REQUIRED',
  155. 'invalid_choice': '%(value)s IS INVALID CHOICE',
  156. 'invalid_list': 'NOT A LIST',
  157. }
  158. f = MultipleChoiceField(choices=[('a', 'aye')], error_messages=e)
  159. self.assertFormErrors([u'REQUIRED'], f.clean, '')
  160. self.assertFormErrors([u'NOT A LIST'], f.clean, 'b')
  161. self.assertFormErrors([u'b IS INVALID CHOICE'], f.clean, ['b'])
  162. def test_splitdatetimefield(self):
  163. e = {
  164. 'required': 'REQUIRED',
  165. 'invalid_date': 'INVALID DATE',
  166. 'invalid_time': 'INVALID TIME',
  167. }
  168. f = SplitDateTimeField(error_messages=e)
  169. self.assertFormErrors([u'REQUIRED'], f.clean, '')
  170. self.assertFormErrors([u'INVALID DATE', u'INVALID TIME'], f.clean, ['a', 'b'])
  171. def test_ipaddressfield(self):
  172. e = {
  173. 'required': 'REQUIRED',
  174. 'invalid': 'INVALID IP ADDRESS',
  175. }
  176. f = IPAddressField(error_messages=e)
  177. self.assertFormErrors([u'REQUIRED'], f.clean, '')
  178. self.assertFormErrors([u'INVALID IP ADDRESS'], f.clean, '127.0.0')
  179. def test_subclassing_errorlist(self):
  180. class TestForm(Form):
  181. first_name = CharField()
  182. last_name = CharField()
  183. birthday = DateField()
  184. def clean(self):
  185. raise ValidationError("I like to be awkward.")
  186. class CustomErrorList(util.ErrorList):
  187. def __unicode__(self):
  188. return self.as_divs()
  189. def as_divs(self):
  190. if not self: return u''
  191. return mark_safe(u'<div class="error">%s</div>' % ''.join([u'<p>%s</p>' % e for e in self]))
  192. # This form should print errors the default way.
  193. form1 = TestForm({'first_name': 'John'})
  194. self.assertEqual(str(form1['last_name'].errors), '<ul class="errorlist"><li>This field is required.</li></ul>')
  195. self.assertEqual(str(form1.errors['__all__']), '<ul class="errorlist"><li>I like to be awkward.</li></ul>')
  196. # This one should wrap error groups in the customized way.
  197. form2 = TestForm({'first_name': 'John'}, error_class=CustomErrorList)
  198. self.assertEqual(str(form2['last_name'].errors), '<div class="error"><p>This field is required.</p></div>')
  199. self.assertEqual(str(form2.errors['__all__']), '<div class="error"><p>I like to be awkward.</p></div>')
  200. class ModelChoiceFieldErrorMessagesTestCase(TestCase, AssertFormErrorsMixin):
  201. def test_modelchoicefield(self):
  202. # Create choices for the model choice field tests below.
  203. from regressiontests.forms.models import ChoiceModel
  204. c1 = ChoiceModel.objects.create(pk=1, name='a')
  205. c2 = ChoiceModel.objects.create(pk=2, name='b')
  206. c3 = ChoiceModel.objects.create(pk=3, name='c')
  207. # ModelChoiceField
  208. e = {
  209. 'required': 'REQUIRED',
  210. 'invalid_choice': 'INVALID CHOICE',
  211. }
  212. f = ModelChoiceField(queryset=ChoiceModel.objects.all(), error_messages=e)
  213. self.assertFormErrors([u'REQUIRED'], f.clean, '')
  214. self.assertFormErrors([u'INVALID CHOICE'], f.clean, '4')
  215. # ModelMultipleChoiceField
  216. e = {
  217. 'required': 'REQUIRED',
  218. 'invalid_choice': '%s IS INVALID CHOICE',
  219. 'list': 'NOT A LIST OF VALUES',
  220. }
  221. f = ModelMultipleChoiceField(queryset=ChoiceModel.objects.all(), error_messages=e)
  222. self.assertFormErrors([u'REQUIRED'], f.clean, '')
  223. self.assertFormErrors([u'NOT A LIST OF VALUES'], f.clean, '3')
  224. self.assertFormErrors([u'4 IS INVALID CHOICE'], f.clean, ['4'])