From 4d1681176afc45c57fb4316892f939bda1bacf1d Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Thu, 18 Dec 2008 02:08:41 -0700 Subject: [PATCH] New Param: added unit tests for TypeError cases in DefaultFrom.__init__() --- ipalib/constants.py | 2 +- ipalib/parameter.py | 8 ++++++-- tests/test_ipalib/test_parameter.py | 8 ++++++++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/ipalib/constants.py b/ipalib/constants.py index ad1e3f7cb..ef2aef72c 100644 --- a/ipalib/constants.py +++ b/ipalib/constants.py @@ -29,7 +29,7 @@ NULLS = (None, '', u'', tuple(), []) TYPE_ERROR = '%s: need a %r; got %r (which is a %r)' -CALLABLE_ERROR = '%s: need a callable; got %r (a %r)' +CALLABLE_ERROR = '%s: need a callable; got %r (which is a %r)' # Used for a tab (or indentation level) when formatting for CLI: diff --git a/ipalib/parameter.py b/ipalib/parameter.py index ca578cd98..fca95b0ef 100644 --- a/ipalib/parameter.py +++ b/ipalib/parameter.py @@ -103,7 +103,9 @@ class DefaultFrom(ReadOnly): :param keys: Optional keys used for source values. """ if not callable(callback): - raise TypeError('callback must be callable; got %r' % callback) + raise TypeError( + CALLABLE_ERROR % ('callback', callback, type(callback)) + ) self.callback = callback if len(keys) == 0: fc = callback.func_code @@ -112,7 +114,9 @@ class DefaultFrom(ReadOnly): self.keys = keys for key in self.keys: if type(key) is not str: - raise_TypeError(key, str, 'keys') + raise TypeError( + TYPE_ERROR % ('keys', str, key, type(key)) + ) lock(self) def __call__(self, **kw): diff --git a/tests/test_ipalib/test_parameter.py b/tests/test_ipalib/test_parameter.py index 4fc9a0e78..d7f8e45c3 100644 --- a/tests/test_ipalib/test_parameter.py +++ b/tests/test_ipalib/test_parameter.py @@ -48,6 +48,14 @@ class test_DefaultFrom(ClassChecker): o = self.cls(lam) assert read_only(o, 'keys') == ('first', 'last') + # Test that TypeError is raised when callback isn't callable: + e = raises(TypeError, self.cls, 'whatever') + assert str(e) == CALLABLE_ERROR % ('callback', 'whatever', str) + + # Test that TypeError is raised when a key isn't an str: + e = raises(TypeError, self.cls, callback, 'givenname', 17) + assert str(e) == TYPE_ERROR % ('keys', str, 17, int) + def test_call(self): """ Test the `ipalib.parameter.DefaultFrom.__call__` method.