Browse Source

More tests on d_hash() + some fixes

Yann Weber 8 years ago
parent
commit
a102a064e8

+ 8
- 1
lodel/editorial_model/components.py View File

@@ -110,6 +110,9 @@ class EmClass(EmComponent):
110 110
         payload = str(super().d_hash()) + ("1" if self.abstract else "0")
111 111
         for p in sorted(self.parents):
112 112
             payload += str(p.d_hash())
113
+        for fuid in sorted(self.__fields.keys()):
114
+            payload += str(self.__fields[fuid].d_hash())
115
+            
113 116
         m.update(bytes(payload, 'utf-8'))
114 117
         return int.from_bytes(m.digest(), byteorder='big')
115 118
 
@@ -226,7 +229,11 @@ class EmGroup(object):
226 229
 
227 230
     def d_hash(self):
228 231
         
229
-        payload = "%s%s%s" % (self.uid, hash(self.display_name), hash(self.help_text))
232
+        payload = "%s%s%s" % (
233
+                                self.uid,
234
+                                'NODNAME' if self.display_name is None else self.display_name.d_hash(),
235
+                                'NOHELP' if self.help_text is None else self.help_text.d_hash()
236
+        )
230 237
         for recurs in (False, True):
231 238
             deps = self.dependencies(recurs)
232 239
             for dep_uid in sorted(deps.keys()):

+ 5
- 2
lodel/editorial_model/model.py View File

@@ -89,11 +89,14 @@ class EditorialModel(object):
89 89
     ## @brief Private getter for __groups or __classes
90 90
     # @see classes() groups()
91 91
     def __elt_getter(self, elts, uid):
92
-        return iter(elts.values()) if uid is None else elts[uid]
92
+        return list(elts.values()) if uid is None else elts[uid]
93 93
     
94 94
     ## @brief Lodel hash
95 95
     def d_hash(self):
96
-        payload = "%s%s" % (self.name,hash(self.description))
96
+        payload = "%s%s" % (
97
+                            self.name,
98
+                            'NODESC' if self.description is None else self.description.d_hash()
99
+        )
97 100
         for guid in sorted(self.__groups):
98 101
             payload += str(self.__groups[guid].d_hash())
99 102
         for cuid in sorted(self.__classes):

+ 42
- 0
tests/editorial_model/test_model.py View File

@@ -2,16 +2,44 @@
2 2
 
3 3
 import unittest
4 4
 
5
+from lodel.editorial_model.model import EditorialModel
5 6
 from lodel.editorial_model.components import EmComponent, EmClass, EmField, EmGroup
6 7
 from lodel.utils.mlstring import MlString
7 8
 from lodel.editorial_model.exceptions import *
8 9
 
10
+class EditorialModelTestCase(unittest.TestCase):
11
+    
12
+    def test_d_hash(self):
13
+        """ Test the deterministic hash method """
14
+        model = EditorialModel("test model", description = "Test EM")
15
+        cls1 = model.new_class('testclass1', display_name = 'Classe de test 1', help_text = 'super aide')
16
+        c1f1 = cls1.new_field('c1testfield1', data_handler = None)
17
+        c1f2 = cls1.new_field('c1testfield2', data_handler = None)
18
+        cls2 = model.new_class('testclass2')
19
+        c2f1 = cls2.new_field('c2testfield1', data_handler = None)
20
+        c2f2 = cls2.new_field('c2testfield2', data_handler = None)
21
+        grp1 = model.new_group('testgroup1')
22
+        grp1.add_components((cls1, c1f1))
23
+        grp2 = model.new_group('testgroup2')
24
+        grp2.add_components((cls2, c1f2, c2f1, c2f2))
25
+        grp2.add_dependencie(grp1)
26
+        e_hash = 105398984207109703509695004279282115094
27
+        self.assertEqual(model.d_hash(), e_hash)
28
+
29
+        c2f1.uid = 'foobar'
30
+        self.assertNotEqual(model.d_hash(), e_hash)
31
+
32
+        c2f1.uid = 'c2testfield1'
33
+        self.assertEqual(model.d_hash(), e_hash)
34
+
35
+
9 36
 class EmComponentTestCase(unittest.TestCase):
10 37
     
11 38
     def test_abstract_init(self):
12 39
         with self.assertRaises(NotImplementedError):
13 40
             EmComponent('test')
14 41
 
42
+
15 43
 class EmClassTestCase(unittest.TestCase):
16 44
 
17 45
     def test_init(self):
@@ -34,6 +62,14 @@ class EmClassTestCase(unittest.TestCase):
34 62
             set(['name', 'string', 'lodel_id'])
35 63
         )
36 64
 
65
+    def test_d_hash(self):
66
+        """ Test the deterministic hash method """
67
+        field = EmField('test field', 'foobar')
68
+        e_hash = 16085043663725855508634914630594968402
69
+        self.assertEqual(field.d_hash(), e_hash)
70
+        field.uid = 'test field.'
71
+        self.assertNotEqual(field.d_hash(), e_hash)
72
+
37 73
 class EmGroupTestCase(unittest.TestCase):
38 74
     
39 75
     def test_init(self):
@@ -120,3 +156,9 @@ class EmGroupTestCase(unittest.TestCase):
120 156
             for j in range(i+1,10):
121 157
                 with self.assertRaises(EditorialModelError):
122 158
                     grps[i].add_dependencie(grps[j])
159
+
160
+    def test_d_hash(self):
161
+        """ Test the deterministic hash method """
162
+        grp = EmGroup('testgrp', display_name = "Test group", help_text="No Help")
163
+        self.assertEqual(grp.d_hash(), 2280847427800301892840867965375148376323815160723628142616247375345365409972670566216414157235977332113867542043807295933781561540623070667142779076339712861412992217365501372435232184530261327450635383095)
164
+

Loading…
Cancel
Save