Browse Source

Added some tests to tests_types.py

Added hierarchy tests
Yann Weber 9 years ago
parent
commit
65b10a6c5d
1 changed files with 111 additions and 2 deletions
  1. 111
    2
      EditorialModel/test/test_types.py

+ 111
- 2
EditorialModel/test/test_types.py View File

@@ -30,9 +30,19 @@ def setUpModule():
30 30
 
31 31
     emclass1 = EmClass.create("entity1", EmClassType.entity)
32 32
     emclass2 = EmClass.create("entity2", EmClassType.entity)
33
+    emclass3 = EmClass.create("entry1", EmClassType.entry)
34
+    emclass4 = EmClass.create("person1", EmClassType.person)
35
+
33 36
     emtype = EmType.create(name='type1', em_class=emclass2)
34 37
     EmType.create(name='type2', em_class=emclass2)
35 38
     EmType.create(name='type3', em_class=emclass2)
39
+
40
+    EmType.create(name='type4', em_class=emclass3)
41
+    EmType.create(name='type5', em_class=emclass3)
42
+
43
+    EmType.create(name='type6', em_class=emclass4)
44
+    EmType.create(name='type7', em_class=emclass4)
45
+
36 46
     emfieldgroup = EmFieldGroup.create(name='fieldgroup1', em_class=emclass1)
37 47
     emfieldtype = get_field_type('integer')
38 48
     EmField.create(name='field1', fieldgroup=emfieldgroup, fieldtype=emfieldtype, rel_to_type_id=emtype.uid)
@@ -52,6 +62,10 @@ class TypeTestCase(TestCase):
52 62
         self.emtype = EmType('type1')
53 63
         self.emtype2 = EmType('type2')
54 64
         self.emtype3 = EmType('type3')
65
+        self.emtype4 = EmType('type4')
66
+        self.emtype5 = EmType('type5')
67
+        self.emtype6 = EmType('type6')
68
+        self.emtype7 = EmType('type7')
55 69
         self.emfieldgroup = EmFieldGroup('fieldgroup1')
56 70
         self.emfieldtype = get_field_type('integer')
57 71
         self.emfield = EmField('field1')
@@ -67,6 +81,7 @@ class TestSelectField(TypeTestCase):
67 81
         self.assertFalse(Em_Field_Type(self.emtype.uid, self.emfield.uid).exists())
68 82
 
69 83
 class TestLinkedTypes(TypeTestCase):
84
+    @unittest.skip("Not yet implemented")
70 85
     def testLinkedtypes(self):
71 86
         self.emtype.add_superior(self.emtype2, EmNature.PARENT)
72 87
         self.emtype3.add_superior(self.emtype, EmNature.PARENT)
@@ -78,10 +93,104 @@ class TestLinkedTypes(TypeTestCase):
78 93
         self.assertIn(self.emtype2, linked_types)
79 94
         self.assertIn(self.emtype3, linked_types)
80 95
 
