refactor and better test in api
This commit is contained in:
@ -210,8 +210,8 @@ def test_callback_submulti_list_list():
|
||||
|
||||
|
||||
def test_values_with_master_and_slaves_submulti():
|
||||
ip_admin_eth0 = StrOption('ip_admin_eth0', "ip réseau autorisé", multi=True)
|
||||
netmask_admin_eth0 = StrOption('netmask_admin_eth0', "masque du sous-réseau", multi=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)
|
||||
|
@ -2,6 +2,7 @@
|
||||
"""
|
||||
import pytest
|
||||
from py.test import raises
|
||||
import weakref
|
||||
from .autopath import do_autopath
|
||||
do_autopath()
|
||||
from tiramisu import Config, StrOption, OptionDescription, MasterSlaves, DynOptionDescription, \
|
||||
@ -55,7 +56,10 @@ def autocheck_owner_without_value(api, path, **kwargs):
|
||||
isslave = False
|
||||
# check if owner is a string "default"
|
||||
if not kwargs.get('permissive', False) and not kwargs.get('propertyerror', False):
|
||||
assert api.option(path).owner.get() == 'default'
|
||||
if not isslave:
|
||||
assert api.option(path).owner.get() == 'default'
|
||||
else:
|
||||
assert api.option(path, 0).owner.get() == 'default'
|
||||
else:
|
||||
if not isslave:
|
||||
raises(PropertiesOptionError, "api.option(path).owner.get()")
|
||||
@ -76,7 +80,10 @@ def autocheck_owner_without_value(api, path, **kwargs):
|
||||
# check if default owner
|
||||
raises(APIError, "api.unrestraint.option(path).owner.isdefault()")
|
||||
if not kwargs.get('permissive', False) and not kwargs.get('propertyerror', False):
|
||||
assert api.option(path).owner.isdefault()
|
||||
if not isslave:
|
||||
assert api.option(path).owner.isdefault()
|
||||
else:
|
||||
assert api.option(path, 0).owner.isdefault()
|
||||
else:
|
||||
raises(PropertiesOptionError, "api.option(path).owner.isdefault()")
|
||||
if not kwargs.get('propertyerror', False):
|
||||
@ -141,7 +148,6 @@ def autocheck_value(api, path, **kwargs):
|
||||
api.option(path).value.set([first_value[0]])
|
||||
elif isslave:
|
||||
api.option(path, 0).value.set(first_value[0])
|
||||
raise Exception('pouet')
|
||||
else:
|
||||
api.option(path).value.set(first_value)
|
||||
else:
|
||||
@ -321,6 +327,14 @@ def autocheck_value2(*args, **kwargs):
|
||||
autocheck_value(*args, **kwargs)
|
||||
|
||||
|
||||
@autocheck
|
||||
def autocheck_display(api, path, **kwargs):
|
||||
"""re set value
|
||||
"""
|
||||
#FIXME utile ?
|
||||
print(api.config)
|
||||
|
||||
|
||||
@autocheck
|
||||
def autocheck_property(api, path, **kwargs):
|
||||
"""get property from path
|
||||
@ -756,6 +770,7 @@ def check_all(api, path, multi, **kwargs):
|
||||
text = u' {} launch tests for {}'.format(ICON, path)
|
||||
if multi:
|
||||
text += u' as a multi'
|
||||
text += u', kwargs: {}'.format(kwargs)
|
||||
print(text)
|
||||
for func in autocheck_registers:
|
||||
if DISPLAY:
|
||||
@ -764,6 +779,7 @@ def check_all(api, path, multi, **kwargs):
|
||||
|
||||
|
||||
def make_api(options, multi):
|
||||
weakrefs = []
|
||||
def make_option(path, option_infos):
|
||||
#FIXME
|
||||
option_type = 'str'
|
||||
@ -779,7 +795,9 @@ def make_api(options, multi):
|
||||
if multi:
|
||||
kwargs['multi'] = True
|
||||
tiramisu_option = OPTIONS_TYPE[option_type]['option']
|
||||
return tiramisu_option(*args, **kwargs)
|
||||
obj = tiramisu_option(*args, **kwargs)
|
||||
weakrefs.append(weakref.ref(obj))
|
||||
return obj
|
||||
|
||||
def make_optiondescriptions(path, collected):
|
||||
infos = collected.get('properties', {})
|
||||
@ -809,7 +827,9 @@ def make_api(options, multi):
|
||||
options.append(option)
|
||||
if properties != []:
|
||||
kwargs['properties'] = tuple(properties)
|
||||
return optiondescription(path, "{}'s optiondescription".format(path), options, **kwargs)
|
||||
obj = optiondescription(path, "{}'s optiondescription".format(path), options, **kwargs)
|
||||
weakrefs.append(weakref.ref(obj))
|
||||
return obj
|
||||
|
||||
collect_options = {}
|
||||
for path, option in options.items():
|
||||
@ -826,16 +846,20 @@ def make_api(options, multi):
|
||||
|
||||
rootod = make_optiondescriptions('root', collect_options)
|
||||
if rootod is None:
|
||||
return
|
||||
cfg = Config(rootod)
|
||||
return getapi(cfg)
|
||||
return None, None
|
||||
cfg = Config(rootod, session_id='conftest')
|
||||
weakrefs.append(weakref.ref(cfg))
|
||||
api = getapi(cfg)
|
||||
weakrefs.append(weakref.ref(api))
|
||||
return api, weakrefs
|
||||
|
||||
|
||||
DICT_PATHS = [
|
||||
#test a config without optiondescription
|
||||
OrderedDict([('first', {}),
|
||||
('second', {'second': {'disabled': True}}),
|
||||
('third', {'third': {'hidden': True}})]),
|
||||
('third', {'third': {'hidden': True}})
|
||||
]),
|
||||
#test a config with an optiondescription
|
||||
OrderedDict([('subod.first', {}),
|
||||
('subod.second', {'second': {'disabled': True}}),
|
||||
@ -864,8 +888,22 @@ DICT_PATHS = [
|
||||
('subodval2.thirdval2', None)]),
|
||||
#test a config with dynoption subdir
|
||||
OrderedDict([('subod.subsubod.first', {'subsubod': {'dyn': True}}),
|
||||
('subod.subsubod.second', {'second': {'disabled': True}}),
|
||||
('subod.subsubod.third', {'third': {'hidden': True}}),
|
||||
('subod.subsubod.second', {'subsubod': {'dyn': True}, 'second': {'disabled': True}}),
|
||||
('subod.subsubod.third', {'subsubod': {'dyn': True}, 'third': {'hidden': True}}),
|
||||
('subod.subsubodval1.firstval1', None),
|
||||
('subod.subsubodval1.secondval1', None),
|
||||
('subod.subsubodval1.thirdval1', None),
|
||||
('subod.subsubodval2.firstval2', None),
|
||||
('subod.subsubodval2.secondval2', None),
|
||||
('subod.subsubodval2.thirdval2', None)]),
|
||||
#test a config with hidden subsubod
|
||||
OrderedDict([('subod.subsubod.first', {'subsubod': {'hidden': True}}),
|
||||
('subod.subsubod.second', {'subsubod': {'hidden': True}}),
|
||||
('subod.subsubod.third', {'subsubod': {'hidden': True}})]),
|
||||
#test a config with hidden dyn subsubod
|
||||
OrderedDict([('subod.subsubod.first', {'subsubod': {'dyn': True, 'hidden': True}}),
|
||||
('subod.subsubod.second', {'subsubod': {'dyn': True, 'hidden': True}}),
|
||||
('subod.subsubod.third', {'subsubod': {'dyn': True, 'hidden': True}}),
|
||||
('subod.subsubodval1.firstval1', None),
|
||||
('subod.subsubodval1.secondval1', None),
|
||||
('subod.subsubodval1.thirdval1', None),
|
||||
@ -883,63 +921,40 @@ def paths(request):
|
||||
|
||||
|
||||
def test_options(paths):
|
||||
def get_kwargs_option(options, kwargs, od=False):
|
||||
if options.get('hidden', False) is True:
|
||||
kwargs['permissive'] = True
|
||||
if not od:
|
||||
kwargs.setdefault('extra_properties', []).append('hidden')
|
||||
if options.get('disabled', False) is True:
|
||||
kwargs['propertyerror'] = True
|
||||
if not od:
|
||||
kwargs.setdefault('extra_properties', []).append('disabled')
|
||||
|
||||
def get_kwargs(path):
|
||||
kwargs = {}
|
||||
spath = path.split('.')
|
||||
get_kwargs_option(paths[path].get(spath[-1], {}), kwargs)
|
||||
if len(spath) > 1:
|
||||
get_kwargs_option(paths[path].get(spath[-2], {}), kwargs, od=True)
|
||||
return kwargs
|
||||
|
||||
lpaths = list(paths.keys())
|
||||
for multi in (False, True):
|
||||
api = make_api(paths, multi)
|
||||
api, weakrefs = make_api(paths, multi)
|
||||
if api is None:
|
||||
continue
|
||||
if len(lpaths) == 9:
|
||||
check_all(api, lpaths[3], multi)
|
||||
check_all(api, lpaths[4], multi, propertyerror=True, extra_properties=['disabled'])
|
||||
check_all(api, lpaths[5], multi, permissive=True, extra_properties=['hidden'])
|
||||
check_all(api, lpaths[6], multi)
|
||||
check_all(api, lpaths[7], multi, propertyerror=True, extra_properties=['disabled'])
|
||||
check_all(api, lpaths[8], multi, permissive=True, extra_properties=['hidden'])
|
||||
check_all(api, lpaths[3], multi, **get_kwargs(lpaths[0]))
|
||||
check_all(api, lpaths[4], multi, **get_kwargs(lpaths[1]))
|
||||
check_all(api, lpaths[5], multi, **get_kwargs(lpaths[2]))
|
||||
check_all(api, lpaths[6], multi, **get_kwargs(lpaths[0]))
|
||||
check_all(api, lpaths[7], multi, **get_kwargs(lpaths[1]))
|
||||
check_all(api, lpaths[8], multi, **get_kwargs(lpaths[2]))
|
||||
else:
|
||||
check_all(api, lpaths[0], multi)
|
||||
check_all(api, lpaths[1], multi, propertyerror=True, extra_properties=['disabled'])
|
||||
check_all(api, lpaths[2], multi, permissive=True, extra_properties=['hidden'])
|
||||
|
||||
|
||||
DICT_PATHS2 = [
|
||||
OrderedDict([('subod.subsubod.first', {'subsubod': {'hidden': True}}),
|
||||
('subod.subsubod.second', {}),
|
||||
('subod.subsubod.third', {})]),
|
||||
OrderedDict([('subod.subsubod.first', {'subsubod': {'dyn': True, 'hidden': True}}),
|
||||
('subod.subsubod.second', {}),
|
||||
('subod.subsubod.third', {}),
|
||||
('subod.subsubodval1.firstval1', None),
|
||||
('subod.subsubodval1.secondval1', None),
|
||||
('subod.subsubodval1.thirdval1', None),
|
||||
('subod.subsubodval2.firstval2', None),
|
||||
('subod.subsubodval2.secondval2', None),
|
||||
('subod.subsubodval2.thirdval2', None)])
|
||||
]
|
||||
|
||||
|
||||
@pytest.fixture(scope="function", params=DICT_PATHS2)
|
||||
def paths2(request):
|
||||
if DISPLAY:
|
||||
print(u'\n{} {}: {}'.format(ICON, request.function.__name__, request.param))
|
||||
return request.param
|
||||
|
||||
|
||||
def test_tree_od_permissive(paths2):
|
||||
"""permissive when optiondescription is hidden
|
||||
"""
|
||||
lpaths = list(paths2.keys())
|
||||
for multi in (False, True):
|
||||
api = make_api(paths2, multi)
|
||||
if api is None:
|
||||
continue
|
||||
if len(lpaths) == 9:
|
||||
check_all(api, lpaths[3], multi, permissive=True)
|
||||
check_all(api, lpaths[4], multi, permissive=True)
|
||||
check_all(api, lpaths[5], multi, permissive=True)
|
||||
check_all(api, lpaths[6], multi, permissive=True)
|
||||
check_all(api, lpaths[7], multi, permissive=True)
|
||||
check_all(api, lpaths[8], multi, permissive=True)
|
||||
else:
|
||||
check_all(api, lpaths[0], multi, permissive=True)
|
||||
check_all(api, lpaths[1], multi, permissive=True)
|
||||
check_all(api, lpaths[2], multi, permissive=True)
|
||||
check_all(api, lpaths[0], multi, **get_kwargs(lpaths[0]))
|
||||
check_all(api, lpaths[1], multi, **get_kwargs(lpaths[1]))
|
||||
check_all(api, lpaths[2], multi, **get_kwargs(lpaths[2]))
|
||||
del(api)
|
||||
for wr in weakrefs:
|
||||
assert wr() is None
|
||||
|
Reference in New Issue
Block a user