Exemplo n.º 1
0
def print_tree(cur, start_node=None, indent='  ', _level=0):
    """
    Print tree to stdout.

    :param start_node: Starting point for tree output.
                       If ``None``, start at root node.
    :type start_node: int, Node, NodaData or None
    :param str indent: String to print per level (default: '  ')
    """
    if start_node is None:
        start_node = get_root_node(cur)

    print('{}{}'.format(indent*_level, start_node))  # noqa

    for child in list(get_children(cur, start_node)):
        print_tree(cur, child, indent=indent, _level=_level+1)
Exemplo n.º 2
0
def print_tree(cur, start_node=None, indent='  ', _level=0):
    """
    Print tree to stdout.

    :param start_node: Starting point for tree output.
                       If ``None``, start at root node.
    :type start_node: int, Node, NodaData or None
    :param str indent: String to print per level (default: '  ')
    """
    if start_node is None:
        start_node = get_root_node(cur)

    print('{}{}'.format(indent*_level, start_node))  # noqa

    for child in list(get_children(cur, start_node)):
        print_tree(cur, child, indent=indent, _level=_level+1)
Exemplo n.º 3
0
def test_get_children_correct_positioning(cur, root, nd1, nd2, nd3):
    ids = [child.id for child in get_children(cur, root)]
    expected = [nd1.id, nd2.id, nd3.id]
    assert ids == expected
Exemplo n.º 4
0
def test_get_children(cur, root, nd1, nd2, nd3):
    ids = {child.id for child in get_children(cur, root)}
    assert len(ids) == 3
    assert nd1.id in ids
    assert nd2.id in ids
    assert nd3.id in ids
Exemplo n.º 5
0
def test_insert_node_at_specific_position(cur, root):
    node0 = insert_node(cur, root, position=0, auto_position=True)
    positions = map(lambda n: n.position, get_children(cur, root))
    assert node0.position == 0
    assert list(positions) == [0, 1, 2, 4]
Exemplo n.º 6
0
def test_ensure_free_position(cur, root):
    ensure_free_position(cur, root, 4)
    positions = map(lambda n: n.position, get_children(cur, root))
    assert list(positions) == [0, 1, 3]
Exemplo n.º 7
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]
Exemplo n.º 8
0
def test_delete_node_shifts_positions(cur, root, nd1):
    delete_node(cur, nd1, auto_position=True)
    positions = map(lambda n: n.position, get_children(cur, root))
    assert list(positions) == [0, 1, 3]
Exemplo n.º 9
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
Exemplo n.º 10
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
Exemplo n.º 11
0
def test_insert_node_at_specific_position(cur, root):
    node0 = insert_node(cur, root, position=0, auto_position=True)
    positions = map(lambda n: n.position, get_children(cur, root))
    assert node0.position == 0
    assert list(positions) == [0, 1, 2, 4]
Exemplo n.º 12
0
def test_ensure_free_position(cur, root):
    ensure_free_position(cur, root, 4)
    positions = map(lambda n: n.position, get_children(cur, root))
    assert list(positions) == [0, 1, 3]
Exemplo n.º 13
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]
Exemplo n.º 14
0
def test_delete_node_shifts_positions(cur, root, nd1):
    delete_node(cur, nd1, auto_position=True)
    positions = map(lambda n: n.position, get_children(cur, root))
    assert list(positions) == [0, 1, 3]
Exemplo n.º 15
0
def test_get_children_correct_positioning(cur, root, nd1, nd2, nd3):
    ids = [child.id for child in get_children(cur, root)]
    expected = [nd1.id, nd2.id, nd3.id]
    assert ids == expected
Exemplo n.º 16
0
def test_get_children(cur, root, nd1, nd2, nd3):
    ids = {child.id for child in get_children(cur, root)}
    assert len(ids) == 3
    assert nd1.id in ids
    assert nd2.id in ids
    assert nd3.id in ids