fix implementation of repository.glob_add_relations (closes ##1625257)
authorAlexandre Fayolle <alexandre.fayolle@logilab.fr>
Wed, 20 Apr 2011 16:55:52 +0200
changeset 7238 576abb8c4626
parent 7237 9f619715665b
child 7240 6f5118b48d6a
fix implementation of repository.glob_add_relations (closes ##1625257) the internal data structure had changed during implementation and the code had not been updated...
server/repository.py
server/test/data/schema.py
server/test/unittest_repository.py
--- a/server/repository.py	Fri Apr 15 15:42:17 2011 +0200
+++ b/server/repository.py	Wed Apr 20 16:55:52 2011 +0200
@@ -1389,23 +1389,31 @@
                     print 'ADD relation', subject, rtype, object
             for subject, object in eids_subj_obj:
                 source = self.locate_relation_source(session, subject, rtype, object)
-                if source in sources:
-                    sources[source][1].append((subject, object))
+                if source not in sources:
+                    relations_by_rtype = {}
+                    sources[source] = relations_by_rtype
+                else:
+                    relations_by_rtype = sources[source]
+                if rtype in relations_by_rtype:
+                    relations_by_rtype[rtype].append((subject, object))
                 else:
-                    sources[source] = rtype, [(subject, object)]
-        for source, (rtype, source_relations) in sources.iteritems():
+                    relations_by_rtype[rtype] = [(subject, object)]
+        for source, relations_by_rtype in sources.iteritems():
             if source.should_call_hooks:
+                for rtype, source_relations in relations_by_rtype.iteritems():
+                    for subject, object in source_relations:
+                        del_existing_rel_if_needed(session, subject, rtype, object)
+                    self.hm.call_hooks('before_add_relation', session,
+                                    rtype=rtype, eids_from_to=source_relations)
+            for rtype, source_relations in relations_by_rtype.iteritems():
+                source.add_relations(session, rtype, source_relations)
+                rschema = self.schema.rschema(rtype)
                 for subject, object in source_relations:
-                    del_existing_rel_if_needed(session, subject, rtype, object)
-                self.hm.call_hooks('before_add_relation', session,
-                                    rtype=rtype, eids_from_to=source_relations)
-            source.add_relations(session, rtype, source_relations)
-            rschema = self.schema.rschema(rtype)
-            for subject, object in source_relations:
-                session.update_rel_cache_add(subject, rtype, object, rschema.symmetric)
+                    session.update_rel_cache_add(subject, rtype, object, rschema.symmetric)
             if source.should_call_hooks:
-                self.hm.call_hooks('after_add_relation', session,
-                                   rtype=rtype, eids_from_to=source_relations)
+                for rtype, source_relations in relations_by_rtype.iteritems():
+                    self.hm.call_hooks('after_add_relation', session,
+                                       rtype=rtype, eids_from_to=source_relations)
 
     def glob_delete_relation(self, session, subject, rtype, object):
         """delete a relation from the repository"""
--- a/server/test/data/schema.py	Fri Apr 15 15:42:17 2011 +0200
+++ b/server/test/data/schema.py	Wed Apr 20 16:55:52 2011 +0200
@@ -232,3 +232,8 @@
     cardinality='?*'
     inlined=True
 
+
+class login_user(RelationDefinition):
+    subject = 'Personne'
+    object = 'CWUser'
+    cardinality = '??'
--- a/server/test/unittest_repository.py	Fri Apr 15 15:42:17 2011 +0200
+++ b/server/test/unittest_repository.py	Wed Apr 20 16:55:52 2011 +0200
@@ -685,6 +685,19 @@
             req.cnx.commit()
         self.assertEqual(cm.exception.errors, {'inline1-subject': u'RQLUniqueConstraint S type T, S inline1 A1, A1 todo_by C, Y type T, Y inline1 A2, A2 todo_by C failed'})
 
+    def test_add_relations_at_creation_with_del_existing_rel(self):
+        req = self.request()
+        person = req.create_entity('Personne', nom=u'Toto', prenom=u'Lanturlu', sexe=u'M')
+        users_rql = 'Any U WHERE U is CWGroup, U name "users"'
+        users = self.execute(users_rql).get_entity(0, 0)
+        req.create_entity('CWUser',
+                      login=u'Toto',
+                      upassword=u'firstname',
+                      firstname=u'firstname',
+                      surname=u'surname',
+                      reverse_login_user=person,
+                      in_group=users)
+        self.commit()
 
 
 class PerformanceTest(CubicWebTC):
@@ -825,5 +838,7 @@
         t1 = time.time()
         self.info('add relations (inlined): %.2gs', t1-t0)
 
+
+
 if __name__ == '__main__':
     unittest_main()