Skip to content

Commit

Permalink
[#3865] IValidators override tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wardi committed Oct 11, 2017
1 parent be4a086 commit 215b5ef
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
16 changes: 13 additions & 3 deletions ckanext/example_ivalidators/plugin.py
Expand Up @@ -9,16 +9,26 @@ class ExampleIValidatorsPlugin(plugins.SingletonPlugin):

def get_validators(self):
return {
'equals_fortytwo': equals_fortytwo,
'negate': negate,
u'equals_fortytwo': equals_fortytwo,
u'negate': negate,
u'unicode_only': unicode_please,
}


def equals_fortytwo(value):
if value != 42:
raise Invalid('not 42')
raise Invalid(u'not 42')
return value


def negate(value):
return -value


def unicode_please(value):
if isinstance(value, bytes):
try:
return value.decode(u'utf8')
except UnicodeDecodeError:
return value.decode(u'cp1252')
return unicode(value)
10 changes: 10 additions & 0 deletions ckanext/example_ivalidators/tests/test_ivalidators.py
Expand Up @@ -27,3 +27,13 @@ def test_custom_validator_passes(self):
def test_custom_converter_converts(self):
c = get_validator('negate')
assert_equals(c(19), -19)

def test_overridden_validator(self):
v = get_validator('unicode_only')
assert_equals(u'Hola cómo estás', v(b'Hola c\xf3mo est\xe1s'))


class TestNoIValidators(object):
def test_no_overridden_validator(self):
v = get_validator('unicode_only')
assert_raises(Invalid, v, b'Hola c\xf3mo est\xe1s')

0 comments on commit 215b5ef

Please sign in to comment.