|
@@ -4,6 +4,7 @@
|
4
|
4
|
|
5
|
5
|
import unittest
|
6
|
6
|
from unittest import TestCase
|
|
7
|
+from unittest.mock import patch
|
7
|
8
|
|
8
|
9
|
import EditorialModel
|
9
|
10
|
import leobject
|
|
@@ -53,7 +54,6 @@ class _LeObjectTestCase(TestCase):
|
53
|
54
|
_LeObject._split_filter(query)
|
54
|
55
|
|
55
|
56
|
## Testing methods that need the generated code
|
56
|
|
-# @todo mock the datasource to test the get, update, delete and insert methods
|
57
|
57
|
class LeObjectTestCase(TestCase):
|
58
|
58
|
|
59
|
59
|
@classmethod
|
|
@@ -201,4 +201,119 @@ class LeObjectTestCase(TestCase):
|
201
|
201
|
filters = ['hello world !']
|
202
|
202
|
with self.assertRaises(ValueError):
|
203
|
203
|
LeObject._prepare_filters(filters, None, None)
|
|
204
|
+
|
|
205
|
+class LeObjectMockDatasourceTestCase(TestCase):
|
|
206
|
+ """ Testing _LeObject using a mock on the datasource """
|
|
207
|
+
|
|
208
|
+ @classmethod
|
|
209
|
+ def setUpClass(cls):
|
|
210
|
+ """ Write the generated code in a temporary directory and import it """
|
|
211
|
+ cls.tmpdir = leobject.test.utils.tmp_load_factory_code()
|
|
212
|
+ @classmethod
|
|
213
|
+ def tearDownClass(cls):
|
|
214
|
+ """ Remove the temporary directory created at class setup """
|
|
215
|
+ leobject.test.utils.cleanup(cls.tmpdir)
|
204
|
216
|
|
|
217
|
+ @patch('leobject.datasources.dummy.DummyDatasource.insert')
|
|
218
|
+ def test_insert(self, dsmock):
|
|
219
|
+ from dyncode import Publication, Numero, LeObject
|
|
220
|
+ ndatas = [
|
|
221
|
+ [{'titre' : 'FooBar'}],
|
|
222
|
+ [{'titre':'hello'},{'titre':'world'}],
|
|
223
|
+ ]
|
|
224
|
+ for ndats in ndatas:
|
|
225
|
+ LeObject.insert(Numero,ndats)
|
|
226
|
+ dsmock.assert_called_once_with(Numero, Publication, ndats)
|
|
227
|
+ dsmock.reset_mock()
|
|
228
|
+
|
|
229
|
+ LeObject.insert('Numero',ndats)
|
|
230
|
+ dsmock.assert_called_once_with(Numero, Publication, ndats)
|
|
231
|
+ dsmock.reset_mock()
|
|
232
|
+
|
|
233
|
+ @patch('leobject.datasources.dummy.DummyDatasource.update')
|
|
234
|
+ def test_update(self, dsmock):
|
|
235
|
+ from dyncode import Publication, Numero, LeObject
|
|
236
|
+
|
|
237
|
+ args = [
|
|
238
|
+ ( ['lodel_id = 1'],
|
|
239
|
+ {'titre':'foobar'},
|
|
240
|
+ [('lodel_id','=','1')],
|
|
241
|
+ []
|
|
242
|
+ ),
|
|
243
|
+ ( ['superior.parent in [1,2,3,4,5,6]', 'titre != "FooBar"'],
|
|
244
|
+ {'titre':'FooBar'},
|
|
245
|
+ [( 'titre','!=','"FooBar"')],
|
|
246
|
+ [( (leobject.leobject.REL_SUP, 'parent') ,' in ', '[1,2,3,4,5,6]')]
|
|
247
|
+ ),
|
|
248
|
+ ]
|
|
249
|
+
|
|
250
|
+ for filters, datas, ds_filters, ds_relfilters in args:
|
|
251
|
+ LeObject.update(Numero, filters, datas)
|
|
252
|
+ dsmock.assert_called_once_with(Numero, Publication, ds_filters, ds_relfilters, datas)
|
|
253
|
+ dsmock.reset_mock()
|
|
254
|
+
|
|
255
|
+ LeObject.update('Numero', filters, datas)
|
|
256
|
+ dsmock.assert_called_once_with(Numero, Publication, ds_filters, ds_relfilters, datas)
|
|
257
|
+ dsmock.reset_mock()
|
|
258
|
+
|
|
259
|
+ @patch('leobject.datasources.dummy.DummyDatasource.delete')
|
|
260
|
+ def test_delete(self, dsmock):
|
|
261
|
+ from dyncode import Publication, Numero, LeObject
|
|
262
|
+
|
|
263
|
+ args = [
|
|
264
|
+ (
|
|
265
|
+ ['lodel_id=1'],
|
|
266
|
+ [('lodel_id', '=', '1')],
|
|
267
|
+ []
|
|
268
|
+ ),
|
|
269
|
+ (
|
|
270
|
+ ['subordinate.parent not in [1,2,3]', 'titre = "titre nul"'],
|
|
271
|
+ [('titre','=', '"titre nul"')],
|
|
272
|
+ [( (leobject.leobject.REL_SUB, 'parent'), ' not in ', '[1,2,3]')]
|
|
273
|
+ ),
|
|
274
|
+ ]
|
|
275
|
+
|
|
276
|
+ for filters, ds_filters, ds_relfilters in args:
|
|
277
|
+ LeObject.delete(Numero, filters)
|
|
278
|
+ dsmock.assert_called_once_with(Numero, Publication, ds_filters, ds_relfilters)
|
|
279
|
+ dsmock.reset_mock()
|
|
280
|
+
|
|
281
|
+ LeObject.delete('Numero', filters)
|
|
282
|
+ dsmock.assert_called_once_with(Numero, Publication, ds_filters, ds_relfilters)
|
|
283
|
+ dsmock.reset_mock()
|
|
284
|
+
|
|
285
|
+ @patch('leobject.datasources.dummy.DummyDatasource.get')
|
|
286
|
+ def test_get(self, dsmock):
|
|
287
|
+ from dyncode import Publication, Numero, LeObject
|
|
288
|
+
|
|
289
|
+ args = [
|
|
290
|
+ (
|
|
291
|
+ ['lodel_id', 'superior.parent'],
|
|
292
|
+ ['titre != "foobar"'],
|
|
293
|
+
|
|
294
|
+ ['lodel_id', (leobject.leobject.REL_SUP, 'parent')],
|
|
295
|
+ [('titre','!=', '"foobar"')],
|
|
296
|
+ []
|
|
297
|
+ ),
|
|
298
|
+ (
|
|
299
|
+ ['lodel_id', 'titre', 'superior.parent', 'subordinate.translation'],
|
|
300
|
+ ['superior.parent in [1,2,3,4,5]'],
|
|
301
|
+
|
|
302
|
+ ['lodel_id', 'titre', (leobject.leobject.REL_SUP,'parent'), (leobject.leobject.REL_SUB, 'translation')],
|
|
303
|
+ [],
|
|
304
|
+ [( (leobject.leobject.REL_SUP, 'parent'), ' in ', '[1,2,3,4,5]')]
|
|
305
|
+ ),
|
|
306
|
+ (
|
|
307
|
+ [],
|
|
308
|
+ [],
|
|
309
|
+
|
|
310
|
+ Numero._fields,
|
|
311
|
+ [],
|
|
312
|
+ []
|
|
313
|
+ ),
|
|
314
|
+ ]
|
|
315
|
+
|
|
316
|
+ for field_list, filters, fl_ds, filters_ds, rfilters_ds in args:
|
|
317
|
+ LeObject.get(filters, field_list, Numero)
|
|
318
|
+ dsmock.assert_called_with(Publication, Numero, fl_ds, filters_ds, rfilters_ds)
|
|
319
|
+ dsmock.reset_mock()
|