better slate tests

This commit is contained in:
Emmanuel Garette 2017-07-09 21:28:22 +02:00
parent c8bc3093c7
commit 01c37c3713
4 changed files with 89 additions and 25 deletions

View File

@ -1,6 +1,6 @@
from .autopath import do_autopath #from autopath import do_autopath
do_autopath() #do_autopath()
#
from tiramisu.option import BoolOption, UnicodeOption, SymLinkOption, \ from tiramisu.option import BoolOption, UnicodeOption, SymLinkOption, \
IntOption, IPOption, NetmaskOption, StrOption, OptionDescription, \ IntOption, IPOption, NetmaskOption, StrOption, OptionDescription, \
DynOptionDescription, MasterSlaves DynOptionDescription, MasterSlaves
@ -55,6 +55,7 @@ def _diff_opt(opt1, opt2):
try: try:
val1 = getattr(opt1, attr) val1 = getattr(opt1, attr)
msg1 = "exists" msg1 = "exists"
tval = val1
except: except:
err1 = True err1 = True
msg1 = "not exists" msg1 = "not exists"
@ -62,12 +63,13 @@ def _diff_opt(opt1, opt2):
try: try:
val2 = getattr(opt2, attr) val2 = getattr(opt2, attr)
msg2 = "exists" msg2 = "exists"
tval = val2
except: except:
err2 = True err2 = True
msg2 = "not exists" msg2 = "not exists"
if not err1 == err2: if not err1 == err2:
raise ValueError("{0} {1} before but {2} after for {3}".format(attr, msg1, msg2, opt1.impl_getname())) raise ValueError("{0} {1} before but {2} after for {3}: {4}".format(attr, msg1, msg2, opt1.impl_getname(), tval))
if val1 is None: if val1 is None:
assert val1 == val2 assert val1 == val2
elif attr == '_children': elif attr == '_children':
@ -78,10 +80,12 @@ def _diff_opt(opt1, opt2):
if val1 == val2 == []: if val1 == val2 == []:
pass pass
else: else:
for idx, req in enumerate(val1[0][0][0]): for idx1, req1 in enumerate(val1):
assert val1[0][0][0][idx][0]._name == val2[0][0][0][idx][0]._name for idx2, req2 in enumerate(val1[idx1]):
assert val1[0][0][0][idx][1] == val2[0][0][0][idx][1] for idx3, req3 in enumerate(val1[idx1][idx2][0]):
assert val1[0][0][1:] == val2[0][0][1:] assert val1[idx1][idx2][0][idx3][0].impl_getname() == val2[idx1][idx2][0][idx3][0].impl_getname()
assert val1[idx1][idx2][0][idx3][1] == val2[idx1][idx2][0][idx3][1]
assert val1[idx1][idx2][1:] == val2[idx1][idx2][1:], '{} - {}\n{} - {}'.format(val1, val2, val1[0][0][1:], val2[0][0][1:])
elif attr == '_opt': elif attr == '_opt':
assert val1._name == val2._name assert val1._name == val2._name
elif attr == '_consistencies': elif attr == '_consistencies':
@ -119,13 +123,34 @@ def _diff_opt(opt1, opt2):
except AttributeError: except AttributeError:
assert val1 == val2 assert val1 == val2
elif attr == '_dependencies': elif attr == '_dependencies':
assert len(val1) == len(val2) assert len(val1) == len(val2), "_dependencies has not same len: {} - {}".format(val1, val2)
lst1 = []
lst2 = []
for idx, val in enumerate(val1): for idx, val in enumerate(val1):
if isinstance(val, MasterSlaves): if isinstance(val, MasterSlaves):
assert val._p_.master.impl_getname() == val2[idx]._p_.master.impl_getname() lst1.append(val._p_.master.impl_getname())
else: else:
assert val.impl_getname() == val2[idx].impl_getname() lst1.append(val.impl_getname())
for idx, val in enumerate(val2):
if isinstance(val, MasterSlaves):
lst2.append(val._p_.master.impl_getname())
else: else:
lst2.append(val.impl_getname())
assert set(lst1) == set(lst2), '{} - {}'.format(lst1, lst2)
elif attr == '_cache_force_store_values':
for idx, tup in enumerate(val1):
assert tup[0] == val2[idx][0]
assert tup[1].impl_getname() == val2[idx][1].impl_getname()
elif attr in ['_extra', '_information']:
dico1 = {}
dico2 = {}
assert len(val1[0]) == len(val2[0])
assert set(val1[0]) == set(val2[0])
for idx, val in enumerate(val1[0]):
idx2 = val1[0].index(val)
assert val1[1][idx] == val1[1][idx2]
else:
#print(attr, val1, val2)
assert val1 == val2, "error for {}".format(attr) assert val1 == val2, "error for {}".format(attr)
@ -198,6 +223,18 @@ def test_diff_opt():
_diff_opts(o1, q) _diff_opts(o1, q)
def test_diff_optdesc():
u = UnicodeOption('u', '')
b = BoolOption('b', '')
od = OptionDescription('od', '', [b], requires=[{'option': u, 'expected': u'u', 'action': 'disabled', 'inverse': True}])
o = OptionDescription('o', '', [u, od])
o1 = OptionDescription('o1', '', [o])
a = dumps(o1)
q = loads(a)
_diff_opts(o1, q)
def test_diff_information(): def test_diff_information():
b = BoolOption('b', '') b = BoolOption('b', '')
b.impl_set_information('doc', 'oh') b.impl_set_information('doc', 'oh')
@ -347,6 +384,28 @@ def test_state_config2():
pass pass
def test_diff_opt_value():
b = BoolOption('b', '')
u = UnicodeOption('u', '', properties=('force_store_value',))
s = SymLinkOption('s', u)
o = OptionDescription('o', '', [b, u, s])
o1 = OptionDescription('o1', '', [o])
try:
cfg = Config(o1, persistent=True, session_id='29090941')
except ValueError:
cfg = Config(o1, session_id='29090941')
cfg._impl_test = True
a = dumps(cfg)
q = loads(a)
_diff_opts(cfg.cfgimpl_get_description(), q.cfgimpl_get_description())
_diff_conf(cfg, q)
try:
delete_session('config', '29090941')
except ValueError:
pass
def test_diff_opt_config(): def test_diff_opt_config():
b = BoolOption('b', '') b = BoolOption('b', '')
u = UnicodeOption('u', '', requires=[{'option': b, 'expected': True, 'action': 'disabled', 'inverse': True}]) u = UnicodeOption('u', '', requires=[{'option': b, 'expected': True, 'action': 'disabled', 'inverse': True}])