96
+class TestTypeHierarchy(TypeTestCase):
97
+
98
+    @staticmethod
99
+    ## Replace instances by uid in subordinates or superiors return values
100
+    def _hierarch_uid(subs):
101
+        res = dict()
102
+        for nat in subs:
103
+            res[nat] = []
104
+            for sub in subs[nat]:
105
+                res[nat].append(sub.uid)
106
+        return res
107
+
108
+    # Check that the superior has been added
109
+    def check_add_sup(self, subtype, suptype, relnat):
110
+        subuid = self._hierarch_uid(suptype.subordinates())
111
+        supuid = self._hierarch_uid(subtype.superiors())
112
+
113
+        for nat in subuid:
114
+            if nat == relnat:
115
+                check = self.assertIn
116
+                msg = " should be in "
117
+            else:
118
+                check = self.assertNotIn
119
+                msg = " should not be in "
120
+            check(subtype.uid, subuid[nat], subtype.name+msg+suptype.name+" subordinates with nature '"+nat+"'")
121
+            check(suptype.uid, supuid[nat], suptype.name+msg+subtype.name+" superiors with nature '"+nat+"'")
122
+        pass
123
+            
124
+
125
+    def testAddSuperiorParent(self):
126
+        self.emtype.add_superior(self.emtype2, EmNature.PARENT)
127
+        self.check_add_sup(self.emtype, self.emtype2, EmNature.PARENT)
128
+
129
+        self.emtype4.add_superior(self.emtype4, EmNature.PARENT)
130
+        self.check_add_sup(self.emtype4, self.emtype4, EmNature.PARENT)
131
+        pass
132
+
133
+    def testAddSuperiorTranslation(self):
134
+        self.emtype.add_superior(self.emtype, EmNature.TRANSLATION)
135
+        self.check_add_sup(self.emtype, self.emtype, EmNature.TRANSLATION)
136
+
137
+        self.emtype4.add_superior(self.emtype4, EmNature.TRANSLATION)
138
+        self.check_add_sup(self.emtype4, self.emtype4, EmNature.TRANSLATION)
139
+        pass
140
+
141
+    def testAddSuperiorIdentity(self):
142
+        self.emtype6.add_superior(self.emtype6, EmNature.IDENTITY)
143
+        self.check_add_sup(self.emtype6, self.emtype6, EmNature.IDENTITY)
144
+        self.emtype6.add_superior(self.emtype7, EmNature.IDENTITY)
145
+        self.check_add_sup(self.emtype6, self.emtype6, EmNature.IDENTITY)
146
+        pass
147
+
148
+    def testIllegalSuperior(self):
149
+        illegal_combinations = [
150
+            (self.emtype, self.emtype4, EmNature.PARENT),
151
+            (self.emtype, self.emtype2, EmNature.TRANSLATION),
152
+            (self.emtype4, self.emtype5, EmNature.PARENT),
153
+            (self.emtype4, self.emtype5, EmNature.TRANSLATION),
154
+            (self.emtype6, self.emtype, EmNature.IDENTITY),
155
+            (self.emtype4, self.emtype, EmNature.PARENT),
156
+            (self.emtype6, self.emtype, EmNature.PARENT),
157
+            (self.emtype, self.emtype2, EmNature.IDENTITY),
158
+
159
+        ]
160
+        for t1, t2, rnat in illegal_combinations:
161
+            with self.assertRaises(ValueError, msg="When trying to add an illegal superior "+str(t2)+" to "+str(t1)+" with '"+rnat+"' as relation nature"):
162
+                t1.add_superior(t2, rnat)
163
+        pass
164
+    
165
+    def testDelSuperior(self):
166
+        self.emtype.add_superior(self.emtype2, EmNature.PARENT)
167
+        self.emtype.add_superior(self.emtype, EmNature.PARENT)
168
+        self.emtype.add_superior(self.emtype, EmNature.TRANSLATION)
169
+
170
+        self.emtype.del_superior(self.emtype2, EmNature.PARENT)
171
+        supuid = self._hierarch_uid(self.emtype.superiors())
172
+        self.assertNotIn(self.emtype2.uid, supuid[EmNature.PARENT], str(self.emtype2)+" should have been deleted as superior of "+str(self.emtype))
173
+
174
+        self.assertIn(self.emtype.uid, supuid[EmNature.PARENT], "Deleted more than wanted in the same relation nature")
175
+        self.assertIn(self.emtype.uid, supuid[EmNature.TRANSLATION], "Deleted more than wanted in another relation nature")
176
+        pass
177
+        
178
+        
179
+
81 180
 class TestDeleteTypes(TypeTestCase):
82
-    @unittest.skip("Test invalid, le type n'existe pas")
83 181
     def testDeleteTypes(self):
84 182
         type_name = self.emtype.name
85
-        self.emtype.delete()
183
+        self.assertTrue(self.emtype.delete(), "delete method returns False but should return True")
86 184
         with self.assertRaises(EmComponentNotExistError, msg="Type not deleted"):
87 185
             EmType(type_name)
186
+
187
+    def testUndeletableTypes(self):
188
+        type_name = self.emtype.name
189
+        self.emtype2.add_superior(self.emtype, 'parent')
190
+        self.assertFalse(self.emtype.delete(), "delete return True but should return False")
191
+        try:
192
+            tmptype = EmType(type_name)
193
+        except EmComponentNotExistError:
194
+            self.fail("The type was deleted but it has subordinates when deleted")
195
+        pass
196
+

Loading…
Cancel
Save