mirror of
https://salsa.debian.org/freeipa-team/freeipa.git
synced 2025-02-25 18:55:28 -06:00
New Param: fixed a few things in Param.convert() and added corresponding unit tests
This commit is contained in:
@@ -309,7 +309,7 @@ class Param(ReadOnly):
|
||||
if self.multivalue:
|
||||
if type(value) in (tuple, list):
|
||||
return tuple(
|
||||
self._normalize_scalarS(v) for v in value
|
||||
self._normalize_scalar(v) for v in value
|
||||
)
|
||||
return (self._normalize_scalar(value),) # Return a tuple
|
||||
return self._normalize_scalar(value)
|
||||
@@ -331,16 +331,16 @@ class Param(ReadOnly):
|
||||
if value in NULLS:
|
||||
return
|
||||
if self.multivalue:
|
||||
if type(value) in (tuple, list):
|
||||
values = filter(
|
||||
lambda val: val not in NULLS,
|
||||
(self._convert_scalar(v, i) for (i, v) in enumerate(value))
|
||||
)
|
||||
if len(values) == 0:
|
||||
return
|
||||
return tuple(values)
|
||||
return (scalar(value, 0),) # Return a tuple
|
||||
return scalar(value)
|
||||
if type(value) not in (tuple, list):
|
||||
value = (value,)
|
||||
values = filter(
|
||||
lambda val: val not in NULLS,
|
||||
(self._convert_scalar(v, i) for (i, v) in enumerate(value))
|
||||
)
|
||||
if len(values) == 0:
|
||||
return
|
||||
return tuple(values)
|
||||
return self._convert_scalar(value)
|
||||
|
||||
def _convert_scalar(self, value, index=None):
|
||||
"""
|
||||
|
||||
@@ -25,7 +25,7 @@ Test the `ipalib.parameter` module.
|
||||
from tests.util import raises, ClassChecker, read_only
|
||||
from tests.data import binary_bytes, utf8_bytes, unicode_str
|
||||
from ipalib import parameter
|
||||
from ipalib.constants import TYPE_ERROR, CALLABLE_ERROR
|
||||
from ipalib.constants import TYPE_ERROR, CALLABLE_ERROR, NULLS
|
||||
|
||||
|
||||
class test_DefaultFrom(ClassChecker):
|
||||
@@ -171,6 +171,35 @@ class test_Param(ClassChecker):
|
||||
assert str(e) == \
|
||||
"Param('my_param'): takes no such kwargs: 'ape', 'great'"
|
||||
|
||||
def test_convert(self):
|
||||
"""
|
||||
Test the `ipalib.parameter.Param.convert` method.
|
||||
"""
|
||||
okay = ('Hello', u'Hello', 0, 4.2, True, False)
|
||||
class Subclass(self.cls):
|
||||
def _convert_scalar(self, value, index=None):
|
||||
return value
|
||||
|
||||
# Test when multivalue=False:
|
||||
o = Subclass('my_param')
|
||||
for value in NULLS:
|
||||
assert o.convert(value) is None
|
||||
for value in okay:
|
||||
assert o.convert(value) is value
|
||||
|
||||
# Test when multivalue=True:
|
||||
o = Subclass('my_param', multivalue=True)
|
||||
for value in NULLS:
|
||||
assert o.convert(value) is None
|
||||
assert o.convert(okay) == okay
|
||||
assert o.convert(NULLS) is None
|
||||
assert o.convert(okay + NULLS) == okay
|
||||
assert o.convert(NULLS + okay) == okay
|
||||
for value in okay:
|
||||
assert o.convert(value) == (value,)
|
||||
assert o.convert([None, value]) == (value,)
|
||||
assert o.convert([value, None]) == (value,)
|
||||
|
||||
def test_convert_scalar(self):
|
||||
"""
|
||||
Test the `ipalib.parameter.Param._convert_scalar` method.
|
||||
|
||||
Reference in New Issue
Block a user