Browse Source

Add a test on LeCrud.select() method (test order, groups, limit and offset arguments) + small bugfixes

Yann Weber 9 years ago
parent
commit
cf691c6538
6 changed files with 122 additions and 26 deletions
  1. 2
    2
      leapi/leclass.py
  2. 21
    9
      leapi/lecrud.py
  3. 1
    4
      leapi/leobject.py
  4. 2
    2
      leapi/letype.py
  5. 90
    2
      leapi/test/test_lecrud.py
  6. 6
    7
      leapi/test/test_lerelation.py

+ 2
- 2
leapi/leclass.py View File

@@ -37,6 +37,6 @@ class _LeClass(_LeObject):
37 37
         return cls.fieldtypes().keys()
38 38
 
39 39
     @classmethod
40
-    def get(cls, query_filters, field_list=None, order=None, group=None, limit=None, offset=0):
40
+    def get(cls, query_filters, field_list=None, order=None, groups=None, limit=None, offset=0):
41 41
         query_filters.append(('class_id', '=', cls._class_id))
42
-        return super().get(query_filters, field_list, order=order, group=group, limit=limit, offset=offset)
42
+        return super().get(query_filters, field_list, order=order, groups=groups, limit=limit, offset=offset)

+ 21
- 9
leapi/lecrud.py View File

@@ -8,6 +8,9 @@ import warnings
8 8
 import importlib
9 9
 import re
10 10
 
11
+REL_SUP = 0
12
+REL_SUB = 1
13
+
11 14
 class LeApiErrors(Exception):
12 15
     ## @brief Instanciate a new exceptions handling multiple exceptions
13 16
     # @param exptexptions dict : A list of data check Exception with concerned field (or stuff) as key
@@ -257,7 +260,7 @@ class _LeCrud(object):
257 260
     # @return A list of lodel editorial components instance
258 261
     # @todo think about LeObject and LeClass instanciation (partial instanciation, etc)
259 262
     @classmethod
260
-    def get(cls, query_filters, field_list=None, order=None, group=None, limit=None, offset=0):
263
+    def get(cls, query_filters, field_list=None, order=None, groups=None, limit=None, offset=0):
261 264
         if field_list is None or len(field_list) == 0:
262 265
             #default field_list
263 266
             field_list = cls.fieldlist()
@@ -273,11 +276,11 @@ class _LeCrud(object):
273 276
             if isinstance(order, Exception):
274 277
                 raise order #can be buffered and raised later, but _prepare_filters raise when fails
275 278
 
276
-        #preparing group
277
-        if group:
278
-            group = cls._prepare_order_fields(group)
279
-            if isinstance(group, Exception):
280
-                raise group # can also be buffered and raised later
279
+        #preparing groups
280
+        if groups:
281
+            groups = cls._prepare_order_fields(groups)
282
+            if isinstance(groups, Exception):
283
+                raise groups # can also be buffered and raised later
281 284
 
282 285
         #checking limit and offset values
283 286
         if not (limit is None):
@@ -288,7 +291,16 @@ class _LeCrud(object):
288 291
                 raise ValueError("Invalid offset given : %d"%offset)
289 292
 
290 293
         #Fetching editorial components from datasource
291
-        results = cls._datasource.select(cls, field_list, filters, relational_filters, order=order, group=group, limit=limit, offset=offset)
294
+        results = cls._datasource.select(
295
+                                            target_cls = cls,
296
+                                            field_list = field_list,
297
+                                            filters = filters,
298
+                                            rel_filters = relational_filters,
299
+                                            order=order,
300
+                                            groups=groups,
301
+                                            limit=limit,
302
+                                            offset=offset
303
+                                        )
292 304
 
293 305
         return results
294 306
 
@@ -553,7 +565,7 @@ class _LeCrud(object):
553 565
 # ( RELATION, NATURE ) where NATURE is a string ( see EditorialModel.classtypes ) and RELATION is one of
554 566
 # the defined constant : 
555 567
 #
