def test_node_not_in_graph(self, target_node, test_input): """Should raise an error if the target_node is not found in the graph""" sm = StructureModel() sm.add_edges_from(test_input) with pytest.raises( NodeNotFound, match="Node {node} not found in the graph".format(node=target_node), ): sm.get_target_subgraph(target_node)
def test_instance(self): """The subgraph returned should still be a StructureModel instance""" sm = StructureModel() sm.add_edges_from([(0, 1), (1, 2), (1, 3), (4, 6)]) subgraph = sm.get_target_subgraph(2) assert isinstance(subgraph, StructureModel)
def test_get_subgraph_string(self, target_node, test_input, expected): """Should be able to return the subgraph with the specified node""" sm = StructureModel() sm.add_edges_from(test_input) subgraph = sm.get_target_subgraph(target_node) expected_graph = StructureModel() expected_graph.add_edges_from(expected) assert set(subgraph.nodes) == set(expected_graph.nodes) assert set(subgraph.edges) == set(expected_graph.edges)
def test_isolates(self): """Should return an isolated node""" nodes = [1, 3, 5, 2, 7] sm = StructureModel() sm.add_nodes_from(nodes) subgraph = sm.get_target_subgraph(1) expected_graph = StructureModel() expected_graph.add_node(1) assert set(subgraph.nodes) == set(expected_graph.nodes) assert set(subgraph.edges) == set(expected_graph.edges)
def test_get_target_subgraph_twice(self): """get_target_subgraph should be able to run more than once""" sm = StructureModel() sm.add_edges_from([(0, 1), (1, 2), (1, 3), (4, 6)]) subgraph = sm.get_target_subgraph(0) subgraph.remove_edge(0, 1) subgraph = subgraph.get_target_subgraph(1) expected_graph = StructureModel() expected_edges = [(1, 2), (1, 3)] expected_graph.add_edges_from(expected_edges) assert set(subgraph.nodes) == set(expected_graph.nodes) assert set(subgraph.edges) == set(expected_graph.edges)
def test_different_origins_and_weights(self): """The subgraph returned should still have the edge data preserved from the original graph""" sm = StructureModel() sm.add_weighted_edges_from([(1, 2, 2.0)], origin="unknown") sm.add_weighted_edges_from([(1, 3, 1.0)], origin="learned") sm.add_weighted_edges_from([(5, 6, 0.7)], origin="expert") subgraph = sm.get_target_subgraph(2) assert set(subgraph.edges.data("origin")) == { (1, 2, "unknown"), (1, 3, "learned"), } assert set(subgraph.edges.data("weight")) == {(1, 2, 2.0), (1, 3, 1.0)}
def test_isolates_nodes_and_edges(self): """Should be able to return the subgraph with the specified node""" edges = [(0, 1), (1, 2), (1, 3), (5, 6), (4, 5)] isolated_nodes = [7, 8, 9] sm = StructureModel() sm.add_edges_from(edges) sm.add_nodes_from(isolated_nodes) subgraph = sm.get_target_subgraph(5) expected_edges = [(5, 6), (4, 5)] expected_graph = StructureModel() expected_graph.add_edges_from(expected_edges) assert set(subgraph.nodes) == set(expected_graph.nodes) assert set(subgraph.edges) == set(expected_graph.edges)