def test_is_connected():
    test_net = Network()
    test_net.add_node(Node(1))

    assert ConnectivityChecker.is_connected(test_net)

    test_net.add_node(Node(2))
    assert not ConnectivityChecker.is_connected(test_net)

    test_net.add_edge(node_id1=1, node_id2=2, weight=5)
    assert ConnectivityChecker.is_connected(test_net)
def test_breadth_first_search():
    test_net = Network()
    test_net.add_node(Node(1))

    assert ConnectivityChecker.breadth_first_search(test_net)

    test_net.add_node(Node(2))
    assert not ConnectivityChecker.breadth_first_search(test_net)

    test_net.add_edge(node_id1=1, node_id2=2, weight=5)
    assert ConnectivityChecker.breadth_first_search(test_net)
Esempio n. 3
0
def test_add_edge():
    node1 = Node(1)
    node2 = Node(2)
    test_net = Network()

    # add nodes, but not edges
    test_net.add_node(node1)
    test_net.add_node(node2)
    assert len(test_net.network_dict[node1.node_id].get_adjacents()) is 0

    # add edge to existing nodes
    test_net.add_edge(node1.node_id, node2.node_id, 5)
    assert len(test_net.network_dict[node1.node_id].get_adjacents()) is 1
    assert test_net.network_dict[node1.node_id].adjacency_dict[
        node2.node_id]['weight'] is 5

    # attempt to add edge that already exists
    test_net.add_edge(node1.node_id, node2.node_id, 5)
    assert len(test_net.network_dict[node1.node_id].get_adjacents()) is 1
    assert test_net.network_dict[node1.node_id].adjacency_dict[
        node2.node_id]['weight'] is 5

    # add inactive node
    node3 = Node(3, status=False)
    test_net.add_node(node3)

    # attempt to add edge to inactive node
    test_net.add_edge(node1.node_id, node3.node_id, 15)
    assert len(test_net.network_dict[node1.node_id].get_adjacents()) is 1
    assert test_net.network_dict[node1.node_id].adjacency_dict[
        node2.node_id]['weight'] is 5

    # attempts to add edge to node that doesn't exist
    test_net.add_edge(node1.node_id, node_id2=4, weight=25)
    assert len(test_net.network_dict[node1.node_id].get_adjacents()) is 1
    assert test_net.network_dict[node1.node_id].adjacency_dict[
        node2.node_id]['weight'] is 5
# demonstrates __str__() function works
print(network_1)

# demonstrates remove_edge function works
print('\t---REMOVE EDGE---')
network_1.remove_edge(1, 2)
print(network_1)

# demonstrates remove_node function works
print('\t---REMOVE NODE---')
network_1.remove_node(3)
print(network_1)

# demonstrates add edge function works
print('\t---ADD EDGE---')
network_1.add_edge(1, 2, 15)
print(network_1)

# demonstrates the add node function works
print('\t---ADD NODE---')
network_1.add_node(
    Node(
        11, 'K', {
            1: {
                'weight': 3,
                'status': True
            },
            2: {
                'weight': 4,
                'status': True
            },