PageRenderTime 224ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/ptah/form/tests/test_validator.py

https://github.com/ptahproject/ptah
Python | 209 lines | 196 code | 13 blank | 0 comment | 4 complexity | 689974dcc7cf90b8fd4d176d766f0800 MD5 | raw file
  1. from ptah.testing import TestCase, BaseTestCase
  2. def invalid_exc(func, *arg, **kw):
  3. from ptah.form import Invalid
  4. try:
  5. func(*arg, **kw)
  6. except Invalid as e:
  7. return e
  8. else:
  9. raise AssertionError('Invalid not raised')
  10. class DummyValidator(object):
  11. def __init__(self, msg=None):
  12. self.msg = msg
  13. def __call__(self, field, value):
  14. from ptah.form import Invalid
  15. if self.msg:
  16. raise Invalid(self.msg, field)
  17. class TestAll(TestCase):
  18. def _makeOne(self, validators):
  19. from ptah.form import All
  20. return All(*validators)
  21. def test_success(self):
  22. validator1 = DummyValidator()
  23. validator2 = DummyValidator()
  24. validator = self._makeOne([validator1, validator2])
  25. self.assertEqual(validator(None, None), None)
  26. def test_failure(self):
  27. validator1 = DummyValidator('msg1')
  28. validator2 = DummyValidator('msg2')
  29. validator = self._makeOne([validator1, validator2])
  30. e = invalid_exc(validator, None, None)
  31. self.assertEqual(e.msg, ['msg1', 'msg2'])
  32. class TestFunction(TestCase):
  33. def _makeOne(self, *arg, **kw):
  34. from ptah.form import Function
  35. return Function(*arg, **kw)
  36. def test_success_function_returns_True(self):
  37. validator = self._makeOne(lambda x: True)
  38. self.assertEqual(validator(None, None), None)
  39. def test_fail_function_returns_empty_string(self):
  40. validator = self._makeOne(lambda x: '')
  41. e = invalid_exc(validator, None, None)
  42. self.assertEqual(e.msg, 'Invalid value')
  43. def test_fail_function_returns_False(self):
  44. validator = self._makeOne(lambda x: False)
  45. e = invalid_exc(validator, None, None)
  46. self.assertEqual(e.msg, 'Invalid value')
  47. def test_fail_function_returns_string(self):
  48. validator = self._makeOne(lambda x: 'fail')
  49. e = invalid_exc(validator, None, None)
  50. self.assertEqual(e.msg, 'fail')
  51. def test_propagation(self):
  52. validator = self._makeOne(lambda x: 'a' in x, 'msg')
  53. self.assertRaises(TypeError, validator, None, None)
  54. class TestRange(TestCase):
  55. def _makeOne(self, **kw):
  56. from ptah.form import Range
  57. return Range(**kw)
  58. def test_success_no_bounds(self):
  59. validator = self._makeOne()
  60. self.assertEqual(validator(None, 1), None)
  61. def test_success_upper_bound_only(self):
  62. validator = self._makeOne(max=1)
  63. self.assertEqual(validator(None, -1), None)
  64. def test_success_minimum_bound_only(self):
  65. validator = self._makeOne(min=0)
  66. self.assertEqual(validator(None, 1), None)
  67. def test_success_min_and_max(self):
  68. validator = self._makeOne(min=1, max=1)
  69. self.assertEqual(validator(None, 1), None)
  70. def test_min_failure(self):
  71. validator = self._makeOne(min=1)
  72. e = invalid_exc(validator, None, 0)
  73. self.assertEqual(e.msg.interpolate(), '0 is less than minimum value 1')
  74. def test_min_failure_msg_override(self):
  75. validator = self._makeOne(min=1, min_err='wrong')
  76. e = invalid_exc(validator, None, 0)
  77. self.assertEqual(e.msg, 'wrong')
  78. def test_max_failure(self):
  79. validator = self._makeOne(max=1)
  80. e = invalid_exc(validator, None, 2)
  81. self.assertEqual(e.msg.interpolate(),
  82. '2 is greater than maximum value 1')
  83. def test_max_failure_msg_override(self):
  84. validator = self._makeOne(max=1, max_err='wrong')
  85. e = invalid_exc(validator, None, 2)
  86. self.assertEqual(e.msg, 'wrong')
  87. class TestRegex(TestCase):
  88. def _makeOne(self, pattern):
  89. from ptah.form import Regex
  90. return Regex(pattern)
  91. def test_valid_regex(self):
  92. self.assertEqual(self._makeOne('a')(None, 'a'), None)
  93. self.assertEqual(self._makeOne('[0-9]+')(None, '1111'), None)
  94. self.assertEqual(self._makeOne('')(None, ''), None)
  95. self.assertEqual(self._makeOne('.*')(None, ''), None)
  96. def test_invalid_regexs(self):
  97. from ptah.form import Invalid
  98. self.assertRaises(Invalid, self._makeOne('[0-9]+'), None, 'a')
  99. self.assertRaises(Invalid, self._makeOne('a{2,4}'), None, 'ba')
  100. def test_regex_not_string(self):
  101. from ptah.form import Invalid
  102. import re
  103. regex = re.compile('[0-9]+')
  104. self.assertEqual(self._makeOne(regex)(None, '01'), None)
  105. self.assertRaises(Invalid, self._makeOne(regex), None, 't')
  106. class TestEmail(TestCase):
  107. def _makeOne(self):
  108. from ptah.form import Email
  109. return Email()
  110. def test_valid_emails(self):
  111. validator = self._makeOne()
  112. self.assertEqual(validator(None, 'me@here.com'), None)
  113. self.assertEqual(validator(None, 'me1@here1.com'), None)
  114. self.assertEqual(validator(None, 'name@here1.us'), None)
  115. self.assertEqual(validator(None, 'name@here1.info'), None)
  116. self.assertEqual(validator(None, 'foo@bar.baz.biz'), None)
  117. def test_empty_email(self):
  118. validator = self._makeOne()
  119. e = invalid_exc(validator, None, '')
  120. self.assertEqual(e.msg, 'Invalid email address')
  121. def test_invalid_emails(self):
  122. validator = self._makeOne()
  123. from ptah.form import Invalid
  124. self.assertRaises(Invalid, validator, None, 'me@here.')
  125. self.assertRaises(Invalid, validator, None, 'name@here.comcom')
  126. self.assertRaises(Invalid, validator, None, '@here.us')
  127. self.assertRaises(Invalid, validator, None, '(name)@here.info')
  128. class TestLength(TestCase):
  129. def _makeOne(self, min=None, max=None):
  130. from ptah.form import Length
  131. return Length(min=min, max=max)
  132. def test_success_no_bounds(self):
  133. validator = self._makeOne()
  134. self.assertEqual(validator(None, ''), None)
  135. def test_success_upper_bound_only(self):
  136. validator = self._makeOne(max=1)
  137. self.assertEqual(validator(None, 'a'), None)
  138. def test_success_minimum_bound_only(self):
  139. validator = self._makeOne(min=0)
  140. self.assertEqual(validator(None, ''), None)
  141. def test_success_min_and_max(self):
  142. validator = self._makeOne(min=1, max=1)
  143. self.assertEqual(validator(None, 'a'), None)
  144. def test_min_failure(self):
  145. validator = self._makeOne(min=1)
  146. e = invalid_exc(validator, None, '')
  147. self.assertEqual(e.msg.interpolate(), 'Shorter than minimum length 1')
  148. def test_max_failure(self):
  149. validator = self._makeOne(max=1)
  150. e = invalid_exc(validator, None, 'ab')
  151. self.assertEqual(e.msg.interpolate(), 'Longer than maximum length 1')
  152. class TestOneOf(BaseTestCase):
  153. _includes = ['ptah.form']
  154. def _makeOne(self, values):
  155. from ptah.form import OneOf
  156. return OneOf(values)
  157. def test_success(self):
  158. validator = self._makeOne([1])
  159. self.assertEqual(validator(None, 1), None)
  160. def test_failure(self):
  161. validator = self._makeOne([1, 2])
  162. e = invalid_exc(validator, None, None)
  163. self.assertEqual(str(e), '"None" is not one of 1, 2')