コード例 #1
0
    def test_draw_graph(self, det_col):
        """ Ensures a graph is drawn with the corredct items. """
        gui = Visualizer(None)
        fake_canvas = Mock()
        fake_canvas.create_oval = Mock()
        fake_canvas.create_line = Mock()
        fake_canvas.winfo_width = Mock(return_value=5)
        fake_canvas.winfo_height = Mock(return_value=5)
        gui._builder = Mock()
        gui._builder.get_object = Mock()
        gui._builder.get_object.return_value = fake_canvas

        # Test vertice drawing
        g = Graph() # Setup graph
        vertex_one = Vertex(0)
        g.add_vertex(vertex_one)
        g.add_vertex(Vertex(1))
        g.add_vertex(Vertex(2))
        gui._draw_graph(g)  # Try drawing
        assert fake_canvas.create_oval.call_count == 3

        # Test edge drawing
        edge = Edge(0, vertex_one, vertex_one, colour=5)   # Setup (Continued from above)
        g.add_edge(edge)
        gui._draw_graph(g)   # Try drawing
        det_col.assert_called_with(5)
        assert fake_canvas.create_line.call_count == 1
コード例 #2
0
 def test_add_vertex(self):
     """ Ensures the safety of the adding of vertices. """
     g = Graph()
     a_vertex = Vertex(0)
     g.add_vertex(a_vertex) # Try adding a vertex
     assert g._vertices[0] is a_vertex
     not_vertex = Edge(0, a_vertex, a_vertex)
     try:
         g.add_vertex(not_vertex)   # Expect an error if its not a Vertex
         assert False
     except TypeError:
         assert True
コード例 #3
0
 def test_remove_edge(self):
     """ Ensures edges can be removed correctly. """
     vertex_one = Vertex(0)  # Create some components
     vertex_two = Vertex(1)
     edge_one = Edge(0, vertex_one, vertex_two)
     edge_two = Edge(0, vertex_one, vertex_two)
     g = Graph()
     g.add_vertex(vertex_one)
     g.add_vertex(vertex_two)
     g.add_edge(edge_one)
     g.add_edge(edge_two)
     g.remove_edge(edge_one)
     assert edge_one not in g.get_edges()
コード例 #4
0
 def test_remove_vertex(self, rm_edge):
     """ Ensures a vertex and its corresponding edges are removed. """
     vertex_one = Vertex(0)  # Create some components
     vertex_two = Vertex(1)
     edge_one = Edge(0, vertex_one, vertex_two)
     g = Graph() # Generate graph and add components
     g.add_vertex(vertex_one)
     g.add_vertex(vertex_two)
     g.add_edge(edge_one)
     g.remove_vertex(vertex_one) # Remove the vertex
     assert vertex_one not in g.get_vertices()
     assert vertex_two in g.get_vertices()
     assert rm_edge.called_with(edge_one)    # Ensure remove edge was called
コード例 #5
0
 def test_clear_edges(self):
     """ Ensures all edges are removed. """
     vertex_one = Vertex(0)  # Create some components
     vertex_two = Vertex(1)
     edge_one = Edge(0, vertex_one, vertex_two)
     edge_two = Edge(0, vertex_one, vertex_two)
     g = Graph()             # Create graph
     g.add_vertex(vertex_one)
     g.add_vertex(vertex_two)
     g.add_edge(edge_one)
     g.add_edge(edge_two)
     g.clear_edges() # Clear edges
     assert edge_one not in g.get_edges()
     assert edge_two not in g.get_edges()
コード例 #6
0
 def test_get_vertex_edges(self):
     """ Ensures the graph can return only edges connected to a distinct vertex. """
     vertex_one = Vertex(0)
     vertex_other = Vertex(1)
     edge_one = Edge(0, vertex_one, vertex_other)
     edge_two = Edge(0, vertex_other, vertex_other)
     edge_three = Edge(0, vertex_other, vertex_one)
     g = Graph() # Generate graph with relevant edges
     g.add_vertex(vertex_one)
     g.add_vertex(vertex_other)
     g.add_edge(edge_one)
     g.add_edge(edge_two)
     g.add_edge(edge_three)
     edges = g.get_vertex_edges(vertex_one)  # Try to get edges connecting to vertex_one
     assert (edge_one and edge_three) in edges
     assert edge_two not in edges