556
-# - leapi.leapi.REL_SUB for "subordinates"
557
-# - leapi.leapi.REL_SUP for "superiors"
568
+# - leapi.lecrud.REL_SUB for "subordinates"
569
+# - leapi.lecrud.REL_SUP for "superiors"
558 570
 #
559 571
 # @note The filters translation process also check if given field are valids compared to the concerned letype and/or the leclass

+ 1
- 4
leapi/leobject.py View File

@@ -15,14 +15,11 @@ import copy
15 15
 import warnings
16 16
 
17 17
 import leapi
18
-from leapi.lecrud import _LeCrud
18
+from leapi.lecrud import _LeCrud, REL_SUP, REL_SUB
19 19
 from leapi.lefactory import LeFactory
20 20
 import EditorialModel
21 21
 from EditorialModel.types import EmType
22 22
 
23
-REL_SUP = 0
24
-REL_SUB = 1
25
-
26 23
 ## @brief Main class to handle objects defined by the types of an Editorial Model
27 24
 class _LeObject(_LeCrud):
28 25
     

+ 2
- 2
leapi/letype.py View File

@@ -65,9 +65,9 @@ class _LeType(_LeClass):
65 65
         return cls._fields
66 66
 
67 67
     @classmethod
68
-    def get(cls, query_filters, field_list = None):
68
+    def get(cls, query_filters, field_list = None, order = None, groups = None, limit = None, offset = 0):
69 69
         query_filters.append(('type_id', '=', cls._type_id))
70
-        return super().get(query_filters, field_list)
70
+        return super().get(query_filters, field_list, order, groups, limit, offset)
71 71
 
72 72
     @classmethod
73 73
     def fieldtypes(cls):

+ 90
- 2
leapi/test/test_lecrud.py View File

@@ -235,7 +235,7 @@ class LeCrudTestCase(TestCase):
235 235
     ## @todo test invalid get
236 236
     @patch('DataSource.dummy.leapidatasource.DummyDatasource.select')
237 237
     def test_get(self, dsmock):
238
-        """ Test the get method without group, limit, sort or offset """
238
+        """ Test the select method without group, limit, sort or offset """
239 239
         from dyncode import Publication, Numero, LeObject, Textes
240 240
         
