def get_sample_tree_2():
    """
    broot []
    ├── b1 {}
    │   └── b1a {}
    └── b2 {}
    """
    t = Tree()
    t.insert_node(Node("broot", keyed=False))
    t.insert_node(Node("b1"), parent_id="broot")
    t.insert_node(Node("b1a"), parent_id="b1", key="a")
    t.insert_node(Node("b2"), parent_id="broot")
    return t
Beispiel #2
0
def test_prefix_repr():
    assert (Tree._line_prefix_repr(
        line_type="ascii-ex",
        is_last_list=(),
    ) == "")
    assert Tree._line_prefix_repr(line_type="ascii-ex",
                                  is_last_list=(True, )) == "└── "
    assert Tree._line_prefix_repr(line_type="ascii-ex",
                                  is_last_list=(False, )) == "├── "

    assert (Tree._line_prefix_repr(line_type="ascii-ex",
                                   is_last_list=(True, False,
                                                 True)) == "    │   └── ")
    assert (Tree._line_prefix_repr(line_type="ascii-ex",
                                   is_last_list=(False, False,
                                                 False)) == "│   │   ├── ")
Beispiel #3
0
def test_validate_node_insertion():
    t = Tree()
    t.insert_node(Node("a"))

    # cannot insert node of wrong class
    class MyNotValidClass(object):
        pass

    with pytest.raises(AttributeError):
        t._validate_node_insertion(MyNotValidClass())
    with pytest.raises(AttributeError):
        t.insert_node({})

    # cannot add node with similar id
    with pytest.raises(DuplicatedNodeError):
        t._validate_node_insertion(Node("a"))
Beispiel #4
0
def test_merge():
    t = get_sample_tree()

    # merge under list
    t_to_merge = get_sample_tree_2()
    t.merge(new_tree=t_to_merge, nid="c")
    tree_sanity_check(t)
    tree_sanity_check(t_to_merge)
    assert (t.show() == """{}
├── a: {}
│   ├── a: []
│   │   ├── AA0
│   │   └── AA1
│   └── b: {}
└── c: []
    ├── {}
    │   └── a: {}
    ├── {}
    ├── C0
    └── C1
""")
    # new tree root is not conserved
    assert "broot" not in t
    assert all(nid in t for nid in ("b1", "b1a", "b2"))
    old_key, old_node = t_to_merge.get("b1")
    new_key, new_node = t.get("b1")
    assert old_key == new_key
    assert old_node is new_node

    # cannot remerge tree, because then there would be node duplicates
    with pytest.raises(DuplicatedNodeError):
        t.merge(new_tree=t_to_merge, nid="aa0")
    tree_sanity_check(t)
    tree_sanity_check(t_to_merge)

    # merge on initial empty tree
    t = Tree()
    t.merge(get_sample_tree_2())
    tree_sanity_check(t)
    assert (t.show() == """[]
├── {}
│   └── a: {}
└── {}
""")
    # in this case new_tree root is conserved since initial tree is empty
    assert all(nid in t for nid in ("broot", "b1", "b1a", "b2"))
Beispiel #5
0
def test_line_repr():
    tts = [
        (
            "no key",
            "└──",
            False,
            "start message",
            "end message",
            40,
            "└──start message             end message",
        ),
        (
            "with key",
            "└── a",
            True,
            "start message",
            "end message",
            40,
            "└── a: start message         end message",
        ),
        (
            "no key / too long",
            "└──",
            False,
            "start message",
            "end message",
            15,
            "└──start mes...",
        ),
        (
            "with key / too long",
            "└── a",
            True,
            "start message",
            "end message",
            15,
            "└── a: start...",
        ),
    ]
    for (
            desc,
            prefix,
            is_key_displayed,
            node_start,
            node_end,
            line_max_length,
            expected,
    ) in tts:
        line_repr = Tree._line_repr(prefix, is_key_displayed, ": ", node_start,
                                    node_end, line_max_length)
        assert expected == line_repr
        assert len(line_repr) == line_max_length