View File

@ -104,10 +104,10 @@ def validate_callback(callback, callback_params, type_, callbackoption):
cur_opt = option cur_opt = option
if cur_opt != callbackoption: if cur_opt != callbackoption:
if not getattr(cur_opt, '_dependencies', None): if not getattr(cur_opt, '_dependencies', None):
options = [] options = set()
else: else:
options = list(cur_opt._dependencies) options = set(cur_opt._dependencies)
options.append(callbackoption) options.add(callbackoption)
cur_opt._dependencies = tuple(options) cur_opt._dependencies = tuple(options)
#____________________________________________________________ #____________________________________________________________
# #
@ -274,11 +274,16 @@ class BaseOption(Base):
self._dependencies = [] self._dependencies = []
for dependency in self._state_dependencies: for dependency in self._state_dependencies:
option = descr.impl_get_opt_by_path(dependency) option = descr.impl_get_opt_by_path(dependency)
if option.impl_is_optiondescription() and \ if option.impl_is_optiondescription():
option.impl_get_group_type() == groups.master: # test if _group_type is already loaded
group_type = getattr(option, '_group_type', undefined)
if group_type is undefined:
group_type = getattr(groups, option._state_group_type)
if group_type == groups.master:
master_path = dependency + '.' + dependency.split('.')[-1] master_path = dependency + '.' + dependency.split('.')[-1]
option = descr.impl_get_opt_by_path(master_path).impl_get_master_slaves() option = descr.impl_get_opt_by_path(master_path).impl_get_master_slaves()
self._dependencies.append(option) self._dependencies.append(option)
self._dependencies = tuple(self._dependencies)
del(self._state_dependencies) del(self._state_dependencies)
else: else:
self._state_dependencies = [] self._state_dependencies = []
@ -995,10 +1000,10 @@ def validate_requires_arg(new_option, multi, requires, name):
""" """
def set_dependency(option): def set_dependency(option):
if not getattr(option, '_dependencies', None): if not getattr(option, '_dependencies', None):
options = [] options = set()
else: else:
options = list(option._dependencies) options = set(option._dependencies)
options.append(new_option) options.add(new_option)
option._dependencies = tuple(options) option._dependencies = tuple(options)
def get_option(require): def get_option(require):

View File

@ -127,10 +127,10 @@ class OptionDescription(BaseOption, StorageOptionDescription):
else: else:
if option.impl_is_master_slaves('master'): if option.impl_is_master_slaves('master'):
if not getattr(option, '_dependencies', None): if not getattr(option, '_dependencies', None):
options = [] options = set()
else: else:
options = list(option._dependencies) options = set(option._dependencies)
options.append(option.impl_get_master_slaves()) options.add(option.impl_get_master_slaves())
option._dependencies = tuple(options) option._dependencies = tuple(options)
option._set_readonly(True) option._set_readonly(True)
is_multi = option.impl_is_multi() is_multi = option.impl_is_multi()

View File

@ -219,7 +219,7 @@ class StorageBase(object):
def _impl_convert_callback(self, descr, load=False): def _impl_convert_callback(self, descr, load=False):
if self.__class__.__name__ in ['OptionDescription', 'SymLinkOption']: if self.__class__.__name__ in ['OptionDescription', 'SymLinkOption']:
return return
if not load and self.impl_get_callback() == (None, {}): if not load and getattr(self, '_val_call', None) is None:
self._state_val_call = None self._state_val_call = None
elif load and self._state_val_call is None: elif load and self._state_val_call is None:
del(self._state_val_call) del(self._state_val_call)