Skip to content

Commit

Permalink
move tree to json #43
Browse files Browse the repository at this point in the history
  • Loading branch information
uralbash committed Jun 16, 2015
1 parent c161a99 commit 0c355cc
Show file tree
Hide file tree
Showing 3 changed files with 208 additions and 80 deletions.
122 changes: 43 additions & 79 deletions sqlalchemy_mptt/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,94 +5,56 @@
# Copyright © 2014 uralbash <root@uralbash.ru>
#
# Distributed under terms of the MIT license.
""" Base mptt tree
.. code::
level Nested sets tree1
1 1(1)22
_______________|___________________
| | |
2 2(2)5 6(4)11 12(7)21
| ^ ^
3 3(3)4 7(5)8 9(6)10 13(8)16 17(10)20
| |
4 14(9)15 18(11)19
level Nested sets tree2
1 1(12)22
_______________|___________________
| | |
2 2(13)5 6(15)11 12(18)21
| ^ ^
3 3(14)4 7(16)8 9(17)10 13(19)16 17(21)20
| |
4 14(20)15 18(22)19
"""
import json
import os

from sqlalchemy import create_engine, event
from sqlalchemy.orm import sessionmaker

from sqlalchemy_mptt import mptt_sessionmaker

from .cases.edit_node import Changes
from .cases.get_tree import Tree
from .cases.initialize import Initialize
from .cases.move_node import MoveAfter, MoveBefore, MoveInside
from .cases.get_tree import Tree
from sqlalchemy_mptt import mptt_sessionmaker


def add_fixture(model, fixtures, session):
"""
Add fixtures to database.
Example:
.. code::
hashes = ({'foo': {'foo': 'bar', '1': '2'}}, {'foo': {'test': 'data'}})
add_fixture(TestHSTORE, hashes)
"""
for fixture in fixtures:
session.add(model(**fixture))


def add_mptt_tree(session, model):
""" Init mptt tree
class Fixtures(object):

.. code::
def __init__(self, session):
self.session = session

level Nested sets tree1
1 1(1)22
_______________|___________________
| | |
2 2(2)5 6(4)11 12(7)21
| ^ ^
3 3(3)4 7(5)8 9(6)10 13(8)16 17(10)20
| |
4 14(9)15 18(11)19
level Nested sets tree2
1 1(12)22
_______________|___________________
| | |
2 2(13)5 6(15)11 12(18)21
| ^ ^
3 3(14)4 7(16)8 9(17)10 13(19)16 17(21)20
| |
4 14(20)15 18(22)19
"""
session.query(model).delete()
session.commit()
tree1 = (
{'ppk': '1', 'parent_id': None},

{'ppk': '2', 'visible': True, 'parent_id': '1'},
{'ppk': '3', 'visible': True, 'parent_id': '2'},

{'ppk': '4', 'visible': True, 'parent_id': '1'},
{'ppk': '5', 'visible': True, 'parent_id': '4'},
{'ppk': '6', 'visible': True, 'parent_id': '4'},

{'ppk': '7', 'visible': True, 'parent_id': '1'},
{'ppk': '8', 'visible': True, 'parent_id': '7'},
{'ppk': '9', 'parent_id': '8'},
{'ppk': '10', 'parent_id': '7'},
{'ppk': '11', 'parent_id': '10'},
)

tree2 = (
{'ppk': '12', 'parent_id': None},

{'ppk': '13', 'parent_id': '12', 'tree_id': '2'},
{'ppk': '14', 'parent_id': '13', 'tree_id': '2'},

{'ppk': '15', 'parent_id': '12', 'tree_id': '2'},
{'ppk': '16', 'parent_id': '15', 'tree_id': '2'},
{'ppk': '17', 'parent_id': '15', 'tree_id': '2'},

{'ppk': '18', 'parent_id': '12', 'tree_id': '2'},
{'ppk': '19', 'parent_id': '18', 'tree_id': '2'},
{'ppk': '20', 'parent_id': '19', 'tree_id': '2'},
{'ppk': '21', 'parent_id': '18', 'tree_id': '2'},
{'ppk': '22', 'parent_id': '21', 'tree_id': '2'},
)
add_fixture(model, tree1, session)
add_fixture(model, tree2, session)
def add(self, model, fixtures):
here = os.path.dirname(os.path.realpath(__file__))
file = open(os.path.join(here, fixtures))
fixtures = json.loads(file.read())
for fixture in fixtures:
self.session.add(model(**fixture))


