tiramisu/test/api/test_multi.py
2017-11-13 22:45:53 +01:00

248 lines
10 KiB
Python

"""test consistencies about multi and submulti"""
from py.test import raises
from .autopath import do_autopath
do_autopath()
from tiramisu.setting import groups
from tiramisu import Config, StrOption, IntOption, OptionDescription, MasterSlaves, getapi, \
submulti, undefined
from tiramisu.error import PropertiesOptionError
def return_val(val=None):
if val is None:
return 'val'
else:
return val
def return_list(value=None):
return ['val', 'val']
def return_list2(value=None):
return [['val', 'val']]
def test_multi_unique():
int_ = IntOption('int', '', multi=True, unique=True)
od_ = OptionDescription('od', '', [int_])
cfg = Config(od_)
api = getapi(cfg)
assert api.option('int').value.get() == []
value = [0]
api.option('int').value.set(value)
assert api.option('int').value.get() == value
value.append(1)
assert api.option('int').value.get() == [0]
raises(ValueError, "api.option('int').value.set([0, 0])")
raises(ValueError, "api.option('int').value.set([1, 0, 2, 3, 4, 5, 6, 0, 7])")
def test_multi_none():
str_ = StrOption('str', '', multi=True)
od_ = OptionDescription('od', '', [str_])
cfg = Config(od_)
cfg.read_only()
api = getapi(cfg)
assert api.option('str').value.get() == []
cfg.read_write()
api.option('str').value.set([None])
assert api.option('str').value.get() == [None]
cfg.read_only()
raises(PropertiesOptionError, "api.option('str').value.get()")
cfg.read_write()
api.option('str').value.set([''])
assert api.option('str').value.get() == ['']
cfg.read_only()
raises(PropertiesOptionError, "api.option('str').value.get()")
# ______________ submulti ______________
def test_append_submulti():
multi = StrOption('multi', '', multi=submulti)
multi2 = StrOption('multi2', '', default_multi=['yes'], multi=submulti)
multi3 = StrOption('multi3', '', default=[['yes']], multi=submulti)
od_ = OptionDescription('od', '', [multi, multi2, multi3])
cfg = Config(od_)
api = getapi(cfg)
#FIXME
owner = cfg.cfgimpl_get_settings().getowner()
assert api.option('multi').value.get() == []
assert api.option('multi').value.get() == []
assert api.option('multi').owner.isdefault()
api.option('multi').value.set([undefined])
assert api.option('multi').owner.get() == owner
value = api.option('multi').value.get()
assert value == [[]]
value.append(['no'])
assert api.option('multi').value.get() == [[]]
api.option('multi').value.set(value)
assert api.option('multi').value.get() == [[], ['no']]
#
assert api.option('multi2').value.get() == []
assert api.option('multi2').owner.isdefault()
api.option('multi2').value.set([undefined])
assert api.option('multi2').owner.get() == owner
assert api.option('multi2').value.get() == [['yes']]
api.option('multi2').value.set([['yes'], ['no']])
assert api.option('multi2').value.get() == [['yes'], ['no']]
#
assert api.option('multi3').value.get() == [['yes']]
assert api.option('multi3').owner.isdefault()
api.option('multi3').value.set([['yes'], undefined])
assert api.option('multi3').owner.get() == owner
assert api.option('multi3').value.get() == [['yes'], []]
api.option('multi3').value.set([['yes'], [], ['no']])
assert api.option('multi3').value.get() == [['yes'], [], ['no']]
def test_append_unvalide_submulti():
multi = StrOption('multi', '', multi=submulti)
multi2 = StrOption('multi2', '', default_multi=['yes'], multi=submulti)
multi3 = StrOption('multi3', '', default=[['yes']], multi=submulti)
od_ = OptionDescription('od', '', [multi, multi2, multi3])
cfg = Config(od_)
api = getapi(cfg)
assert api.option('multi').value.get() == []
assert api.option('multi').owner.isdefault()
raises(ValueError, "api.option('multi').value.set([1])")
assert api.option('multi').value.get() == []
assert api.option('multi').owner.isdefault()
#
assert api.option('multi2').value.get() == []
raises(ValueError, "api.option('multi2').value.set('no')")
assert api.option('multi2').owner.isdefault()
assert api.option('multi2').value.get() == []
#
assert api.option('multi3').value.get() == [['yes']]
assert api.option('multi3').owner.isdefault()
def test_submulti_unique():
int_ = IntOption('int', '', multi=submulti, unique=True)
od_ = OptionDescription('od', '', [int_])
cfg = Config(od_)
api = getapi(cfg)
assert api.option('int').value.get() == []
api.option('int').value.set([[0]])
assert api.option('int').value.get() == [[0]]
raises(ValueError, "api.option('int').value.set([[0, 0]])")
api.option('int').value.set([[0], [0]])
raises(ValueError, "api.option('int').value.set([[0], [1, 0, 2, 3, 4, 5, 6, 0, 7]])")
def test_callback_reset():
multi = StrOption('multi', '', multi=submulti, callback=return_val)
od_ = OptionDescription('od', '', [multi])
cfg = Config(od_)
cfg.read_write()
api = getapi(cfg)
#FIXME
owner = cfg.cfgimpl_get_settings().getowner()
assert api.option('multi').value.get() == [['val']]
assert api.option('multi').owner.isdefault()
api.option('multi').value.set([['val'], undefined])
assert api.option('multi').owner.get() == owner
assert api.option('multi').value.get() == [['val'], ['val']]
api.option('multi').value.reset()
assert api.option('multi').owner.isdefault()
assert api.option('multi').value.get() == [['val']]
api.option('multi').value.set([['val1']])
assert api.option('multi').owner.get() == owner
assert api.option('multi').value.get() == [['val1']]
def test_callback_submulti_list():
multi = StrOption('multi', '', multi=submulti, callback=return_list)
od_ = OptionDescription('od', '', [multi])
cfg = Config(od_)
cfg.read_write()
api = getapi(cfg)
owner = cfg.cfgimpl_get_settings().getowner()
assert api.option('multi').value.get() == [['val', 'val']]
assert api.option('multi').owner.isdefault()
api.option('multi').value.set([['val', 'val'], undefined])
assert api.option('multi').owner.get() == owner
assert api.option('multi').value.get() == [['val', 'val'], ['val', 'val']]
api.option('multi').value.set([['val', 'val'], undefined, undefined])
assert api.option('multi').value.get() == [['val', 'val'], ['val', 'val'], ['val', 'val']]
api.option('multi').value.reset()
assert api.option('multi').owner.isdefault()
def test_callback_submulti_list_list():
multi = StrOption('multi', '', multi=submulti, callback=return_list2)
od = OptionDescription('od', '', [multi])
cfg = Config(od)
cfg.read_write()
api = getapi(cfg)
owner = cfg.cfgimpl_get_settings().getowner()
assert api.option('multi').value.get() == [['val', 'val']]
assert api.option('multi').owner.isdefault()
api.option('multi').value.set([['val', 'val'], undefined])
assert api.option('multi').owner.get() == owner
assert api.option('multi').value.get() == [['val', 'val'], []]
api.option('multi').value.reset()
assert api.option('multi').owner.isdefault()
#FIXME
#def test_multi_submulti_meta():
# multi = StrOption('multi', '', multi=submulti)
# od = OptionDescription('od', '', [multi])
# conf1 = Config(od, session_id='conf1')
# conf1.read_write()
# conf2 = Config(od, session_id='conf2')
# conf2.read_write()
# meta = MetaConfig([conf1, conf2])
# meta.read_write()
# meta.multi = [['val']]
# assert meta.multi == [['val']]
# multi = conf1.multi[0]
# multi.append()
# assert conf1.multi == [['val', None]]
# assert meta.multi == [['val']]
# ____________________________________________________________
# Master Slaves
def test_values_with_master_and_slaves_submulti():
ip_admin_eth0 = StrOption('ip_admin_eth0', "ip reseau autorise", multi=True)
netmask_admin_eth0 = StrOption('netmask_admin_eth0', "masque du sous-reseau", multi=submulti)
interface1 = MasterSlaves('f_ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0])
conf = OptionDescription('conf', '', [interface1])
cfg = Config(conf)
api = getapi(cfg)
cfg.read_write()
owner = cfg.cfgimpl_get_settings().getowner()
#FIXME
assert interface1.impl_get_group_type() == groups.master
assert api.option('f_ip_admin_eth0.ip_admin_eth0').owner.isdefault()
#FIXME raises(IndexError, "api.option('f_ip_admin_eth0.netmask_admin_eth0', 0).value.get()")
api.option('f_ip_admin_eth0.ip_admin_eth0').value.set(['192.168.230.145'])
assert api.option('f_ip_admin_eth0.ip_admin_eth0').value.get() == ['192.168.230.145']
assert api.option('f_ip_admin_eth0.netmask_admin_eth0', 0).value.get() == []
#FIXME raises(IndexError, "api.option('f_ip_admin_eth0.netmask_admin_eth0', 1).value.get()")
assert api.option('f_ip_admin_eth0.ip_admin_eth0').owner.get() == owner
assert api.option('f_ip_admin_eth0.netmask_admin_eth0', 0).owner.isdefault()
api.option('f_ip_admin_eth0.ip_admin_eth0').value.set(['192.168.230.145', '192.168.230.147'])
assert api.option('f_ip_admin_eth0.netmask_admin_eth0', 0).value.get() == []
assert api.option('f_ip_admin_eth0.netmask_admin_eth0', 1).value.get() == []
#FIXME raises(IndexError, "api.option('f_ip_admin_eth0.netmask_admin_eth0', 2).value.get()")
#FIXME raises(IndexError, "api.option('f_ip_admin_eth0.netmask_admin_eth0', 2).value.set(['255.255.255.0'])")
api.option('f_ip_admin_eth0.netmask_admin_eth0', 0).value.set(['255.255.255.0'])
assert api.option('f_ip_admin_eth0.netmask_admin_eth0', 0).value.get() == ['255.255.255.0']
assert api.option('f_ip_admin_eth0.netmask_admin_eth0', 1).value.get() == []
api.option('f_ip_admin_eth0.netmask_admin_eth0', 1).value.set(['255.255.255.255'])
assert api.option('f_ip_admin_eth0.netmask_admin_eth0', 0).value.get() == ['255.255.255.0']
assert api.option('f_ip_admin_eth0.netmask_admin_eth0', 1).value.get() == ['255.255.255.255']
assert api.option('f_ip_admin_eth0.ip_admin_eth0').value.get() == ['192.168.230.145', '192.168.230.147']
raises(ValueError, "api.option('f_ip_admin_eth0.ip_admin_eth0').value.set(['192.168.230.145'])")
api.option('f_ip_admin_eth0.ip_admin_eth0').value.pop(1)
assert api.option('f_ip_admin_eth0.ip_admin_eth0').value.get() == ['192.168.230.145']
assert api.option('f_ip_admin_eth0.netmask_admin_eth0', 0).value.get() == ['255.255.255.0']
#FIXME raises(IndexError, "api.option('f_ip_admin_eth0.netmask_admin_eth0', 1).value.get()")