Ejemplo n.º 1
0
def test_get_children():
    node1 = Node('node1')
    node2 = Node('node2')
    node22 = Node('node22')
    node3 = Node('node3')

    node1._add_child(node2)
    node1._add_child(node22)
    node2._add_child(node3)

    assert node1._get_children() == (node2, node22)
    assert node2._get_children() == (node3, )

    clean()
Ejemplo n.º 2
0
def test_memory_leaks_getters():

    # Now test the getters

    root = Node("root")

    node1 = Node("node1")
    node2 = Node("node2")
    node3 = Node("node3")

    refc_before_link_root = node_ctype._get_reference_counts(root)
    refc_before_link1 = node_ctype._get_reference_counts(node1)
    refc_before_link2 = node_ctype._get_reference_counts(node2)
    refc_before_link3 = node_ctype._get_reference_counts(node3)

    None_counts_before = node_ctype._get_reference_counts(None)

    # Add 3 nodes to root
    root._add_children([node1, node2,
                        node3])  # node1 +1, node2 +1, node3 + 1, root +3

    node_again = root._get_child("node1")  # node1 +1

    assert node_ctype._get_reference_counts(node1) == refc_before_link1 + 2

    del node_again  # node1 -1

    assert node_ctype._get_reference_counts(node1) == refc_before_link1 + 1

    children = root._get_children()  #node1 +1, node2 +1, node3 +1

    assert len(children) == 3

    assert node_ctype._get_reference_counts(node1) == refc_before_link1 + 2
    assert node_ctype._get_reference_counts(node2) == refc_before_link2 + 2
    assert node_ctype._get_reference_counts(node3) == refc_before_link3 + 2
    assert node_ctype._get_reference_counts(root) == refc_before_link_root + 3

    # test get_parent

    root_again = node1._get_parent()  # root +1

    assert node_ctype._get_reference_counts(root) == refc_before_link_root + 4

    del root_again  # root -1

    assert node_ctype._get_reference_counts(root) == refc_before_link_root + 3

    # test _get_path

    path = node2._get_path()  # this shouldn't change any ref count

    assert node_ctype._get_reference_counts(node1) == refc_before_link1 + 2
    assert node_ctype._get_reference_counts(node2) == refc_before_link2 + 2
    assert node_ctype._get_reference_counts(node3) == refc_before_link3 + 2
    assert node_ctype._get_reference_counts(root) == refc_before_link_root + 3

    # test _get_child_from_path

    node4 = Node("node4")
    refc_before_link4 = node_ctype._get_reference_counts(node4)

    node3._add_child(node4)  # node3 +1, node4 + 1

    node4_again = root._get_child_from_path("node3.node4")  # node4 +1

    assert node_ctype._get_reference_counts(node4) == refc_before_link4 + 2
    assert node_ctype._get_reference_counts(node3) == refc_before_link3 + 3

    del node4_again  # node4 -1

    assert node_ctype._get_reference_counts(node4) == refc_before_link4 + 1