add some tests for values

cannot use __setitem__ to set value for an option
in append, len should not greater than master option
This commit is contained in:
Emmanuel Garette 2013-08-25 18:06:07 +02:00
parent f482737a47
commit 36ed6f874f
6 changed files with 46 additions and 13 deletions

View file

@ -187,3 +187,15 @@ def test_has_value():
assert not g5 in config.cfgimpl_get_values() assert not g5 in config.cfgimpl_get_values()
config.od.g5 = 'yes' config.od.g5 = 'yes'
assert g5 in config.cfgimpl_get_values() assert g5 in config.cfgimpl_get_values()
def test_values_not_setitem():
g1 = IntOption('g1', '', 1)
g2 = StrOption('g2', '', 'héhé')
g3 = UnicodeOption('g3', '', u'héhé')
g4 = BoolOption('g4', '', True)
g5 = StrOption('g5', '')
d1 = OptionDescription('od', '', [g1, g2, g3, g4, g5])
root = OptionDescription('root', '', [d1])
config = Config(root)
raises(ValueError, "config.cfgimpl_get_values()[g1] = 2")

View file

@ -133,6 +133,7 @@ def test_consistency_ip_netmask_multi_master():
c.b = ['255.255.255.255'] c.b = ['255.255.255.255']
c.b = ['255.255.255.0'] c.b = ['255.255.255.0']
raises(ValueError, "c.a = ['192.168.1.0']") raises(ValueError, "c.a = ['192.168.1.0']")
c.a = ['192.168.1.2', '192.168.1.3']
def test_consistency_network_netmask_multi_master(): def test_consistency_network_netmask_multi_master():

View file

@ -5,6 +5,7 @@ from tiramisu.setting import owners
from tiramisu.config import Config from tiramisu.config import Config
from tiramisu.option import ChoiceOption, BoolOption, IntOption, FloatOption, \ from tiramisu.option import ChoiceOption, BoolOption, IntOption, FloatOption, \
StrOption, OptionDescription StrOption, OptionDescription
from tiramisu.error import ConfigError
def make_description(): def make_description():
@ -70,3 +71,18 @@ def test_setowner_without_valid_owner():
assert cfg.dummy is False assert cfg.dummy is False
assert cfg.getowner(gcdummy) == 'default' assert cfg.getowner(gcdummy) == 'default'
raises(TypeError, "cfg.cfgimpl_get_settings().setowner('gen_config')") raises(TypeError, "cfg.cfgimpl_get_settings().setowner('gen_config')")
def test_setowner_for_value():
gcdummy = BoolOption('dummy', 'dummy', default=False)
descr = OptionDescription('tiramisu', '', [gcdummy])
cfg = Config(descr)
assert cfg.dummy is False
assert cfg.getowner(gcdummy) == 'default'
owners.add_owner("new")
raises(ConfigError, "cfg.cfgimpl_get_values().setowner(gcdummy, owners.new)")
cfg.dummy = False
assert cfg.getowner(gcdummy) == owners.user
cfg.cfgimpl_get_values().setowner(gcdummy, owners.new)
assert cfg.getowner(gcdummy) == owners.new
raises(TypeError, "cfg.cfgimpl_get_values().setowner(gcdummy, 'new')")

View file

@ -180,6 +180,9 @@ def test_values_with_master_and_slaves():
assert cfg.ip_admin_eth0.netmask_admin_eth0 == [None] assert cfg.ip_admin_eth0.netmask_admin_eth0 == [None]
assert cfg.getowner(ip_admin_eth0) == owner assert cfg.getowner(ip_admin_eth0) == owner
assert cfg.getowner(netmask_admin_eth0) == owners.default assert cfg.getowner(netmask_admin_eth0) == owners.default
cfg.ip_admin_eth0.ip_admin_eth0 = ["192.168.230.145", "192.168.230.147"]
raises(SlaveError, 'cfg.ip_admin_eth0.netmask_admin_eth0.append(None)')
raises(SlaveError, 'cfg.ip_admin_eth0.netmask_admin_eth0.pop(0)')
def test_reset_values_with_master_and_slaves(): def test_reset_values_with_master_and_slaves():
@ -395,3 +398,12 @@ def test_multi_extend_master():
cfg.read_write() cfg.read_write()
raises(SlaveError, "cfg.ip_admin_eth0.ip_admin_eth0.extend(['ok'])") raises(SlaveError, "cfg.ip_admin_eth0.ip_admin_eth0.extend(['ok'])")
raises(SlaveError, "cfg.ip_admin_eth0.netmask_admin_eth0.extend(['ok'])") raises(SlaveError, "cfg.ip_admin_eth0.netmask_admin_eth0.extend(['ok'])")
def test_multi_non_valid_value():
ip_admin_eth0 = StrOption('ip_admin_eth0', "ip réseau autorisé", multi=True)
maconfig = OptionDescription('toto', '', [ip_admin_eth0])
cfg = Config(maconfig)
cfg.read_write()
cfg.ip_admin_eth0 = ['a']
raises(ValueError, 'cfg.ip_admin_eth0[0] = 1')

View file

@ -223,7 +223,7 @@ class Settings(object):
return Property(self, self._getproperties(opt, path), opt, path) return Property(self, self._getproperties(opt, path), opt, path)
def __setitem__(self, opt, value): def __setitem__(self, opt, value):
raise ValueError('you must only append/remove properties') raise ValueError('you should only append/remove properties')
def reset(self, opt=None, _path=None, all_properties=False): def reset(self, opt=None, _path=None, all_properties=False):
if all_properties and (_path or opt): if all_properties and (_path or opt):

View file

@ -218,8 +218,7 @@ class Values(object):
return value return value
def __setitem__(self, opt, value): def __setitem__(self, opt, value):
path = self._get_opt_path(opt) raise ValueError('you should only set value with config')
self.setitem(opt, value, path)
def setitem(self, opt, value, path, force_permissive=False, def setitem(self, opt, value, path, force_permissive=False,
is_write=True): is_write=True):
@ -409,16 +408,9 @@ class Multi(list):
dvalue = values._getcallback_value(slave, index=index) dvalue = values._getcallback_value(slave, index=index)
else: else:
dvalue = slave.impl_getdefault_multi() dvalue = slave.impl_getdefault_multi()
old_value = values.getitem(slave, path, values.getitem(slave, path,
validate_properties=False) validate_properties=False).append(
if len(old_value) < self.__len__(): dvalue, force=True)
values.getitem(slave, path,
validate_properties=False).append(
dvalue, force=True)
else:
values.getitem(slave, path,
validate_properties=False)[
index] = dvalue
def sort(self, cmp=None, key=None, reverse=False): def sort(self, cmp=None, key=None, reverse=False):
if self.opt.impl_get_multitype() in [multitypes.slave, if self.opt.impl_get_multitype() in [multitypes.slave,