241 241
         args = [
@@ -299,9 +299,97 @@ class LeCrudTestCase(TestCase):
299 299
 
300 300
         for callcls, field_list, filters, fl_ds, filters_ds, rfilters_ds in args:
301 301
             callcls.get(filters, field_list)
302
-            dsmock.assert_called_with(callcls, fl_ds, filters_ds, rfilters_ds, order=None, group=None, limit=None, offset=0)
302
+            dsmock.assert_called_with(
303
+                                        target_cls = callcls,
304
+                                        field_list = fl_ds,
305
+                                        filters = filters_ds,
306
+                                        rel_filters = rfilters_ds,
307
+                                        order=None,
308
+                                        groups=None,
309
+                                        limit=None,
310
+                                        offset=0
311
+                                    )
303 312
             dsmock.reset_mock()
304 313
     
314
+    @patch('DataSource.dummy.leapidatasource.DummyDatasource.select')
315
+    def test_get_complete(self, dsmock):
316
+        """ Test the select method with group limit sort and offset arguments """
317
+        from dyncode import Numero
318
+
319
+        args = [
320
+            (
321
+                Numero,
322
+                {
323
+                    'query_filters': [],
324
+                    'field_list': ['lodel_id'],
325
+                    'groups': ['titre'],
326
+                    'limit': 10,
327
+
328
+                },
329
+                {
330
+                    'target_cls': Numero,
331
+                    'field_list': ['lodel_id'],
332
+                    'filters': [],
333
+                    'rel_filters': [],
334
+                    'groups': [('titre', 'ASC')],
335
+                    'order': None,
336
+                    'limit': 10,
337
+                    'offset': 0,
338
+                },
339
+            ),
340
+            (
341
+                Numero,
342
+                {
343
+                    'query_filters': ['superior.parent = 20'],
344
+                    'field_list': ['lodel_id'],
345
+                    'offset': 1024,
346
+                    'order': ['titre', ('lodel_id', 'desc')]
347
+
348
+                },
349
+                {
350
+                    'target_cls': Numero,
351
+                    'field_list': ['lodel_id'],
352
+                    'filters': [],
353
+                    'rel_filters': [((leapi.lecrud.REL_SUP, 'parent'), '=', '20')],
354
+                    'groups': None,
355
+                    'order': [('titre', 'ASC'), ('lodel_id', 'DESC')],
356
+                    'limit': None,
357
+                    'offset': 1024,
358
+                },
359
+            ),
360
+            (
361
+                Numero,
362
+                {
363
+                    'query_filters': ['superior.parent = 20'],
364
+                    'field_list': ['lodel_id'],
365
+                    'offset': 1024,
366
+                    'limit': 2,
367
+                    'order': ['titre', ('lodel_id', 'desc')],
368
+                    'groups': ['titre'],
369
+
370
+                },
371
+                {
372
+                    'target_cls': Numero,
373
+                    'field_list': ['lodel_id'],
374
+                    'filters': [],
375
+                    'rel_filters': [((leapi.lecrud.REL_SUP, 'parent'), '=', '20')],
376
+                    'groups': [('titre', 'ASC')],
377
+                    'order': [('titre', 'ASC'), ('lodel_id', 'DESC')],
378
+                    'limit': 2,
379
+                    'offset': 1024,
380
+                },
381
+            ),
382
+        ]
383
+
384
+        for callcls, getargs, exptargs in args:
385
+            if callcls.implements_letype:
386
+                exptargs['filters'].append( ('type_id', '=', callcls._type_id) )
387
+            if callcls.implements_leclass:
388
+                exptargs['filters'].append( ('class_id', '=', callcls._class_id) )
389
+            callcls.get(**getargs) 
390
+            dsmock.assert_called_once_with(**exptargs)
391
+            dsmock.reset_mock()
392
+                
305 393
     #
306 394
     # Utils methods check
307 395
     #

+ 6
- 7
leapi/test/test_lerelation.py View File

@@ -82,7 +82,7 @@ class LeHierarch(LeRelationTestCase):
82 82
     
83 83
     @patch('DataSource.dummy.leapidatasource.DummyDatasource.select')
84 84
     def test_get(self, dsmock):
85
-        """ Tests the LeHierarch.get() method """
85
+        """ Tests the LeHierarch.get() method without limit group order etc."""
86 86
         from dyncode import LeCrud, Publication, Numero, Personnes, LeObject, Rubrique, LeHierarch, LeRelation
87 87
 
88 88
         queries = [
@@ -114,12 +114,11 @@ class LeHierarch(LeRelationTestCase):
114 114
             cargs = dsmock.call_args
115 115
 
116 116
             self.assertEqual(len(cargs), 2)
117
-            cargs=cargs[0]
118
-
119
-            self.assertEqual(cargs[0], target, "%s != %s"%(cargs, eargs))
120
-            self.assertEqual(set(cargs[1]), set(field_ds), "%s != %s"%(cargs, eargs))
121
-            self.assertEqual(cargs[2], filters_ds, "%s != %s"%(cargs, eargs))
122
-            self.assertEqual(cargs[3], rfilters_ds, "%s != %s"%(cargs, eargs))
117
+            cargs=cargs[1]
118
+            self.assertEqual(cargs['target_cls'], target, "%s != %s"%(cargs, eargs))
119
+            self.assertEqual(set(cargs['field_list']), set(field_ds), "%s != %s"%(cargs, eargs))
120
+            self.assertEqual(cargs['filters'], filters_ds, "%s != %s"%(cargs, eargs))
121
+            self.assertEqual(cargs['rel_filters'], rfilters_ds, "%s != %s"%(cargs, eargs))
123 122
 
124 123
             dsmock.reset_mock()
125 124
     

Loading…
Cancel
Save