Browse Source

Adds tests for LeGetQuery + bugfixes

- separates LeGetQuery tests and LeFilteredQuery tests
- small bugfixes associated to tests
Yann Weber 8 years ago
parent
commit
bf879a833c
3 changed files with 117 additions and 37 deletions
  1. 23
    7
      lodel/leapi/query.py
  2. 0
    30
      tests/leapi/query/test_filtered.py
  3. 94
    0
      tests/leapi/query/test_get.py

+ 23
- 7
lodel/leapi/query.py View File

@@ -195,7 +195,7 @@ class LeFilteredQuery(LeQuery):
195 195
 field name" % fieldname)
196 196
                 continue   
197 197
             # Checking field against target_class
198
-            ret = self.__check_field(self._target_class, field)
198
+            ret = self._check_field(self._target_class, field)
199 199
             if isinstance(ret, Exception):
200 200
                 err_l[field] = ret
201 201
                 continue
@@ -292,7 +292,7 @@ a relational field, but %s.%s was present in the filter"
292 292
         pass
293 293
 
294 294
     @classmethod
295
-    def __check_field(cls, target_class, fieldname):
295
+    def _check_field(cls, target_class, fieldname):
296 296
         try:
297 297
             target_class.field(fieldname)
298 298
         except NameError:
@@ -463,15 +463,14 @@ class LeGetQuery(LeFilteredQuery):
463 463
         
464 464
         # Checking kwargs and assigning default values if there is some
465 465
         for argname in kwargs:
466
-            if argname not in ('order', 'group', 'limit', 'offset'):
466
+            if argname not in ('field_list', 'order', 'group', 'limit', 'offset'):
467 467
                 raise TypeError("Unexpected argument '%s'" % argname)
468 468
 
469 469
         if 'field_list' not in kwargs:
470
-            #field_list = target_class.get_field_list
471
-            self.__field_list = target_class.fieldnames(include_ro = True)
470
+            self.set_field_list(target_class.fieldnames(include_ro = True))
472 471
         else:
473
-            #target_class.check_fields(kwargs['field_list'])
474
-            self.__field_list = kwargs['field_list']
472
+            self.set_field_list(kwargs['field_list'])
473
+
475 474
         if 'order' in kwargs:
476 475
             #check kwargs['order']
477 476
             self.__order = kwargs['order']
@@ -493,6 +492,23 @@ class LeGetQuery(LeFilteredQuery):
493 492
             except ValueError:
494 493
                 raise ValueError("offset argument expected to be an integer >= 0")
495 494
     
495
+    ##@brief Set the field list
496
+    # @param field_list list | None : If None use all fields
497
+    # @return None
498
+    # @throw LeQueryError if unknown field given
499
+    def set_field_list(self, field_list):
500
+        err_l = dict()
501
+        for fieldname in field_list:
502
+            ret = self._check_field(self._target_class, fieldname)
503
+            if isinstance(ret, Exception):
504
+                expt = NameError(   "No field named '%s' in %s" % ( fieldname,
505
+                                                                    self._target_class.__name__))
506
+                err_l[fieldname] =  expt
507
+        if len(err_l) > 0:
508
+            raise LeQueryError( msg = "Error while setting field_list in a get query",
509
+                                exceptions = err_l)
510
+        self.__field_list = list(set(field_list))
511
+    
496 512
     ##@brief Execute the get query
497 513
     def execute(self, datasource):
498 514
         super().execute(datasource)

+ 0
- 30
tests/leapi/query/test_filtered.py View File

@@ -6,36 +6,6 @@ from tests.leapi.query.utils import dyncode_module as dyncode
6 6
 from lodel.leapi.leobject import LeApiDataCheckError
7 7
 from lodel.leapi.query import LeDeleteQuery, LeUpdateQuery, LeGetQuery
8 8
 
9
-class LeGetQueryTestCase(unittest.TestCase):
10
-    
11
-    def test_init_default(self):
12
-        """ Testing GetQuery instanciation default values"""
13
-        tclass_list = [ dyncode.Object,
14
-                        dyncode.Entry,
15
-                        dyncode.Person,
16
-                        dyncode.Text,
17
-                        dyncode.Section,
18
-                        dyncode.Publication,
19
-                        dyncode.Text_Person,
20
-        ]
21
-        for tclass in tclass_list:
22
-            get_q = LeGetQuery(tclass, [])
23
-            qinfos = get_q.dump_infos()
24
-            self.assertEqual(   set(qinfos['field_list']),
25
-                                set(tclass.fieldnames(True)))
26
-            self.assertEqual(   qinfos['limit'],
27
-                                None)
28
-            self.assertEqual(   qinfos['offset'],
29
-                                0)
30
-            self.assertEqual(   qinfos['group'],
31
-                                None)
32
-            self.assertEqual(   qinfos['order'],
33
-                                None)
34
-            self.assertEqual(   qinfos['query_filter'],
35
-                                ([],[]))
36
-            self.assertEqual(   qinfos['target_class'],
37
-                                tclass)
38
-
39 9
 class LeFilteredQueryTestCase(unittest.TestCase):
