Example #1
0
def test_node_set_attributes():
    root = Node(1)
    assert root.left is None
    assert root.right is None
    assert root.val == 1
    assert root.value == 1
    assert repr(root) == 'Node(1)'

    root.value = 2
    assert root.value == 2
    assert root.val == 2
    assert repr(root) == 'Node(2)'

    root.val = 1
    assert root.value == 1
    assert root.val == 1
    assert repr(root) == 'Node(1)'

    left_child = Node(2)
    root.left = left_child
    assert root.left is left_child
    assert root.right is None
    assert root.val == 1
    assert root.left.left is None
    assert root.left.right is None
    assert root.left.val == 2
    assert repr(left_child) == 'Node(2)'

    right_child = Node(3)
    root.right = right_child
    assert root.left is left_child
    assert root.right is right_child
    assert root.val == 1
    assert root.right.left is None
    assert root.right.right is None
    assert root.right.val == 3
    assert repr(right_child) == 'Node(3)'

    last_node = Node(4)
    left_child.right = last_node
    assert root.left.right is last_node
    assert repr(root.left.right) == 'Node(4)'

    with pytest.raises(NodeValueError) as err:
        # noinspection PyTypeChecker
        Node('this_is_not_an_integer')
    assert str(err.value) == 'node value must be a number'

    with pytest.raises(NodeTypeError) as err:
        # noinspection PyTypeChecker
        Node(1, 'this_is_not_a_node')
    assert str(err.value) == 'left child must be a Node instance'

    with pytest.raises(NodeTypeError) as err:
        # noinspection PyTypeChecker
        Node(1, Node(1), 'this_is_not_a_node')
    assert str(err.value) == 'right child must be a Node instance'

    with pytest.raises(NodeValueError) as err:
        root.val = 'this_is_not_an_integer'
    assert root.val == 1
    assert str(err.value) == 'node value must be a number'

    with pytest.raises(NodeValueError) as err:
        root.value = 'this_is_not_an_integer'
    assert root.value == 1
    assert str(err.value) == 'node value must be a number'

    with pytest.raises(NodeTypeError) as err:
        root.left = 'this_is_not_a_node'
    assert root.left is left_child
    assert str(err.value) == 'left child must be a Node instance'

    with pytest.raises(NodeTypeError) as err:
        root.right = 'this_is_not_a_node'
    assert root.right is right_child
    assert str(err.value) == 'right child must be a Node instance'
Example #2
0
def test_node_set_attributes():
    root = Node(1)
    assert root.left is None
    assert root.right is None
    assert root.val == 1
    assert root.value == 1
    assert repr(root) == "Node(1)"

    root.value = 2
    assert root.value == 2
    assert root.val == 2
    assert repr(root) == "Node(2)"

    root.val = 1
    assert root.value == 1
    assert root.val == 1
    assert repr(root) == "Node(1)"

    left_child = Node(2)
    root.left = left_child
    assert root.left is left_child
    assert root.right is None
    assert root.val == 1
    assert root.left.left is None
    assert root.left.right is None
    assert root.left.val == 2
    assert repr(left_child) == "Node(2)"

    right_child = Node(3)
    root.right = right_child
    assert root.left is left_child
    assert root.right is right_child
    assert root.val == 1
    assert root.right.left is None
    assert root.right.right is None
    assert root.right.val == 3
    assert repr(right_child) == "Node(3)"

    last_node = Node(4)
    left_child.right = last_node
    assert root.left.right is last_node
    assert repr(root.left.right) == "Node(4)"

    with pytest.raises(NodeValueError) as err:
        # noinspection PyTypeChecker
        Node("this_is_not_an_integer")
    assert str(err.value) == "node value must be a float or int"

    with pytest.raises(NodeTypeError) as err:
        # noinspection PyTypeChecker
        Node(1, "this_is_not_a_node")
    assert str(err.value) == "left child must be a Node instance"

    with pytest.raises(NodeTypeError) as err:
        # noinspection PyTypeChecker
        Node(1, Node(1), "this_is_not_a_node")
    assert str(err.value) == "right child must be a Node instance"

    with pytest.raises(NodeValueError) as err:
        root.val = "this_is_not_an_integer"
    assert root.val == 1
    assert str(err.value) == "node value must be a float or int"

    with pytest.raises(NodeValueError) as err:
        root.value = "this_is_not_an_integer"
    assert root.value == 1
    assert str(err.value) == "node value must be a float or int"

    with pytest.raises(NodeTypeError) as err:
        root.left = "this_is_not_a_node"
    assert root.left is left_child
    assert str(err.value) == "left child must be a Node instance"

    with pytest.raises(NodeTypeError) as err:
        root.right = "this_is_not_a_node"
    assert root.right is right_child
    assert str(err.value) == "right child must be a Node instance"