Exemplo n.º 1
0
    def test_add_edge(self):
        graph = BidirectionalGraph()
        source = BDNode('source')
        dest = BDNode('dest')
        fake_source = 'source'
        fake_dest = 'dest'

        # Trying to add an edge with non-nodes returns None
        self.assertIsNone(graph.add_edge(source, fake_dest))
        self.assertIsNone(graph.add_edge(fake_source, dest))
        self.assertIsNone(graph.add_edge(fake_source, fake_dest))

        # Trying to add an edge when both nodes aren't in the graph returns None
        self.assertIsNone(graph.add_edge(source, dest))
        graph.add_node(source)
        self.assertIsNone(graph.add_edge(source, dest))
        graph.add_node(dest)

        # Adding an edge with both nodes in the graph returns a new edge,
        # if the edge doesn't yet exist in the graph
        res = graph.add_edge(source, dest)
        self.assertIsInstance(res, BDEdge)
        self.assertEqual(res.count, 1)

        # Adding an edge with both nodes in the graph increments the count of
        # the edge that already exists
        inc = graph.add_edge(source, dest)
        self.assertIsInstance(inc, BDEdge)
        self.assertEqual(inc, res)
        self.assertEqual(inc.count, 2)
Exemplo n.º 2
0
    def test_add_edge(self):
        graph = BidirectionalGraph()
        source = BDNode("source")
        dest = BDNode("dest")
        fake_source = "source"
        fake_dest = "dest"

        # Trying to add an edge with non-nodes returns None
        self.assertIsNone(graph.add_edge(source, fake_dest))
        self.assertIsNone(graph.add_edge(fake_source, dest))
        self.assertIsNone(graph.add_edge(fake_source, fake_dest))

        # Trying to add an edge when both nodes aren't in the graph returns None
        self.assertIsNone(graph.add_edge(source, dest))
        graph.add_node(source)
        self.assertIsNone(graph.add_edge(source, dest))
        graph.add_node(dest)

        # Adding an edge with both nodes in the graph returns a new edge,
        # if the edge doesn't yet exist in the graph
        res = graph.add_edge(source, dest)
        self.assertIsInstance(res, BDEdge)
        self.assertEqual(res.count, 1)

        # Adding an edge with both nodes in the graph increments the count of
        # the edge that already exists
        inc = graph.add_edge(source, dest)
        self.assertIsInstance(inc, BDEdge)
        self.assertEqual(inc, res)
        self.assertEqual(inc.count, 2)