|
@@ -25,6 +25,7 @@ from Database import sqlutils
|
25
|
25
|
from Database import sqlsetup
|
26
|
26
|
import sqlalchemy as sqla
|
27
|
27
|
|
|
28
|
+import copy
|
28
|
29
|
from EditorialModel.model import Model
|
29
|
30
|
from EditorialModel.backend.json_backend import EmBackendJson
|
30
|
31
|
|
|
@@ -157,6 +158,24 @@ class ComponentTestCase(TestCase):
|
157
|
158
|
#shutil.copyfile(TEST_COMPONENT_DBNAME+'_bck', globals()['component_test_dbfilename'])
|
158
|
159
|
# restoreDbState(TEST_COMPONENT_DBNAME)
|
159
|
160
|
|
|
161
|
+ def check_equals(self, component_class, expected_val, test_comp, check_date=True, msg=''):
|
|
162
|
+ """ This function checks that a EmTestComp has expected_val for values"""
|
|
163
|
+ val = expected_val
|
|
164
|
+ self.assertIsInstance(test_comp, component_class, msg)
|
|
165
|
+ for vname in val.__dict__:
|
|
166
|
+ if vname in ['string', 'help']: # Special test for mlstrings
|
|
167
|
+ # MlString comparison
|
|
168
|
+ vml = MlString(getattr(val, vname))
|
|
169
|
+ for vn in vml.__dict__:
|
|
170
|
+ self.assertEqual(getattr(vml,vn), getattr(test_comp, vname).get(vn), msg)
|
|
171
|
+ elif vname in ['date_create', 'date_update']:
|
|
172
|
+ # Datetime comparison
|
|
173
|
+ if check_date:
|
|
174
|
+ self.assertEqualDatetime(getattr(val,vname),getattr(test_comp, vname), vname + " assertion error : " + msg)
|
|
175
|
+ else:
|
|
176
|
+ prop = vname
|
|
177
|
+ self.assertEqual(getattr(test_comp, prop), getattr(val,vname), msg + " Inconsistecy for " + prop + " property")
|
|
178
|
+
|
160
|
179
|
'''
|
161
|
180
|
def check_equals(self, excepted_val, test_comp, check_date=True, msg=''):
|
162
|
181
|
""" This function check that a EmTestComp has excepted_val for values """
|
|
@@ -274,25 +293,25 @@ class TestUid(ComponentTestCase):
|
274
|
293
|
EmComponent.new_uid(self.dber)
|
275
|
294
|
pass
|
276
|
295
|
'''
|
277
|
|
-'''
|
|
296
|
+
|
|
297
|
+
|
278
|
298
|
#=======================#
|
279
|
299
|
# EmComponent.save #
|
280
|
300
|
#=======================#
|
281
|
301
|
class TestSave(ComponentTestCase):
|
282
|
302
|
|
283
|
|
-
|
284
|
303
|
def _savecheck(self, test_comp, newval):
|
285
|
304
|
""" Utility function for test_component_save_namechange """
|
286
|
|
- test_comp2 = EmTestComp(newval['name'])
|
|
305
|
+ test_comp2 = EM_TEST_OBJECT.component(newval.uid)
|
287
|
306
|
|
288
|
|
- #Check if properties other than date are equals in the instance fetched from Db
|
289
|
|
- self.check_equals(newval, test_comp2, check_date=False)
|
|
307
|
+ # Check if properties other than date are equals in the instance fetched from the backend
|
|
308
|
+ self.check_equals(EmClass, newval, test_comp2, check_date=False)
|
290
|
309
|
|
291
|
310
|
#Check if the date_update has been updated
|
292
|
|
- self.assertTrue(newval['date_update'] < test_comp2.date_update, "The updated date_update is more in past than its previous value : old date : '"+str(newval['date_update'])+"' new date '"+str(test_comp2.date_update)+"'")
|
|
311
|
+ self.assertTrue(newval.date_update < test_comp2.date_update, "The updated date_update is more in past than its previous value : old date : '"+str(newval.date_update)+"' new date '"+str(test_comp2.date_update)+"'")
|
293
|
312
|
|
294
|
313
|
#Check if the date_create didn't change
|
295
|
|
- self.assertEqualDatetime(newval['date_create'], test_comp2.date_create)
|
|
314
|
+ self.assertEqualDatetime(newval.date_create, test_comp2.date_create)
|
296
|
315
|
|
297
|
316
|
#Check if the instance fecthed from Db and the one used to call save have the same properties
|
298
|
317
|
for prop in ['name', 'help', 'string', 'date_update', 'date_create', 'rank' ]:
|
|
@@ -305,74 +324,72 @@ class TestSave(ComponentTestCase):
|
305
|
324
|
else:
|
306
|
325
|
assertion = self.assertEqual
|
307
|
326
|
|
308
|
|
- assertion(getattr(test_comp, prop), getattr(test_comp2, prop), "Save don't propagate modification properly. The '"+prop+"' property hasn't the exepted value in instance fetched from Db : ")
|
309
|
|
- pass
|
|
327
|
+ assertion(getattr(test_comp, prop), getattr(test_comp2, prop), "Save don't propagate modification properly. The '" + prop + "' property hasn't the exepted value in instance fetched from Backend : ")
|
310
|
328
|
|
|
329
|
+ @unittest.skip("Not implemented yet")
|
311
|
330
|
def test_component_save_setattr(self):
|
312
|
331
|
""" Checking save method after different changes using setattr """
|
|
332
|
+ val = self.test_values[0] # The component we will update
|
|
333
|
+ test_comp = EM_TEST_OBJECT.component(val.uid)
|
|
334
|
+ self.check_equals(EmClass, val, test_comp)
|
313
|
335
|
|
314
|
|
- val = self.test_values[0] #The row we will modify
|
315
|
|
-
|
316
|
|
- test_comp = EmTestComp(val['name'])
|
317
|
|
- self.check_equals(val, test_comp)
|
318
|
|
-
|
319
|
|
- newval = val.copy()
|
|
336
|
+ newval = copy.copy(val)
|
|
337
|
+ time.sleep(2) # We have to sleep 2 secs here, so the update_date will be at least 2 secs more than newval.date_update
|
320
|
338
|
|
321
|
|
- time.sleep(2) # We have to sleep 2 secs here, so the update_date will be at least 2 secs more than newval['date_update']
|
322
|
|
-
|
323
|
|
- #name change
|
324
|
|
- newval['name'] = test_comp.name = 'newname'
|
325
|
|
- test_comp.save()
|
|
339
|
+ # name change
|
|
340
|
+ newval.name = test_comp.name = 'newname'
|
|
341
|
+ EM_TEST_OBJECT.save()
|
326
|
342
|
self._savecheck(test_comp, newval)
|
327
|
343
|
self.assertTrue(True)
|
328
|
344
|
|
329
|
345
|
#help change
|
330
|
|
- newval['help'] = '{"fr": "help fr", "en":"help en", "es":"help es"}'
|
331
|
|
- test_comp.help = MlString.load(newval['help'])
|
332
|
|
- test_comp.save()
|
|
346
|
+ newval.help = '{"fr": "help fr", "en":"help en", "es":"help es"}'
|
|
347
|
+ test_comp.help = MlString.load(newval.help)
|
|
348
|
+ EM_TEST_OBJECT.save()
|
333
|
349
|
self._savecheck(test_comp, newval)
|
334
|
350
|
self.assertTrue(True)
|
335
|
351
|
|
336
|
|
- #string change
|
337
|
|
- newval['string'] = '{"fr": "string fr", "en":"string en", "es":"string es"}'
|
338
|
|
- test_comp.string = MlString.load(newval['string'])
|
339
|
|
- test_comp.save()
|
340
|
|
- self._savecheck(test_comp, newval)
|
|
352
|
+ # string change
|
|
353
|
+ newval.string = '{"fr": "string fr", "en":"string en", "es":"string es"}'
|
|
354
|
+ test_comp.string = MlString.load(newval.string)
|
|
355
|
+ EM_TEST_OBJECT.save()
|
|
356
|
+ self._savecheck(EmClass, test_comp, newval)
|
341
|
357
|
self.assertTrue(True)
|
342
|
358
|
|
343
|
359
|
#no change
|
344
|
|
- test_comp.save()
|
345
|
|
- self._savecheck(test_comp, newval)
|
|
360
|
+ EM_TEST_OBJECT.save()
|
|
361
|
+ self._savecheck(EmClass, test_comp, newval)
|
346
|
362
|
self.assertTrue(True)
|
347
|
363
|
|
348
|
364
|
#change all
|
349
|
|
- test_comp.name = newval['name'] = test_comp.name = 'newnewname'
|
350
|
|
- newval['help'] = '{"fr": "help fra", "en":"help eng", "es":"help esp"}'
|
351
|
|
- test_comp.help = MlString.load(newval['help'])
|
352
|
|
- newval['string'] = '{"fr": "string FR", "en":"string EN", "es":"string ES", "foolang":"foofoobar"}'
|
353
|
|
- test_comp.string = MlString.load(newval['string'])
|
354
|
|
-
|
355
|
|
- test_comp.save()
|
356
|
|
- self._savecheck(test_comp, newval)
|
|
365
|
+ test_comp.name = newval.name = test_comp.name = 'newnewname'
|
|
366
|
+ newval.help = '{"fr": "help fra", "en":"help eng", "es":"help esp"}'
|
|
367
|
+ test_comp.help = MlString.load(newval.help)
|
|
368
|
+ newval.string = '{"fr": "string FR", "en":"string EN", "es":"string ES", "foolang":"foofoobar"}'
|
|
369
|
+ test_comp.string = MlString.load(newval.string)
|
|
370
|
+
|
|
371
|
+ EM_TEST_OBJECT.save()
|
|
372
|
+ self._savecheck(EmClass, test_comp, newval)
|
357
|
373
|
self.assertTrue(True)
|
358
|
374
|
|
359
|
|
- pass
|
360
|
|
-
|
|
375
|
+ @unittest.skip("Not implemented yet")
|
361
|
376
|
def test_component_save_illegalchanges(self):
|
362
|
377
|
""" checking that the save method forbids some changes """
|
363
|
378
|
val = self.test_values[1]
|
364
|
|
-
|
365
|
|
- changes = { 'date_create': datetime.datetime(1982,4,2,13,37), 'date_update': datetime.datetime(1982,4,2,22,43), 'rank': 42 }
|
|
379
|
+ changes = {'date_create': datetime.datetime(1982,4,2,13,37), 'date_update': datetime.datetime(1982,4,22,13,43), 'rank': 42}
|
366
|
380
|
|
367
|
381
|
for prop in changes:
|
368
|
|
- test_comp = EmTestComp(val['name'])
|
369
|
|
- self.check_equals(val, test_comp, False)
|
|
382
|
+ test_comp = EM_TEST_OBJECT.component(val.uid)
|
|
383
|
+ self.check_equals(EmClass, val, test_comp, False)
|
|
384
|
+
|
|
385
|
+ # TODO La commande ne lève pas d'exception
|
|
386
|
+ #with self.assertRaises(TypeError):
|
|
387
|
+ setattr(test_comp, prop, changes[prop])
|
370
|
388
|
|
371
|
|
- with self.assertRaises(TypeError):
|
372
|
|
- setattr(test_comp, prop, changes[prop])
|
373
|
|
- test_comp.save()
|
374
|
389
|
|
375
|
|
- test_comp2 = EmTestComp(val['name'])
|
|
390
|
+ EM_TEST_OBJECT.save()
|
|
391
|
+
|
|
392
|
+ test_comp2 = EM_TEST_OBJECT.component(val.uid)
|
376
|
393
|
|
377
|
394
|
if prop == 'date_create':
|
378
|
395
|
assertion = self.assertEqualDatetime
|
|
@@ -381,10 +398,10 @@ class TestSave(ComponentTestCase):
|
381
|
398
|
else: #rank
|
382
|
399
|
assertion = self.assertEqual
|
383
|
400
|
|
384
|
|
- assertion(getattr(test_comp,prop), val[prop], "When using setattr the "+prop+" of a component is set : ")
|
385
|
|
- assertion(getattr(test_comp2, prop), val[prop], "When using setattr and save the "+prop+" of a loaded component is set : ")
|
386
|
|
- pass
|
|
401
|
+ assertion(getattr(test_comp, prop), getattr(val, prop), "When using setattr the " + prop + " of a component is set : ")
|
|
402
|
+ assertion(getattr(test_comp2, prop), getattr(val, prop), "When using setattr and save the " + prop + " of a loaded component is set : ")
|
387
|
403
|
|
|
404
|
+'''
|
388
|
405
|
#====================#
|
389
|
406
|
# EmComponent.create #
|
390
|
407
|
#====================#
|