Example #1
0
class VertexTests(unittest.TestCase):
    def setUp(self):
        self.vertex = Vertex("A")

    def test_init(self):
        assert self.vertex.id is 'A'
        assert not self.vertex.neighbors

    def test_add_neighbor(self):
        # Adding a neighbor
        neighbor_one = "B"
        self.vertex.add_neighbor(neighbor_one)
        assert len(self.vertex.get_neighbors()) is 1
        assert neighbor_one in self.vertex.neighbors
        assert self.vertex.get_edge_weight(neighbor_one) is 0

        # Adding another neighbor
        neighbor_two = 'C'
        self.vertex.add_neighbor(neighbor_two, 4)
        assert len(self.vertex.get_neighbors()) is 2
        assert neighbor_two in self.vertex.neighbors
        assert self.vertex.get_edge_weight(neighbor_two) is 4

        # Handling duplicate vertex
        self.vertex.add_neighbor(neighbor_one, 8)
        assert len(self.vertex.get_neighbors()) is 2
        assert neighbor_one in self.vertex.neighbors
        assert self.vertex.get_edge_weight(neighbor_one) is 8

    def test_negatives(self):
        # Requesting a weight from a non existing edge
        assert self.vertex.get_edge_weight("B") is None
Example #2
0
    def test_get_edge_weight(self):
        ramon = Vertex('Ramon Geronimo')
        jessie = Vertex('Jessie Pichardo')
        joel = Vertex('Joel Pichardo')

        ramon.add_neighbor(jessie, 10)
        ramon.add_neighbor(joel, 5)
        assert ramon.get_edge_weight(jessie) == 10
        assert ramon.get_edge_weight(joel) == 5
Example #3
0
 def test_get_edge_weight(self):
     v1 = Vertex(1)
     v2 = Vertex(2)
     v3 = Vertex(3)
     # Test default weight variable
     v2.add_neighbor(v1)
     assert v2.get_edge_weight(v1) == 1
     # Test passed in weight variable
     v2.add_neighbor(v3, 3)
     assert v2.get_edge_weight(v3) == 3
Example #4
0
    def test_get_edge_weight(self):
        label = "Sugar"
        vertex = Vertex(label)

        vertex.add_neighbor("Kevin", weight = 0)
        vertex.add_neighbor("Chewie", weight = 4)
        vertex.add_neighbor("Maggie", weight = 8)
        vertex.add_neighbor("Ducky", weight = 2)

        # Should raise ValueError telling us "Rainbow Unicorn" is not a neighbor
        self.assertRaises(ValueError, vertex.get_edge_weight, "Rainbow Unicorn")        

        # Should tell us Sugar has 4 neighbors
        assert len(vertex.neighbors) == 4
        # Should tell us Sugar's neighbors have a weight of 0, 4, 8, or 2
        assert vertex.get_edge_weight("Kevin") == 0
        assert vertex.get_edge_weight("Chewie") == 4
        assert vertex.get_edge_weight("Maggie") == 8
        assert vertex.get_edge_weight("Ducky") == 2
    def test_add_neighbor(self):
        vertex_1 = Vertex('A')
        # add a neighbor without weight
        vertex_2 = 'B'
        vertex_1.add_neighbor(vertex_2)
        assert vertex_2 in vertex_1.neighbors
        assert len(vertex_1.get_neighbors()) is 1
        assert vertex_1.get_edge_weight(vertex_2) is 0

        vertex_3 = 'C'
        vertex_1.add_neighbor(vertex_3, 5)
        assert vertex_3 in vertex_1.neighbors
        assert len(vertex_1.get_neighbors()) is 2
        assert vertex_1.get_edge_weight(vertex_3) is 5

        # duplicate neighbor
        vertex_1.add_neighbor(vertex_2, 3)
        assert vertex_2 in vertex_1.neighbors
        assert len(vertex_1.get_neighbors()) is 2
        assert vertex_1.get_edge_weight(vertex_2) is 3
 def test_get_edge_weight(self):
     test_vertex = Vertex(1)
     test_vertex.add_neighbor(2, 1)
     assert test_vertex.get_edge_weight(2) == 1