Browse Source

Add test + small bugfixes in fieldtypes + implementations of short utils methods

Yann Weber 8 years ago
parent
commit
a0a83ad19b

+ 5
- 6
EditorialModel/fieldtypes/leo.py View File

@@ -12,7 +12,7 @@ class EmFieldType(GenericFieldType):
12 12
     ftype = 'leobject'
13 13
 
14 14
     def __init__(self, superior=True, **kwargs):
15
-        super(EmFieldType, self).__init__(ftype = 'leobject', **kwargs)
15
+        super(EmFieldType, self).__init__(ftype = 'leobject', superior = superior, **kwargs)
16 16
 
17 17
     def _check_data_value(self, value):
18 18
         err = None
@@ -25,18 +25,17 @@ class EmFieldType(GenericFieldType):
25 25
     
26 26
     ## @brief If field value is an integer, returns a partially instanciated LeObject (only with an ID)
27 27
     def construct_data(self, lec, fname, datas):
28
-        import leapi.lecrud as lecrud
29 28
         if isinstance(datas[fname], int):
30
-            leobject = lecrud._LeCrud.name2class('LeObject')
31
-            return leobject.get(datas[fname])
29
+            leobject = lec.name2class('LeObject')
30
+            return leobject(datas[fname])
32 31
         else:
33 32
             return datas[fname]
34 33
     
35 34
     def check_data_consistency(self, lec, fname, datas):
36 35
         if self.superior:
37
-            return self.check_sup_consistency()
36
+            return self.check_sup_consistency(lec, fname, datas)
38 37
         else:
39
-            return self.check_sub_consistency()
38
+            return self.check_sub_consistency(lec, fname, datas)
40 39
 
41 40
     def check_sup_consistency(self, lec, fname, datas):
42 41
         pass

+ 21
- 12
EditorialModel/fieldtypes/naturerelation.py View File

@@ -14,26 +14,35 @@ class EmFieldType(EmFieldType):
14 14
         super(EmFieldType, self).__init__(**kwargs)
15 15
 
16 16
     def check_data_value(self, value):
17
-        return value is None or ( value in classtypes.EmNature.getall())
18
-
17
+        if value is None or ( value in classtypes.EmNature.getall()):
18
+            return value, None
19
+        else:
20
+            return None, ValueError("Unknown relation nature '%s'"%value)
21
+    
22
+    ## @todo implements types checks
19 23
     def check_data_consistency(self, lec, fname, datas):
20 24
         #Checking given component
21
-        if not isinstance(lec, lerelation.LeRelation):
22
-            return ValueError("A field naturerelation has to be in a LeRelation object, but this one is in a '%s'"%lec.__name__)
25
+        #Replace with a test from _LeCrud
26
+        #if not isinstance(lec, lerelation._LeRelation):
27
+        #    return ValueError("A field naturerelation has to be in a LeRelation object, but this one is in a '%s'"%lec.__name__)
23 28
         nature = datas[fname]
24 29
         lesup = datas['lesup']
25 30
         lesub = datas['lesub']
26 31
         if nature is None:
27
-            if not isinstance(lec, lerelation.LeRel2Type):
28
-                return ValueError("Only LeRel2Type are allowed to have NULL nature")
32
+            #if not isinstance(lec, lerelation.LeRel2Type):
33
+            #Replace with a test from _LeCrud
34
+            #    return ValueError("Only LeRel2Type are allowed to have NULL nature")
35
+            pass
29 36
         else:
30
-            if not isinstance(lec, lerelation.LeHierarch):
31
-                return ValueError("Only LeHierarch has not NULL nature")
37
+            #if not isinstance(lec, lerelation.LeHierarch):
38
+            #Replace with a test from _LeCrud
39
+            #    return ValueError("Only LeHierarch has not NULL nature")
32 40
             #Checking if nature <=> lesup & lesub classtypes
33
-            if nature not in classtypes.EmClassType.natures(lesub._classtype):
34
-                return ValueError("Invalid nature '%s' for %s"%(nature, lesub.__class__.__name__))
35
-
36
-            if not lesup.is_root():
41
+            if not lesub.is_partial():
42
+                if nature not in classtypes.EmClassType.natures(lesub._classtype):
43
+                    return ValueError("Invalid nature '%s' for %s"%(nature, lesub.__class__.__name__))
44
+            
45
+            if not lesup.is_partial() and not lesup.is_root():
37 46
                 if nature not in classtypes.EmClassType.natures(lesup._classtype):
38 47
                     return ValueError("Invalid nature '%s' for %s"%(nature, lesup.__class__.__name__))
39 48
         return True

+ 5
- 1
leapi/lecrud.py View File

