mirror of
https://salsa.debian.org/freeipa-team/freeipa.git
synced 2025-02-25 18:55:28 -06:00
New Param: decided on calling signature for rules; added unit tests for Bytes._rule_minlength, _rule_maxlength, and _rule_length
This commit is contained in:
parent
c121d0064b
commit
6d6c0d81dd
@ -500,30 +500,36 @@ class Bytes(Param):
|
||||
self.nice, self.minlength)
|
||||
)
|
||||
|
||||
def _rule_minlength(self, value):
|
||||
def _rule_minlength(self, _, name, value):
|
||||
"""
|
||||
Check minlength constraint.
|
||||
"""
|
||||
assert type(value) is str
|
||||
if len(value) < self.minlength:
|
||||
return 'Must be at least %(minlength)d bytes long.' % dict(
|
||||
return _('%(name)s must be at least %(minlength)d bytes') % dict(
|
||||
name=name,
|
||||
minlength=self.minlength,
|
||||
)
|
||||
|
||||
def _rule_maxlength(self, value):
|
||||
def _rule_maxlength(self, _, name, value):
|
||||
"""
|
||||
Check maxlength constraint.
|
||||
"""
|
||||
assert type(value) is str
|
||||
if len(value) > self.maxlength:
|
||||
return 'Can be at most %(maxlength)d bytes long.' % dict(
|
||||
return _('%(name)s can be at most %(maxlength)d bytes') % dict(
|
||||
name=name,
|
||||
maxlength=self.maxlength,
|
||||
)
|
||||
|
||||
def _rule_length(self, value):
|
||||
def _rule_length(self, _, name, value):
|
||||
"""
|
||||
Check length constraint.
|
||||
"""
|
||||
assert type(value) is str
|
||||
if len(value) != self.length:
|
||||
return 'Must be exactly %(length)d bytes long.' % dict(
|
||||
return _('%(name)s must be exactly %(length)d bytes') % dict(
|
||||
name=name,
|
||||
length=self.length,
|
||||
)
|
||||
|
||||
|
@ -346,6 +346,108 @@ class test_Bytes(ClassChecker):
|
||||
assert str(e) == \
|
||||
"Bytes('my_bytes'): minlength == maxlength; use length=7 instead"
|
||||
|
||||
def test_rule_minlength(self):
|
||||
"""
|
||||
Test the `ipalib.parameter.Bytes._rule_minlength` method.
|
||||
"""
|
||||
name = 'My Bytes'
|
||||
o = self.cls('my_bytes', minlength=3)
|
||||
assert o.minlength == 3
|
||||
m = o._rule_minlength
|
||||
translation = u'name=%(name)r, minlength=%(minlength)r'
|
||||
dummy = dummy_ugettext(translation)
|
||||
assert dummy.translation is translation
|
||||
|
||||
# Test with passing values:
|
||||
for value in ('abc', 'four', '12345'):
|
||||
assert m(dummy, name, value) is None
|
||||
assert not hasattr(dummy, 'message')
|
||||
|
||||
# Test with a failing value:
|
||||
assert_equal(
|
||||
m(dummy, name, 'ab'),
|
||||
translation % dict(name=name, minlength=3),
|
||||
)
|
||||
assert dummy.message == \
|
||||
'%(name)s must be at least %(minlength)d bytes'
|
||||
|
||||
def test_rule_maxlength(self):
|
||||
"""
|
||||
Test the `ipalib.parameter.Bytes._rule_maxlength` method.
|
||||
"""
|
||||
name = 'My Bytes'
|
||||
o = self.cls('my_bytes', maxlength=4)
|
||||
assert o.maxlength == 4
|
||||
m = o._rule_maxlength
|
||||
translation = u'name=%(name)r, maxlength=%(maxlength)r'
|
||||
dummy = dummy_ugettext(translation)
|
||||
assert dummy.translation is translation
|
||||
|
||||
# Test with passing values:
|
||||
for value in ('ab', '123', 'four'):
|
||||
assert m(dummy, name, value) is None
|
||||
assert not hasattr(dummy, 'message')
|
||||
|
||||
# Test with a failing value:
|
||||
assert_equal(
|
||||
m(dummy, name, '12345'),
|
||||
translation % dict(name=name, maxlength=4),
|
||||
)
|
||||
assert dummy.message == \
|
||||
'%(name)s can be at most %(maxlength)d bytes'
|
||||
|
||||
def test_rule_maxlength(self):
|
||||
"""
|
||||
Test the `ipalib.parameter.Bytes._rule_maxlength` method.
|
||||
"""
|
||||
name = 'My Bytes'
|
||||
o = self.cls('my_bytes', maxlength=4)
|
||||
assert o.maxlength == 4
|
||||
m = o._rule_maxlength
|
||||
translation = u'name=%(name)r, maxlength=%(maxlength)r'
|
||||
dummy = dummy_ugettext(translation)
|
||||
assert dummy.translation is translation
|
||||
|
||||
# Test with passing values:
|
||||
for value in ('ab', '123', 'four'):
|
||||
assert m(dummy, name, value) is None
|
||||
assert not hasattr(dummy, 'message')
|
||||
|
||||
# Test with a failing value:
|
||||
assert_equal(
|
||||
m(dummy, name, '12345'),
|
||||
translation % dict(name=name, maxlength=4),
|
||||
)
|
||||
assert dummy.message == \
|
||||
'%(name)s can be at most %(maxlength)d bytes'
|
||||
|
||||
def test_rule_length(self):
|
||||
"""
|
||||
Test the `ipalib.parameter.Bytes._rule_length` method.
|
||||
"""
|
||||
name = 'My Bytes'
|
||||
o = self.cls('my_bytes', length=4)
|
||||
assert o.length == 4
|
||||
m = o._rule_length
|
||||
translation = u'name=%(name)r, length=%(length)r'
|
||||
dummy = dummy_ugettext(translation)
|
||||
assert dummy.translation is translation
|
||||
|
||||
# Test with passing values:
|
||||
for value in ('1234', 'four'):
|
||||
assert m(dummy, name, value) is None
|
||||
assert not hasattr(dummy, 'message')
|
||||
|
||||
# Test with failing values:
|
||||
for value in ('ab', '123', '12345', 'abcdef'):
|
||||
assert_equal(
|
||||
m(dummy, name, value),
|
||||
translation % dict(name=name, length=4),
|
||||
)
|
||||
assert dummy.message == \
|
||||
'%(name)s must be exactly %(length)d bytes'
|
||||
dummy = dummy_ugettext(translation)
|
||||
|
||||
|
||||
class test_Str(ClassChecker):
|
||||
"""
|
||||
|
@ -290,8 +290,11 @@ class PluginTester(object):
|
||||
class dummy_ugettext(object):
|
||||
__called = False
|
||||
|
||||
def __init__(self):
|
||||
self.translation = u'The translation'
|
||||
def __init__(self, translation=None):
|
||||
if translation is None:
|
||||
translation = u'The translation'
|
||||
self.translation = translation
|
||||
assert type(self.translation) is unicode
|
||||
|
||||
def __call__(self, message):
|
||||
assert type(message) is str
|
||||
|
Loading…
Reference in New Issue
Block a user