예제 #1
0
 def test__edges__then_all_edges(self):
     # when
     result = self.test_object.edges
     # then
     assert_that(sorted(result)).is_equal_to(
         [Edge(Vertex(0), Vertex(3)), Edge(Vertex(1), Vertex(5)), Edge(Vertex(2), Vertex(9)),
          Edge(Vertex(4), Vertex(6)), Edge(Vertex(7), Vertex(9))])
예제 #2
0
 def test__adjacent_edges__then_outgoing_edges(self):
     # when
     result = self.test_object.adjacent_edges(Vertex(1))
     # then
     assert_that(sorted(result)).is_equal_to([
         Edge(Vertex(1), Vertex(0)),
         Edge(Vertex(4), Vertex(1)),
         Edge(Vertex(5), Vertex(1))
     ])
    def test__properties_getitem__when_not_existing__then_value_error(self):
        # when
        def function(item):
            return self.test_object.properties[item]

        # then
        assert_that(function).raises(ValueError).when_called_with(Vertex(14))
        assert_that(function).raises(ValueError).when_called_with(
            Edge(Vertex(2), Vertex(8)))
        assert_that(function).raises(ValueError).when_called_with(
            Edge(Vertex(0), Vertex(-1)))
 def test__adjacent_edges__then_outgoing_edges(self):
     # given
     self.test_object.add_edge_between(Vertex(1), Vertex(1))
     self.test_object.add_edge_between(Vertex(1), Vertex(3))
     self.test_object.add_edge_between(Vertex(1), Vertex(4))
     self.test_object.add_edge_between(Vertex(1), Vertex(7))
     self.test_object.add_edge_between(Vertex(1), Vertex(9))
     self.test_object.add_edge_between(Vertex(2), Vertex(1))
     self.test_object.add_edge_between(Vertex(6), Vertex(1))
     # when
     result = self.test_object.adjacent_edges(Vertex(1))
     # then
     assert_that(sorted(result)).is_equal_to([
         Edge(Vertex(1), Vertex(1)),
         Edge(Vertex(1), Vertex(3)),
         Edge(Vertex(1), Vertex(4)),
         Edge(Vertex(1), Vertex(7)),
         Edge(Vertex(1), Vertex(9))
     ])
 def test__reversed_copy__then_new_graph_with_reversed_edges(self):
     # given
     vertex = Vertex(5)
     vertex_property = "123456"
     edge_property = "zxcvb"
     edge = self.test_object.add_edge_between(Vertex(1), Vertex(2))
     self.test_object.add_edge_between(Vertex(3), Vertex(5))
     self.test_object.add_edge_between(Vertex(4), Vertex(9))
     self.test_object.add_edge_between(Vertex(5), Vertex(4))
     self.test_object.add_edge_between(Vertex(5), Vertex(7))
     self.test_object.add_edge_between(Vertex(6), Vertex(2))
     self.test_object.add_edge_between(Vertex(6), Vertex(6))
     self.test_object.add_edge_between(Vertex(7), Vertex(8))
     self.test_object.add_edge_between(Vertex(9), Vertex(1))
     self.test_object.add_edge_between(Vertex(9), Vertex(6))
     self.test_object.properties[vertex] = vertex_property
     self.test_object.properties[edge] = edge_property
     # when
     result = self.test_object.reversed_copy()
     # then
     assert_that(sorted(result.vertices)).is_equal_to(
         sorted(self.test_object.vertices))
     assert_that(sorted(result.edges)).is_equal_to([
         Edge(Vertex(1), Vertex(9)),
         Edge(Vertex(2), Vertex(1)),
         Edge(Vertex(2), Vertex(6)),
         Edge(Vertex(4), Vertex(5)),
         Edge(Vertex(5), Vertex(3)),
         Edge(Vertex(6), Vertex(6)),
         Edge(Vertex(6), Vertex(9)),
         Edge(Vertex(7), Vertex(5)),
         Edge(Vertex(8), Vertex(7)),
         Edge(Vertex(9), Vertex(4))
     ])
     assert_that(result.properties[vertex]).is_equal_to(vertex_property)
     assert_that(result.properties[Vertex(9)]).is_none()
     assert_that(result.properties[result.get_edge(
         2, 1)]).is_equal_to(edge_property)
     assert_that(result.properties[result.get_edge(5, 3)]).is_none()
 def test__reverse__then_all_edges_have_reversed_direction(self):
     # given
     vertex = Vertex(5)
     vertex_property = "123456"
     edge_property = "zxcvb"
     edge = self.test_object.add_edge_between(Vertex(1), Vertex(2))
     self.test_object.add_edge_between(Vertex(3), Vertex(5))
     self.test_object.add_edge_between(Vertex(4), Vertex(9))
     self.test_object.add_edge_between(Vertex(5), Vertex(4))
     self.test_object.add_edge_between(Vertex(5), Vertex(7))
     self.test_object.add_edge_between(Vertex(6), Vertex(2))
     self.test_object.add_edge_between(Vertex(6), Vertex(6))
     self.test_object.add_edge_between(Vertex(7), Vertex(8))
     self.test_object.add_edge_between(Vertex(9), Vertex(1))
     self.test_object.add_edge_between(Vertex(9), Vertex(6))
     self.test_object.properties[vertex] = vertex_property
     self.test_object.properties[edge] = edge_property
     # when
     self.test_object.reverse()
     # then
     assert_that(sorted(self.test_object.edges)).is_equal_to([
         Edge(Vertex(1), Vertex(9)),
         Edge(Vertex(2), Vertex(1)),
         Edge(Vertex(2), Vertex(6)),
         Edge(Vertex(4), Vertex(5)),
         Edge(Vertex(5), Vertex(3)),
         Edge(Vertex(6), Vertex(6)),
         Edge(Vertex(6), Vertex(9)),
         Edge(Vertex(7), Vertex(5)),
         Edge(Vertex(8), Vertex(7)),
         Edge(Vertex(9), Vertex(4))
     ])
     assert_that(
         self.test_object.properties[vertex]).is_equal_to(vertex_property)
     assert_that(self.test_object.properties[Vertex(9)]).is_none()
     assert_that(self.test_object.properties[self.test_object.get_edge(2, 1)]) \
         .is_equal_to(edge_property)
     assert_that(self.test_object.properties[self.test_object.get_edge(
         5, 3)]).is_none()
 def test__edges__then_all_edges(self):
     # given
     self.test_object.add_edge_between(Vertex(7), Vertex(7))
     self.test_object.add_edge_between(Vertex(1), Vertex(5))
     self.test_object.add_edge_between(Vertex(2), Vertex(4))
     self.test_object.add_edge_between(Vertex(8), Vertex(0))
     self.test_object.add_edge_between(Vertex(6), Vertex(3))
     self.test_object.add_edge_between(Vertex(3), Vertex(6))
     self.test_object.add_edge_between(Vertex(9), Vertex(3))
     # when
     result = self.test_object.edges
     # then
     assert_that(sorted(result)).is_equal_to([
         Edge(Vertex(1), Vertex(5)),
         Edge(Vertex(2), Vertex(4)),
         Edge(Vertex(3), Vertex(6)),
         Edge(Vertex(6), Vertex(3)),
         Edge(Vertex(7), Vertex(7)),
         Edge(Vertex(8), Vertex(0)),
         Edge(Vertex(9), Vertex(3))
     ])
 def test__as_directed__then_directed_graph(self):
     # given
     vertex = Vertex(5)
     vertex_property = "123456"
     edge_property = "zxcvb"
     edge = self.test_object.add_edge_between(Vertex(1), Vertex(5))
     self.test_object.add_edge_between(Vertex(7), Vertex(7))
     self.test_object.add_edge_between(Vertex(2), Vertex(4))
     self.test_object.add_edge_between(Vertex(8), Vertex(0))
     self.test_object.add_edge_between(Vertex(6), Vertex(3))
     self.test_object.add_edge_between(Vertex(9), Vertex(3))
     self.test_object.properties[vertex] = vertex_property
     self.test_object.properties[edge] = edge_property
     # when
     result = self.test_object.as_directed()
     # then
     assert_that(result).is_instance_of(DirectedGraph)
     assert_that(sorted(result.vertices)).is_equal_to(
         sorted(self.test_object.vertices))
     assert_that(sorted(result.edges)).is_equal_to([
         Edge(Vertex(0), Vertex(8)),
         Edge(Vertex(1), Vertex(5)),
         Edge(Vertex(2), Vertex(4)),
         Edge(Vertex(3), Vertex(6)),
         Edge(Vertex(3), Vertex(9)),
         Edge(Vertex(4), Vertex(2)),
         Edge(Vertex(5), Vertex(1)),
         Edge(Vertex(6), Vertex(3)),
         Edge(Vertex(7), Vertex(7)),
         Edge(Vertex(8), Vertex(0)),
         Edge(Vertex(9), Vertex(3))
     ])
     assert_that(result.properties[vertex]).is_equal_to(vertex_property)
     assert_that(result.properties[Vertex(9)]).is_none()
     assert_that(result.properties[result.get_edge(
         1, 5)]).is_equal_to(edge_property)
     assert_that(result.properties[result.get_edge(
         5, 1)]).is_equal_to(edge_property)
     assert_that(result.properties[result.get_edge(8, 0)]).is_none()