fix select principal so results are predictable during tests
authorSylvain <syt@logilab.fr>
Wed, 11 Feb 2009 17:54:55 +0100
changeset 599 9ef680acd92a
parent 598 4705ceb624cc
child 600 1fbf11a876a8
fix select principal so results are predictable during tests
devtools/repotest.py
server/rqlannotation.py
server/test/unittest_rql2sql.py
--- a/devtools/repotest.py	Wed Feb 11 17:44:08 2009 +0100
+++ b/devtools/repotest.py	Wed Feb 11 17:54:55 2009 +0100
@@ -249,7 +249,8 @@
 _orig_select_principal = rqlannotation._select_principal
 
 def _select_principal(scope, relations):
-    return _orig_select_principal(scope, sorted(relations, key=lambda x: x.r_type))
+    return _orig_select_principal(scope, relations,
+                                  _sort=lambda rels: sorted(rels, key=lambda x: x.r_type))
 
 try:
     from cubicweb.server.msplanner import PartPlanInformation
--- a/server/rqlannotation.py	Wed Feb 11 17:44:08 2009 +0100
+++ b/server/rqlannotation.py	Wed Feb 11 17:54:55 2009 +0100
@@ -146,16 +146,17 @@
 
 class CantSelectPrincipal(Exception): pass
 
-def _select_principal(sqlscope, relations):
+def _select_principal(sqlscope, relations, _sort=lambda x:x):
     """given a list of rqlst relations, select one which will be used to
     represent an invariant variable (e.g. using on extremity of the relation
     instead of the variable's type table
     """
+    # _sort argument is there for test
     diffscope_rels = {}
     has_same_scope_rel = False
     ored_rels = set()
     diffscope_rels = set()
-    for rel in relations:
+    for rel in _sort(relations):
         # note: only eid and has_text among all final relations may be there
         if rel.r_type in ('eid', 'identity'):
             has_same_scope_rel = rel.sqlscope is sqlscope
@@ -175,18 +176,17 @@
                 if isinstance(common_parent(rel1, rel2), Or):
                     ored_rels.discard(rel1)
                     ored_rels.discard(rel2)
-    for rel in ored_rels:
+    for rel in _sort(ored_rels):
         if rel.sqlscope is sqlscope:
             return rel
         diffscope_rels.add(rel)
     # if DISTINCT query, can use variable from a different scope as principal
     # since introduced duplicates will be removed
     if sqlscope.stmt.distinct and diffscope_rels:
-        return iter(diffscope_rels).next()
+        return iter(_sort(diffscope_rels)).next()
     # XXX  could use a relation for a different scope if it can't generate
     # duplicates, so we would have to check cardinality
-    raise CantSelectPrincipal()
-    
+    raise CantSelectPrincipal()    
 
 def _select_main_var(relations):
     """given a list of rqlst relations, select one which will be used as main
--- a/server/test/unittest_rql2sql.py	Wed Feb 11 17:44:08 2009 +0100
+++ b/server/test/unittest_rql2sql.py	Wed Feb 11 17:54:55 2009 +0100
@@ -805,7 +805,7 @@
      'F name "read", F require_group E, U in_group E)), U eid 1',
      '''SELECT A.eid, rel_documented_by0.eid_to
 FROM Affaire AS A LEFT OUTER JOIN documented_by_relation AS rel_documented_by0 ON (rel_documented_by0.eid_from=A.eid)
-WHERE ((rel_documented_by0.eid_to IS NULL) OR (EXISTS(SELECT 1 FROM require_permission_relation AS rel_require_permission1, EPermission AS F, require_group_relation AS rel_require_group2, in_group_relation AS rel_in_group3 WHERE rel_documented_by0.eid_to=rel_require_permission1.eid_from AND rel_require_permission1.eid_to=F.eid AND F.name=read AND rel_require_group2.eid_from=F.eid AND rel_in_group3.eid_from=1 AND rel_in_group3.eid_to=rel_require_group2.eid_to)))'''),
+WHERE ((rel_documented_by0.eid_to IS NULL) OR (EXISTS(SELECT 1 FROM require_permission_relation AS rel_require_permission1, EPermission AS F, require_group_relation AS rel_require_group2, in_group_relation AS rel_in_group3 WHERE rel_documented_by0.eid_to=rel_require_permission1.eid_from AND rel_require_permission1.eid_to=F.eid AND F.name=read AND rel_require_group2.eid_from=F.eid AND rel_in_group3.eid_to=rel_require_group2.eid_to AND rel_in_group3.eid_from=1)))'''),
 
     ("Any X WHERE X eid 12, P? connait X",
      '''SELECT X.eid