Browse Source

Implements delete instance methods for relations classes

Yann Weber 9 years ago
parent
commit
59d489bd67
2 changed files with 30 additions and 17 deletions
  1. 21
    6
      leapi/lerelation.py
  2. 9
    11
      leapi/test/test_lerelation.py

+ 21
- 6
leapi/lerelation.py View File

@@ -17,33 +17,42 @@ class _LeRelation(lecrud._LeCrud):
17 17
     _rel_fieldtypes = dict()
18 18
 
19 19
     def __init__(self, rel_id, **kwargs):
20
-       pass
21
-
20
+        self.id_relation = rel_id
21
+    
22
+    ## @brief Forge a filter to match the superior
22 23
     @classmethod
23 24
     def sup_filter(self, leo):
24 25
         if isinstance(leo, leobject._LeObject):
25 26
             return ('lesup', '=', leo)
26 27
 
28
+    ## @brief Forge a filter to match the superior
27 29
     @classmethod
28 30
     def sub_filter(self, leo):
29 31
         if isinstance(leo, leobject._LeObject):
30 32
             return ('lesub', '=', leo)
31 33
 
34
+    ## @return a dict with field name as key and fieldtype instance as value
32 35
     @classmethod
33 36
     def fieldtypes(cls):
34 37
         rel_ft = dict()
38
+        rel_ft.update(cls._uid_fieldtype)
39
+
35 40
         rel_ft.update(cls._lesup_fieldtype)
36 41
         rel_ft.update(cls._lesub_fieldtype)
42
+
37 43
         rel_ft.update(cls._rel_fieldtypes)
38 44
         if cls.implements_lerel2type():
39 45
             rel_ft.update(cls._rel_attr_fieldtypes)
40 46
         return rel_ft
41
-
47
+    
42 48
     @classmethod
43 49
     def _prepare_relational_fields(cls, field):
44 50
         return lecrud.LeApiQueryError("Relational field '%s' given but %s doesn't is not a LeObject" % (field,
45 51
                                                                                                         cls.__name__))
46
-
52
+    ## @brief Prepare filters before sending them to the datasource
53
+    # @param cls : Concerned class
54
+    # @param filters_l list : List of filters
55
+    # @return prepared and checked filters
47 56
     @classmethod
48 57
     def _prepare_filters(cls, filters_l):
49 58
         filters = list()
@@ -93,14 +102,20 @@ class _LeRelation(lecrud._LeCrud):
93 102
 
94 103
 ## @brief Abstract class to handle hierarchy relations
95 104
 class _LeHierarch(_LeRelation):
96
-    def __init__(self, rel_id):
97
-        pass
105
+    
106
+    ## @brief Delete current instance from DB
107
+    def delete(self):
108
+        lecrud._LeCrud._delete(self)
98 109
 
99 110
 ## @brief Abstract class to handle rel2type relations
100 111
 class _LeRel2Type(_LeRelation):
101 112
     ## @brief Stores the list of fieldtypes handling relations attributes
102 113
     _rel_attr_fieldtypes = dict()
103 114
     
115
+    ## @brief Delete current instance from DB
116
+    def delete(self):
117
+        lecrud._LeCrud._delete(self)
118
+
104 119
     ## @brief Given a superior and a subordinate, returns the classname of the give rel2type
105 120
     # @param lesupclass LeClass : LeClass child class (can be a LeType or a LeClass child)
106 121
     # @param lesubclass LeType : A LeType child class

+ 9
- 11
leapi/test/test_lerelation.py View File

@@ -67,7 +67,6 @@ class LeRelationTestCase(TestCase):
67 67
             for i in range(3):
68 68
                 self.assertEqual(filter_res[i], res[i], "%s != %s"%(filter_res, res))
69 69
 
70
-    # @unittest.skip("Wait LeRelation._prepare_filters() and LeRelation.delete() to unskip")
71 70
     @patch('DataSource.dummy.leapidatasource.DummyDatasource.delete')
72 71
     def test_delete(self, dsmock):
73 72
         """ Testing LeHierarch insert method """
@@ -101,7 +100,7 @@ class LeHierarch(LeRelationTestCase):
101 100
                 [],
