mirror of
https://salsa.debian.org/freeipa-team/freeipa.git
synced 2025-02-25 18:55:28 -06:00
204: Fixed logic error in check_min_max(); started work on argument validation for Unicode
This commit is contained in:
parent
89ea3acd0a
commit
e6cecfdcf2
@ -21,6 +21,7 @@
|
||||
Type system for coercing and normalizing input values.
|
||||
"""
|
||||
|
||||
import re
|
||||
from plugable import ReadOnly, lock
|
||||
import errors
|
||||
|
||||
@ -31,9 +32,9 @@ def check_min_max(min_value, max_value, min_name, max_name):
|
||||
for (name, value) in [(min_name, min_value), (max_name, max_value)]:
|
||||
if not (value is None or type(value) is int):
|
||||
raise TypeError(
|
||||
'`%s` must be an int or None, got: %r' % (name, value)
|
||||
'%s must be an int or None, got: %r' % (name, value)
|
||||
)
|
||||
if None not in (min_value, max_value) and min_value >= max_value:
|
||||
if None not in (min_value, max_value) and min_value > max_value:
|
||||
d = dict(
|
||||
k0=min_name,
|
||||
v0=min_value,
|
||||
@ -41,7 +42,7 @@ def check_min_max(min_value, max_value, min_name, max_name):
|
||||
v1=max_value,
|
||||
)
|
||||
raise ValueError(
|
||||
'%(k1)s > %(k0)s: %(k0)s=%(v0)r, %(k1)s=%(v1)r' % d
|
||||
'%(k0)s > %(k1)s: %(k0)s=%(v0)r, %(k1)s=%(v1)r' % d
|
||||
)
|
||||
|
||||
|
||||
@ -82,17 +83,16 @@ class Int(Type):
|
||||
|
||||
|
||||
class Unicode(Type):
|
||||
def __init__(self, min_length=None, max_length=None, pattern=None):
|
||||
integers = (min_length, max_length)
|
||||
for i in integers:
|
||||
if not (i is None or type(i) is int):
|
||||
raise TypeError('Must be an int or None: %r' % i)
|
||||
if None not in integers and min_value >= max_value:
|
||||
def __init__(self, length=None,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_value not less than max_value: %r, %r' % (
|
||||
min_value, max_value
|
||||
)
|
||||
'min_length must zero or greater, got: %r' % min_length
|
||||
)
|
||||
self.min_length = min_length
|
||||
self.max_length = max_length
|
||||
self.pattern = pattern
|
||||
if pattern is None:
|
||||
self.regex = None
|
||||
else:
|
||||
self.regex = re.compile(pattern)
|
||||
|
@ -47,9 +47,9 @@ def test_check_min_max():
|
||||
]
|
||||
for value in fail_type:
|
||||
e = raises(TypeError, f, value, None, 'low', 'high')
|
||||
assert str(e) == '`low` must be an int or None, got: %r' % value
|
||||
assert str(e) == 'low must be an int or None, got: %r' % value
|
||||
e = raises(TypeError, f, None, value, 'low', 'high')
|
||||
assert str(e) == '`high` must be an int or None, got: %r' % value
|
||||
assert str(e) == 'high must be an int or None, got: %r' % value
|
||||
fail_value = [
|
||||
(10, 5),
|
||||
(-5, -10),
|
||||
@ -57,7 +57,7 @@ def test_check_min_max():
|
||||
]
|
||||
for (l, h) in fail_value:
|
||||
e = raises(ValueError, f, l, h, 'low', 'high')
|
||||
assert str(e) == 'high > low: low=%r, high=%r' % (l, h)
|
||||
assert str(e) == 'low > high: low=%r, high=%r' % (l, h)
|
||||
|
||||
|
||||
class test_Type(ClassChecker):
|
||||
@ -104,11 +104,11 @@ class test_Int(ClassChecker):
|
||||
for value in fail_type:
|
||||
e = raises(TypeError, self.cls, min_value=value)
|
||||
assert str(e) == (
|
||||
'`min_value` must be an int or None, got: %r' % value
|
||||
'min_value must be an int or None, got: %r' % value
|
||||
)
|
||||
e = raises(TypeError, self.cls, max_value=value)
|
||||
assert str(e) == (
|
||||
'`max_value` must be an int or None, got: %r' % value
|
||||
'max_value must be an int or None, got: %r' % value
|
||||
)
|
||||
|
||||
fail_value = [
|
||||
@ -135,6 +135,15 @@ class test_Unicode(ClassChecker):
|
||||
|
||||
def test_init(self):
|
||||
o = self.cls()
|
||||
assert o.name == 'Unicode'
|
||||
assert o.min_length is None
|
||||
assert o.max_length is None
|
||||
assert o.pattern is None
|
||||
assert o.regex is None
|
||||
|
||||
okay = (
|
||||
(0, 5, r'(hello|world)'),
|
||||
(8, 8, r'\d{4}'),
|
||||
)
|
||||
for (l, h, pat) in okay:
|
||||
o = self.cls(min_length=l, max_length=h, pattern=pat)
|
||||
|
Loading…
Reference in New Issue
Block a user