mirror of
https://salsa.debian.org/freeipa-team/freeipa.git
synced 2025-02-25 18:55:28 -06:00
205: Continued work on Unicode.__init__() and corresponding unit tests
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user