select.undefine_variable properly cleanup solutions (and restore them on undo) stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Mon, 11 Oct 2010 11:33:19 +0200
branchstable
changeset 594 6eac2a0ea919
parent 593 7e89c6736382
child 595 d0c3ec1701b9
select.undefine_variable properly cleanup solutions (and restore them on undo)
stmts.py
test/unittest_nodes.py
undo.py
--- a/stmts.py	Mon Oct 04 16:48:00 2010 +0200
+++ b/stmts.py	Mon Oct 11 11:33:19 2010 +0200
@@ -736,7 +736,10 @@
         # effective undefine operation
         if self.should_register_op:
             from rql.undo import UndefineVarOperation
-            self.undo_manager.add_operation(UndefineVarOperation(var))
+            solutions = [d.copy() for d in self.solutions]
+            self.undo_manager.add_operation(UndefineVarOperation(var, self, solutions))
+        for sol in self.solutions:
+            sol.pop(var.name, None)
         del self.defined_vars[var.name]
 
     def _var_index(self, var):
--- a/test/unittest_nodes.py	Mon Oct 04 16:48:00 2010 +0200
+++ b/test/unittest_nodes.py	Mon Oct 11 11:33:19 2010 +0200
@@ -191,6 +191,21 @@
         tree.check_references()
         self.assertEquals(tree.as_string(), 'Any X,Y ORDERBY X,Y')
 
+    def test_select_undefine_variable(self):
+        tree = sparse('Any X,Y ORDERBY X,Y WHERE X work_for Y')
+        tree.save_state()
+        select = tree.children[0]
+        select.undefine_variable(select.defined_vars['Y'])
+        self.assertEqual(select.solutions, [{'X': 'Person'},
+                                             {'X': 'Student'}])
+        tree.check_references()
+        self.assertEqual(tree.as_string(), 'Any X ORDERBY X')
+        tree.recover()
+        tree.check_references()
+        self.assertEqual(tree.as_string(), 'Any X,Y ORDERBY X,Y WHERE X work_for Y')
+        self.assertEqual(select.solutions, [{'X': 'Person', 'Y': 'Company'},
+                                             {'X': 'Student', 'Y': 'Company'}])
+
     def test_select_set_distinct(self):
         tree = self._parse('DISTINCT Any X')
         tree.save_state()
--- a/undo.py	Mon Oct 04 16:48:00 2010 +0200
+++ b/undo.py	Mon Oct 11 11:33:19 2010 +0200
@@ -74,18 +74,21 @@
 
 class MakeVarOperation(NodeOperation):
     """Defines how to undo make_variable()."""
-
     def undo(self, selection):
         """undo the operation on the selection"""
         self.stmt.undefine_variable(self.node)
 
 class UndefineVarOperation(NodeOperation):
     """Defines how to undo undefine_variable()."""
+    def __init__(self, node, stmt, solutions):
+        NodeOperation.__init__(self, node, stmt)
+        self.solutions = solutions
 
     def undo(self, selection):
         """undo the operation on the selection"""
         var = self.node
         self.stmt.defined_vars[var.name] = var
+        self.stmt.solutions = self.solutions
 
 class SelectVarOperation(NodeOperation):
     """Defines how to undo add_selected()."""