class TreeTestingMixin(
Expand Down Expand Up @@ -124,7 +86,9 @@ def setUp(self):
Session = mptt_sessionmaker(sessionmaker(bind=self.engine))
self.session = Session()
self.base.metadata.create_all(self.engine)
add_mptt_tree(self.session, self.model)
fixture = Fixtures(self.session)
fixture.add(self.model, 'fixtures/tree.json')

self.result = self.session.query(
self.model.ppk, self.model.left, self.model.right,
self.model.level, self.model.parent_id, self.model.tree_id)
Expand Down
59 changes: 58 additions & 1 deletion sqlalchemy_mptt/tests/cases/move_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def test_move_before_to_top_level(self):
"""
node = self.session.query(self.model).filter(self.model.ppk == 4).one()
node.move_before("1")
node.move_before(1)
# id lft rgt lvl parent tree
self.assertEqual([(1, 1, 16, 1, None, 2),
(2, 2, 5, 2, 1, 2),
Expand Down Expand Up @@ -66,6 +66,63 @@ def test_move_before_to_top_level(self):
(21, 17, 20, 3, 18, 3),
(22, 18, 19, 4, 21, 3)], self.result.all())

def test_move_one_tree_before_another(self):
""" For example move node(12) before node(1)
initial state of the tree :mod:`sqlalchemy_mptt.tests.add_mptt_tree`
.. code::
<--------------------------------
|
level Nested sets tree1 |
1 1(1)22 |
_______________|___________________ |
| | | |
2 2(2)5 6(4)11 12(7)21 |
| ^ ^ |
3 3(3)4 7(5)8 9(6)10 13(8)16 17(10)20 |
| | |
4 14(9)15 18(11)19 |
|
level Nested sets tree2 |
1 1(12)22 ----------------------------
_______________|___________________
| | |
2 2(13)5 6(15)11 12(18)21
| ^ ^
3 3(14)4 7(16)8 9(17)10 13(19)16 17(21)20
| |
4 14(20)15 18(22)19
"""
node = self.session.query(self.model)\
.filter(self.model.ppk == 12).one()
node.move_before("1")
# id lft rgt lvl parent tree
self.assertEqual([(1, 1, 22, 1, None, 2),
(2, 2, 5, 2, 1, 2),
(3, 3, 4, 3, 2, 2),
(4, 6, 11, 2, 1, 2),
(5, 7, 8, 3, 4, 2),
(6, 9, 10, 3, 4, 2),
(7, 12, 21, 2, 1, 2),
(8, 13, 16, 3, 7, 2),
(9, 14, 15, 4, 8, 2),
(10, 17, 20, 3, 7, 2),
(11, 18, 19, 4, 10, 2),

(12, 1, 22, 1, None, 1),
(13, 2, 5, 2, 12, 1),
(14, 3, 4, 3, 13, 1),
(15, 6, 11, 2, 12, 1),
(16, 7, 8, 3, 15, 1),
(17, 9, 10, 3, 15, 1),
(18, 12, 21, 2, 12, 1),
(19, 13, 16, 3, 18, 1),
(20, 14, 15, 4, 19, 1),
(21, 17, 20, 3, 18, 1),
(22, 18, 19, 4, 21, 1)], self.result.all())

def test_move_before_function(self):
""" For example move node(8) before node(4)
Expand Down
107 changes: 107 additions & 0 deletions sqlalchemy_mptt/tests/fixtures/tree.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
[
{
"parent_id": null,
"ppk": "1"
},
{
"parent_id": "1",
"ppk": "2",
"visible": 1
},
{
"parent_id": "2",
"ppk": "3",
"visible": 1
},
{
"parent_id": "1",
"ppk": "4",
"visible": 1
},
{
"parent_id": "4",
"ppk": "5",
"visible": 1
},
{
"parent_id": "4",
"ppk": "6",
"visible": 1
},
{
"parent_id": "1",
"ppk": "7",
"visible": 1
},
{
"parent_id": "7",
"ppk": "8",
"visible": 1
},
{
"parent_id": "8",
"ppk": "9"
},
{
"parent_id": "7",
"ppk": "10"
},
{
"parent_id": "10",
"ppk": "11"
},
{
"parent_id": null,
"ppk": "12"
},
{
"parent_id": "12",
"ppk": "13",
"tree_id": "2"
},
{
"parent_id": "13",
"ppk": "14",
"tree_id": "2"
},
{
"parent_id": "12",
"ppk": "15",
"tree_id": "2"
},
{
"parent_id": "15",
"ppk": "16",
"tree_id": "2"
},
{
"parent_id": "15",
"ppk": "17",
"tree_id": "2"
},
{
"parent_id": "12",
"ppk": "18",
"tree_id": "2"
},
{
"parent_id": "18",
"ppk": "19",
"tree_id": "2"
},
{
"parent_id": "19",
"ppk": "20",
"tree_id": "2"
},
{
"parent_id": "18",
"ppk": "21",
"tree_id": "2"
},
{
"parent_id": "21",
"ppk": "22",
"tree_id": "2"
}
]

0 comments on commit 0c355cc

Please sign in to comment.