diff options
Diffstat (limited to 'ipalib')
-rw-r--r-- | ipalib/errors.py | 2 | ||||
-rw-r--r-- | ipalib/tests/test_errors.py | 33 |
2 files changed, 35 insertions, 0 deletions
diff --git a/ipalib/errors.py b/ipalib/errors.py index eb08a7be..cf213d70 100644 --- a/ipalib/errors.py +++ b/ipalib/errors.py @@ -119,6 +119,8 @@ class ValidationError(IPAError): :param error: The error message describing the failure. :param index: If multivalue, index of value in multivalue tuple """ + assert type(name) is str + assert index is None or (type(index) is int and index >= 0) self.name = name self.value = value self.error = error diff --git a/ipalib/tests/test_errors.py b/ipalib/tests/test_errors.py index b0b5483c..83dc6e6e 100644 --- a/ipalib/tests/test_errors.py +++ b/ipalib/tests/test_errors.py @@ -160,3 +160,36 @@ class test_IPAError(ClassChecker): e = custom_error(*args) assert e.args == args assert str(e) == f % args + + +class test_ValidationError(ClassChecker): + """ + Tests the `errors.ValidationError` exception. + """ + _cls = errors.ValidationError + + def test_class(self): + assert self.cls.__bases__ == (errors.IPAError,) + + def test_init(self): + """ + Tests the `errors.ValidationError.__init__` method. + """ + name = 'login' + value = 'Whatever' + error = 'Must be lowercase.' + for index in (None, 3): + e = self.cls(name, value, error, index=index) + assert e.name is name + assert e.value is value + assert e.error is error + assert e.index is index + assert str(e) == 'invalid %r value %r: %s' % (name, value, error) + # Check that index default is None: + assert self.cls(name, value, error).index is None + # Check non str name raises AssertionError: + raises(AssertionError, self.cls, unicode(name), value, error) + # Check non int index raises AssertionError: + raises(AssertionError, self.cls, name, value, error, index=5.0) + # Check negative index raises AssertionError: + raises(AssertionError, self.cls, name, value, error, index=-2) |