Beispiel #1
0
    def test__init__from_graph(self) -> None:
        g = Graph([(2, 1, 5.0, {"color": "red"}), (4, 3)])
        assert g.weight == 6.0, "graph should have weight 6.0"

        dg = DiGraph(g)
        assert dg.has_edge(
            2, 1), "digraph should have edge (2, 1) copied from graph"
        assert (dg.get_edge(2, 1)["color"] == "red"
                ), "edge (2, 1) should have 'color' attribute set to red"
        assert dg.get_edge(
            2, 1).weight == 5.0, "edge (2, 1) should have weight 5.0"
        assert dg.has_edge(
            4, 3), "graph should have edge (4, 3) copied from graph"
        assert dg.edge_count == 2, "graph should have two edges"
        assert dg.weight == 6.0, "graph should have weight 6.0"
        assert dg.get_edge(2, 1) is not g.get_edge(  # type: ignore
            2, 1), "digraph should have deep copies of edges and vertices"
Beispiel #2
0
    def test_add_edge(self) -> None:
        g = DiGraph()
        edge = g.add_edge(2, 1, weight=4.5, color="blue", mass=42)
        assert isinstance(edge, DiEdge), "new edge should an DiEdge object"
        assert g.get_edge(
            2, 1).weight == 4.5, "edge (2, 1) should have weight 4.5"
        assert edge[
            "color"] == "blue", "edge should have 'color' attribute set to 'blue'"
        assert edge[
            "mass"] == 42, "edge should have 'mass' attribute set to 42"

        edge_dup = g.add_edge(2, 1, weight=1.5, color="red", mass=57)
        assert (
            edge is edge_dup
        ), "adding an edge with same vertices as existing edge should return existing edge"

        g.add_edge(3, 4)
        assert (g.get_edge(3, 4).weight == edge_module.DEFAULT_WEIGHT
                ), "edge should have default weight"
        assert not g.get_edge(3, 4).has_attributes_dict(
        ), "edge should not have attributes dictionary"
        assert not g.has_edge(1, 2), "digraph should not have edge (1, 2)"