Skip to content

Commit

Permalink
resource: start tests for branch moving
Browse files Browse the repository at this point in the history
  • Loading branch information
ergo committed Oct 28, 2016
1 parent d6c4f51 commit 7e5c409
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
39 changes: 35 additions & 4 deletions ziggurat_foundations/models/services/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,15 @@ def groups_for_perm(cls, instance, perm_name, group_ids=None,
@classmethod
def subtree_deeper(cls, object_id, limit_depth=1000000, flat=True,
db_session=None):
""" This returns you subree of ordered objects relative
"""
This returns you subree of ordered objects relative
to the start object id currently only postgresql
:param object_id:
:param limit_depth:
:param flat:
:param db_session:
:return:
"""
raw_q = """
WITH RECURSIVE subtree AS (
Expand All @@ -232,6 +239,13 @@ def subtree_deeper(cls, object_id, limit_depth=1000000, flat=True,

@classmethod
def build_subtree_strut(self, result):
"""
Returns a dictionary in form of
{node:Resource, children:{node_id: Resource}}
:param result:
:return:
"""
items = list(result)
struct_dict = OrderedDict()
if len(items) == 0:
Expand All @@ -251,8 +265,15 @@ def build_subtree_strut(self, result):
@classmethod
def path_upper(cls, object_id, limit_depth=1000000, flat=True,
db_session=None):
""" This returns you path to root node starting from object_id
"""
This returns you path to root node starting from object_id
currently only for postgresql
:param object_id:
:param limit_depth:
:param flat:
:param db_session:
:return:
"""
raw_q = """
WITH RECURSIVE subtree AS (
Expand All @@ -271,5 +292,15 @@ def path_upper(cls, object_id, limit_depth=1000000, flat=True,
return q

@classmethod
def move_before_position(cls, object_id, parent_id, position):
pass
def move_to_position(cls, resource_id, position, parent_id=None,
db_session=None):
"""
Moves node to new location in the tree
:param resource_id:
:param position:
:param parent_id: new parent id
:param db_session:
:return:
"""
return True
13 changes: 13 additions & 0 deletions ziggurat_foundations/tests/test_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,16 @@ def test_going_up(self, db_session):
root = create_default_tree(db_session=db_session)
result = ResourceService.path_upper(9, db_session=db_session)
assert [r.resource_id for r in result] == [9, 7, 1, -1]

@pytest.mark.skipif(not_postgres, reason="requires postgres")
def test_move_up_on_same_branch(self, db_session):
import pprint
root = create_default_tree(db_session=db_session)
ResourceService.move_to_position(3, position=2, db_session=db_session)
result = ResourceService.subtree_deeper(
root['node'].resource_id, limit_depth=2, db_session=db_session)
tree = ResourceService.build_subtree_strut(result)
pprint.pprint(tree)
assert tree['children'][3]['node'].ordering == 2
assert tree['children'][2]['node'].ordering == 4
assert 1 == 2

0 comments on commit 7e5c409

Please sign in to comment.