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)
def test_add_node(): node1 = Node(1) test_net = Network() # add node to empty graph assert len(test_net.nodes()) is 0 test_net.add_node(node1) assert len(test_net.nodes()) is 1 assert node1.node_id in test_net.nodes() # attempt to add node that already exists test_net.add_node(node1) assert len(test_net.nodes()) is 1 assert node1.node_id in test_net.nodes()
def test_mark_edge_inactive(): node1 = Node(1, adjacency_dict={2: {'weight': 3, 'status': True}}) node2 = Node(2, adjacency_dict={1: {'weight': 3, 'status': True}}) test_net = Network({1: node1, 2: node2}) # test existing nodes with shared, active edge test_net.mark_edge_inactive(node1.node_id, node2.node_id) assert not node1.adjacency_dict[node2.node_id]['status'] assert not node2.adjacency_dict[node1.node_id]['status'] assert not node1.get_adjacents() assert not node2.get_adjacents() assert len(test_net.network_dict) is 2 assert len(test_net.network_dict) is len(test_net.nodes()) # test existing nodes with shared, inactive edge test_net.mark_edge_inactive(node1.node_id, node2.node_id) assert not node1.adjacency_dict[node2.node_id]['status'] assert not node2.adjacency_dict[node1.node_id]['status'] assert not node1.get_adjacents() assert not node2.get_adjacents() assert len(test_net.network_dict) is 2 assert len(test_net.network_dict) is len(test_net.nodes()) # test existing nodes with shared, inactive edge test_net.add_node(Node(3)) test_net.mark_edge_inactive(node1.node_id, 3) assert not node1.adjacency_dict[node2.node_id]['status'] assert not node2.adjacency_dict[node1.node_id]['status'] assert not node1.get_adjacents() assert not node2.get_adjacents() assert len(test_net.network_dict) is 3 assert len(test_net.network_dict) is len(test_net.nodes()) # test with nonexistent node test_net.add_node(Node(3)) test_net.mark_edge_inactive(node1.node_id, 4) assert not node1.adjacency_dict[node2.node_id]['status'] assert not node2.adjacency_dict[node1.node_id]['status'] assert not node1.get_adjacents() assert not node2.get_adjacents() assert len(test_net.network_dict) is 3 assert len(test_net.network_dict) is len(test_net.nodes())
def import_nodes(worksheet): """ Imports node from each row and adds it to the network. These nodes only contain the following fields: node_id, hospital_name, region, city, and state. (adjacency_dict is handled in generate_distance_vector() and status is assumed to be True) :param Worksheet worksheet: worksheet to read data from :return: Network """ network = Network() for x in range(2, worksheet.max_row + 1): network.add_node( node=Node(node_id=int(worksheet[f'{column_indices["unique id"]}{x}'].value), hospital_name=worksheet[f'{column_indices["hospital name"]}{x}'].value, region=int(worksheet[f'{column_indices["region"]}{x}'].value), city=worksheet[f'{column_indices["city"]}{x}'].value, state=worksheet[f'{column_indices["state"]}{x}'].value), feedback=False) generate_distance_vector(network=network) return network
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
from network_simulator.Network import Node, Network from network_simulator.OrganGenerator import OrganGenerator from network_simulator.OrganList import OrganList test_net = Network() test_net.add_node(Node(1)) n = 3 def test_generate_organs(): organs = OrganGenerator.generate_organs(graph=test_net, n=n) assert len(organs) <= n * 6 for organ in organs: assert organ.current_location in test_net.nodes() assert 0 <= organ.organ_type.value <= 5 assert 0 <= organ.blood_type.blood_type_letter.value <= 3 assert 0 <= organ.blood_type.blood_type_polarity.value <= 1 def test_generate_organs_to_list(): organ_list = OrganList() OrganGenerator.generate_organs_to_list(graph=test_net, n=n, organ_list=organ_list) # assert len(organ_list.organ_list) <= n * 6 for organ in organ_list.organ_list: assert organ.current_location in test_net.nodes() assert 0 <= organ.organ_type.value <= 5 assert 0 <= organ.blood_type.blood_type_letter.value <= 3 assert 0 <= organ.blood_type.blood_type_polarity.value <= 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 }, 10: { 'weight': 8, 'status': True } })) print(network_1) # demonstrates is_connected() function works if ConnectivityChecker.is_connected(network_1): print("The network is connected!\n") else: print("The network is not connected!\n")
def test_mark_edge_active(): node1 = Node(1, adjacency_dict={2: {'weight': 3, 'status': False}}) node2 = Node(2, adjacency_dict={1: {'weight': 3, 'status': False}}) test_net = Network({1: node1, 2: node2}) # test existing, active nodes with shared, inactive edge test_net.mark_edge_active(node1.node_id, node2.node_id) assert node1.adjacency_dict[node2.node_id]['status'] assert node2.adjacency_dict[node1.node_id]['status'] assert len(node1.adjacency_dict) is 1 assert len(node2.adjacency_dict) is 1 assert len(test_net.network_dict) is 2 assert len(test_net.network_dict) is len(test_net.nodes()) # test existing, active nodes with shared, active edge test_net.mark_edge_active(node1.node_id, node2.node_id) assert node1.adjacency_dict[node2.node_id]['status'] assert node2.adjacency_dict[node1.node_id]['status'] assert len(node1.adjacency_dict) is 1 assert len(node2.adjacency_dict) is 1 assert len(test_net.network_dict) is 2 assert len(test_net.network_dict) is len(test_net.nodes()) # test existing, inactive node with shared, inactive edge test_net.mark_node_inactive(node1.node_id) test_net.mark_edge_active(node1.node_id, node2.node_id) assert not node1.adjacency_dict[node2.node_id]['status'] assert not node2.adjacency_dict[node1.node_id]['status'] assert len(node1.adjacency_dict) is 1 assert len(node2.adjacency_dict) is 1 assert len(test_net.network_dict) is 2 assert len(test_net.network_dict) is not len(test_net.nodes()) assert not node1.status assert node2.status # test existing nodes without shared edge test_net.add_node(Node(3)) test_net.mark_edge_active(node2.node_id, 3) test_net.mark_node_inactive(node1.node_id) test_net.mark_edge_active(node1.node_id, node2.node_id) assert not node1.adjacency_dict[node2.node_id]['status'] assert not node2.adjacency_dict[node1.node_id]['status'] assert len(node1.adjacency_dict) is 1 assert len(node2.adjacency_dict) is 1 assert len(test_net.network_dict) is 3 assert len(test_net.network_dict) is not len(test_net.nodes()) assert not node1.status assert node2.status # test node that doesn't exist test_net.mark_edge_active(node2.node_id, 4) test_net.add_node(Node(3)) test_net.mark_edge_active(node2.node_id, 3) test_net.mark_node_inactive(node1.node_id) test_net.mark_edge_active(node1.node_id, node2.node_id) assert not node1.adjacency_dict[node2.node_id]['status'] assert not node2.adjacency_dict[node1.node_id]['status'] assert len(node1.adjacency_dict) is 1 assert len(node2.adjacency_dict) is 1 assert len(test_net.network_dict) is 3 assert len(test_net.network_dict) is not len(test_net.nodes()) assert not node1.status assert node2.status
def build_network(nodes, data): network = Network() for node in nodes: network.add_node(node=Node(node), feedback=False) return add_edges(network=network, data=data)