40 10
 
41 11
     q_classes = [ LeDeleteQuery, LeUpdateQuery, LeGetQuery ]

+ 94
- 0
tests/leapi/query/test_get.py View File

@@ -0,0 +1,94 @@
1
+import unittest
2
+
3
+import itertools
4
+
5
+import tests.loader_utils
6
+from tests.leapi.query.utils import dyncode_module as dyncode
7
+
8
+from lodel.leapi.leobject import LeApiDataCheckError
9
+from lodel.leapi.query import LeDeleteQuery, LeUpdateQuery, LeGetQuery, LeQueryError
10
+
11
+class LeGetQueryTestCase(unittest.TestCase):
12
+    
13
+    def test_init_default(self):
14
+        """ Testing GetQuery instanciation arguments default value """
15
+        tclass_list = [ dyncode.Object,
16
+                        dyncode.Entry,
17
+                        dyncode.Person,
18
+                        dyncode.Text,
19
+                        dyncode.Section,
20
+                        dyncode.Publication,
21
+                        dyncode.Text_Person ]
22
+
23
+        for tclass in tclass_list:
24
+            get_q = LeGetQuery(tclass, [])
25
+            qinfos = get_q.dump_infos()
26
+            self.assertEqual(   set(qinfos['field_list']),
27
+                                set(tclass.fieldnames(True)))
28
+            self.assertEqual(   qinfos['limit'],
29
+                                None)
30
+            self.assertEqual(   qinfos['offset'],
31
+                                0)
32
+            self.assertEqual(   qinfos['group'],
33
+                                None)
34
+            self.assertEqual(   qinfos['order'],
35
+                                None)
36
+            self.assertEqual(   qinfos['query_filter'],
37
+                                ([],[]))
38
+            self.assertEqual(   qinfos['target_class'],
39
+                                tclass)
40
+
41
+    def test_field_list(self):
42
+        """ Testing GetQuery field list argument processing """
43
+        tclass_list = [ dyncode.Object,
44
+                        dyncode.Entry,
45
+                        dyncode.Person,
46
+                        dyncode.Text,
47
+                        dyncode.Section,
48
+                        dyncode.Publication,
49
+                        dyncode.Text_Person ]
50
+        
51
+        for tclass in tclass_list:
52
+            # testing all field list possible combinations
53
+            field_list = tclass.fieldnames(True)
54
+            for r in range(1, len(field_list) + 1):
55
+                combinations = [ list(c) for c in itertools.combinations(field_list, r)]
56
+                for test_flist in combinations:
57
+                    expected = set(test_flist)
58
+                    get_q = LeGetQuery(tclass, [], field_list = test_flist)
59
+                    qinfos = get_q.dump_infos()
60
+                    self.assertEqual(   sorted(qinfos['field_list']),
61
+                                        sorted(test_flist))
62
+                
63
+    def test_field_list_duplicated(self):
64
+        """ Testing GetQuery field list argument deduplication """
65
+        tclass_list = [ dyncode.Object,
66
+                        dyncode.Text,
67
+                        dyncode.Section,
68
+                        dyncode.Publication,
69
+                        dyncode.Text_Person ]
70
+        for tclass in tclass_list:
71
+            fl = [  'lodel_id',
72
+                    'lodel_id',
73
+                    'help_text',
74
+                    'help_text',
75
+                    'help_text']
76
+            get_q = LeGetQuery(tclass, [], field_list = fl)
77
+            self.assertEqual(   sorted(list(set(fl))),
78
+                                sorted(get_q.dump_infos()['field_list']))
79
+
80
+    def test_field_list_invalid(self):
81
+        """ Testing GetQuery invalid field name detection in field list """
82
+        bad_field_lists = ( ('non-existing',),
83
+                            (1,),
84
+                            (True,),
85
+                            (None,),
86
+                            ('lodel_id', 'non-existing',),
87
+                            ('lodel_id', 1,),
88
+                            ('lodel_id', True,),
89
+                            ('lodel_id', None,) )
90
+
91
+        for bad_field_list in bad_field_lists:
92
+            with self.assertRaises(LeQueryError):
93
+                LeGetQuery(dyncode.Object, [], field_list = bad_field_list)
94
+

Loading…
Cancel
Save