def test_edge_orders(self):
        with self.assertRaises(Exception) as cm:
            self.graph.setEdgeOrder(tlp.node(NB_NODES), [])
            self.assertEquals(
                cm.exception.args[0],
                'Node with id %s does not belong to graph "%s" (id %s)' %
                (NB_NODES, self.graph.getName(), self.graph.getId()))

        with self.assertRaises(Exception) as cm:
            self.graph.swapEdgeOrder(tlp.node(NB_NODES),
                                     tlp.edge(), tlp.edge())
            self.assertEquals(
                cm.exception.args[0],
                'Node with id %s does not belong to graph "%s" (id %s)' %
                (NB_NODES, self.graph.getName(), self.graph.getId()))

        with self.assertRaises(Exception) as cm:
            self.graph.swapEdgeOrder(self.graph.getRandomNode(),
                                     tlp.edge(NB_EDGES), tlp.edge(NB_EDGES+1))
            self.assertEquals(
                cm.exception.args[0],
                'Edge with id %s does not belong to graph "%s" (id %s)' %
                (NB_EDGES, self.graph.getName(), self.graph.getId()))

        for n in self.graph.getNodes():
            edges = list(self.graph.getInOutEdges(n))
            edges = list(reversed(edges))
            self.graph.setEdgeOrder(n, edges)
            self.assertEqual(list(self.graph.getInOutEdges(n)), edges)
            self.assertEqual(self.graph.incidence(n), edges)

            self.graph.swapEdgeOrder(n, edges[0], edges[-1])
            edges[0], edges[-1] = edges[-1], edges[0]
            self.assertEqual(list(self.graph.getInOutEdges(n)), edges)
            self.assertEqual(self.graph.incidence(n), edges)
    def test_del_edges_from_list(self):
        invalid_edges = [tlp.edge(NB_EDGES), tlp.edge(NB_EDGES+1)]
        with self.assertRaises(Exception) as cm:
            self.graph.delEdges(invalid_edges)
            self.assertEquals(
                cm.exception.args[0],
                'Edge with id %s does not belong to graph "%s" (id %s)' %
                (NB_EDGES, self.graph.getName(), self.graph.getId()))

        self.graph.delEdges(self.edges)
        self.assertEqual(self.graph.numberOfEdges(), 0)
    def test_is_element(self):
        for n in self.nodes:
            self.assertTrue(self.graph.isElement(n))
            self.assertTrue(n in self.graph)

        for e in self.edges:
            self.assertTrue(self.graph.isElement(e))
            self.assertTrue(e in self.graph)

        for n in self.graph.getNodes():
            self.assertIn(n, self.nodes)

        for n in self.graph.nodes():
            self.assertIn(n, self.nodes)

        for e in self.graph.getEdges():
            self.assertIn(e, self.edges)
            self.assertIn(self.graph.source(e), self.nodes)
            self.assertIn(self.graph.target(e), self.nodes)
            src, tgt = self.graph.ends(e)
            self.assertIn(src, self.nodes)
            self.assertIn(tgt, self.nodes)

        for e in self.graph.edges():
            self.assertIn(e, self.edges)
            self.assertIn(self.graph.source(e), self.nodes)
            self.assertIn(self.graph.target(e), self.nodes)

        for i in range(NB_NODES - 1):
            self.assertTrue(self.graph.hasEdge(self.nodes[i], self.nodes[i+1]))
            self.assertIn(self.graph.existEdge(self.nodes[i], self.nodes[i+1]),
                          self.edges)
            self.assertTrue((self.nodes[i], self.nodes[i+1]) in self.graph)
            self.assertTrue(self.graph.hasEdge(self.nodes[i], self.nodes[-1]))
            self.assertIn(self.graph.existEdge(self.nodes[i], self.nodes[-1]),
                          self.edges)
            self.assertTrue((self.nodes[i], self.nodes[-1]) in self.graph)

        self.assertFalse(self.graph.isElement(tlp.node(NB_NODES)))
        self.assertTrue(tlp.node(NB_NODES) not in self.graph)
        self.assertFalse(self.graph.isElement(tlp.edge(NB_EDGES)))
        self.assertTrue(tlp.edge(NB_EDGES) not in self.graph)
    def test_add_invalid_node_or_edge_to_root_graph(self):
        with self.assertRaises(Exception) as cm:
            self.graph.addNode(tlp.node(NB_NODES))
            self.assertEquals(
                cm.exception.args[0],
                'Node with id %s does not belong to graph "%s" (id %s)' %
                (NB_NODES, self.graph.getName(), self.graph.getId()))

        with self.assertRaises(Exception) as cm:
            self.graph.addEdge(tlp.edge(NB_EDGES))
            self.assertEquals(
                cm.exception.args[0],
                'Edge with id %s does not belong to graph "%s" (id %s)' %
                (NB_EDGES, self.graph.getName(), self.graph.getId()))
    def test_del_elements(self):
        with self.assertRaises(Exception) as cm:
            self.graph.delNode(tlp.node(NB_NODES))
            self.assertEquals(
                cm.exception.args[0],
                'Node with id %s does not belong to graph "%s" (id %s)' %
                (NB_NODES, self.graph.getName(), self.graph.getId()))

        with self.assertRaises(Exception) as cm:
            self.graph.delEdge(tlp.edge(NB_EDGES))
            self.assertEquals(
                cm.exception.args[0],
                'Edge with id %s does not belong to graph "%s" (id %s)' %
                (NB_EDGES, self.graph.getName(), self.graph.getId()))

        for i, e in enumerate(self.edges):
            self.graph.delEdge(e)
            self.assertEqual(self.graph.numberOfEdges(), NB_EDGES - (i+1))
            self.assertFalse(self.graph.isElement(e))

        for i, n in enumerate(self.nodes):
            self.graph.delNode(n)
            self.assertEqual(self.graph.numberOfNodes(), NB_NODES - (i+1))
            self.assertFalse(self.graph.isElement(n))
    def test_edge_extremities_modification(self):

        with self.assertRaises(Exception) as cm:
            self.graph.ends(tlp.edge(NB_EDGES))
            self.assertEquals(
                cm.exception.args[0],
                'Edge with id %s does not belong to graph "%s" (id %s)' %
                (NB_EDGES, self.graph.getName(), self.graph.getId()))

        with self.assertRaises(Exception) as cm:
            self.graph.reverse(tlp.edge(NB_EDGES))
            self.assertEquals(
                cm.exception.args[0],
                'Edge with id %s does not belong to graph "%s" (id %s)' %
                (NB_EDGES, self.graph.getName(), self.graph.getId()))

        with self.assertRaises(Exception) as cm:
            self.graph.setSource(tlp.edge(NB_EDGES), tlp.node())
            self.assertEquals(
                cm.exception.args[0],
                'Edge with id %s does not belong to graph "%s" (id %s)' %
                (NB_EDGES, self.graph.getName(), self.graph.getId()))

        with self.assertRaises(Exception) as cm:
            self.graph.setTarget(tlp.edge(NB_EDGES), tlp.node())
            self.assertEquals(
                cm.exception.args[0],
                'Edge with id %s does not belong to graph "%s" (id %s)' %
                (NB_EDGES, self.graph.getName(), self.graph.getId()))

        with self.assertRaises(Exception) as cm:
            self.graph.setSource(self.graph.getRandomEdge(),
                                 tlp.node(NB_NODES))
            self.assertEquals(
                cm.exception.args[0],
                'Node with id %s does not belong to graph "%s" (id %s)' %
                (NB_NODES, self.graph.getName(), self.graph.getId()))

        with self.assertRaises(Exception) as cm:
            self.graph.setTarget(self.graph.getRandomEdge(),
                                 tlp.node(NB_NODES))
            self.assertEquals(
                cm.exception.args[0],
                'Node with id %s does not belong to graph "%s" (id %s)' %
                (NB_NODES, self.graph.getName(), self.graph.getId()))

        with self.assertRaises(Exception) as cm:
            self.graph.setEnds(tlp.edge(NB_EDGES), tlp.node(), tlp.node())
            self.assertEquals(
                cm.exception.args[0],
                'Edge with id %s does not belong to graph "%s" (id %s)' %
                (NB_EDGES, self.graph.getName(), self.graph.getId()))

        with self.assertRaises(Exception) as cm:
            self.graph.setEnds(self.graph.getRandomEdge(),
                               tlp.node(NB_NODES), tlp.node(NB_NODES+1))
            self.assertEquals(
                cm.exception.args[0],
                'Node with id %s does not belong to graph "%s" (id %s)' %
                (NB_NODES, self.graph.getName(), self.graph.getId()))

        for e in self.graph.edges():
            src, tgt = self.graph.ends(e)
            self.graph.reverse(e)
            self.assertEqual(self.graph.source(e), tgt)
            self.assertEqual(self.graph.target(e), src)

            new_src = self.graph.getRandomNode()
            new_tgt = self.graph.getRandomNode()
            self.graph.setSource(e, new_src)
            self.graph.setTarget(e, new_tgt)
            self.assertEqual(self.graph.source(e), new_src)
            self.assertEqual(self.graph.target(e), new_tgt)
            self.assertEqual(self.graph.ends(e), (new_src, new_tgt))

            new_src = self.graph.getRandomNode()
            new_tgt = self.graph.getRandomNode()
            self.graph.setEnds(e, new_src, new_tgt)
            self.assertEqual(self.graph.source(e), new_src)
            self.assertEqual(self.graph.target(e), new_tgt)
            self.assertEqual(self.graph.ends(e), (new_src, new_tgt))