[querier] test and fix potential bug in insertion query with multiple relations inside, caused by a name conflict. Also fix another bug non reproduced by using a set instead of a list (list may leads to compare entity instances which isn't allowed). stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 03 Jun 2010 13:20:07 +0200
branchstable
changeset 5657 8f56691f01de
parent 5656 abe97430b3f5
child 5660 97343804414b
[querier] test and fix potential bug in insertion query with multiple relations inside, caused by a name conflict. Also fix another bug non reproduced by using a set instead of a list (list may leads to compare entity instances which isn't allowed).
server/querier.py
server/test/unittest_querier.py
--- a/server/querier.py	Thu Jun 03 10:20:00 2010 +0200
+++ b/server/querier.py	Thu Jun 03 13:20:07 2010 +0200
@@ -419,7 +419,7 @@
         # list of new or updated entities definition (utils.Entity)
         self.e_defs = [[]]
         # list of new relation definition (3-uple (from_eid, r_type, to_eid)
-        self.r_defs = []
+        self.r_defs = set()
         # indexes to track entity definitions bound to relation definitions
         self._r_subj_index = {}
         self._r_obj_index = {}
@@ -432,7 +432,7 @@
 
     def add_relation_def(self, rdef):
         """add an relation definition to build"""
-        self.r_defs.append(rdef)
+        self.r_defs.add(rdef)
         if not isinstance(rdef[0], int):
             self._r_subj_index.setdefault(rdef[0], []).append(rdef)
         if not isinstance(rdef[2], int):
@@ -458,9 +458,9 @@
         for i, row in enumerate(self.e_defs[:]):
             self.e_defs[i][colidx] = edefs[0]
             samplerow = self.e_defs[i]
-            for edef in edefs[1:]:
+            for edef_ in edefs[1:]:
                 row = samplerow[:]
-                row[colidx] = edef
+                row[colidx] = edef_
                 self.e_defs.append(row)
         # now, see if this entity def is referenced as subject in some relation
         # definition
@@ -469,8 +469,8 @@
                 expanded = self._expanded(rdef)
                 result = []
                 for exp_rdef in expanded:
-                    for edef in edefs:
-                        result.append( (edef, exp_rdef[1], exp_rdef[2]) )
+                    for edef_ in edefs:
+                        result.append( (edef_, exp_rdef[1], exp_rdef[2]) )
                 self._expanded_r_defs[rdef] = result
         # and finally, see if this entity def is referenced as object in some
         # relation definition
@@ -479,8 +479,8 @@
                 expanded = self._expanded(rdef)
                 result = []
                 for exp_rdef in expanded:
-                    for edef in edefs:
-                        result.append( (exp_rdef[0], exp_rdef[1], edef) )
+                    for edef_ in edefs:
+                        result.append( (exp_rdef[0], exp_rdef[1], edef_) )
                 self._expanded_r_defs[rdef] = result
 
     def _expanded(self, rdef):
--- a/server/test/unittest_querier.py	Thu Jun 03 10:20:00 2010 +0200
+++ b/server/test/unittest_querier.py	Thu Jun 03 13:20:07 2010 +0200
@@ -915,12 +915,33 @@
         self.assert_(rset.rows)
         self.assertEquals(rset.description, [('Personne', 'Societe',)])
 
+    def test_insert_7_2(self):
+        self.execute("INSERT Personne X, Societe Y: X nom N, Y nom 'toto', X travaille Y WHERE U login N")
+        rset = self.execute('Any X, Y WHERE Y nom "toto", X travaille Y')
+        self.assertEquals(len(rset), 2)
+        self.assertEquals(rset.description, [('Personne', 'Societe',),
+                                             ('Personne', 'Societe',)])
+
     def test_insert_8(self):
         self.execute("INSERT Societe Y, Personne X: Y nom N, X nom 'toto', X travaille Y WHERE U login 'admin', U login N")
         rset = self.execute('Any X, Y WHERE X nom "toto", Y nom "admin", X travaille Y')
         self.assert_(rset.rows)
         self.assertEquals(rset.description, [('Personne', 'Societe',)])
 
+    def test_insert_9(self):
+        self.execute("INSERT Societe X: X nom  'Lo'")
+        self.execute("INSERT Societe X: X nom  'Gi'")
+        self.execute("INSERT SubDivision X: X nom  'Lab'")
+        rset = self.execute("INSERT Personne X: X nom N, X travaille Y, X travaille_subdivision Z WHERE Y is Societe, Z is SubDivision, Y nom N")
+        self.assertEquals(len(rset), 2)
+        self.assertEquals(rset.description, [('Personne',), ('Personne',)])
+        # self.assertSetEquals(set(x.nom for x in rset.entities()),
+        #                      ['Lo', 'Gi'])
+        # self.assertSetEquals(set(y.nom for x in rset.entities() for y in x.travaille),
+        #                      ['Lo', 'Gi'])
+        # self.assertEquals([y.nom for x in rset.entities() for y in x.travaille_subdivision],
+        #                      ['Lab', 'Lab'])
+
     def test_insert_query_error(self):
         self.assertRaises(Exception,
                           self.execute,