mirror of
https://salsa.debian.org/freeipa-team/freeipa.git
synced 2024-12-24 16:10:02 -06:00
New Param: added new Flag param class and its unit test
This commit is contained in:
parent
659bb4c142
commit
8cc38e681f
@ -219,6 +219,7 @@ class Param(ReadOnly):
|
|||||||
('normalizer', callable, None),
|
('normalizer', callable, None),
|
||||||
('default_from', DefaultFrom, None),
|
('default_from', DefaultFrom, None),
|
||||||
('create_default', callable, None),
|
('create_default', callable, None),
|
||||||
|
('autofill', bool, False),
|
||||||
('flags', frozenset, frozenset()),
|
('flags', frozenset, frozenset()),
|
||||||
|
|
||||||
# The 'default' kwarg gets appended in Param.__init__():
|
# The 'default' kwarg gets appended in Param.__init__():
|
||||||
@ -329,6 +330,17 @@ class Param(ReadOnly):
|
|||||||
**self.__kw
|
**self.__kw
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def __call__(self, value, **kw):
|
||||||
|
"""
|
||||||
|
One stop shopping.
|
||||||
|
"""
|
||||||
|
if value in NULLS:
|
||||||
|
value = self.get_default(**kw)
|
||||||
|
else:
|
||||||
|
value = self.convert(self.normalize(value))
|
||||||
|
self.validate(value)
|
||||||
|
return value
|
||||||
|
|
||||||
def clone(self, **overrides):
|
def clone(self, **overrides):
|
||||||
"""
|
"""
|
||||||
Return a new `Param` instance similar to this one.
|
Return a new `Param` instance similar to this one.
|
||||||
@ -619,6 +631,20 @@ class Bool(Param):
|
|||||||
A parameter for boolean values (stored in the ``bool`` type).
|
A parameter for boolean values (stored in the ``bool`` type).
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
type = bool
|
||||||
|
|
||||||
|
|
||||||
|
class Flag(Bool):
|
||||||
|
"""
|
||||||
|
A boolean parameter that always gets filled in with a default value.
|
||||||
|
|
||||||
|
This `Bool` subclass forces ``autofill=True`` in `Flag.__init__()`.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, name, *rules, **kw):
|
||||||
|
kw['autofill'] = True
|
||||||
|
super(Flag, self).__init__(name, *rules, **kw)
|
||||||
|
|
||||||
|
|
||||||
class Int(Param):
|
class Int(Param):
|
||||||
"""
|
"""
|
||||||
@ -635,6 +661,7 @@ class Float(Param):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
type = float
|
type = float
|
||||||
|
type_error = _('must be a decimal number')
|
||||||
|
|
||||||
|
|
||||||
class Bytes(Param):
|
class Bytes(Param):
|
||||||
@ -648,6 +675,7 @@ class Bytes(Param):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
type = str
|
type = str
|
||||||
|
type_error = _('must be binary data')
|
||||||
|
|
||||||
kwargs = Param.kwargs + (
|
kwargs = Param.kwargs + (
|
||||||
('minlength', int, None),
|
('minlength', int, None),
|
||||||
@ -657,8 +685,8 @@ class Bytes(Param):
|
|||||||
|
|
||||||
)
|
)
|
||||||
|
|
||||||
def __init__(self, name, **kw):
|
def __init__(self, name, *rules, **kw):
|
||||||
super(Bytes, self).__init__(name, **kw)
|
super(Bytes, self).__init__(name, *rules, **kw)
|
||||||
|
|
||||||
if not (
|
if not (
|
||||||
self.length is None or
|
self.length is None or
|
||||||
|
@ -163,6 +163,7 @@ class test_Param(ClassChecker):
|
|||||||
assert o.default_from is None
|
assert o.default_from is None
|
||||||
assert o.create_default is None
|
assert o.create_default is None
|
||||||
assert o._get_default is None
|
assert o._get_default is None
|
||||||
|
assert o.autofill is False
|
||||||
assert o.flags == frozenset()
|
assert o.flags == frozenset()
|
||||||
|
|
||||||
# Test that ValueError is raised when a kwarg from a subclass
|
# Test that ValueError is raised when a kwarg from a subclass
|
||||||
@ -543,6 +544,33 @@ class test_Param(ClassChecker):
|
|||||||
assert o.normalizer.value is default
|
assert o.normalizer.value is default
|
||||||
|
|
||||||
|
|
||||||
|
class test_Flag(ClassChecker):
|
||||||
|
"""
|
||||||
|
Test the `ipalib.parameter.Flag` class.
|
||||||
|
"""
|
||||||
|
_cls = parameter.Flag
|
||||||
|
|
||||||
|
def test_init(self):
|
||||||
|
"""
|
||||||
|
Test the `ipalib.parameter.Flag.__init__` method.
|
||||||
|
"""
|
||||||
|
o = self.cls('my_flag')
|
||||||
|
assert o.type is bool
|
||||||
|
assert isinstance(o, parameter.Bool)
|
||||||
|
assert o.autofill is True
|
||||||
|
|
||||||
|
# Test with autofill=False
|
||||||
|
o = self.cls('my_flag', autofill=False)
|
||||||
|
assert o.autofill is True
|
||||||
|
|
||||||
|
# Test when cloning:
|
||||||
|
orig = self.cls('my_flag')
|
||||||
|
for clone in [orig.clone(), orig.clone(autofill=False)]:
|
||||||
|
assert clone.autofill is True
|
||||||
|
assert clone is not orig
|
||||||
|
assert type(clone) is self.cls
|
||||||
|
|
||||||
|
|
||||||
class test_Bytes(ClassChecker):
|
class test_Bytes(ClassChecker):
|
||||||
"""
|
"""
|
||||||
Test the `ipalib.parameter.Bytes` class.
|
Test the `ipalib.parameter.Bytes` class.
|
||||||
|
Loading…
Reference in New Issue
Block a user