Exemplo n.º 1
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"))
Exemplo n.º 2
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"})
Exemplo n.º 3
0
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
Exemplo n.º 4
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)
Exemplo n.º 5
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)
Exemplo n.º 6
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)
Exemplo n.º 7
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