102 101
 
103 102
                 LeHierarch,
104
-                [ 'nature', 'rank', 'lesub', 'depth', 'lesup'],
103
+                [ 'nature', 'rank', 'lesub', 'depth', 'lesup', 'id_relation'],
105 104
                 [('lesup', '=', Numero(42))],
106 105
                 [],
107 106
             ),
@@ -187,10 +186,9 @@ class LeHierarch(LeRelationTestCase):
187 186
             dsmock.reset_mock()
188 187
     
189 188
 
190
-    @unittest.skip("Wait for LeRelation.delete()")
191 189
     @patch('DataSource.dummy.leapidatasource.DummyDatasource.delete')
192 190
     def test_delete(self, dsmock):
193
-        """ Testing LeHierarch insert method """
191
+        """ Testing LeHierarch delete method """
194 192
         from dyncode import LeCrud, Publication, Numero, Personnes, LeObject, Rubrique, LeHierarch, LeRelation
195 193
         rel = LeHierarch(10)
196 194
         rel.delete()
@@ -209,11 +207,11 @@ class LeHierarch(LeRelationTestCase):
209 207
 
210 208
 class LeRel2TypeTestCase(LeRelationTestCase):
211 209
     
212
-    @unittest.skip("Wait implementation to unskip")
210
+    @unittest.skip("Wait for implmentation (mainly implements nature = none for non hierarch)")
213 211
     @patch('DataSource.dummy.leapidatasource.DummyDatasource.insert')
214 212
     def test_insert(self, dsmock):
215 213
         """ test LeHierach update method"""
216
-        from dyncode import LeObject, Article, Textes, Personne, Personnes, LeHierarch, LeRel2Type, Rel_textes2personne
214
+        from dyncode import LeObject, Article, Textes, Personne, Personnes, LeHierarch, LeRel2Type, Rel_Textes2Personne
217 215
 
218 216
         queries = [
219 217
             {
@@ -245,7 +243,7 @@ class LeRel2TypeTestCase(LeRelationTestCase):
245 243
         ]
246 244
 
247 245
         for query in queries:
248
-            Rel_textes2personne.insert(query)
246
+            Rel_Textes2Personne.insert(query)
249 247
 
250 248
             eres = {'nature': None}
251 249
             eres.uopdate(query)
@@ -253,19 +251,19 @@ class LeRel2TypeTestCase(LeRelationTestCase):
253 251
                 if isinstance(eres[fname], int):
254 252
                     eres[fname] = LeObject(eres[fname])
255 253
 
256
-            dsmock.assert_called_once_with(Rel_textes2personne, **eres)
254
+            dsmock.assert_called_once_with(Rel_Textes2Personne, **eres)
257 255
             dsmock.reset_mock()
258 256
 
259 257
             LeRel2Type.insert(query, "Rel_textes2personne")
260 258
 
261
-            dsmock.assert_called_once_with(Rel_textes2personne, **eres)
259
+            dsmock.assert_called_once_with(Rel_Textes2Personne, **eres)
262 260
             dsmock.reset_mock()
263 261
 
264 262
     @unittest.skip("Wait implementation to unskip")
265 263
     @patch('DataSource.dummy.leapidatasource.DummyDatasource.insert')
266 264
     def test_insert_fails(self, dsmock):
267 265
         """ test LeHierach update method"""
268
-        from dyncode import LeObject, Rubrique, Numero, Article, Textes, Personne, Personnes, LeHierarch, LeRel2Type, Rel_textes2personne
266
+        from dyncode import LeObject, Rubrique, Numero, Article, Textes, Personne, Personnes, LeHierarch, LeRel2Type, Rel_Textes2Personne
269 267
 
270 268
         queries = [
271 269
             {
@@ -321,4 +319,4 @@ class LeRel2TypeTestCase(LeRelationTestCase):
321 319
                 LeRel2Type.insert(query, 'Rel_textes2personne')
322 320
             
323 321
             with self.assertRaises(lecrud.LeApiQueryError):
324
-                Rel_textes2personne.insert(query)
322
+                Rel_Textes2Personne.insert(query)

Loading…
Cancel
Save