diff options
author | Jason Gerard DeRose <jderose@redhat.com> | 2008-08-27 22:26:35 +0000 |
---|---|---|
committer | Jason Gerard DeRose <jderose@redhat.com> | 2008-08-27 22:26:35 +0000 |
commit | 2984041d00f509b34a6ba7f0f0f79135ba6842a3 (patch) | |
tree | f0c4d47fa09fd1a3877e0c07c8459804b6e35743 | |
parent | e6cecfdcf299db564a9055ad69b1c0bc75d4af31 (diff) | |
download | freeipa.git-2984041d00f509b34a6ba7f0f0f79135ba6842a3.tar.gz freeipa.git-2984041d00f509b34a6ba7f0f0f79135ba6842a3.tar.xz freeipa.git-2984041d00f509b34a6ba7f0f0f79135ba6842a3.zip |
205: Continued work on Unicode.__init__() and corresponding unit tests
-rw-r--r-- | ipalib/ipa_types.py | 10 | ||||
-rw-r--r-- | ipalib/tests/test_ipa_types.py | 51 |
2 files changed, 53 insertions, 8 deletions
diff --git a/ipalib/ipa_types.py b/ipalib/ipa_types.py index d94a44fc..f0baf1ba 100644 --- a/ipalib/ipa_types.py +++ b/ipalib/ipa_types.py @@ -83,11 +83,15 @@ class Int(Type): class Unicode(Type): - def __init__(self, length=None,min_length=None, max_length=None, pattern=None): + def __init__(self, min_length=None, max_length=None, pattern=None): check_min_max(min_length, max_length, 'min_length', 'max_length') if min_length is not None and min_length < 0: - raise ValueError( - 'min_length must zero or greater, got: %r' % min_length + raise ValueError('min_length must be >= 0, got: %r' % min_length) + if max_length is not None and max_length < 1: + raise ValueError('max_length must be >= 1, got: %r' % max_length) + if not (pattern is None or isinstance(pattern, basestring)): + raise TypeError( + 'pattern must be a basestring or None, got: %r' % pattern ) self.min_length = min_length self.max_length = max_length diff --git a/ipalib/tests/test_ipa_types.py b/ipalib/tests/test_ipa_types.py index 03346684..8e4f379f 100644 --- a/ipalib/tests/test_ipa_types.py +++ b/ipalib/tests/test_ipa_types.py @@ -117,7 +117,10 @@ class test_Int(ClassChecker): (-5, -10), ] for (l, h) in fail_value: - raises(ValueError, self.cls, min_value=l, max_value=h) + e = raises(ValueError, self.cls, min_value=l, max_value=h) + assert str(e) == ( + 'min_value > max_value: min_value=%d, max_value=%d' % (l, h) + ) def test_validate(self): o = self.cls(min_value=2, max_value=7) @@ -141,9 +144,47 @@ class test_Unicode(ClassChecker): assert o.pattern is None assert o.regex is None + # Test min_length, max_length: okay = ( - (0, 5, r'(hello|world)'), - (8, 8, r'\d{4}'), + (0, 1), + (8, 8), ) - for (l, h, pat) in okay: - o = self.cls(min_length=l, max_length=h, pattern=pat) + for (l, h) in okay: + o = self.cls(min_length=l, max_length=h) + assert o.min_length == l + assert o.max_length == h + + fail_type = [ + '10', + 10.0, + 10L, + True, + False, + object, + ] + for value in fail_type: + e = raises(TypeError, self.cls, min_length=value) + assert str(e) == ( + 'min_length must be an int or None, got: %r' % value + ) + e = raises(TypeError, self.cls, max_length=value) + assert str(e) == ( + 'max_length must be an int or None, got: %r' % value + ) + + fail_value = [ + (10, 5), + (5, -5), + (0, -10), + ] + for (l, h) in fail_value: + e = raises(ValueError, self.cls, min_length=l, max_length=h) + assert str(e) == ( + 'min_length > max_length: min_length=%d, max_length=%d' % (l, h) + ) + + for (key, lower) in [('min_length', 0), ('max_length', 1)]: + value = lower - 1 + kw = {key: value} + e = raises(ValueError, self.cls, **kw) + assert str(e) == '%s must be >= %d, got: %d' % (key, lower, value) |