def test_remove_node(self):
        """
        Test to ensure that nodes will be removed correctly
        """
        data = {
            "nodes": [
                {
                    "data": {
                        "id": "0"
                    }
                },
                {
                    "data": {
                        "id": "1"
                    }
                },
                {
                    "data": {
                        "id": "2"
                    }
                },
            ],
            "edges": [
                {
                    "data": {
                        "source": "0",
                        "target": "1"
                    }
                },
                {
                    "data": {
                        "source": "1",
                        "target": "2"
                    }
                },
                {
                    "data": {
                        "source": "2",
                        "target": "0"
                    }
                },
            ],
        }
        expected_nodes = [
            Node(data={"id": "1"}, position={}),
            Node(data={"id": "2"}, position={}),
        ]
        expected_edges = [
            Edge(classes="", data={
                "source": "1",
                "target": "2"
            }),
        ]

        graph = Graph()
        graph.add_graph_from_json(data)
        graph.remove_node(graph.nodes[0])
        compare_edges(expected_edges, graph.edges)
        compare_nodes(expected_nodes, graph.nodes)
Exemple #2
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 = []