better slate tests
authorEmmanuel Garette <egarette@cadoles.com>
Sun, 9 Jul 2017 19:28:22 +0000 (21:28 +0200)
committerEmmanuel Garette <egarette@cadoles.com>
Sun, 9 Jul 2017 19:28:22 +0000 (21:28 +0200)
test/test_state.py
tiramisu/option/baseoption.py
tiramisu/option/optiondescription.py
tiramisu/storage/dictionary/option.py

index 5a54c4a..cc22408 100644 (file)
@@ -1,6 +1,6 @@
-from .autopath import do_autopath
-do_autopath()
-
+#from autopath import do_autopath
+#do_autopath()
+#
 from tiramisu.option import BoolOption, UnicodeOption, SymLinkOption, \
     IntOption, IPOption, NetmaskOption, StrOption, OptionDescription, \
     DynOptionDescription, MasterSlaves
@@ -55,6 +55,7 @@ def _diff_opt(opt1, opt2):
         try:
             val1 = getattr(opt1, attr)
             msg1 = "exists"
+            tval = val1
         except:
             err1 = True
             msg1 = "not exists"
@@ -62,12 +63,13 @@ def _diff_opt(opt1, opt2):
         try:
             val2 = getattr(opt2, attr)
             msg2 = "exists"
+            tval = val2
         except:
             err2 = True
             msg2 = "not exists"
 
         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:
             assert val1 == val2
         elif attr == '_children':
@@ -78,10 +80,12 @@ def _diff_opt(opt1, opt2):
             if val1 == val2 == []:
                 pass
             else:
-                for idx, req in enumerate(val1[0][0][0]):
-                    assert val1[0][0][0][idx][0]._name == val2[0][0][0][idx][0]._name
-                    assert val1[0][0][0][idx][1] == val2[0][0][0][idx][1]
-                    assert val1[0][0][1:] == val2[0][0][1:]
+                for idx1, req1 in enumerate(val1):
+                    for idx2, req2 in enumerate(val1[idx1]):
+                        for idx3, req3 in enumerate(val1[idx1][idx2][0]):
+                            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':
             assert val1._name == val2._name
         elif attr == '_consistencies':
@@ -119,13 +123,34 @@ def _diff_opt(opt1, opt2):
             except AttributeError:
                 assert val1 == val2
         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):
                 if isinstance(val, MasterSlaves):
-                    assert val._p_.master.impl_getname() == val2[idx]._p_.master.impl_getname()
+                    lst1.append(val._p_.master.impl_getname())
+                else:
+                    lst1.append(val.impl_getname())
+            for idx, val in enumerate(val2):
+                if isinstance(val, MasterSlaves):
+                    lst2.append(val._p_.master.impl_getname())
                 else:
-                    assert val.impl_getname() == val2[idx].impl_getname()
+                    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)
 
 
@@ -198,6 +223,18 @@ def test_diff_opt():
     _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():
     b = BoolOption('b', '')
     b.impl_set_information('doc', 'oh')
@@ -347,6 +384,28 @@ def test_state_config2():
         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():
     b = BoolOption('b', '')
     u = UnicodeOption('u', '', requires=[{'option': b, 'expected': True, 'action': 'disabled', 'inverse': True}])
index 3b0102e..a4999d2 100644 (file)
@@ -104,10 +104,10 @@ def validate_callback(callback, callback_params, type_, callbackoption):
                             cur_opt = option
                         if cur_opt != callbackoption:
                             if not getattr(cur_opt, '_dependencies', None):
-                                options = []
+                                options = set()
                             else:
-                                options = list(cur_opt._dependencies)
-                            options.append(callbackoption)
+                                options = set(cur_opt._dependencies)
+                            options.add(callbackoption)
                             cur_opt._dependencies = tuple(options)
 #____________________________________________________________
 #
@@ -274,11 +274,16 @@ class BaseOption(Base):
                 self._dependencies = []
                 for dependency in self._state_dependencies:
                     option = descr.impl_get_opt_by_path(dependency)
-                    if option.impl_is_optiondescription() and \
-                            option.impl_get_group_type() == groups.master:
-                        master_path = dependency + '.' + dependency.split('.')[-1]
-                        option = descr.impl_get_opt_by_path(master_path).impl_get_master_slaves()
+                    if option.impl_is_optiondescription():
+                        # 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]
+                            option = descr.impl_get_opt_by_path(master_path).impl_get_master_slaves()
                     self._dependencies.append(option)
+                self._dependencies = tuple(self._dependencies)
                 del(self._state_dependencies)
             else:
                 self._state_dependencies = []
@@ -995,10 +1000,10 @@ def validate_requires_arg(new_option, multi, requires, name):
     """
     def set_dependency(option):
         if not getattr(option, '_dependencies', None):
-            options = []
+            options = set()
         else:
-            options = list(option._dependencies)
-        options.append(new_option)
+            options = set(option._dependencies)
+        options.add(new_option)
         option._dependencies = tuple(options)
 
     def get_option(require):
index 32abf7b..9aa803a 100644 (file)
@@ -127,10 +127,10 @@ class OptionDescription(BaseOption, StorageOptionDescription):
             else:
                 if option.impl_is_master_slaves('master'):
                     if not getattr(option, '_dependencies', None):
-                        options = []
+                        options = set()
                     else:
-                        options = list(option._dependencies)
-                    options.append(option.impl_get_master_slaves())
+                        options = set(option._dependencies)
+                    options.add(option.impl_get_master_slaves())
                     option._dependencies = tuple(options)
                 option._set_readonly(True)
                 is_multi = option.impl_is_multi()
index df6692a..fecbf84 100644 (file)
@@ -219,7 +219,7 @@ class StorageBase(object):
     def _impl_convert_callback(self, descr, load=False):
         if self.__class__.__name__ in ['OptionDescription', 'SymLinkOption']:
             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
         elif load and self._state_val_call is None:
             del(self._state_val_call)