From f754a90400ff3f6c63125d93e1646b9d9aad40c8 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Fri, 3 Jul 2015 12:11:47 +0500 Subject: [PATCH] fix get_tree with emty table. --- CHANGES.rst | 49 +++---------------------- CHANGES_OLD.rst | 43 ++++++++++++++++++++++ sqlalchemy_mptt/__init__.py | 2 +- sqlalchemy_mptt/mixins.py | 2 +- sqlalchemy_mptt/tests/cases/get_tree.py | 17 +++++++++ 5 files changed, 67 insertions(+), 46 deletions(-) create mode 100644 CHANGES_OLD.rst diff --git a/CHANGES.rst b/CHANGES.rst index fbb83ac..791f4b7 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,3 +1,8 @@ +0.1.6 (2015-07-03) +================== + +- fix bug with ``get_tree`` when no rows in database. + 0.1.5 (2015-06-25) ================== @@ -62,47 +67,3 @@ Bug Fixes - Fix concurrency issue with multiple session (#36) - Flushing the session now expire the instance and it's children (#33) - -0.0.9 (2014-10-09) -================== - -- Add MANIFEST.in -- New docs -- fixes in setup.py - -0.0.8 (2014-08-15) -================== - -- Add CONTRIBUTORS.txt - -Features --------- - -- Automatically register tree classes enhancement (#28) -- Added support polymorphic tree models (#24) - -Bug Fixes ---------- - -- Fix expire left/right attributes of parent somewhen after the `before_insert` event (#30) -- Fix tree_id is incorrectly set to an existing tree if no parent is set (#23) -- Fix package is not installable if sqlalchemy is not (yet) installed (#22) - -0.0.7 (2014-08-04) -================== - -- Add LICENSE.txt - -Bug Fixes ---------- - -- fix get_db_pk function - - -0.0.6 (2014-07-31) -================== - -Bug Fixes ---------- - -- Allow the primary key to not be named "id" #20. See https://github.com/ITCase/sqlalchemy_mptt/issues/20 diff --git a/CHANGES_OLD.rst b/CHANGES_OLD.rst new file mode 100644 index 0000000..ef2f1b4 --- /dev/null +++ b/CHANGES_OLD.rst @@ -0,0 +1,43 @@ +0.0.9 (2014-10-09) +================== + +- Add MANIFEST.in +- New docs +- fixes in setup.py + +0.0.8 (2014-08-15) +================== + +- Add CONTRIBUTORS.txt + +Features +-------- + +- Automatically register tree classes enhancement (#28) +- Added support polymorphic tree models (#24) + +Bug Fixes +--------- + +- Fix expire left/right attributes of parent somewhen after the `before_insert` event (#30) +- Fix tree_id is incorrectly set to an existing tree if no parent is set (#23) +- Fix package is not installable if sqlalchemy is not (yet) installed (#22) + +0.0.7 (2014-08-04) +================== + +- Add LICENSE.txt + +Bug Fixes +--------- + +- fix get_db_pk function + + +0.0.6 (2014-07-31) +================== + +Bug Fixes +--------- + +- Allow the primary key to not be named "id" #20. See https://github.com/ITCase/sqlalchemy_mptt/issues/20 diff --git a/sqlalchemy_mptt/__init__.py b/sqlalchemy_mptt/__init__.py index 303f98c..a96f42c 100644 --- a/sqlalchemy_mptt/__init__.py +++ b/sqlalchemy_mptt/__init__.py @@ -10,7 +10,7 @@ from .mixins import BaseNestedSets from .events import TreesManager -__version__ = "0.1.6.dev1" +__version__ = "0.1.6" __mixins__ = [BaseNestedSets] __all__ = ['BaseNestedSets', 'mptt_sessionmaker'] diff --git a/sqlalchemy_mptt/mixins.py b/sqlalchemy_mptt/mixins.py index 8cf9c76..6a4eecf 100644 --- a/sqlalchemy_mptt/mixins.py +++ b/sqlalchemy_mptt/mixins.py @@ -209,7 +209,7 @@ def query(nodes): nodes = nodes.order_by(cls.tree_id, cls.level, cls.left).all() # search minimal level of nodes. - min_level = min([node.level for node in nodes]) + min_level = min([node.level for node in nodes] or [None]) def get_node_id(node): return getattr(node, node.get_pk_name()) diff --git a/sqlalchemy_mptt/tests/cases/get_tree.py b/sqlalchemy_mptt/tests/cases/get_tree.py index 1cabd11..61ec239 100644 --- a/sqlalchemy_mptt/tests/cases/get_tree.py +++ b/sqlalchemy_mptt/tests/cases/get_tree.py @@ -4,6 +4,23 @@ def get_obj(session, model, id): class Tree(object): + def test_get_empty_tree(self): + """ + No rows in database. + """ + self.session.query(self.model).delete() + self.session.flush() + tree = self.model.get_tree(self.session) + self.assertEqual(tree, []) + + def test_get_empty_tree_with_custom_query(self): + """ + No rows with id < 0. + """ + query = lambda x: x.filter(self.model.get_pk_column() < 0) + tree = self.model.get_tree(self.session, query=query) + self.assertEqual(tree, []) + def test_get_tree(self): """.. note::