225: Added errors.check_type() and errors.check_isinstance() functions; added corresponding unit tests

This commit is contained in:
Jason Gerard DeRose
2008-08-29 23:53:04 +00:00
parent 44ff0b3d23
commit 2fa8d3be74
2 changed files with 108 additions and 16 deletions

View File

@@ -21,6 +21,7 @@
All custom errors raised by `ipalib` package. All custom errors raised by `ipalib` package.
""" """
TYPE_FORMAT = '%s: need a %r; got %r'
def raise_TypeError(name, type_, value): def raise_TypeError(name, type_, value):
""" """
@@ -30,9 +31,9 @@ def raise_TypeError(name, type_, value):
``name`` - The name (identifier) of the argument in question. ``name`` - The name (identifier) of the argument in question.
``type`` - The type expected for the arguement. ``type`` - The type expected for the argument.
``value`` - The value (of incorrect type) revieved for argument. ``value`` - The value (of incorrect type) passed as argument.
There is no edict that all TypeError should be raised with raise_TypeError, There is no edict that all TypeError should be raised with raise_TypeError,
but when it fits, use it... it makes the unit tests faster to write and but when it fits, use it... it makes the unit tests faster to write and
@@ -48,21 +49,40 @@ def raise_TypeError(name, type_, value):
TypeError: message: need a <type 'str'>; got u'Hello.' TypeError: message: need a <type 'str'>; got u'Hello.'
:param name: The name (identifier) of the argument in question. :param name: The name (identifier) of the argument in question.
:param type_: The type expected for the arguement. :param type_: The type expected for the argument.
:param value: The value (of incorrect type) revieved for argument. :param value: The value (of incorrect type) passed argument.
""" """
format = '%s: need a %r; got %r' assert type(name) is str, TYPE_FORMAT % ('name', str, name)
assert type(name) is str, format % ('name', str, name) assert type(type_) is type, TYPE_FORMAT % ('type_', type, type_)
assert type(type_) is type, format % ('type_', type, type_)
assert type(value) is not type_, 'value: %r is a %r' % (value, type_) assert type(value) is not type_, 'value: %r is a %r' % (value, type_)
e = TypeError(format % (name, type_, value)) e = TypeError(TYPE_FORMAT % (name, type_, value))
setattr(e, 'name', name) setattr(e, 'name', name)
setattr(e, 'type', type_) setattr(e, 'type', type_)
setattr(e, 'value', value) setattr(e, 'value', value)
raise e raise e
def check_type(name, type_, value, allow_None=False):
assert type(name) is str, TYPE_FORMAT % ('name', str, name)
assert type(type_) is type, TYPE_FORMAT % ('type_', type, type_)
assert type(allow_None) is bool, TYPE_FORMAT % ('allow_None', bool, allow_None)
if value is None and allow_None:
return
if type(value) is not type_:
raise_TypeError(name, type_, value)
def check_isinstance(name, type_, value, allow_None=False):
assert type(name) is str, TYPE_FORMAT % ('name', str, name)
assert type(type_) is type, TYPE_FORMAT % ('type_', type, type_)
assert type(allow_None) is bool, TYPE_FORMAT % ('allow_None', bool, allow_None)
if value is None and allow_None:
return
if not isinstance(value, type_):
raise_TypeError(name, type_, value)
class IPAError(Exception): class IPAError(Exception):
""" """
Use this base class for your custom IPA errors unless there is a Use this base class for your custom IPA errors unless there is a

View File

@@ -25,32 +25,104 @@ from tstutil import raises, ClassChecker
from ipalib import errors from ipalib import errors
type_format = '%s: need a %r; got %r'
def check_TypeError(f, name, type_, value, **kw):
e = raises(TypeError, f, name, type_, value, **kw)
assert e.name is name
assert e.type is type_
assert e.value is value
assert str(e) == type_format % (name, type_, value)
def test_raise_TypeError(): def test_raise_TypeError():
""" """
Tests the `errors.raise_TypeError` function. Tests the `errors.raise_TypeError` function.
""" """
f = errors.raise_TypeError f = errors.raise_TypeError
format = '%s: need a %r; got %r'
name = 'message' name = 'message'
type_ = unicode type_ = unicode
value = 'Hello.' value = 'Hello.'
e = raises(TypeError, f, name, type_, value)
assert e.name is name check_TypeError(f, name, type_, value)
assert e.type is type_
assert e.value is value
assert str(e) == format % (name, type_, value)
# name not an str: # name not an str:
fail = 42 fail = 42
e = raises(AssertionError, f, fail, type_, value) e = raises(AssertionError, f, fail, type_, value)
assert str(e) == format % ('name', str, fail) assert str(e) == type_format % ('name', str, fail)
# type_ not a type: # type_ not a type:
fail = unicode() fail = unicode()
e = raises(AssertionError, f, name, fail, value) e = raises(AssertionError, f, name, fail, value)
assert str(e) == format % ('type_', type, fail) assert str(e) == type_format % ('type_', type, fail)
# type(value) is type_: # type(value) is type_:
fail = u'How are you?' fail = u'How are you?'
e = raises(AssertionError, f, name, type_, fail) e = raises(AssertionError, f, name, type_, fail)
assert str(e) == 'value: %r is a %r' % (fail, type_) assert str(e) == 'value: %r is a %r' % (fail, type_)
def test_check_type():
"""
Tests the `errors.check_type` function.
"""
f = errors.check_type
name = 'greeting'
value = 'How are you?'
# Should pass:
f(name, str, value)
f(name, str, None, allow_None=True)
# Should raise TypeError
check_TypeError(f, name, str, None)
check_TypeError(f, name, basestring, value)
check_TypeError(f, name, unicode, value)
# name not an str
fail = unicode(name)
e = raises(AssertionError, f, fail, str, value)
assert str(e) == type_format % ('name', str, fail)
# type_ not a type:
fail = 42
e = raises(AssertionError, f, name, fail, value)
assert str(e) == type_format % ('type_', type, fail)
# allow_None not a bool:
fail = 0
e = raises(AssertionError, f, name, str, value, allow_None=fail)
assert str(e) == type_format % ('allow_None', bool, fail)
def test_check_isinstance():
"""
Tests the `errors.check_isinstance` function.
"""
f = errors.check_isinstance
name = 'greeting'
value = 'How are you?'
# Should pass:
f(name, str, value)
f(name, basestring, value)
f(name, str, None, allow_None=True)
# Should raise TypeError
check_TypeError(f, name, str, None)
check_TypeError(f, name, unicode, value)
# name not an str
fail = unicode(name)
e = raises(AssertionError, f, fail, str, value)
assert str(e) == type_format % ('name', str, fail)
# type_ not a type:
fail = 42
e = raises(AssertionError, f, name, fail, value)
assert str(e) == type_format % ('type_', type, fail)
# allow_None not a bool:
fail = 0
e = raises(AssertionError, f, name, str, value, allow_None=fail)
assert str(e) == type_format % ('allow_None', bool, fail)