def test_issubclass_and_isinstance(self) -> None: g = DiGraph() v1 = g.add_vertex(1) assert isinstance( v1, vertex_module.VertexBase ), "v1 should be an instance of superclass VertexBase" assert isinstance(v1, DiVertex), "v1 should be a DiVertex instance" assert issubclass( DiVertex, vertex_module.VertexBase), "DiVertex should be VertexBase subclass"
def test_vertex1_vertex2(self) -> None: v1 = pp_module.VertexData("1") v2 = pp_module.VertexData("2") edge_data1 = pp_module.EdgeData(v2, v1) assert edge_data1.vertex1 == v2, "vertex1 should be v2" assert edge_data1.vertex2 == v1, "vertex2 should be v1" g = DiGraph() g.add_edge(2, 1) edge_data2 = pp_module.EdgeData.from_edge_obj(g.get_edge(2, 1)) assert edge_data2.vertex1.label == "2", "vertex1 should be 2" assert edge_data2.vertex2.label == "1", "vertex2 should be 1"
def test_repr_str_and_label(self) -> None: dg = DiGraph([(2, 1)]) assert dg.get_edge(2, 1).label == "(2, 1)", "edge label should be (2, 1)" assert (dg.get_edge(2, 1).__str__() == dg.get_edge( 2, 1).__repr__()), "edge __repr__ should equal __str__" assert dg.get_edge( 2, 1).__str__() == "(2, 1)", "edge __str__ should be (2, 1)" dg.add_edge(3, 4, weight=1.5) assert dg.get_edge(2, 1).label == "(2, 1)", "edge label should be (2, 1)" assert ( dg.get_edge(2, 1).__str__() == "(2, 1, 1.0)" ), "edge __str__ should be (2, 1, 1.0) after adding weighted edge to graph"
def test_issubclass_and_isinstance(self) -> None: g = DiGraph() edge: DiEdge = g.add_edge(1, 2) assert isinstance( edge, edge_module.EdgeBase ), "edge should be an instance of superclass EdgeBase" assert isinstance( edge, edge_module.MutableEdgeBase ), "edge should be an instance of superclass MutableEdgeBase" assert isinstance(edge, DiEdge), "edge should be an DiEdge instance" assert issubclass( DiEdge, edge_module.EdgeBase), "DiEdge should be EdgeBase subclass" assert issubclass(DiEdge, edge_module.MutableEdgeBase ), "DiEdge should be MutableEdgeBase subclass"
def test_get_label(self) -> None: v1 = pp_module.VertexData("1") v2 = pp_module.VertexData("2") edge_data1 = pp_module.EdgeData(v2, v1) assert edge_data1.get_label( is_directed=False) == "(1, 2)", "label should be (1, 2)" assert edge_data1.get_label( is_directed=True) == "(2, 1)", "label should be (2, 1)" g = DiGraph() g.add_edge(2, 1) edge_data2 = pp_module.EdgeData.from_edge_obj(g.get_edge(2, 1)) assert edge_data2.get_label( is_directed=False) == "(1, 2)", "label should be (1, 2)" assert edge_data2.get_label( is_directed=True) == "(2, 1)", "label should be (2, 1)"
def test_incident_edges(self) -> None: g = DiGraph() g.add_edge(1, 0) g.add_edge(1, 2) g.add_edge(3, 1) assert g[1].incident_edges() == { g.get_edge(1, 0), g.get_edge(1, 2), g.get_edge(3, 1), }, "incident edges should be (1, 0), (1, 2), and (3, 1)" assert g[1].incident_edges_incoming() == {g.get_edge( 3, 1)}, "incoming edge should be (3, 1)" assert g[1].incident_edges_outgoing() == { g.get_edge(1, 0), g.get_edge(1, 2), }, "outgoing edges should include (1, 0) and (1, 2)"
def test_loop_edge(self) -> None: g = DiGraph() g.add_vertex(0) assert not g[0].loop_edge, "vertex 0 should not have a loop edge" g.add_edge(1, 1) assert g[1].incident_edges() == {g.get_edge( 1, 1)}, "vertex 1 should have self loop as incident edge" assert g[1].loop_edge, "vertex 1 should have a self loop"
def test_create_edge_label(self) -> None: g = Graph() v1 = g.add_vertex(1) v2 = g.add_vertex(2) assert (edge_module.create_edge_label( v1, v1, is_directed=g.is_directed()) == "(1, 1)" ), "loop edge label should be (1, 1)" assert (edge_module.create_edge_label( v1, v2, is_directed=g.is_directed()) == "(1, 2)" ), "edge label should be (1, 2)" assert (edge_module.create_edge_label( v2, v1, is_directed=g.is_directed()) == "(1, 2)" ), "edge label should be (1, 2)" assert (edge_module.create_edge_label( 5, 9, is_directed=g.is_directed()) == "(5, 9)" ), "edge label should be (5, 9)" assert (edge_module.create_edge_label( "t", "s", is_directed=g.is_directed()) == "(s, t)" ), "edge label should be (s, t)" g2 = DiGraph() v3 = g2.add_vertex(3) v4 = g2.add_vertex(4) assert (edge_module.create_edge_label( v3, v4, is_directed=g2.is_directed()) == "(3, 4)" ), "edge label should be (3, 4)" assert (edge_module.create_edge_label( v4, v3, is_directed=g2.is_directed()) == "(4, 3)" ), "edge label should be (4, 3)" assert (edge_module.create_edge_label( "t", "s", is_directed=g2.is_directed()) == "(t, s)" ), "edge label should be (t, s)"
def test_is_vertex_type(self) -> None: g = Graph() v: Vertex = g.add_vertex(1) assert vertex_module.is_vertex_type( v), "Vertex object should be a VertexType" g2 = DiGraph() di_v: DiVertex = g2.add_vertex(1) assert vertex_module.is_vertex_type( di_v), "DiVertex object should be a VertexType" g3 = MultiGraph() multi_v: MultiVertex = g3.add_vertex(1) assert vertex_module.is_vertex_type( multi_v), "MultiVertex object should be a VertexType" g4 = MultiDiGraph() multi_di_v: MultiDiVertex = g4.add_vertex(1) assert vertex_module.is_vertex_type( multi_di_v), "MultiDiVertex should be a VertexType" assert vertex_module.is_vertex_type( 10), "int vertex label should be a VertexType" assert vertex_module.is_vertex_type( "s"), "str vertex label should be a VertexType" assert vertex_module.is_vertex_type(("s", { "color": "blue" })), "vertex tuple should be a VertexType" assert not vertex_module.is_vertex_type( 10.99), "float should not be a VertexType" assert not vertex_module.is_vertex_type( ("s", "t")), "edge tuple should not be a VertexType" assert not vertex_module.is_vertex_type( ("s", "t", 4.5)), "edge tuple with edge weight should not be a VertexType" g.add_edge("s", "t") assert not vertex_module.is_vertex_type(g.get_edge( "s", "t")), "edge object should not be a VertexType"
def test_adj_vertices(self) -> None: g = DiGraph([(1, 1)]) assert g[1].adj_vertices() == { g[1] }, "vertex with directed self-loop should be adjacent to itself" assert g[1].adj_vertices_incoming() == { g[1] }, "vertex with directed self-loop should connect to itself via an incoming edge" assert g[1].adj_vertices_outgoing() == { g[1] }, "vertex with directed self-loop should connect to itself via an outgoing edge" g.add_edge(1, 2) assert g[1].adj_vertices() == { g[1], g[2], }, "vertex 1 should be adjacent to itself and vertex 2" assert g[1].adj_vertices_incoming() == { g[1] }, "vertex 1 should only have itself as incoming adjacent vertex" assert g[1].adj_vertices_outgoing() == { g[1], g[2], }, "vertex 1 should only have itself and vertex 2 as outgoing adjacent vertex"
def test_degree(self) -> None: g = DiGraph() v0 = g.add_vertex("0") assert v0.indegree == 0, "vertex 0 should have indegree 0" assert v0.outdegree == 0, "vertex 0 should have outdegree 0" g.add_edge(1, 1) assert g[1].degree == 2, "vertex with self loop should have degree 2" assert g[ 1].indegree == 1, "vertex with self loop should have indegree 1" assert g[ 1].outdegree == 1, "vertex with self loop should have outdegree 1" g.add_edge(1, 2) assert g[1].outdegree == 2, "vertex 1 should have outdegree 2" assert g[ 1].indegree == 1, "vertex with self loop and outgoing edge should have indegree 1" assert g[ 2].outdegree == 0, "vertex with no outgoing edges should have outdegree 0" assert g[ 2].indegree == 1, "vertex with one incoming edge should have indegree 1" g.add_edge(3, 1) assert g[1].indegree == 2, "vertex 1 should have indegree 2"
def test_equality_operator(self) -> None: dg = DiGraph([(1, 2), (3, 4, 3.5), (4, 5, 7.5, { "color": "blue" }), (6, 7, 9.5, { "k": "v" })]) dg2 = DiGraph([(2, 1), (3, 4), (4, 5, 7.5, { "color": "red" }), (6, 7, 9.5, { "k": "v" })]) assert dg.get_edge(1, 2) == dg.get_edge( 1, 2), "edge (1, 2) should equal itself within the same graph" assert dg.get_edge(1, 2) != dg2.get_edge( 2, 1), "edges (1, 2) and (2, 1) should not be equal in digraph" assert dg.get_edge(3, 4) != dg2.get_edge( 3, 4), "edges (3, 4) should not be equal due to different weights" assert dg.get_edge(4, 5) != dg2.get_edge( 4, 5), "edges (4, 5) should not be equal due to different attributes" assert dg.get_edge(6, 7) == dg2.get_edge( 6, 7), "edges (6, 7) should be equal"