@@ -169,7 +169,11 @@ class _LeCrud(object):
169 169
         #Checks datas
170 170
         checked_datas = dict()
171 171
         for name, value in [ (name, value) for name, value in datas.items() if name in correct ]:
172
-            checked_datas[name], err = cls.fieldtypes()[name].check_data_value(value)
172
+            ft = cls.fieldtypes()
173
+            ft = ft[name]
174
+            r = ft.check_data_value(value)
175
+            checked_datas[name], err = r
176
+            #checked_datas[name], err = cls.fieldtypes()[name].check_data_value(value)
173 177
             if err:
174 178
                 err_l.append(err)
175 179
 

+ 9
- 0
leapi/leobject.py View File

@@ -49,6 +49,15 @@ class _LeObject(_LeCrud):
49 49
         qfilter = '{uid_fname} = {uid}'.format(uid_fname = uid_fname, uid = getattr(self, uid_fname))
50 50
         return leobject.get([qfilter])[0]
51 51
     
52
+    ## @return True if the LeObject is partially instanciated
53
+    def is_partial(self):
54
+        return not hasattr(self, '_classtype')
55
+
56
+    ## @brief Check if a LeObject is the relation tree Root
57
+    # @todo implementation
58
+    def is_root(self):
59
+        return False
60
+
52 61
     ## @brief Dirty & quick comparison implementation
53 62
     def __cmp__(self, other):
54 63
         return 0 if self == other else 1

+ 0
- 3
leapi/lerelation.py View File

@@ -31,7 +31,6 @@ class _LeRelation(lecrud._LeCrud):
31 31
         if isinstance(leo, leobject._LeObject):
32 32
             return ('lesub', '=', leo)
33 33
 
34
-
35 34
     @classmethod
36 35
     def fieldtypes(cls):
37 36
         rel_ft = dict()
@@ -44,8 +43,6 @@ class _LeRelation(lecrud._LeCrud):
44 43
     @classmethod
45 44
     def _prepare_relational_fields(cls, field):
46 45
         return lecrud.LeApiQueryError("Relational field '%s' given but %s doesn't is not a LeObject"%(field,cls.__name__))
47
-
48
-
49 46
             
50 47
 
51 48
 ## @brief Abstract class to handle hierarchy relations

+ 36
- 1
leapi/test/test_lerelation.py View File

@@ -72,6 +72,7 @@ class LeHierarch(LeRelationTestCase):
72 72
     @unittest.skip("Wait for  LeRelation._prepare_filters() to unskip")
73 73
     @patch('leapi.datasources.dummy.DummyDatasource.select')
74 74
     def test_get(self, dsmock):
75
+        """ Tests the LeHierarch.get() method """
75 76
         from dyncode import LeCrud, Publication, Numero, Personnes, LeObject, Rubrique, LeHierarch, LeRelation
76 77
 
77 78
         queries = [
@@ -111,7 +112,41 @@ class LeHierarch(LeRelationTestCase):
111 112
             self.assertEqual(cargs[3], rfilters_ds, "%s != %s"%(cargs, eargs))
112 113
 
113 114
             dsmock.reset_mock()
114
-        
115
+    
116
+    @unittest.skip("Wait for  LeRelation._prepare_filters() to unskip")
117
+    @patch('leapi.datasources.dummy.DummyDatasource.insert')
118
+    def test_insert(self, dsmock):
119
+        from dyncode import LeCrud, Publication, Numero, Personnes, LeObject, Rubrique, LeHierarch, LeRelation
120
+        queries = [
121
+            {
122
+                'lesup': Rubrique(7),
123
+                'lesub': Numero(42),
124
+                'nature': 'parent',
125
+            },
126
+            {
127
+                'lesup': 7,
128
+                'lesub': 42,
129
+                'nature': 'parent',
130
+            },
131
+            {
132
+                'lesup': LeObject(7),
133
+                'lesub': LeObject(42),
134
+                'nature': 'parent',
135
+            },
136
+            {
137
+                'lesup': LeObject(7),
138
+                'lesub': 42,
139
+                'nature': 'parent',
140
+            }
141
+        ]
142
+        for query in queries:
143
+            LeHierarch.insert(query)
144
+            dsmock.assert_called_once_with(LeHierarch, **query)
145
+            dsmock.reset_mock()
146
+
147
+            LeRelation.insert(query, 'LeHierarch')
148
+            dsmock.assert_called_once_with(LeHierarch, **query)
149
+            dsmock.reset_mock()
115 150
 
116 151
 class LeRel2TypeTestCase(LeRelationTestCase):
117 152
     pass

Loading…
Cancel
Save