Browse Source

Bugfix in lefactory method that allow to sort EmClass by dependencies + test update

Yann Weber 9 years ago
parent
commit
01df586379
2 changed files with 11 additions and 19 deletions
  1. 3
    14
      lodel/leapi/lefactory.py
  2. 8
    5
      tests/leapi/test_lefactory.py

+ 3
- 14
lodel/leapi/lefactory.py View File

46
 # @return a list of EmClass instances
46
 # @return a list of EmClass instances
47
 def emclass_sorted_by_deps(emclass_list):
47
 def emclass_sorted_by_deps(emclass_list):
48
     def emclass_deps_cmp(cls_a, cls_b):
48
     def emclass_deps_cmp(cls_a, cls_b):
49
-        if len(cls_a.parents) + len(cls_b.parents) == 0:
50
-            return 0
51
-        elif len(cls_a.parents) == 0:
52
-            return -1
53
-        elif len(cls_b.parents) == 0:
54
-            return 1
55
-
56
-        if cls_a in cls_b.parents_recc:
57
-            return -1
58
-        elif cls_b in cls_a.parents_recc:
59
-            return 1
60
-        else:
61
-            return 0
62
-    return sorted(emclass_list, key = functools.cmp_to_key(emclass_deps_cmp))
49
+        return len(cls_a.parents_recc) - len(cls_b.parents_recc)
50
+    ret = sorted(emclass_list, key = functools.cmp_to_key(emclass_deps_cmp))
51
+    return ret
63
 
52
 
64
 ## @brief Returns a list of EmClass that will be represented as LeObject child classes
53
 ## @brief Returns a list of EmClass that will be represented as LeObject child classes
65
 def get_classes(model):
54
 def get_classes(model):

+ 8
- 5
tests/leapi/test_lefactory.py View File

1
 #-*- coding: utf-8 -*-
1
 #-*- coding: utf-8 -*-
2
 
2
 
3
 import unittest
3
 import unittest
4
+import random
5
+
4
 from lodel.editorial_model.model import EditorialModel
6
 from lodel.editorial_model.model import EditorialModel
5
 from lodel.leapi import lefactory
7
 from lodel.leapi import lefactory
6
 
8
 
20
         cls3 = model.new_class('testclass3', parents = [cls2])
22
         cls3 = model.new_class('testclass3', parents = [cls2])
21
         cls4 = model.new_class('testclass4', parents = [cls1, cls3])
23
         cls4 = model.new_class('testclass4', parents = [cls1, cls3])
22
         cls5 = model.new_class('testclass5', parents = [cls4])
24
         cls5 = model.new_class('testclass5', parents = [cls4])
23
-        cls6 = model.new_class('testclass6')
25
+        cls6 = model.new_class('testclass6', parents = [cls5])
24
 
26
 
25
         cls1.new_field('testfield1', data_handler='varchar')
27
         cls1.new_field('testfield1', data_handler='varchar')
26
         cls1.new_field('testfield2', data_handler='varchar', nullable = True)
28
         cls1.new_field('testfield2', data_handler='varchar', nullable = True)
35
     def test_emclass_sorted_by_deps(self):
37
     def test_emclass_sorted_by_deps(self):
36
         """ Test the function that sort EmClass by dependencies """
38
         """ Test the function that sort EmClass by dependencies """
37
         cls_list = self.model.classes()
39
         cls_list = self.model.classes()
38
-        sorted_cls = lefactory.emclass_sorted_by_deps(cls_list)
39
-        seen = set() # Stores the EmClass allready seen will walking through array
40
         
40
         
41
-        for _ in range(10): #Bad sorts algorithm can have random behavior
41
+        for _ in range(100): #Bad sorts algorithm can have random behavior
42
+            random.shuffle(cls_list)
43
+            sorted_cls = lefactory.emclass_sorted_by_deps(cls_list)
44
+            seen = set() # Stores the EmClass allready seen will walking through array
42
             for emclass in sorted_cls:
45
             for emclass in sorted_cls:
43
-                for parent in emclass.parents:
46
+                for parent in emclass.parents_recc:
44
                     self.assertIn(parent, seen)
47
                     self.assertIn(parent, seen)
45
                 seen |= set((emclass,))
48
                 seen |= set((emclass,))
46
 
49
 

Loading…
Cancel
Save