server/test/unittest_undo.py
changeset 8483 4ba11607d84a
parent 8266 704dda5c07f7
child 8544 3d049071957e
equal deleted inserted replaced
8469:82272decfa99 8483:4ba11607d84a
   201         p = session.create_entity('Personne', nom=u'louis', fiche=c)
   201         p = session.create_entity('Personne', nom=u'louis', fiche=c)
   202         self.commit()
   202         self.commit()
   203         c.cw_delete()
   203         c.cw_delete()
   204         txuuid = self.commit()
   204         txuuid = self.commit()
   205         c2 = session.create_entity('Card', title=u'hip', content=u'hip')
   205         c2 = session.create_entity('Card', title=u'hip', content=u'hip')
   206         p.set_relations(fiche=c2)
   206         p.cw_set(fiche=c2)
   207         self.commit()
   207         self.commit()
   208         self.assertUndoTransaction(txuuid, [
   208         self.assertUndoTransaction(txuuid, [
   209             "Can't restore object relation fiche to entity "
   209             "Can't restore object relation fiche to entity "
   210             "%s which is already linked using this relation." % p.eid])
   210             "%s which is already linked using this relation." % p.eid])
   211         self.commit()
   211         self.commit()
   215     def test_undo_deletion_integrity_2(self):
   215     def test_undo_deletion_integrity_2(self):
   216         # test validation error raised if we can't restore a required relation
   216         # test validation error raised if we can't restore a required relation
   217         session = self.session
   217         session = self.session
   218         g = session.create_entity('CWGroup', name=u'staff')
   218         g = session.create_entity('CWGroup', name=u'staff')
   219         session.execute('DELETE U in_group G WHERE U eid %(x)s', {'x': self.toto.eid})
   219         session.execute('DELETE U in_group G WHERE U eid %(x)s', {'x': self.toto.eid})
   220         self.toto.set_relations(in_group=g)
   220         self.toto.cw_set(in_group=g)
   221         self.commit()
   221         self.commit()
   222         self.toto.cw_delete()
   222         self.toto.cw_delete()
   223         txuuid = self.commit()
   223         txuuid = self.commit()
   224         g.cw_delete()
   224         g.cw_delete()
   225         self.commit()
   225         self.commit()
   263         tutu = self.create_user(req, 'tutu', commit=False)
   263         tutu = self.create_user(req, 'tutu', commit=False)
   264         txuuid = self.commit()
   264         txuuid = self.commit()
   265         email = self.request().create_entity('EmailAddress', address=u'tutu@cubicweb.org')
   265         email = self.request().create_entity('EmailAddress', address=u'tutu@cubicweb.org')
   266         prop = self.request().create_entity('CWProperty', pkey=u'ui.default-text-format',
   266         prop = self.request().create_entity('CWProperty', pkey=u'ui.default-text-format',
   267                                             value=u'text/html')
   267                                             value=u'text/html')
   268         tutu.set_relations(use_email=email, reverse_for_user=prop)
   268         tutu.cw_set(use_email=email, reverse_for_user=prop)
   269         self.commit()
   269         self.commit()
   270         with self.assertRaises(ValidationError) as cm:
   270         with self.assertRaises(ValidationError) as cm:
   271             self.cnx.undo_transaction(txuuid)
   271             self.cnx.undo_transaction(txuuid)
   272         self.assertEqual(cm.exception.entity, tutu.eid)
   272         self.assertEqual(cm.exception.entity, tutu.eid)
   273         self.assertEqual(cm.exception.errors,
   273         self.assertEqual(cm.exception.errors,
   276     def test_undo_creation_integrity_2(self):
   276     def test_undo_creation_integrity_2(self):
   277         session = self.session
   277         session = self.session
   278         g = session.create_entity('CWGroup', name=u'staff')
   278         g = session.create_entity('CWGroup', name=u'staff')
   279         txuuid = self.commit()
   279         txuuid = self.commit()
   280         session.execute('DELETE U in_group G WHERE U eid %(x)s', {'x': self.toto.eid})
   280         session.execute('DELETE U in_group G WHERE U eid %(x)s', {'x': self.toto.eid})
   281         self.toto.set_relations(in_group=g)
   281         self.toto.cw_set(in_group=g)
   282         self.commit()
   282         self.commit()
   283         with self.assertRaises(ValidationError) as cm:
   283         with self.assertRaises(ValidationError) as cm:
   284             self.cnx.undo_transaction(txuuid)
   284             self.cnx.undo_transaction(txuuid)
   285         self.assertEqual(cm.exception.entity, g.eid)
   285         self.assertEqual(cm.exception.entity, g.eid)
   286         self.assertEqual(cm.exception.errors,
   286         self.assertEqual(cm.exception.errors,
   302         NB: processed by `_undo_r` as expected"""
   302         NB: processed by `_undo_r` as expected"""
   303         session = self.session
   303         session = self.session
   304         c = session.create_entity('Card', title=u'hop', content=u'hop')
   304         c = session.create_entity('Card', title=u'hop', content=u'hop')
   305         p = session.create_entity('Personne', nom=u'louis', fiche=c)
   305         p = session.create_entity('Personne', nom=u'louis', fiche=c)
   306         self.commit()
   306         self.commit()
   307         p.set_relations(fiche=None)
   307         p.cw_set(fiche=None)
   308         txuuid = self.commit()
   308         txuuid = self.commit()
   309         self.assertUndoTransaction(txuuid)
   309         self.assertUndoTransaction(txuuid)
   310         self.commit()
   310         self.commit()
   311         p.cw_clear_all_caches()
   311         p.cw_clear_all_caches()
   312         self.assertEqual(p.fiche[0].eid, c.eid)
   312         self.assertEqual(p.fiche[0].eid, c.eid)
   317         NB: processed by `_undo_r` as expected"""
   317         NB: processed by `_undo_r` as expected"""
   318         session = self.session
   318         session = self.session
   319         c = session.create_entity('Card', title=u'hop', content=u'hop')
   319         c = session.create_entity('Card', title=u'hop', content=u'hop')
   320         p = session.create_entity('Personne', nom=u'louis', fiche=c)
   320         p = session.create_entity('Personne', nom=u'louis', fiche=c)
   321         self.commit()
   321         self.commit()
   322         p.set_relations(fiche=None)
   322         p.cw_set(fiche=None)
   323         txuuid = self.commit()
   323         txuuid = self.commit()
   324         c.cw_delete()
   324         c.cw_delete()
   325         self.commit()
   325         self.commit()
   326         self.assertUndoTransaction(txuuid, [
   326         self.assertUndoTransaction(txuuid, [
   327             "Can't restore relation fiche, object entity %d doesn't exist anymore." % c.eid])
   327             "Can't restore relation fiche, object entity %d doesn't exist anymore." % c.eid])
   337         Caution processed by `_undo_u`, not `_undo_a` !"""
   337         Caution processed by `_undo_u`, not `_undo_a` !"""
   338         session = self.session
   338         session = self.session
   339         c = session.create_entity('Card', title=u'hop', content=u'hop')
   339         c = session.create_entity('Card', title=u'hop', content=u'hop')
   340         p = session.create_entity('Personne', nom=u'louis')
   340         p = session.create_entity('Personne', nom=u'louis')
   341         self.commit()
   341         self.commit()
   342         p.set_relations(fiche=c)
   342         p.cw_set(fiche=c)
   343         txuuid = self.commit()
   343         txuuid = self.commit()
   344         self.assertUndoTransaction(txuuid)
   344         self.assertUndoTransaction(txuuid)
   345         self.commit()
   345         self.commit()
   346         p.cw_clear_all_caches()
   346         p.cw_clear_all_caches()
   347         self.assertFalse(p.fiche)
   347         self.assertFalse(p.fiche)
   352         Caution processed by `_undo_u`, not `_undo_a` !"""
   352         Caution processed by `_undo_u`, not `_undo_a` !"""
   353         session = self.session
   353         session = self.session
   354         c = session.create_entity('Card', title=u'hop', content=u'hop')
   354         c = session.create_entity('Card', title=u'hop', content=u'hop')
   355         p = session.create_entity('Personne', nom=u'louis')
   355         p = session.create_entity('Personne', nom=u'louis')
   356         self.commit()
   356         self.commit()
   357         p.set_relations(fiche=c)
   357         p.cw_set(fiche=c)
   358         txuuid = self.commit()
   358         txuuid = self.commit()
   359         c.cw_delete()
   359         c.cw_delete()
   360         self.commit()
   360         self.commit()
   361         self.assertUndoTransaction(txuuid)
   361         self.assertUndoTransaction(txuuid)
   362 
   362 
   367         session = self.session
   367         session = self.session
   368         c1 = session.create_entity('Card', title=u'hop', content=u'hop')
   368         c1 = session.create_entity('Card', title=u'hop', content=u'hop')
   369         c2 = session.create_entity('Card', title=u'hip', content=u'hip')
   369         c2 = session.create_entity('Card', title=u'hip', content=u'hip')
   370         p = session.create_entity('Personne', nom=u'louis', fiche=c1)
   370         p = session.create_entity('Personne', nom=u'louis', fiche=c1)
   371         self.commit()
   371         self.commit()
   372         p.set_relations(fiche=c2)
   372         p.cw_set(fiche=c2)
   373         txuuid = self.commit()
   373         txuuid = self.commit()
   374         self.assertUndoTransaction(txuuid)
   374         self.assertUndoTransaction(txuuid)
   375         self.commit()
   375         self.commit()
   376         p.cw_clear_all_caches()
   376         p.cw_clear_all_caches()
   377         self.assertEqual(p.fiche[0].eid, c1.eid)
   377         self.assertEqual(p.fiche[0].eid, c1.eid)
   383         session = self.session
   383         session = self.session
   384         c1 = session.create_entity('Card', title=u'hop', content=u'hop')
   384         c1 = session.create_entity('Card', title=u'hop', content=u'hop')
   385         c2 = session.create_entity('Card', title=u'hip', content=u'hip')
   385         c2 = session.create_entity('Card', title=u'hip', content=u'hip')
   386         p = session.create_entity('Personne', nom=u'louis', fiche=c1)
   386         p = session.create_entity('Personne', nom=u'louis', fiche=c1)
   387         self.commit()
   387         self.commit()
   388         p.set_relations(fiche=c2)
   388         p.cw_set(fiche=c2)
   389         txuuid = self.commit()
   389         txuuid = self.commit()
   390         c1.cw_delete()
   390         c1.cw_delete()
   391         self.commit()
   391         self.commit()
   392         self.assertUndoTransaction(txuuid, [
   392         self.assertUndoTransaction(txuuid, [
   393             "can't restore entity %s of type Personne, target of fiche (eid %s)"
   393             "can't restore entity %s of type Personne, target of fiche (eid %s)"
   399     def test_undo_attr_update_ok(self):
   399     def test_undo_attr_update_ok(self):
   400         session = self.session
   400         session = self.session
   401         p = session.create_entity('Personne', nom=u'toto')
   401         p = session.create_entity('Personne', nom=u'toto')
   402         session.commit()
   402         session.commit()
   403         self.session.set_cnxset()
   403         self.session.set_cnxset()
   404         p.set_attributes(nom=u'titi')
   404         p.cw_set(nom=u'titi')
   405         txuuid = self.commit()
   405         txuuid = self.commit()
   406         self.assertUndoTransaction(txuuid)
   406         self.assertUndoTransaction(txuuid)
   407         p.cw_clear_all_caches()
   407         p.cw_clear_all_caches()
   408         self.assertEqual(p.nom, u'toto')
   408         self.assertEqual(p.nom, u'toto')
   409 
   409 
   410     def test_undo_attr_update_ko(self):
   410     def test_undo_attr_update_ko(self):
   411         session = self.session
   411         session = self.session
   412         p = session.create_entity('Personne', nom=u'toto')
   412         p = session.create_entity('Personne', nom=u'toto')
   413         session.commit()
   413         session.commit()
   414         self.session.set_cnxset()
   414         self.session.set_cnxset()
   415         p.set_attributes(nom=u'titi')
   415         p.cw_set(nom=u'titi')
   416         txuuid = self.commit()
   416         txuuid = self.commit()
   417         p.cw_delete()
   417         p.cw_delete()
   418         self.commit()
   418         self.commit()
   419         self.assertUndoTransaction(txuuid, [
   419         self.assertUndoTransaction(txuuid, [
   420             u"can't restore state of entity %s, it has been deleted inbetween" % p.eid])
   420             u"can't restore state of entity %s, it has been deleted inbetween" % p.eid])