def test_has_vertex(self) -> None: g = Graph() g.add_edge(1, 2) assert g.has_vertex(1), "vertex specified as int should be in graph" assert g.has_vertex("1"), "vertex specified as str should be in graph" v1 = g[1] assert g.has_vertex( v1), "vertex specified as object should be in graph" assert not g.has_vertex(3), "vertex 3 should not be in the graph"
def test_add_vertices_from(self) -> None: g = Graph() g.add_vertices_from([1, "2", ("v3", {"color": "blue", "mass": 42})]) assert g.has_vertex(1), "graph should have vertex 1" assert g.has_vertex(2), "graph should have vertex 2" assert g.has_vertex("v3"), "graph should have vertex v3" assert g["v3"][ "color"] == "blue", "vertex should have 'color' attribute set to 'blue'" assert g["v3"][ "mass"] == 42, "vertex should have 'mass' attribute set to 42"
def test_add_vertex(self) -> None: g = Graph() g.add_vertex(1) g.add_vertex("2") g.add_vertex("v3", color="blue", mass=42) assert g.has_vertex(1), "graph should have vertex 1" assert g.has_vertex(2), "graph should have vertex 2" assert g.has_vertex("v3"), "graph should have vertex v3" assert g["v3"][ "color"] == "blue", "vertex should have 'color' attribute set to 'blue'" assert g["v3"][ "mass"] == 42, "vertex should have 'mass' attribute set to 42"
def test_remove_vertex(self) -> None: g = Graph([(1, 2), (3, 3)]) g.add_vertex(4) assert g.has_vertex(4), "graph should have vertex 4" g.remove_vertex(4) assert not g.has_vertex( 4), "graph should not have vertex 4 after removal" assert g.has_vertex(3), "graph should have semi-isolated vertex 3" g.remove_vertex(3) assert not g.has_vertex( 3), "graph should not have vertex 3 after removal" with pytest.raises(exception.VertizeeException): g.remove_vertex(1)
def test_remove_edge(self) -> None: g = Graph([(1, 2), (2, 3), (3, 4)]) assert g.edge_count == 3, "graph should have 3 edges" g.remove_edge(1, 2) assert g.edge_count == 2, "after edge removal, graph should have 2 edges" assert g.has_vertex( 1), "isolated vertex 1 should not have been removed" g.remove_edge(2, 3, remove_semi_isolated_vertices=True) assert g.edge_count == 1, "after edge removal, graph should have 1 edge" assert not g.has_vertex( 2 ), "with `remove_semi_isolated_vertices` set to True, vertex 2 should have been removed" with pytest.raises(exception.EdgeNotFound): g.remove_edge(8, 9)