Beispiel #6
0
def test_insert_root():
    t = Tree()
    root_node = Node(identifier="a")
    t.insert_node(root_node)
    assert to_key_id(t.list()) == [(None, "a")]
    t._nodes_map["a"] is root_node
    assert t._nodes_parent["a"] is None
    assert t._nodes_children_list["a"] == []
    assert t._nodes_children_map["a"] == {}
    tree_sanity_check(t)

    # cannot add second root
    with pytest.raises(MultipleRootError):
        t.insert_node(Node(identifier="b"))
    assert to_key_id(t.list()) == [(None, "a")]
    tree_sanity_check(t)

    # wrong node insertion
    with pytest.raises(AttributeError):
        Tree().insert_node({"key": "a"})
Beispiel #7
0
def test_insert_node_below():
    t = Tree()
    t.insert_node(Node("root_id"))

    # cannot insert under not existing parent
    with pytest.raises(NotFoundNodeError):
        t.insert_node(Node(identifier="a"), parent_id="what")
    tree_sanity_check(t)

    # insert node below another one
    node_a = Node("a_id")
    t.insert_node(node_a, parent_id="root_id", key="a")
    assert set(t._nodes_map.keys()) == {"root_id", "a_id"}
    assert t._nodes_map["a_id"] == node_a
    assert t._nodes_parent["root_id"] is None
    assert t._nodes_parent["a_id"] == "root_id"
    assert t._nodes_children_list["a_id"] == []
    assert t._nodes_children_map["root_id"] == {"a_id": "a"}
    tree_sanity_check(t)

    # try to insert node with same id than existing node
    with pytest.raises(DuplicatedNodeError):
        t.insert_node(Node("a_id"), parent_id="root_id", key="b")
    tree_sanity_check(t)
Beispiel #8
0
def test_insert_node_above():
    # above root
    t = Tree()
    t.insert_node(Node("initial_root"))
    t.insert_node(node=Node("new_root"),
                  child_id="initial_root",
                  key="between")
    assert t.root == "new_root"
    assert to_key_id(t.children("new_root")) == [("between", "initial_root")]
    tree_sanity_check(t)
    assert (t.show() == """{}
└── between: {}
""")
    # above node
    t = get_sample_tree()
    t.insert_node(Node("new"), child_id="aa0", key="to")
    assert "new" in t
    assert (t.show() == """{}
├── a: {}
│   ├── a: []
│   │   ├── {}
│   │   │   └── to: AA0
│   │   └── AA1
│   └── b: {}
└── c: []
    ├── C0
    └── C1
""")
    tree_sanity_check(t)
Beispiel #9
0
def test_insert_tree_at_root():
    t = Tree()
    t.insert_tree(get_sample_tree())
    tree_sanity_check(t)
    assert (t.show() == """{}
├── a: {}
│   ├── a: []
│   │   ├── AA0
│   │   └── AA1
│   └── b: {}
└── c: []
    ├── C0
    └── C1
""")

    # cannot insert at root if already present root
    t = Tree()
    t.insert_node(Node("present_root"))
    with pytest.raises(MultipleRootError):
        t.insert_tree(get_sample_tree())
    tree_sanity_check(t)
Beispiel #10
0
def test_is_empty():
    assert Tree().is_empty()
    t = get_sample_tree()
    assert not t.is_empty()
Beispiel #11
0
def get_sample_tree():
    """
    root {}
    ├── a {}
    │   ├── aa []
    │   │   ├── aa0
    │   │   └── aa1
    │   └── ab {}
    └── c []
        ├── c0
        └── c1
    """
    t = Tree()
    t.insert_node(Node(identifier="root"))
    t.insert_node(Node(identifier="a"), parent_id="root", key="a")
    t.insert_node(Node(identifier="aa", keyed=False), parent_id="a", key="a")
    t.insert_node(Node(identifier="aa0", repr_="AA0"), parent_id="aa")
    t.insert_node(Node(identifier="aa1", repr_="AA1"), parent_id="aa")
    t.insert_node(Node(identifier="ab"), parent_id="a", key="b")
    t.insert_node(Node(identifier="c", keyed=False), parent_id="root", key="c")
    t.insert_node(Node(identifier="c0", repr_="C0"), parent_id="c")
    t.insert_node(Node(identifier="c1", repr_="C1"), parent_id="c")

    tree_sanity_check(t)
    return t