Browse Source

Added tests + bugfix on LeType __init__ method

Yann Weber 9 years ago
parent
commit
0349007243
3 changed files with 33 additions and 5 deletions
  1. 0
    2
      leapi/lecrud.py
  2. 13
    3
      leapi/letype.py
  3. 20
    0
      leapi/test/test_lecrud.py

+ 0
- 2
leapi/lecrud.py View File

@@ -380,5 +380,3 @@ class _LeCrud(object):
380 380
     def _field_is_relational(field):
381 381
         return field.startswith('superior.') or field.startswith('subordinate')
382 382
 
383
-
384
-

+ 13
- 3
leapi/letype.py View File

@@ -11,7 +11,7 @@
11 11
 # @note LeObject will be generated by leapi.lefactory.LeFactory
12 12
 
13 13
 import leapi
14
-from leapi.lecrud import _LeCrud
14
+from leapi.lecrud import _LeCrud, LeApiDataCheckError
15 15
 from leapi.leclass import _LeClass
16 16
 from leapi.leobject import LeObjectError
17 17
 
@@ -35,7 +35,9 @@ class _LeType(_LeClass):
35 35
         if self._leclass is None:
36 36
             raise NotImplementedError("Abstract class")
37 37
 
38
-        self.lodel_id = lodel_id
38
+        self.lodel_id, err = self._uid_fieldtype['lodel_id'].check_data_value(lodel_id)
39
+        if isinstance(err, Exception):
40
+            raise err
39 41
 
40 42
         if 'type_id' in kwargs:
41 43
             if self.__class__._type_id != int(kwargs['type_id']):
@@ -45,10 +47,18 @@ class _LeType(_LeClass):
45 47
                 raise RuntimeError("Trying to instanciate a %s with a clas_id that is not correct"%self.__class__.__name__)
46 48
 
47 49
         ## Populate the object from the datas received in kwargs
50
+        err_l = list()
48 51
         for name, value in kwargs.items():
49 52
             if name not in self._fields:
50 53
                 raise AttributeError("No such field '%s' for %s"%(name, self.__class__.__name__))
51
-            setattr(self, name, value)
54
+
55
+            cvalue, err =  self.fieldtypes()[name].check_data_value(value)
56
+            if isinstance(err, Exception):
57
+                err_l.append(err)
58
+            else:
59
+                setattr(self, name, value)
60
+        if len(err_l) > 0:
61
+            raise LeApiDataCheckError("Invalid arguments given to constructor", err_l)
52 62
 
53 63
     @classmethod
54 64
     def fieldlist(cls):

+ 20
- 0
leapi/test/test_lecrud.py View File

@@ -182,6 +182,26 @@ class LeCrudTestCase(TestCase):
182 182
                 assert not dsmock.called
183 183
         pass
184 184
     
185
+    @patch('leapi.datasources.dummy.DummyDatasource.update')
186
+    def test_update(self, dsmock):
187
+        from dyncode import Publication, Numero, LeObject
188
+        
189
+        args_l = [
190
+            (
191
+                Numero,
192
+                {'lodel_id':'1'},
193
+                {'titre': 'foobar'},
194
+
195
+                [('lodel_id', '=', 1)],
196
+                []
197
+            ),
198
+        ]
199
+
200
+        for ccls, initarg, qdatas, efilters, erelfilters in args_l:
201
+            obji = ccls(**initarg)
202
+            obji.update(qdatas)
203
+            dsmock.assert_called_once_with(ccls, efilters, erelfilters, qdatas)
204
+    
185 205
     ## @todo test invalid get
186 206
     @patch('leapi.datasources.dummy.DummyDatasource.select')
187 207
     def test_get(self, dsmock):

Loading…
Cancel
Save