New Param: fixed a few things in Param.convert() and added corresponding unit tests

This commit is contained in:
Jason Gerard DeRose
2008-12-18 00:02:38 -07:00
parent 8ef6819059
commit bf8154fa50
2 changed files with 41 additions and 12 deletions

View File

@@ -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):
"""

View File

@@ -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.