|
@@ -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
|
+
|