def parent_is_child(): parent = Node() child = Node() grandchild = Node() parent.add_child(child) child.add_child(grandchild) try: child.parent = grandchild except ValueError: pass except: assert False, "A child's parent cannot be its own child."
def some_nodes(): return [ Node(contents={'name': 'a'}), Node(contents={ 'parent': 'a', 'name': 'b' }), Node(contents={ 'parent': 'b', 'name': 'c' }), Node(contents={ 'parent': 'b', 'name': 'd' }), Node(contents={'name': 'A'}), Node(contents={ 'parent': 'A', 'name': 'B' }), Node(contents={ 'parent': 'B', 'name': 'C' }), Node(contents={ 'parent': 'B', 'name': 'D' }) ]
def parent_is_self(): parent = Node() child = parent try: parent.add_child(child) except ValueError: pass except: assert False, "A child cannot be its own parent."
def test_orphaned_from_parent(some_nodes): nodes = [Node(contents={'parent': 'missing', 'name': 'orphan'})] \ + some_nodes roots = from_parent(nodes, get_key='name', get_parent='parent') actual = [n.contents['name'] for n in roots] expected = ['a', 'A', 'orphan'] for name in actual: assert name in expected, \ f"{name} missing from expected root nodes." for name in expected: assert name in actual, \ f"{name} missing from root nodes read in."
def initialize(): # Tree structure # # .F. # .B. G. # A .D. H. # C E I # A, B, C, D, E, F, G, H, I = [Node(contents=c) for c in 'ABCDEFGHI'] # left F.add_child(B) B.add_child(A) B.add_child(D) D.add_child(C) D.add_child(E) # right F.add_child(G) G.add_child(H) H.add_child(I) return { 'nodes': (A, B, C, D, E, F, G, H, I), 'root': F }
def test_node_remove_child(): child = Node("child") parent = Node("parent") parent.add_child(child) parent.remove_child(child) assert parent._children == []
def test_node_add_child(): child = Node("child") parent = Node("parent") parent.add_child(child) assert parent.contents == "parent" assert parent._children[0].contents == "child"
def parent_good(): parent = Node() child = Node() parent.add_child(child) child.parent = parent assert child.parent == parent
def test_node_get_parent(): node = Node() assert node.parent is None
def test_node_creation(): node = Node(contents=3.1415) assert node.contents == 3.1415