def test_add_edge(): graph = Graph() end = graph.add_node('banana') start = graph.add_node('apple') graph.add_edge(start, end) assert graph._adjacency_list[start][0] == (end, 0) assert graph.get_neighbors(start) == [(end, 0)] graph.add_edge(end, start) assert graph.get_neighbors(end) == [(start, 0)]
def test_add_edge_with_weight_letters(): graph = Graph() end = graph.add_node('dog') start = graph.add_node('cat') graph.add_edge(start, end, "a") assert graph.get_neighbors(start) == [(end, "a")] graph.add_edge(end, start, "b") assert graph.get_neighbors(end) == [(start, "b")]
def test_add_edge_with_weight(): graph = Graph() end = graph.add_node('banana') start = graph.add_node('apple') graph.add_edge(start, end, 44) assert graph.get_neighbors(start) == [(end, 44)] graph.add_edge(end, start, 23) assert graph.get_neighbors(end) == [(start, 23)]
def test_one_node(): graph = Graph() graph.add_node('node1') graph.add_edge('node1','node1') actual = graph.get_neighbors('node1') expected = 'node1 :edge--> node1 ... weight: 1' assert expected == actual
def test_one_node_one_edge(): g = Graph() g.add_node('a') g.add_edge('a', 'a') actual = g.get_neighbors('a') expected = 'a --> a edge weight: 1' assert expected == actual
def test_get_neighbors(): gr = Graph() start = gr.add_node('start') end = gr.add_node('end') gr.add_edge(start, end, 4) neighbors = gr.get_neighbors(start) assert neighbors[0][0].value == 'end' assert neighbors[0][1] == 4
def test_edge_added_to_graph(): g = Graph() g.add_node('a') g.add_node('b') g.add_edge('a', 'b', 1) actual = g.get_neighbors('a') expected = 'a --> b edge weight: 1' assert actual == expected
def test_edge(): graph=Graph() graph.add_node('a') graph.add_node('b') graph.add_edge('a', 'b', 8) actual = graph.get_neighbors('a') expected = 'a :edge--> b ... weight: 8' assert actual == expected
def test_helper2_no_neighbors(): graph = Graph() a = graph.add_node("a") neighbors_dict = helper2(graph.get_neighbors(a)) assert neighbors_dict == {}
def test_get_neighbors_no_neighbors(): graph = Graph() banana = graph.add_node('banana') neighbors = graph.get_neighbors(banana) assert len(neighbors) == 0 assert neighbors == []
def test_all_neighbors_can_be_retrived_with_weight(): g = Graph() g.add_node('a') g.add_node('b') g.add_edge('a', 'b', 4) actual = g.get_neighbors('a') expected = 'a --> b edge weight: 4' assert expected == actual
def test_get_neighbors(): graph = Graph() hi = graph.add_node('hi') hello = graph.add_node('hello') graph.add_edge(hi, hello) actual = graph.get_neighbors(hi) assert actual == [(hello, 0)]
def test_helper2(): graph = Graph() a = graph.add_node("a") b = graph.add_node("b") c = graph.add_node("c") d = graph.add_node("d") graph.add_edge(a, b, 150) graph.add_edge(a, c, 100) graph.add_edge(a, d, 50) neighbors_dict = helper2(graph.get_neighbors(a)) assert neighbors_dict == { "b": (b, 150), "c": (c, 100), "d": (d, 50), }
def test_get_neighbors(): graph = Graph() banana = graph.add_node('banana') apple = graph.add_node('apple') graph.add_edge(apple, banana, 44) neighbors = graph.get_neighbors(apple) assert len(neighbors) == 1 assert neighbors[0][0].value == 'banana' assert isinstance(neighbors[0][0], Vertex) assert neighbors[0][1] == 44
def test_add_edge_more_nodes(): graph = Graph() a_vertex = graph.add_node('a') b_vertex = graph.add_node('b') c_vertex = graph.add_node('c') d_vertex = graph.add_node('d') graph.add_edge(a_vertex, b_vertex, 1) graph.add_edge(a_vertex, c_vertex, 2) graph.add_edge(a_vertex, d_vertex, 3) neighbors = graph.get_neighbors(a_vertex) assert neighbors[0][0].value == 'b' assert neighbors[0][1] == 1 assert isinstance(neighbors[0][0], Vertex) assert neighbors[1][0].value == 'c' assert neighbors[1][1] == 2 assert isinstance(neighbors[1][0], Vertex) assert neighbors[2][0].value == 'd' assert neighbors[2][1] == 3 assert isinstance(neighbors[2][0], Vertex)