Esempio n. 1
0
def test_change_parent(cur, root, nd1, nd2, nd2_1, nd2_1_1,
                       nd2_leaf):
    """
    Tree layout before move:
    /
      - nd1
      - nd2
        - nd2-1
          - nd2-1-1
            - nd2-leaf
      - nd3

    Expected tree layout after move:

    /
      - nd1
        - nd2-1
          - nd2-1-1
            - nd2-leaf
      - nd2
      - nd3
    """
    # We expect nd2-1 to be child of nd2 and nd2-1-1 to be child
    # of nd2-1.

    # Move nd2-1 from nd2 to nd1
    _temp_node = change_parent(cur, nd2_1.id, nd1, auto_position=False)

    # Return value should have new parent set
    assert _temp_node.parent == nd1.id

    # nd2-1 should have nd1 as parent
    node = get_node(cur, nd2_1.id)
    assert node.parent == nd1.id

    # nd2-1-1 should still have the same parent (nd2-1)
    child_node = get_node(cur, nd2_1_1.id)
    assert child_node.parent == nd2_1.id

    # nd2-leaf should still have the same parent (nd2-1-1)
    child_node = get_node(cur, nd2_leaf.id)
    assert child_node.parent == nd2_1_1.id

    # The ancestor set of nd2-1 should now contain nd1 and root
    assert set(get_ancestor_ids(cur, nd2_1)) == {root.id, nd1.id}

    # The ancestor set of nd2-1-1 should now contain nd2-1, nd1 and root
    expected = {root.id, nd1.id, nd2_1.id}
    assert set(get_ancestor_ids(cur, nd2_1_1)) == expected

    # The ancestor set of nd2-leaf should now contain node-2-1-1, nd2-1,
    # nd1 and root
    expected = {root.id, nd1.id, nd2_1.id, nd2_1_1.id}
    assert set(get_ancestor_ids(cur, nd2_leaf)) == expected

    # The ancestor set of nd2 should now only contain root
    assert set(get_ancestor_ids(cur, nd2)) == {root.id}

    # Check if nd2-1, nd2-1-1 and nd2-leaf are part of nd1's descendant
    # set now
    expected = {nd2_1.id, nd2_1_1.id, nd2_leaf.id}
    assert set(get_descendant_ids(cur, nd1)) == expected

    # nd2's descendant set should be empty now
    assert set(get_descendant_ids(cur, nd2)) == set()

    # Last but not least, the children function proof what we checked above too
    assert len(set(get_children(cur, nd1))) == 1
    assert len(set(get_children(cur, nd2))) == 0
Esempio n. 2
0
def test_change_parent_dont_move_into_own_subtree(cur, nd1, nd2_1):
    with pytest.raises(ValueError):
        change_parent(cur, nd1, nd2_1)
Esempio n. 3
0
def test_change_parent_dont_move_into_own_subtree(cur, nd1, nd2_1):
    with pytest.raises(exceptions.CantMoveIntoOwnSubtree):
        change_parent(cur, nd1, nd2_1)
Esempio n. 4
0
def test_change_parent_to_specific_position(cur, root, nd2_1):
    change_parent(cur, nd2_1, root, position=0, auto_position=True)
    positions = map(lambda n: n.position, get_children(cur, root))
    assert list(positions) == [0, 1, 2, 4]
Esempio n. 5
0
def test_change_parent_starts_couting_at_zero(cur, root, nd2, nd2_1):
    change_parent(cur, nd2_1, nd2, position=None, auto_position=True)
    nd2_1 = get_node(cur, nd2_1.id)
    assert nd2_1.position == 0
Esempio n. 6
0
def test_change_parent_to_highest_position(cur, root, nd2, nd2_1):
    highest_position = find_highest_position(cur, root)
    change_parent(cur, nd2_1, root, position=None, auto_position=True)
    nd2_1 = get_node(cur, nd2_1.id)
    assert nd2_1.position == highest_position + 1