From 36ed6f874f470f3e34bb466f31e096c68d654044 Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Sun, 25 Aug 2013 18:06:07 +0200 Subject: [PATCH] add some tests for values cannot use __setitem__ to set value for an option in append, len should not greater than master option --- test/test_config.py | 12 ++++++++++++ test/test_option_consistency.py | 1 + test/test_option_owner.py | 16 ++++++++++++++++ test/test_parsing_group.py | 12 ++++++++++++ tiramisu/setting.py | 2 +- tiramisu/value.py | 16 ++++------------ 6 files changed, 46 insertions(+), 13 deletions(-) diff --git a/test/test_config.py b/test/test_config.py index df3b84d..b50b7d0 100644 --- a/test/test_config.py +++ b/test/test_config.py @@ -187,3 +187,15 @@ def test_has_value(): assert not g5 in config.cfgimpl_get_values() config.od.g5 = 'yes' 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") diff --git a/test/test_option_consistency.py b/test/test_option_consistency.py index 72aeadb..8dde2a8 100644 --- a/test/test_option_consistency.py +++ b/test/test_option_consistency.py @@ -133,6 +133,7 @@ def test_consistency_ip_netmask_multi_master(): c.b = ['255.255.255.255'] c.b = ['255.255.255.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(): diff --git a/test/test_option_owner.py b/test/test_option_owner.py index dbefd07..eb3790a 100644 --- a/test/test_option_owner.py +++ b/test/test_option_owner.py @@ -5,6 +5,7 @@ from tiramisu.setting import owners from tiramisu.config import Config from tiramisu.option import ChoiceOption, BoolOption, IntOption, FloatOption, \ StrOption, OptionDescription +from tiramisu.error import ConfigError def make_description(): @@ -70,3 +71,18 @@ def test_setowner_without_valid_owner(): assert cfg.dummy is False assert cfg.getowner(gcdummy) == 'default' 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')") diff --git a/test/test_parsing_group.py b/test/test_parsing_group.py index 5f4985c..7b9dffe 100644 --- a/test/test_parsing_group.py +++ b/test/test_parsing_group.py @@ -180,6 +180,9 @@ def test_values_with_master_and_slaves(): assert cfg.ip_admin_eth0.netmask_admin_eth0 == [None] assert cfg.getowner(ip_admin_eth0) == owner 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(): @@ -395,3 +398,12 @@ def test_multi_extend_master(): cfg.read_write() raises(SlaveError, "cfg.ip_admin_eth0.ip_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') diff --git a/tiramisu/setting.py b/tiramisu/setting.py index 25e9485..3c649fe 100644 --- a/tiramisu/setting.py +++ b/tiramisu/setting.py @@ -223,7 +223,7 @@ class Settings(object): return Property(self, self._getproperties(opt, path), opt, path) 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): if all_properties and (_path or opt): diff --git a/tiramisu/value.py b/tiramisu/value.py index 9535869..fb62698 100644 --- a/tiramisu/value.py +++ b/tiramisu/value.py @@ -218,8 +218,7 @@ class Values(object): return value def __setitem__(self, opt, value): - path = self._get_opt_path(opt) - self.setitem(opt, value, path) + raise ValueError('you should only set value with config') def setitem(self, opt, value, path, force_permissive=False, is_write=True): @@ -409,16 +408,9 @@ class Multi(list): dvalue = values._getcallback_value(slave, index=index) else: dvalue = slave.impl_getdefault_multi() - old_value = values.getitem(slave, path, - validate_properties=False) - if len(old_value) < self.__len__(): - values.getitem(slave, path, - validate_properties=False).append( - dvalue, force=True) - else: - values.getitem(slave, path, - validate_properties=False)[ - index] = dvalue + values.getitem(slave, path, + validate_properties=False).append( + dvalue, force=True) def sort(self, cmp=None, key=None, reverse=False): if self.opt.impl_get_multitype() in [multitypes.slave,