diff --git a/ckanext/example_ivalidators/plugin.py b/ckanext/example_ivalidators/plugin.py index 7d559b5c2ef..8597b04c511 100644 --- a/ckanext/example_ivalidators/plugin.py +++ b/ckanext/example_ivalidators/plugin.py @@ -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) diff --git a/ckanext/example_ivalidators/tests/test_ivalidators.py b/ckanext/example_ivalidators/tests/test_ivalidators.py index ca8d292397f..3d41161288a 100644 --- a/ckanext/example_ivalidators/tests/test_ivalidators.py +++ b/ckanext/example_ivalidators/tests/test_ivalidators.py @@ -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')