[test] sort to avoid random failure due to dict order stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 23 Sep 2011 15:41:21 +0200
branchstable
changeset 7850 d14b77c42b06
parent 7849 7937cf60f2ce
child 7851 54e6abed520a
[test] sort to avoid random failure due to dict order
devtools/repotest.py
rqlrewrite.py
test/unittest_rqlrewrite.py
--- a/devtools/repotest.py	Fri Sep 23 15:05:09 2011 +0200
+++ b/devtools/repotest.py	Fri Sep 23 15:41:21 2011 +0200
@@ -329,9 +329,10 @@
 
 # monkey patch some methods to get predicatable results #######################
 
-from cubicweb.rqlrewrite import RQLRewriter
-_orig_insert_snippets = RQLRewriter.insert_snippets
-_orig_build_variantes = RQLRewriter.build_variantes
+from cubicweb import rqlrewrite
+_orig_iter_relations = rqlrewrite.iter_relations
+_orig_insert_snippets = rqlrewrite.RQLRewriter.insert_snippets
+_orig_build_variantes = rqlrewrite.RQLRewriter.build_variantes
 
 def _insert_snippets(self, snippets, varexistsmap=None):
     _orig_insert_snippets(self, sorted(snippets, snippet_cmp), varexistsmap)
@@ -415,9 +416,13 @@
 def _syntax_tree_search(*args, **kwargs):
     return deepcopy(_orig_syntax_tree_search(*args, **kwargs))
 
+def _ordered_iter_relations(stinfo):
+    return sorted(_orig_iter_relations(stinfo), key=lambda x:x.r_type)
+
 def do_monkey_patch():
-    RQLRewriter.insert_snippets = _insert_snippets
-    RQLRewriter.build_variantes = _build_variantes
+    rqlrewrite.iter_relations = _ordered_iter_relations
+    rqlrewrite.RQLRewriter.insert_snippets = _insert_snippets
+    rqlrewrite.RQLRewriter.build_variantes = _build_variantes
     ExecutionPlan._check_permissions = _check_permissions
     ExecutionPlan.tablesinorder = None
     ExecutionPlan.init_temp_table = _init_temp_table
@@ -426,8 +431,9 @@
     PyroRQLSource.syntax_tree_search = _syntax_tree_search
 
 def undo_monkey_patch():
-    RQLRewriter.insert_snippets = _orig_insert_snippets
-    RQLRewriter.build_variantes = _orig_build_variantes
+    rqlrewrite.iter_relations = _orig_iter_relations
+    rqlrewrite.RQLRewriter.insert_snippets = _orig_insert_snippets
+    rqlrewrite.RQLRewriter.build_variantes = _orig_build_variantes
     ExecutionPlan._check_permissions = _orig_check_permissions
     ExecutionPlan.init_temp_table = _orig_init_temp_table
     PartPlanInformation.merge_input_maps = _orig_merge_input_maps
--- a/rqlrewrite.py	Fri Sep 23 15:05:09 2011 +0200
+++ b/rqlrewrite.py	Fri Sep 23 15:41:21 2011 +0200
@@ -119,6 +119,10 @@
     return newsolutions
 
 
+def iter_relations(stinfo):
+    # this is a function so that test may return relation in a predictable order
+    return stinfo['relations'] - stinfo['rhsrelations']
+
 class Unsupported(Exception):
     """raised when an rql expression can't be inserted in some rql query
     because it create an unresolvable query (eg no solutions found)
@@ -349,7 +353,7 @@
             while todo:
                 varname, stinfo = todo.pop()
                 done.add(varname)
-                for rel in stinfo['relations'] - stinfo['rhsrelations']:
+                for rel in iter_relations(stinfo):
                     if rel in done:
                         continue
                     done.add(rel)
@@ -380,7 +384,7 @@
                         if vref.name not in done and rschema.inlined:
                             # we can use vref here define in above for loop
                             ostinfo = vref.variable.stinfo
-                            for orel in ostinfo['relations'] - ostinfo['rhsrelations']:
+                            for orel in iter_relations(ostinfo):
                                 orschema = get_rschema(orel.r_type)
                                 if orschema.final or orschema.inlined:
                                     todo.append( (vref.name, ostinfo) )
--- a/test/unittest_rqlrewrite.py	Fri Sep 23 15:05:09 2011 +0200
+++ b/test/unittest_rqlrewrite.py	Fri Sep 23 15:41:21 2011 +0200
@@ -21,9 +21,8 @@
 from yams import BadSchemaDefinition
 from rql import parse, nodes, RQLHelper
 
-from cubicweb import Unauthorized
+from cubicweb import Unauthorized, rqlrewrite
 from cubicweb.schema import RRQLExpression, ERQLExpression
-from cubicweb.rqlrewrite import RQLRewriter
 from cubicweb.devtools import repotest, TestServerConfiguration
 
 
@@ -62,9 +61,10 @@
             @staticmethod
             def simplify(mainrqlst, needcopy=False):
                 rqlhelper.simplify(rqlst, needcopy)
-    rewriter = RQLRewriter(mock_object(vreg=FakeVReg, user=(mock_object(eid=1))))
+    rewriter = rqlrewrite.RQLRewriter(
+        mock_object(vreg=FakeVReg, user=(mock_object(eid=1))))
     snippets = []
-    for v, exprs in snippets_map.items():
+    for v, exprs in sorted(snippets_map.items()):
         rqlexprs = [isinstance(snippet, basestring)
                     and mock_object(snippet_rqlst=parse('Any X WHERE '+snippet).children[0],
                                     expression='Any X WHERE '+snippet)
@@ -210,8 +210,8 @@
                         }, {})
         # XXX suboptimal
         self.failUnlessEqual(rqlst.as_string(),
-                             "Any C,A,R WITH A,R,C BEING "
-                             "(Any A,R,C WHERE A ref R, A? inlined_card C, "
+                             "Any C,A,R WITH A,C,R BEING "
+                             "(Any A,C,R WHERE A? inlined_card C, A ref R, "
                              "(A is NULL) OR (EXISTS(A inlined_card B, B require_permission D, "
                              "B is Card, D is CWPermission)), "
                              "A is Affaire, C is Card, EXISTS(C require_permission E, E is CWPermission))")