Ejemplo n.º 1
0
    def test_lonely_nodes(self):
        """
        Test to ensure that nodes with no associated edges end up in the graph
        """
        G1 = nx.complete_graph(5)
        G2 = nx.Graph()
        G2.add_node("unconnected_node")
        G2 = nx.complete_graph(1)
        graph = Graph()
        graph.add_graph_from_networkx(G1)
        graph.add_graph_from_networkx(G2)

        expected_nodes = [
            Node(data={"id": "0"}, position={}),
            Node(data={"id": "1"}, position={}),
            Node(data={"id": "2"}, position={}),
            Node(data={"id": "3"}, position={}),
            Node(data={"id": "4"}, position={}),
            Node(data={"id": "unconnected_node"}, position={}),
        ]

        # remove individual node using node as input
        graph.remove_node(graph.nodes[0])

        expected_nodes = [
            Node(data={"id": "1"}, position={}),
            Node(data={"id": "2"}, position={}),
            Node(data={"id": "3"}, position={}),
            Node(data={"id": "4"}, position={}),
            Node(data={"id": "unconnected_node"}, position={}),
        ]

        # remove individual node using index node as input
        graph.remove_node_by_id("3")

        expected_nodes = [
            Node(data={"id": "1"}, position={}),
            Node(data={"id": "2"}, position={}),
            Node(data={"id": "4"}, position={}),
            Node(data={"id": "unconnected_node"}, position={}),
        ]

        # remove all nodes of the graph
        graph.clear()
        expected_nodes = []
    def test_remove_node_by_id(self):
        """
        Test to ensure that nodes will be removed given the id
        for different graphs with the corresponding edges
        """
        data = {
            "nodes": [
                {
                    "data": {
                        "id": "0"
                    }
                },
                {
                    "data": {
                        "id": "1"
                    }
                },
                {
                    "data": {
                        "id": "2"
                    }
                },
            ],
            "edges": [
                {
                    "data": {
                        "source": "0",
                        "target": "1",
                        "weight": "1"
                    }
                },
                {
                    "data": {
                        "source": "0",
                        "target": "1",
                        "weight": "2"
                    }
                },
                {
                    "data": {
                        "source": "1",
                        "target": "0"
                    }
                },
                {
                    "data": {
                        "source": "1",
                        "target": "2"
                    }
                },
                {
                    "data": {
                        "source": "2",
                        "target": "0"
                    }
                },
            ],
        }
        expected_nodes = [
            Node(data={"id": "1"}, position={}),
            Node(data={"id": "2"}, position={}),
        ]
        expected_edges_undirected = [
            Edge(classes="", data={
                "source": "1",
                "target": "2"
            }),
        ]
        expected_edges_directed = [
            Edge(classes=" directed ", data={
                "source": "1",
                "target": "2"
            }),
        ]
        expected_edges_multiple = [
            Edge(classes=" multiple_edges ",
                 data={
                     "source": "1",
                     "target": "2"
                 }),
        ]

        graph = Graph()
        graph.add_graph_from_json(data)
        graph.remove_node_by_id("0")
        compare_edges(expected_edges_undirected, graph.edges)
        compare_nodes(expected_nodes, graph.nodes)

        graph = Graph()
        graph.add_graph_from_json(data, directed=True)
        graph.remove_node_by_id("0")
        compare_edges(expected_edges_directed, graph.edges)
        compare_nodes(expected_nodes, graph.nodes)
        graph.remove_node_by_id("1")
        compare_edges(expected_edges_directed[1:], graph.edges)
        compare_nodes(expected_nodes[1:], graph.nodes)

        graph = Graph()
        graph.add_graph_from_json(data, multiple_edges=True)
        graph.remove_node_by_id("0")
        compare_edges(expected_edges_multiple, graph.edges)
        compare_nodes(expected_nodes, graph.nodes)
        graph.remove_node_by_id("1")
        compare_edges(expected_edges_multiple[1:], graph.edges)
        compare_nodes(expected_nodes[1:], graph.nodes)