36 a = (a[0], [e.expression for e in a[1]]) |
36 a = (a[0], [e.expression for e in a[1]]) |
37 b = (b[0], [e.expression for e in b[1]]) |
37 b = (b[0], [e.expression for e in b[1]]) |
38 return cmp(a, b) |
38 return cmp(a, b) |
39 |
39 |
40 def test_plan(self, rql, expected, kwargs=None): |
40 def test_plan(self, rql, expected, kwargs=None): |
41 plan = self._prepare_plan(rql, kwargs) |
41 with self.session.new_cnx() as cnx: |
42 self.planner.build_plan(plan) |
42 plan = self._prepare_plan(cnx, rql, kwargs) |
43 try: |
43 self.planner.build_plan(plan) |
44 self.assertEqual(len(plan.steps), len(expected), |
44 try: |
45 'expected %s steps, got %s' % (len(expected), len(plan.steps))) |
45 self.assertEqual(len(plan.steps), len(expected), |
46 # step order is important |
46 'expected %s steps, got %s' % (len(expected), len(plan.steps))) |
47 for i, step in enumerate(plan.steps): |
47 # step order is important |
48 compare_steps(self, step.test_repr(), expected[i]) |
48 for i, step in enumerate(plan.steps): |
49 except AssertionError: |
49 compare_steps(self, step.test_repr(), expected[i]) |
50 pprint([step.test_repr() for step in plan.steps]) |
50 except AssertionError: |
51 raise |
51 pprint([step.test_repr() for step in plan.steps]) |
|
52 raise |
52 |
53 |
53 def compare_steps(self, step, expected): |
54 def compare_steps(self, step, expected): |
54 try: |
55 try: |
55 self.assertEqual(step[0], expected[0], 'expected step type %s, got %s' % (expected[0], step[0])) |
56 self.assertEqual(step[0], expected[0], 'expected step type %s, got %s' % (expected[0], step[0])) |
56 if len(step) > 2 and isinstance(step[1], list) and isinstance(expected[1], list): |
57 if len(step) > 2 and isinstance(step[1], list) and isinstance(expected[1], list): |
280 self.repo.vreg.rqlhelper.backend = 'postgres' # so FTIRANK is considered |
281 self.repo.vreg.rqlhelper.backend = 'postgres' # so FTIRANK is considered |
281 |
282 |
282 def tearDown(self): |
283 def tearDown(self): |
283 undo_monkey_patch() |
284 undo_monkey_patch() |
284 |
285 |
285 def _prepare_plan(self, rql, kwargs=None): |
286 def _prepare_plan(self, cnx, rql, kwargs=None): |
286 rqlst = self.o.parse(rql, annotate=True) |
287 rqlst = self.o.parse(rql, annotate=True) |
287 self.o.solutions(self.session, rqlst, kwargs) |
288 self.o.solutions(cnx, rqlst, kwargs) |
288 if rqlst.TYPE == 'select': |
289 if rqlst.TYPE == 'select': |
289 self.repo.vreg.rqlhelper.annotate(rqlst) |
290 self.repo.vreg.rqlhelper.annotate(rqlst) |
290 for select in rqlst.children: |
291 for select in rqlst.children: |
291 select.solutions.sort() |
292 select.solutions.sort() |
292 else: |
293 else: |
293 rqlst.solutions.sort() |
294 rqlst.solutions.sort() |
294 with self.session.ensure_cnx_set: |
295 return self.o.plan_factory(rqlst, kwargs, cnx) |
295 return self.o.plan_factory(rqlst, kwargs, self.session) |
|
296 |
296 |
297 |
297 |
298 # monkey patch some methods to get predicatable results ####################### |
298 # monkey patch some methods to get predicatable results ####################### |
299 |
299 |
300 from cubicweb import rqlrewrite |
300 from cubicweb import rqlrewrite |