cubicweb/server/test/unittest_repository.py
branch3.26
changeset 12726 1a2c7d6397ec
parent 12044 70bb46dfa87b
child 12731 8e5f2c449e60
equal deleted inserted replaced
12725:0481ece35cb2 12726:1a2c7d6397ec
   319             self.assertEqual(bk.title, 'index')
   319             self.assertEqual(bk.title, 'index')
   320             bk.cw_set(title=u'root')
   320             bk.cw_set(title=u'root')
   321             self.assertEqual(bk.title, 'root')
   321             self.assertEqual(bk.title, 'root')
   322             cnx.commit()
   322             cnx.commit()
   323             self.assertEqual(bk.title, 'root')
   323             self.assertEqual(bk.title, 'root')
       
   324 
       
   325     def test_delete_entity_with_inlined_relation(self):
       
   326         """Test deletion of entity with inlined relations.
       
   327 
       
   328         In this case, inlined relation column should not be deleted (= e.g. not
       
   329         updated to NULL), but hooks before_delete_relation and
       
   330         afer_delete_relation must be called.
       
   331         """
       
   332         class OnDeleteInlined(Hook):
       
   333             __regid__ = 'on_delete_inlined'
       
   334             __select__ = Hook.__select__ & hook.match_rtype('personne_inlined')
       
   335             events = ('before_delete_relation', 'after_delete_relation')
       
   336             CALLED = []
       
   337 
       
   338             def __call__(self):
       
   339                 OnDeleteInlined.CALLED.append(self.event)
       
   340 
       
   341         with self.admin_access.cnx() as cnx:
       
   342             # allow only one null on cw_personne_inlined column
       
   343             cnx.system_sql(
       
   344                 'CREATE UNIQUE INDEX test_composite_idx ON cw_personne(true) '
       
   345                 'WHERE cw_personne_inlined is NULL')
       
   346             cnx.commit()
       
   347 
       
   348             # deletion of p1 should not set personne_inlined to NULL (otherwise
       
   349             # the unique index will raise)
       
   350             p0 = cnx.create_entity('Personne', nom=u'p0')
       
   351             p1 = cnx.create_entity('Personne', nom=u'p1', personne_inlined=p0)
       
   352             cnx.commit()
       
   353             with self.temporary_appobjects(OnDeleteInlined):
       
   354                 cnx.entity_from_eid(p1.eid).cw_delete()
       
   355             assert OnDeleteInlined.CALLED == [
       
   356                 'before_delete_relation', 'after_delete_relation']
       
   357             cnx.commit()
       
   358 
       
   359             # XXX: This case is not handled because entities need to be deleted
       
   360             # in a specific order
       
   361             # p1 = cnx.create_entity('Personne', nom=u'p1', personne_inlined=p0)
       
   362             # cnx.commit()
       
   363             # cnx.execute('DELETE Personne X WHERE X eid IN ({}, {})'.format(p1.eid, p0.eid))
       
   364             # cnx.commit()
       
   365             cnx.entity_from_eid(p0.eid).cw_delete()
       
   366             cnx.commit()
       
   367 
       
   368             # deletion of p1 should not set personne_inlined to NULL
       
   369             p1 = cnx.create_entity('Personne', nom=u'p1')
       
   370             p1.cw_set(personne_inlined=p1)
       
   371             p0 = cnx.create_entity('Personne', nom=u'p0')
       
   372             cnx.commit()
       
   373             cnx.entity_from_eid(p1.eid).cw_delete()
       
   374             cnx.commit()
       
   375 
   324 
   376 
   325 class SchemaDeserialTC(CubicWebTC):
   377 class SchemaDeserialTC(CubicWebTC):
   326 
   378 
   327     appid = 'data-schemaserial'
   379     appid = 'data-schemaserial'
   328 
   380