def test_add_edge_between_nonexistent_vertices(): """Test whether method DirectedGraph.add_edge() works correctly.""" g = DirectedGraph() v0 = Vertex(0, {}) v1 = Vertex(1, {}) # neither vertex are put into the directed graph, so it is impossible # to add an edge to connect them. ATM the exception thrown by the # add_edge method is not very specific, it might need to be improved. with pytest.raises(Exception): g.add_edge(from_id=v0.id, to_id=v1.id)
def test_get_unvisited_neighbor__none_available(): start_v = Vertex('1') end_v = Vertex('2') start_v.connect(end_vertex=end_v) end_v.visited = True assert start_v.get_unvisited_neighbor() is None
def test_has_neighbor(): start_v = Vertex('1') assert start_v.has_neighbor() is False # Add a neighbor start_v.connect(end_vertex=Vertex('2')) assert start_v.has_neighbor()
def test_has_and_set_predecessor(): end_v = Vertex('2') assert end_v.has_predecessor() is False start_v = Vertex('1') # The predecessor end_v.set_predecessor(start_v) assert end_v.has_predecessor()
def test_add_neigbor(): """Check the methods add_neighbor and get_neighbours.""" v = Vertex(1, {}) assert not v.get_neighbours() v2 = Vertex(2, {}) v.add_neighbor(v2, 100) assert v2 in v.get_neighbours()
def test_get_weight(): """Check the method get_weight.""" v = Vertex(1, {}) v2 = Vertex(2, {}) v.add_neighbor(v2, 100) assert v.get_weight(v2) == 100 # v3 is not in any relatioship with v with pytest.raises(Exception): v3 = Vertex(3, {}) assert v.get_weight(v3) == 100
def test_find_common_reachable_vertex_empty_graph(): """Test if method DirectedGraph.find_common_reachable_vertices() works correctly.""" list_vertices = DirectedGraph.find_common_reachable_vertices(input_vertices=None) assert list_vertices is None list_vertices = DirectedGraph.find_common_reachable_vertices(input_vertices=[]) assert list_vertices is None v0 = Vertex(0, {}) list_vertices = DirectedGraph.find_common_reachable_vertices(input_vertices=[v0]) assert list_vertices is not None assert list_vertices[0] == v0
def test_get_unvisited_neighbor__valid_result(): start_v = Vertex('1') end_v = Vertex('2') start_v.connect(end_vertex=end_v) result = start_v.get_unvisited_neighbor() assert result == end_v
def test_string_representation_4(): """Check special method __str__ for vertices with properies.""" v = Vertex(1, {}) v.set_prop_value("name", "value") s = str(v) assert s == """ Vertex: 1 Neighbours: [] Properties: {'name': 'value'} """ v = Vertex(2, {}) v.set_prop_value("other", "XXX") s = str(v) assert s == """
def test_properties(): """Check methods get_prop_value and set_prop_value.""" v = Vertex(1, {}) with pytest.raises(Exception): assert v.get_prop_value("something") is None v.set_prop_value("name", "value") assert v.get_prop_value("name") == "value" with pytest.raises(Exception): # this statement still should raise an exception assert v.get_prop_value("something") is None
def test_string_representation_5(): """Check special method __str__ for vertices with neighbours.""" v = Vertex(100, {}) v2 = Vertex(2, {}) v.add_neighbor(v2, 200) s = str(v) assert s == """ Vertex: 100 Neighbours: [2] Properties: {} """ v3 = Vertex(3, {}) v.add_neighbor(v3, 300) s = str(v) assert s == """ Vertex: 100 Neighbours: [2, 3] Properties: {} """ or s == """
def test_connect__edge_vertext(): start_v = Vertex('1') end_v = Vertex('2') start_v.connect(end_vertex=end_v, edge_weight=5) edge = start_v.edgelist[0] assert edge.vertex == end_v
def connected_vertex(): start_v = Vertex('1') end_v = Vertex('2') start_v.connect(end_vertex=end_v, edge_weight=5) # Add edge return start_v
def test_connect__creates_Edge_in_edgelist(): start_v = Vertex('1') end_v = Vertex('2') start_v.connect(end_vertex=end_v, edge_weight=5) assert len(start_v.edgelist) == 1
def test_connect__default_weight_is_0(): start_v = Vertex('1') end_v = Vertex('2') start_v.connect(end_vertex=end_v) assert start_v.edgelist[0].weight == 0
def test_connect__success_returns_True(): start_v = Vertex('1') end_v = Vertex('2') assert start_v.connect(end_vertex=end_v, edge_weight=5)
def test_init__predecessor_is_None(): v = Vertex('vertex') assert v.predecessor is None
def test_init__cost_is_0(): v = Vertex('vertex') assert v.cost == 0
def test_init__prev_vertex_is_None(): v = Vertex('vertex') assert v.prev_vertex is None
def test_connect__dupe_edge_returns_False(): start_v = Vertex('1') end_v = Vertex('2') start_v.connect(end_vertex=end_v, edge_weight=5) # Add edge result = start_v.connect(end_vertex=end_v, edge_weight=5) # Add again assert result is False
def test_connect__same_vertices_returns_False(): start_v = Vertex('1') assert start_v.connect(end_vertex=start_v, edge_weight=5) is False
def test_get_cost(): end_v = Vertex('2') assert end_v.cost == 0
def test_string_representation_2(): """Check special method __str__ for simple vertex.""" v = Vertex(100, {}) s = str(v) assert s == """
def test_init__label(): v = Vertex('vertex') assert v.label == 'vertex'
def test_has_predecessor__DNE_returns_False(): end_v = Vertex('2') assert end_v.has_predecessor() is False
def test_get_predecessor(): end_v = Vertex('2') start_v = Vertex('1') # The predecessor end_v.set_predecessor(start_v) assert end_v.get_predecessor() == start_v
def test_connect__same_vertices__no_edge_added(): start_v = Vertex('1') start_v.connect(end_vertex=start_v, edge_weight=5) assert len(start_v.edgelist) == 0
def test_init__visited_is_False(): v = Vertex('vertex') assert v.visited is False
def test_set_cost(): end_v = Vertex('2') end_v.set_cost(20) assert end_v.cost == 20
def test_init__edgelist_is_empty(): v = Vertex('vertex') assert v.edgelist == []