Skip to content

Commit

Permalink
Merge pull request #58 from sthenault/id0
Browse files Browse the repository at this point in the history
Fix case where id are explicitly specified and starts by 0
  • Loading branch information
uralbash committed Jun 22, 2018
2 parents 990636c + 029b889 commit ef10ba5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
11 changes: 6 additions & 5 deletions sqlalchemy_mptt/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def mptt_before_insert(mapper, connection, instance):
db_pk = instance.get_pk_column()
table_pk = getattr(table.c, db_pk.name)

if not instance.parent_id:
if instance.parent_id is None:
instance.left = 1
instance.right = 2
instance.level = instance.get_default_level()
Expand Down Expand Up @@ -176,7 +176,7 @@ def mptt_before_delete(mapper, connection, instance, delete=True):
)
)

if instance.parent_id or not delete:
if instance.parent_id is not None or not delete:
""" Update key of current tree
UPDATE tree
Expand Down Expand Up @@ -358,14 +358,15 @@ def mptt_before_update(mapper, connection, instance):
).fetchone()

# if instance just update w/o move
# XXX why this str() around parent_id comparison?
if not left_sibling \
and str(node_parent_id) == str(instance.parent_id) \
and not mptt_move_inside:
if left_sibling_tree_id is None:
return

# fix tree shorting
if instance.parent_id:
if instance.parent_id is not None:
(
parent_id,
parent_pos_right,
Expand All @@ -385,14 +386,14 @@ def mptt_before_update(mapper, connection, instance):
table_pk == instance.parent_id
)
).fetchone()
if not node_parent_id and node_tree_id == parent_tree_id:
if node_parent_id is None and node_tree_id == parent_tree_id:
instance.parent_id = None
return

# delete from old tree
mptt_before_delete(mapper, connection, instance, False)

if instance.parent_id:
if instance.parent_id is not None:
""" Put there right position of new parent node (there moving node
should be moved)
"""
Expand Down
27 changes: 26 additions & 1 deletion sqlalchemy_mptt/tests/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@

import unittest

from sqlalchemy import Column, Boolean, Integer
from sqlalchemy import Column, Boolean, Integer, create_engine
from sqlalchemy.event import contains
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

from sqlalchemy_mptt import mptt_sessionmaker

from . import TreeTestingMixin
from ..mixins import BaseNestedSets
Expand Down Expand Up @@ -150,3 +153,25 @@ def test_remove(self):
)
)
tree_manager.register_events()


class Tree0Id(unittest.TestCase):
"""Test case where node id is provided and starts with 0
See comments in https://github.com/uralbash/sqlalchemy_mptt/issues/57
"""
def test(self):
engine = create_engine('sqlite:///:memory:')
Session = mptt_sessionmaker(sessionmaker(bind=engine))
session = Session()
Base.metadata.create_all(engine)

root = Tree(id=0)
child = Tree(id=1, parent_id=0)

session.add(root)
session.add(child)
session.commit()

self.assertEqual(root.tree_id, 1)
self.assertEqual(child.tree_id, 1)

0 comments on commit ef10ba5

Please sign in to comment.