|
@@ -5,8 +5,12 @@
|
5
|
5
|
import unittest
|
6
|
6
|
from unittest import TestCase
|
7
|
7
|
|
|
8
|
+import EditorialModel
|
|
9
|
+import leobject
|
|
10
|
+import leobject.test.utils
|
8
|
11
|
from leobject.leobject import _LeObject
|
9
|
12
|
|
|
13
|
+## Testing static methods that don't need the generated code
|
10
|
14
|
class _LeObjectTestCase(TestCase):
|
11
|
15
|
|
12
|
16
|
def test_split_query_filter(self):
|
|
@@ -47,3 +51,154 @@ class _LeObjectTestCase(TestCase):
|
47
|
51
|
for query in invalid_queries:
|
48
|
52
|
with self.assertRaises(ValueError, msg='But the query was not valid : "%s"'%query):
|
49
|
53
|
_LeObject._split_filter(query)
|
|
54
|
+
|
|
55
|
+## Testing methods that need the generated code
|
|
56
|
+# @todo mock the datasource to test the get, update, delete and insert methods
|
|
57
|
+class LeObjectTestCase(TestCase):
|
|
58
|
+
|
|
59
|
+ @classmethod
|
|
60
|
+ def setUpClass(cls):
|
|
61
|
+ """ Write the generated code in a temporary directory and import it """
|
|
62
|
+ cls.tmpdir = leobject.test.utils.tmp_load_factory_code()
|
|
63
|
+ @classmethod
|
|
64
|
+ def tearDownClass(cls):
|
|
65
|
+ """ Remove the temporary directory created at class setup """
|
|
66
|
+ leobject.test.utils.cleanup(cls.tmpdir)
|
|
67
|
+
|
|
68
|
+ def test_uid2leobj(self):
|
|
69
|
+ """ Testing _Leobject.uid2leobj() """
|
|
70
|
+ import dyncode
|
|
71
|
+ for i in dyncode.LeObject._me_uid.keys():
|
|
72
|
+ cls = dyncode.LeObject.uid2leobj(i)
|
|
73
|
+ if leobject.letype.LeType in cls.__bases__:
|
|
74
|
+ self.assertEqual(i, cls._type_id)
|
|
75
|
+ elif leobject.leclass.LeClass in cls.__bases__:
|
|
76
|
+ self.assertEqual(i, cls._class_id)
|
|
77
|
+ else:
|
|
78
|
+ self.fail("Bad value returned : '%s'"%cls)
|
|
79
|
+ i=10
|
|
80
|
+ while i in dyncode.LeObject._me_uid.keys():
|
|
81
|
+ i+=1
|
|
82
|
+ with self.assertRaises(KeyError):
|
|
83
|
+ dyncode.LeObject.uid2leobj(i)
|
|
84
|
+
|
|
85
|
+ def test_prepare_targets(self):
|
|
86
|
+ """ Testing _prepare_targets() method """
|
|
87
|
+ from dyncode import Publication, Numero, LeObject
|
|
88
|
+
|
|
89
|
+ test_v = {
|
|
90
|
+ (None, None) : (None, None),
|
|
91
|
+
|
|
92
|
+ (Publication, Numero): (Publication, Numero),
|
|
93
|
+ (Publication, None): (Publication, None),
|
|
94
|
+ (None, Numero): (Publication, Numero),
|
|
95
|
+
|
|
96
|
+ (Publication,'Numero'): (Publication, Numero),
|
|
97
|
+ ('Publication', Numero): (Publication, Numero),
|
|
98
|
+
|
|
99
|
+ ('Publication', 'Numero'): (Publication, Numero),
|
|
100
|
+ ('Publication', None): (Publication, None),
|
|
101
|
+ (None, 'Numero'): (Publication, Numero),
|
|
102
|
+ }
|
|
103
|
+
|
|
104
|
+ for (leclass, letype), (rleclass, rletype) in test_v.items():
|
|
105
|
+ self.assertEqual((rletype,rleclass), LeObject._prepare_targets(letype, leclass))
|
|
106
|
+
|
|
107
|
+ def test_invalid_prepare_targets(self):
|
|
108
|
+ """ Testing _prepare_targets() method with invalid arguments """
|
|
109
|
+ from dyncode import Publication, Numero, LeObject, Personnes
|
|
110
|
+
|
|
111
|
+ test_v = [
|
|
112
|
+ ('',''),
|
|
113
|
+ (Personnes, Numero),
|
|
114
|
+ (leobject.leclass.LeClass, Numero),
|
|
115
|
+ (Publication, leobject.letype.LeType),
|
|
116
|
+ ('foobar', Numero),
|
|
117
|
+ (Publication, 'foobar'),
|
|
118
|
+ (Numero, Numero),
|
|
119
|
+ (Publication, Publication),
|
|
120
|
+ (None, Publication),
|
|
121
|
+ ('foobar', 'foobar'),
|
|
122
|
+ (42,1337),
|
|
123
|
+ (type, Numero),
|
|
124
|
+ (LeObject, Numero),
|
|
125
|
+ (LeObject, LeObject),
|
|
126
|
+ (Publication, LeObject),
|
|
127
|
+ ]
|
|
128
|
+
|
|
129
|
+ for (leclass, letype) in test_v:
|
|
130
|
+ with self.assertRaises(ValueError):
|
|
131
|
+ LeObject._prepare_targets(letype, leclass)
|
|
132
|
+
|
|
133
|
+ def test_check_fields(self):
|
|
134
|
+ """ Testing the _check_fields() method """
|
|
135
|
+ from dyncode import Publication, Numero, LeObject, Personnes
|
|
136
|
+
|
|
137
|
+ #Valid fields given
|
|
138
|
+ LeObject._check_fields(None, Publication, Publication._fieldtypes.keys())
|
|
139
|
+ LeObject._check_fields(Numero, None, Numero._fields)
|
|
140
|
+
|
|
141
|
+ #Specials fields
|
|
142
|
+ LeObject._check_fields(Numero, Publication, ['lodel_id'])
|
|
143
|
+ #Common fields
|
|
144
|
+ LeObject._check_fields(None, None, EditorialModel.classtypes.common_fields.keys())
|
|
145
|
+
|
|
146
|
+ #Invalid fields
|
|
147
|
+ with self.assertRaises(leobject.leobject.LeObjectQueryError):
|
|
148
|
+ LeObject._check_fields(None, None, Numero._fields)
|
|
149
|
+
|
|
150
|
+ def test_prepare_filters(self):
|
|
151
|
+ """ Testing the _prepare_filters() method """
|
|
152
|
+ from dyncode import Publication, Numero, LeObject, Personnes
|
|
153
|
+
|
|
154
|
+ #Simple filters
|
|
155
|
+ filters = [
|
|
156
|
+ 'lodel_id = 1',
|
|
157
|
+ 'superior.parent > 2'
|
|
158
|
+ ]
|
|
159
|
+
|
|
160
|
+ filt, rfilt = LeObject._prepare_filters(filters, Numero, None)
|
|
161
|
+ self.assertEqual(filt, [('lodel_id', '=', '1')])
|
|
162
|
+ self.assertEqual(rfilt, [('parent', '>', '2')])
|
|
163
|
+
|
|
164
|
+ #All fields, no relationnal and class given
|
|
165
|
+ filters = []
|
|
166
|
+ res_filt = []
|
|
167
|
+ for field in Numero._fields:
|
|
168
|
+ filters.append('%s=1'%field)
|
|
169
|
+ res_filt.append((field, '=', '1'))
|
|
170
|
+
|
|
171
|
+ filt, rfilt = LeObject._prepare_filters(filters, None, Publication)
|
|
172
|
+ self.assertEqual(rfilt, [])
|
|
173
|
+ self.assertEqual(filt, res_filt)
|
|
174
|
+
|
|
175
|
+ #Mixed type filters (tuple and string)
|
|
176
|
+ filters = [
|
|
177
|
+ ('lodel_id', '<=', '0'),
|
|
178
|
+ 'superior.parent = 2',
|
|
179
|
+ ]
|
|
180
|
+
|
|
181
|
+ filt, rfilt = LeObject._prepare_filters(filters, Numero, None)
|
|
182
|
+ self.assertEqual(filt, [('lodel_id', '<=', '0')])
|
|
183
|
+ self.assertEqual(rfilt, [('parent', '=', '2')])
|
|
184
|
+
|
|
185
|
+ def test_prepare_filters_invalid(self):
|
|
186
|
+ """ Testing the _prepare_filters() method """
|
|
187
|
+ from dyncode import Publication, Numero, LeObject, Personnes
|
|
188
|
+
|
|
189
|
+ #Numero fields filters but no letype nor leclass given
|
|
190
|
+ filters = []
|
|
191
|
+ res_filt = []
|
|
192
|
+ for field in Numero._fields:
|
|
193
|
+ filters.append('%s=1'%field)
|
|
194
|
+ res_filt.append((field, '=', '1'))
|
|
195
|
+
|
|
196
|
+ with self.assertRaises(leobject.leobject.LeObjectQueryError):
|
|
197
|
+ LeObject._prepare_filters(filters, None, None)
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+ #simply invalid filters
|
|
201
|
+ filters = ['hello world !']
|
|
202
|
+ with self.assertRaises(ValueError):
|
|
203
|
+ LeObject._prepare_filters(filters, None, None)
|
|
204
|
+
|