angri avatar angri committed bd5f6b9

implemented delete_subtree()

Comments (0)

Files changed (2)

sqlamp/__init__.py

                        old_tree_id=old_tree_id, old_path=old_path,
                        old_depth=old_depth)
 
+    def delete_subtree(self, session, node_id):
+        opts = self._mp_opts
+        [[old_path, old_depth, old_tree_id]] = session.execute(
+            sqlalchemy.select([opts.path_field, opts.depth_field,
+                               opts.tree_id_field], opts.pk_field == node_id)
+        )
+        filter_ = opts.filter_descendants(old_tree_id, old_path, and_self=True)
+        session.execute(sqlalchemy.delete(opts.table, filter_))
+        self._pull_nodes('up', session, old_tree_id, old_path, old_depth)
+
     def move_subtree_before(self, session, node_id, anchor_id):
         self._move_subtree_by_sibling('before', session, node_id, anchor_id)
 

tests/functional-tests.py

         data_after = query.execute().fetchall()
         self.assertEqual(data_before, data_after)
 
+    def test_delete_subtree(self):
+        self._fill_tree()
+        Cls.mp.delete_subtree(self.sess, self.n('child212').id)
+        self.sess.expunge_all()
+        child21 = self.n('child21')
+        tree = [(n.name, n.mp_path, n.mp_depth)
+                for n in child21.mp.query_descendants()]
+        self.assertEqual(tree, [
+            ('child211', '0000', 2),
+        ])
+
+    def test_delete_subtree_gaps(self):
+        self._fill_tree()
+        Cls.mp.delete_subtree(self.sess, self.n('child211').id)
+        self.sess.expunge_all()
+        child21 = self.n('child21')
+        tree = [(n.name, n.mp_path, n.mp_depth)
+                for n in child21.mp.query_descendants()]
+        self.assertEqual(tree, [
+            ('child212', '0000', 2),
+              ('child2121', '000000', 3),
+              ('child2122', '000001', 3),
+                ('child21221', '00000100', 4),
+                ('child21222', '00000101', 4),
+        ])
+
 
 def get_suite():
     loader = unittest.TestLoader()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.