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)
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"))
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)