def test_dublicate_values_to_a_graph_adds_some_nodes(num): """Test that adding duplicate values to the graph add only unique items.""" from graph_1 import Graph g = Graph() for x in range(num): g.add_node(x % 5) assert len(g.node_set) == 5 if num > 5 else num
def test_adding_unique_values_to_a_graph_adds_all_nodes(num): """Test that adding unique values to the graph adds all of them.""" from graph_1 import Graph g = Graph() for x in range(num): g.add_node(x) assert len(g.node_set) == num
def test_add_edges_removes_duplicate_edge(): """Test add edge adds an edge and removes old duplicate.""" from graph_1 import Graph g = Graph() g.add_edge(2, 3) g.add_edge(2, 3) assert g._g == {2: [3], 3: []}
def test_adding_unique_edges_to_a_graph_adds_all_edges(num): """Test that adding unique edges to the graph adds all edges.""" from graph_1 import Graph g = Graph() for x in range(num): g.add_edge(x, x + 1) assert len(g.edge_set) == num
def test_edges_of_filled_graph_has_all_edges(num): """Test that edges lists all the edges in a graph.""" from graph_1 import Graph g = Graph() for x in range(num): g.add_edge(x, x + 1) assert len(g.edges()) == num
def test_adding_duplicate_edges_to_a_graph_adds_unique_edges(num): """Test that adding duplicate edges to the graph unique edges.""" from graph_1 import Graph g = Graph() for x in range(num): g.add_edge(x % 5, x % 5 + 1) assert len(g.edge_set) == 5 if num > 5 else num
def test_adjacent_returns_true_if_specific_pair_of_values_given_exist(num): """Test adjacent is true if pair of values given exist in graph as edge.""" from graph_1 import Graph g = Graph() for x in range(num): g.add_edge(x, x + 1) for x in range(num): assert g.adjacent(x, x + 1)
def test_has_node_returns_true_if_node_serched_is_in_graph(num): """Test that has node returns true if looking for node present in graph.""" from graph_1 import Graph g = Graph() for x in range(num): g.add_node(x) for x in range(num): assert g.has_node(x)
def t_graph(): """Make a graph for traversal and weights.""" g = Graph() g.add_edge(1, 2, 5) g.add_edge(1, 3, 9) g.add_edge(2, 4, 4) g.add_edge(2, 5, 2) g.add_edge(3, 6, 1) g.add_edge(3, 7) return g
def full_graph_1(): """Create a graph with nodes and edges.""" from graph_1 import Graph g = Graph() g.add_edge(1, 2) g.add_edge(4, 2) g.add_edge(3, 5) g.add_edge(7, 9) g.add_edge(6, 8) g.add_edge(5, 3) g.add_node(21) return g
def __init__(self, writer, df, sheet, application_path, unit=False): self.id = next(self._ids) self.df = df # Change the df "All" column and index created using df.pivot_table to "Total" self.df = self.df.rename(columns={"All": "Total"}) self.df = self.df.rename(index={"All": "Total"}) self.sheet = sheet self.writer = writer self.workbook = self.writer.book self.application_path = application_path # Setup bold format self.bold = self.workbook.add_format({"bold": True}) # Create instance of graph class and save the created figure to working (or other chosen) directory self.graph = Graph(self.df).store_figure(application_path) # Create the next section to deal with formatting differently depending on whether we are # creating tables with revenue (currency) or quantities self.unit = unit if self.unit: self.currency = self.workbook.add_format( {"num_format": "£#,##0.00"}) self.bold_curr = self.workbook.add_format({ "bold": True, "num_format": "£#,##0.00" }) self.bold_curr_border = self.workbook.add_format({ "bold": True, "num_format": "£#,##0.00", "border": 1 }) else: self.currency = "" self.bold_curr = self.workbook.add_format({"bold": True}) self.bold_curr_border = self.workbook.add_format({ "bold": True, "border": 1 }) self.borders = self.workbook.add_format({"border": 1}) # Set header format self.header_format = self.workbook.add_format({ "bold": True, "text_wrap": True, "valign": "center", "fg_color": "#4F81BD", "border": 1, "font_color": "white" }) # Setup start and end rows to determine where to apply formats etc. self.start_row = 1 self.end_row = self.start_row + len(self.df) - 1 self.count = 0
def graph_no_edges(): """Test graph with nodes only.""" from graph_1 import Graph example_graph = Graph() example_graph.add_node('Grapefruit') example_graph.add_node(82) example_graph.add_node(99) example_graph.add_node('Luftballons') return example_graph
def graph_5(): """Making one graph instance with len of 5 per test.""" g = Graph() g.add_edge(1, 2) g.add_edge(2, 3) g.add_edge(3, 4) g.add_edge(4, 5) g.add_edge(5, 6) return g
def node_weight_graph(): """Create a graph with nodes, but no edges.""" from weight_graph import Graph g = Graph() g.add_node(1) g.add_node(2) g.add_node(3) g.add_node(4) g.add_node(5) return g
def edge_weight_graph(): """"Create a weight graph with edges.""" from weight_graph import Graph g = Graph() g.add_edge(1, 2, 1) g.add_edge(3, 4, 2) g.add_edge(5, 6, 3) g.add_edge(7, 8, 4) g.add_edge(9, 10, 5) return g
def full_weight_graph_tree(): """Create a graph with nodes and edge that connect to each other.""" from weight_graph import Graph g = Graph() g.add_edge(1, 2, 3) g.add_edge(1, 3, 5) g.add_edge(2, 4, 2) g.add_edge(2, 5, 9) g.add_edge(3, 6, 1) g.add_edge(3, 7, 3) return g
def full_graph_tree(): """Create a graph with nodes and edge that connect to eachother.""" from graph_1 import Graph g = Graph() g.add_edge(1, 2) g.add_edge(1, 3) g.add_edge(2, 4) g.add_edge(2, 5) g.add_edge(3, 6) g.add_edge(3, 7) return g
def test_nodes_of_filled_graph_has_all_nodes(num): """Test that nodes lists all the nodes in a graph.""" from graph_1 import Graph g = Graph() for x in range(num): g.add_node(x) assert len(g.nodes()) == num assert sorted(g.nodes()) == list(range(num))
def g3_fixt(): """Graph test fixture with 3 nodes.""" from graph_1 import Graph g = Graph() g.add_node(1) g.add_node(2) g.add_node(3) return g
def new_graph(): """Graph for testing.""" from graph_1 import Graph empty_graph = Graph() return empty_graph
def test_graph_init_no_values_taken(): """Ensure we raise an error if we try to init with a value.""" from graph_1 import Graph with pytest.raises(TypeError): a_graph = Graph(2)
def graph(): """Graph for our traversals.""" from graph_1 import Graph new_graph = Graph() new_graph.add_node('A') new_graph.add_node('B') new_graph.add_node('C') new_graph.add_node('D') new_graph.add_node('E') new_graph.add_node('F') new_graph.add_edge('A', 'B') new_graph.add_edge('A', 'C') new_graph.add_edge('B', 'D') new_graph.add_edge('B', 'E') new_graph.add_edge('C', 'B') new_graph.add_edge('F', 'A') new_graph.add_edge('C', 'F') return new_graph
raise KeyError('Node not in graph.') peeped = [] queue = Queue() queue.enqueue(start) while len(queue) > 0: node = queue.dequeue() if node not in peeped: peeped.append(node) for neighbor in graph._graph[node]: if neighbor not in peeped: queue.enqueue(neighbor) return peeped if __name__ == '__main__': new_graph = Graph() new_graph.add_node('A') new_graph.add_node('B') new_graph.add_node('C') new_graph.add_node('D') new_graph.add_node('E') new_graph.add_node('F') new_graph.add_edge('A', 'B') new_graph.add_edge('A', 'C') new_graph.add_edge('B', 'D') new_graph.add_edge('B', 'E') new_graph.add_edge('C', 'B') new_graph.add_edge('F', 'A') new_graph.add_edge('C', 'F') print(new_graph._graph)
def test_nodes_return_added_node_with_empty_list(): """Test add node creates empty dict as starting value.""" from graph_1 import Graph g = Graph() g.add_node(3) assert g._g[3] == []
def test_empty_constructor_constructs_empty_graph(): """Test that a new graph is empty.""" from graph_1 import Graph g = Graph() assert len(g.node_set) == 0 assert len(g.edge_set) == 0
def test_add_edges_return_val(): """Test add edge adds an edge to val1 node.""" from graph_1 import Graph g = Graph() g.add_edge(2, 3) assert g._g == {2: [3], 3: []}
def test_nodes_returns_list(): """Test.""" from graph_1 import Graph g = Graph() g_list = g.nodes() assert g_list == []
def test_init_isinstance_dict(): """Test is g an instance of Graph.""" from graph_1 import Graph g = Graph() assert isinstance(g, Graph)
def trav_fixt(): """Graph test fixture with nodes and edges.""" from graph_1 import Graph g = Graph() g.add_edge(1, 2) g.add_edge(1, 3) g.add_edge(1, 4) g.add_edge(2, 5) g.add_edge(2, 6) g.add_edge(4, 7) g.add_edge(6, 8) g.add_edge(6, 9) g.add_edge(9, 10) return g
def graph_1(): """Making one empty graph instance per test.""" return Graph()