def test_add_vertex(self): graph = Graph() lst = ["A", "B", "C", "D"] [graph.addVertex(vert) for vert in lst] assert len(graph.vertList) is 4 assert graph.numVertices is 4 assert graph.getVertex('A').getId() is 'A' assert graph.getVertex('B').getId() is 'B'
def test_add_vertex(self): graph = Graph() graph.add_vertex("a") assert graph.numVertices == 1 assert "a" in graph.vertList.keys() graph.add_vertex("b") graph.add_vertex("c") graph.add_vertex("d") graph.add_vertex("e") assert graph.numVertices == 5 graph.add_vertex("a") assert graph.numVertices == 5 assert len(graph.vertList) == 5
def test_add_vertex(self): graph = Graph() graph.add_vertex("a") assert graph.vert_count == 1 assert "a" in graph.vert_list.keys() graph.add_vertex("b") graph.add_vertex("c") graph.add_vertex("d") graph.add_vertex("e") assert graph.vert_count == 5 graph.add_vertex("a") assert graph.vert_count == 5 assert len(graph.vert_list) == 5
def test_vertex_count(self): g = Graph('sample_graph_1.txt') assert g.vertex_count() == 4 assert g.vertex_count() != 0 other_g = Graph('sample_graph_2.txt') assert other_g.vertex_count() == 10 assert other_g.vertex_count() != 0 another_g = Graph('sample_graph_3.txt') assert another_g.vertex_count() == 10 assert another_g.vertex_count() != 0
def test_add_vertex(self): graph = Graph() graph.add_vertex("apple") graph.add_vertex("banana") self.assertEqual(2, graph.num_vertices) # self.assertIsInstance(graph.get_vertex("apple"), Vertex) self.assertIsInstance(graph.vertices["apple"], Vertex)
def create_graph(graph_data): is_graph = graph_data[0] is 'G' graph = Graph() for vertex in graph_data[1].split(','): graph.addVertex(vertex) counter = 0 for word in graph_data[2:]: counter += 1 if is_graph: graph.addEdge(word[3], word[1], word[5:].replace(')', '')) graph.addEdge(word[1], word[3], word[5:].replace(')', '')) else: graph.addEdge(word[1], word[3], word[5:].replace(')', '')) return graph, counter
def text_get_verticies(self): graph = Graph() # Create test Verticies v1, v2, v3 = Vertex("a"), Vertex("b"), Vertex("c") # Add verticies graph.add_vertex(v1) graph.add_vertex(v2) graph.add_vertex(v3) self.assertListEqual(graph.get_vertices, ["a", "b", "c"])
def test_add_vertex(self): graph = Graph() # Creates test Verticies v1, v2, v3 = Vertex("a"), Vertex("b"), Vertex("c") # Add vertex "a" graph.add_vertex(v1) self.assertEqual(graph.numVertices, 1) self.assertEqual(graph.numEdges, 0) # Add vertex "b" graph.add_vertex(v2) self.assertEqual(graph.numVertices, 2) self.assertEqual(graph.numEdges, 0) # Add vertex "c" graph.add_vertex(v3) self.assertEqual(graph.numVertices, 3) self.assertEqual(graph.numEdges, 0)
def test_get_edges(self): graph = Graph() # Create test Verticies v1, v2, v3 = Vertex("a"), Vertex("b"), Vertex("c") # Add verticies graph.add_vertex(v1) graph.add_vertex(v2) graph.add_vertex(v3) self.assertEqual(graph.numVertices, 3) self.assertEqual(graph.numEdges, 0) # Create edges edges = [("a", "b", 10), ("b", "c", 10), ("c", "a", 4)] # Iterate through edges for edge in edges: fromVert, toVert, weight = edge graph.add_edge(fromVert, toVert, weight) self.assertEqual(graph.numEdges, 3)
def test_add_edge(self): graph = Graph() graph.add_vertex("a") graph.add_vertex("b") graph.add_edge("a", "b", "4") vertex_a = graph.get_vertex("a") vertex_b = graph.get_vertex("b") assert "b" in vertex_a.neighbors assert "a" in vertex_b.neighbors assert "b" not in vertex_b.neighbors assert "a" not in vertex_a.neighbors assert vertex_a.get_edge_weight("b") == 4 assert vertex_b.get_edge_weight("a") == 4 self.assertRaises(ValueError, graph.add_edge, "x", "y") self.assertRaises(ValueError, graph.add_edge, "a", "a")
def setUp(self): self.graph = Graph()
def test_build_new_graph(): g = Graph() g.build_new_graph('graph.txt') assert g.graph['1'] assert len(g.graph['1']) == 2
def test_intance(): assert Graph()
def test_init(self): # Tests the initialization of the Graph class graph = Graph() self.assertDictEqual(graph.vertList, {}) self.assertEqual(graph.numVertices, 0) self.assertEqual(graph.numEdges, 0)
def test_add_edge(self): graph = Graph() lst = ["A", "B", "C", "D"] [graph.addVertex(vert) for vert in lst] graph.addEdge('A', 'B')
def test_breadth_first_search(self): g = Graph() g.add_vertex(1) g.add_vertex(2) g.add_vertex(3) g.add_vertex(4) g.add_vertex(5) g.add_edge(1, 2) g.add_edge(1, 4) g.add_edge(2, 3) g.add_edge(2, 4) g.add_edge(2, 5) g.add_edge(3, 5) g.breadth_first_search(1, 5) assert len(path) == 3 assert distance == 2
def test_add_vertex(self): g = Graph() g.addVertex(1) g.addVertex(2) self.assertEqual(2, len(g.getVertices()))
def test_edge_count(self): g = Graph('sample_graph_1.txt') assert g.edge_count() == 4 assert g.edge_count() != 0 other_g = Graph('sample_graph_2.txt') assert other_g.edge_count() == 12 assert other_g.edge_count() != 0 another_g = Graph('sample_graph_3.txt') assert another_g.edge_count() == 27 # make sure its not including empty parentheses or invalid data assert another_g.edge_count() != 29 assert another_g.edge_count() != 31 assert another_g.edge_count() != 0
class GraphTests(unittest.TestCase): def setUp(self): self.graph = Graph() def test_init(self): assert not self.graph.vertices_dict assert self.graph.num_vertices is 0 def test_add_vertex(self): # Add one vertex self.graph.add_vertex("B") assert self.graph.num_vertices is 1 assert len(self.graph.vertices_dict.keys()) is 1 assert "B" in self.graph.vertices_dict # Add another vertex self.graph.add_vertex("A") assert self.graph.num_vertices is 2 assert len(self.graph.vertices_dict.keys()) is 2 assert "A" in self.graph.vertices_dict # Should avodata adding duplicate self.graph.add_vertex("B") assert self.graph.num_vertices is 2 assert len(self.graph.vertices_dict.keys()) is 2 def test_get_vertex(self): # Add vertex self.graph.add_vertex("B") # Find the newly added vertex found_vertex = self.graph.get_vertex("B") assert found_vertex.data is "B" # Test for non existing vertex assert self.graph.get_vertex("C") is None def test_add_edge(self): self.graph.undirected = True # Populate the graph with vertices A, B, C, D, E, F, G self.graph.add_vertex("A") self.graph.add_vertex("B") self.graph.add_vertex("C") self.graph.add_vertex("D") self.graph.add_vertex("E") self.graph.add_vertex("F") self.graph.add_vertex("G") # Add edge from A to B with the cost of 10 self.graph.add_edge("A", "B", 10) vertex_a = self.graph.get_vertex("A") vertex_b = self.graph.get_vertex("B") vertex_c = self.graph.get_vertex("C") # Vertex A should have an edge to vertex B with the cost of 10 assert "B" in vertex_a.neighbors assert vertex_a.get_edge_weight("B") is 10 assert "A" in vertex_b.neighbors assert vertex_b.get_edge_weight("A") is 10 self.graph.add_edge("A", "C", 5) self.graph.add_edge("B", "C", 10) assert "C" in vertex_a.neighbors and "C" in vertex_b.neighbors assert vertex_a.get_edge_weight("C") is 5 assert vertex_b.get_edge_weight("C") is 10 assert vertex_c.get_edge_weight("A") is 5 and vertex_c.get_edge_weight( "B") is 10 # Test for bad inputs assert self.graph.add_edge("O", "B", 12) is None def test_get_verticles(self): # Populate the graph with vertices A, B, C self.graph.add_vertex("B") self.graph.add_vertex("A") self.graph.add_vertex("C") vertices_list = self.graph.get_vertices() assert list assert len(vertices_list) is 3 assert "A" in vertices_list assert "B" in vertices_list assert "C" in vertices_list def test_get_edges(self): # Populate the graph with vertices A, B, C self.graph.add_vertex("B") self.graph.add_vertex("A") self.graph.add_vertex("C") self.graph.add_edge("A", "B", 10) self.graph.add_edge("A", "C", 5) self.graph.add_edge("B", "C", 10) edges_set = self.graph.get_edges() assert len(edges_set) is 3 assert ("A", "B", 10) in edges_set or ("B", "A", 10) assert ("A", "C", 5) in edges_set or ("C", "A", 5) in edges_set assert ("B", "C", 10) in edges_set or ("C", "B", 10) in edges_set def test_reading_file(self): filename = 'graph_data.txt' self.graph.read_file(filename) # D # 1, 2, 3, 4, 5 # (1, 2) # (1, 4) # (2, 3) # (2, 4) # (2, 5) # (3, 5) # (5, 2) # Graph should have expected vertices expected_vertices = ['1', '2', '3', '4', '5'] assert len(self.graph.get_vertices()) is 5 for key in expected_vertices: assert key in self.graph.vertices_dict # Graph should have expected edges expected_edges = [ ('1', '2', 0), ('1', '4', 0), ('2', '3', 0), ('2', '4', 0), ('2', '5', 0), ('3', '5', 0), ] assert len(self.graph.edges_list) is 6 for edge in expected_edges: assert edge in self.graph.edges_list
def test_init(self): nothing = Graph('empty.txt') assert nothing.file == 'empty.txt'
def test_init(self): graph = Graph() assert any(graph.vert_list) is False assert graph.vertCount == 0
def test_empty_graph(self): nothing = Graph('empty.txt') assert nothing.vertex_count() == 0 assert nothing.edge_count() == 0 assert nothing.edge_list() == []
def test_get_vertex(self): graph = Graph() graph.add_vertex("a") assert graph.get_vertex("a") self.assertRaises(ValueError, graph.get_vertex, "z")
def test_add_edge(self): graph = Graph() graph.add_vertex("apple") graph.add_vertex("banana") graph.add_vertex("coconut") graph.add_edge("apple", "banana") graph.add_edge("apple", "coconut", 3) self.assertEqual(3, graph.num_vertices) self.assertEqual(2, graph.num_edges) graph.add_edge("pineapple", "strawberry") self.assertEqual(5, graph.num_vertices) self.assertEqual(3, graph.num_edges) self.assertCountEqual( ["apple", "banana", "coconut", "pineapple", "strawberry"], graph.get_vertices())
def test_get_vertices(self): graph = Graph() graph.add_vertex("a") graph.add_vertex("b") graph.add_vertex("c") graph.add_vertex("d") graph.add_vertex("e") assert "a" in graph.get_vertices() assert "b" in graph.get_vertices() assert "c" in graph.get_vertices() assert "d" in graph.get_vertices() assert "e" in graph.get_vertices() assert "x" not in graph.get_vertices()
def test_init(self): graph = Graph() assert len(graph.vertList) is 0 assert graph.numVertices is 0