Browse Source

Write some test for LeRelation + __eq__ for LeObject + import bugfix in LeRelation

The test are skipped and can be unskipped as soon as LeRelation._prepare_filters() is implemented
Yann Weber 8 years ago
parent
commit
6da730f782
3 changed files with 126 additions and 2 deletions
  1. 5
    0
      leapi/leobject.py
  2. 3
    2
      leapi/lerelation.py
  3. 118
    0
      leapi/test/test_lerelation.py

+ 5
- 0
leapi/leobject.py View File

@@ -51,11 +51,16 @@ class _LeObject(_LeCrud):
51 51
     
52 52
     ## @brief Dirty & quick comparison implementation
53 53
     def __cmp__(self, other):
54
+        return 0 if self == other else 1
55
+    ## @brief Dirty & quick equality implementation
56
+    # @todo check class
57
+    def __eq__(self, other):
54 58
         uid_fname = self.uidname()
55 59
         if not hasattr(other, uid_fname):
56 60
             return False
57 61
         return getattr(self, uid_fname) == getattr(other, uid_fname)
58 62
         
63
+        
59 64
     ## @brief Quick str cast method implementation
60 65
     def __str__(self):
61 66
         return "<%s lodel_id=%d>"%(self.__class__, getattr(self, self.uidname()))

+ 3
- 2
leapi/lerelation.py View File

@@ -4,6 +4,7 @@ import copy
4 4
 
5 5
 import EditorialModel.fieldtypes.leo as ft_leo
6 6
 from . import lecrud
7
+from . import leobject
7 8
 
8 9
 ## @brief Main class for relations
9 10
 class _LeRelation(lecrud._LeCrud):
@@ -22,12 +23,12 @@ class _LeRelation(lecrud._LeCrud):
22 23
  
23 24
     @classmethod
24 25
     def sup_filter(self, leo):
25
-        if isinstance(leo, _LeObject):
26
+        if isinstance(leo, leobject._LeObject):
26 27
             return ('lesup', '=', leo)
27 28
 
28 29
     @classmethod
29 30
     def sub_filter(self, leo):
30
-        if isinstance(leo, _LeObject):
31
+        if isinstance(leo, leobject._LeObject):
31 32
             return ('lesub', '=', leo)
32 33
 
33 34
 

+ 118
- 0
leapi/test/test_lerelation.py View File

@@ -0,0 +1,118 @@
1
+"""
2
+    Tests for _LeRelation object family
3
+"""
4
+
5
+import unittest
6
+from unittest import TestCase
7
+from unittest.mock import patch
8
+
9
+import EditorialModel
10
+import leapi
11
+import leapi.test.utils
12
+from leapi.lecrud import _LeCrud
13
+
14
+class LeRelationTestCase(TestCase):
15
+    @classmethod
16
+    def setUpClass(cls):
17
+        """ Write the generated code in a temporary directory and import it """
18
+        cls.tmpdir = leapi.test.utils.tmp_load_factory_code()
19
+    @classmethod
20
+    def tearDownClass(cls):
21
+        """ Remove the temporary directory created at class setup """
22
+        leapi.test.utils.cleanup(cls.tmpdir)
23
+    
24
+    @unittest.skip("Wating LeRelation._prepare_filters() to unskip")
25
+    def test_prepare_filters(self):
26
+        """ Testing the _prepare_filters() method """
27
+        from dyncode import Numero, LeObject, LeRelation
28
+        
29
+        filters = [
30
+            (
31
+                'rank = 1',
32
+                ('rank', '=', '1')
33
+            ),
34
+            (
35
+                'nature = "parent"',
36
+                ('nature', '=', '"parent"')
37
+            ),
38
+            (
39
+                'lesup = 21',
40
+                ('lesup', '=', LeObject(21))
41
+            ),
42
+            (
43
+                'lesub = 22',
44
+                ('lesub', '=', LeObject(22))
45
+            ),
46
+            (
47
+                ('rank', '=', '1'),
48
+                ('rank', '=', '1'),
49
+            ),
50
+            (
51
+                ('lesup', '=', LeObject(21)),
52
+                ('lesup', '=', LeObject(21)),
53
+            ),
54
+            (
55
+                ('lesub', '=', Numero(42)),
56
+                ('lesub', '=', Numero(42)),
57
+            ),
58
+        ]
59
+        
60
+        for filter_arg, filter_res in filters:
61
+            res, rel_res = LeRelation._prepare_filters([filter_arg])
62
+            self.assertEqual(len(res), 1)
63
+            self.assertEqual(len(rel_res), 0)
64
+            res = res[0]
65
+            
66
+            for i in range(3):
67
+               self.assertEqual(filter_res[i], res[i], "%s != %s"%(filter_res, res))
68
+
69
+
70
+class LeHierarch(LeRelationTestCase):
71
+    
72
+    @unittest.skip("Wait for  LeRelation._prepare_filters() to unskip")
73
+    @patch('leapi.datasources.dummy.DummyDatasource.select')
74
+    def test_get(self, dsmock):
75
+        from dyncode import LeCrud, Publication, Numero, Personnes, LeObject, Rubrique, LeHierarch, LeRelation
76
+
77
+        queries = [
78
+            (
79
+                ['lesup = 42', 'lesub = 24'], #filters
80
+                ['lesup', 'lesub', 'nature', 'rank'], #field_l
81
+
82
+                LeHierarch, #target
83
+                ['lesup', 'lesub', 'nature', 'rank'], #field_ds
84
+                [('lesup','=',LeObject(42)), ('lesub', '=', LeObject(24))], #filters_ds
85
+                [], #rfilters_ds
86
+
87
+            ),
88
+            (
89
+                [ LeRelation.sup_filter(Numero(42)) ],
90
+                [],
91
+
92
+                LeHierarch,
93
+                [ 'nature', 'rank', 'lesub', 'depth', 'lesup'],
94
+                [('lesup', '=', Numero(42))],
95
+                [],
96
+            ),
97
+        ]
98
+
99
+        for filters, field_l, target, field_ds, filters_ds, rfilters_ds in queries:
100
+            eargs = (filters, field_l, target, field_ds, filters_ds, rfilters_ds)
101
+
102
+            LeHierarch.get(filters, field_l)
103
+            cargs = dsmock.call_args
104
+
105
+            self.assertEqual(len(cargs), 2)
106
+            cargs=cargs[0]
107
+
108
+            self.assertEqual(cargs[0], target, "%s != %s"%(cargs, eargs))
109
+            self.assertEqual(set(cargs[1]), set(field_ds), "%s != %s"%(cargs, eargs))
110
+            self.assertEqual(cargs[2], filters_ds, "%s != %s"%(cargs, eargs))
111
+            self.assertEqual(cargs[3], rfilters_ds, "%s != %s"%(cargs, eargs))
112
+
113
+            dsmock.reset_mock()
114
+        
115
+
116
+class LeRel2TypeTestCase(LeRelationTestCase):
117
+    pass
118
+

Loading…
Cancel
Save