Permalink
Browse files

Merge pull request #88 from Cito/develop

Fix issue reported by Moritz.
  • Loading branch information...
ralphbean committed Jun 12, 2013
2 parents 5add35c + a9d4894 commit 2416cefb82ee7805308c61af2bcb4d179a3d0c7c
Showing with 17 additions and 13 deletions.
  1. +3 −3 tests/test_validation.py
  2. +11 −9 tw2/core/testbase/base.py
  3. +3 −1 tw2/core/validation.py
@@ -438,9 +438,9 @@ def test_required(self):
class TestBoolValidator(tb.ValidatorTest):
validator = BoolValidator
to_python_attrs = [{}, {}, {}, {}, {}, {}, {}, {}, {},]
to_python_params = ['on', 'yes', 'true', '1', 1, True, 'Yes', 'True', 'off']
to_python_expected = [True, True, True, True, True, True, True, True, False]
to_python_attrs = [{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}]
to_python_params = ['on', 'yes', 'true', '1', 1, True, 'Yes', 'True', 'off', '', None]
to_python_expected = [True, True, True, True, True, True, True, True, False, False, False]
class TestOneOfValidator(tb.ValidatorTest):
validator = OneOfValidator
@@ -189,8 +189,8 @@ def test_display(self):
return
for engine in self._get_all_possible_engines():
yield self._check_rendering_vs_expected, engine, \
self.attrs, self.params, self.expected
self._check_rendering_vs_expected(engine,
self.attrs, self.params, self.expected)
def _check_validation(self, attrs, params, expected, raises=None):
if raises is not None:
@@ -209,8 +209,8 @@ def test_validate(self):
params[0] = self.attrs
if len(params) < 4:
params.append(None)
yield self._check_validation, params[0], \
params[1], params[2], params[3]
self._check_validation(params[0],
params[1], params[2], params[3])
class ValidatorTest(unittest.TestCase):
@@ -315,7 +315,7 @@ def test_validate(self):
if self.expected:
triples = six.moves.zip(self.attrs, self.params, self.expected)
for attrs, params, expected in triples:
yield self._check_validation, attrs, params, expected
self._check_validation(attrs, params, expected)
def test_from_python(self):
if self.from_python_expected:
@@ -325,19 +325,21 @@ def test_from_python(self):
self.from_python_expected,
)
for attrs, params, expected in triples:
yield self._check_validation, attrs, params, \
expected, 'from_python'
self._check_validation(attrs, params,
expected, 'from_python')
def test_to_python(self):
name = self.__class__.__name__
tbv = name == 'TestBoolValidator'
if self.to_python_expected:
triples = six.moves.zip(
self.to_python_attrs,
self.to_python_params,
self.to_python_expected,
)
for attrs, params, expected in triples:
yield self._check_validation, attrs, params, \
expected, 'to_python'
self._check_validation(attrs, params,
expected, 'to_python')
import webob as wo
import webtest as wt
@@ -212,6 +212,7 @@ class Validator(six.with_metaclass(ValidatorMeta, object)):
}
required = False
strip = True
if_empty = None
def __init__(self, **kw):
for k in kw:
@@ -222,7 +223,7 @@ def to_python(self, value, state=None):
if self._is_empty(value):
if self.required:
raise ValidationError('required', self)
return None
return self.if_empty
if self.strip and isinstance(value, six.string_types):
value = value.strip()
value = self._convert_to_python(value, state)
@@ -398,6 +399,7 @@ class BoolValidator(RangeValidator):
msgs = {
'required': ('bool_required', _('You must select this'))
}
if_empty = False
def _convert_to_python(self, value, state=None):
return str(value).lower() in ('on', 'yes', 'true', '1', 'y', 't')

0 comments on commit 2416cef

Please sign in to comment.