Exemplo n.º 1
0
    def test_replace_connection_valid_replacements(self):
        edge = graph.Edge()
        vertex1 = graph.Vertex()
        vertex2 = graph.Vertex()
        vertex3 = graph.Vertex()
        edge.vertex1 = vertex1
        edge.vertex2 = vertex2

        def func(x):
            if x == vertex1:
                return vertex3
            else:
                return None
        edge.replace_connection(func)
        assert edge.vertex1 == vertex3
        assert edge.vertex2 == vertex2
Exemplo n.º 2
0
    def test_replace_connection_invalid(self):
        edge = graph.Edge()
        vertex1 = graph.Vertex()
        vertex2 = graph.Vertex()
        edge.vertex1 = vertex1
        edge.vertex2 = vertex2

        def func(_):
            return ''
        with pytest.raises(ValueError):
            edge.replace_connection(func)

        def func(_):
            return graph.Edge()
        with pytest.raises(ValueError):
            edge.replace_connection(func)
Exemplo n.º 3
0
 def test_matches_wrong_arg(self):
     """
     If called with a non GraphElement argument then an error is to be
     thrown.
     """
     v = graph.Vertex()
     with pytest.raises(ModelGenArgumentError):
         v.matches('')
Exemplo n.º 4
0
 def test_deepcopy_with_connections(self):
     """
     Connections to other graph elements should be left untouched by
     the deepcopy.
     """
     mapping = {}
     vertex1 = graph.Vertex()
     vertex2 = graph.Vertex()
     vertex3 = graph.Vertex()
     memodict = {id(vertex1): vertex3}
     edge = graph.Edge(vertex1, vertex2)
     copy = edge.__deepcopy__(memodict, mapping)
     assert id(copy) != id(edge)
     assert id(copy.vertex1) == id(edge.vertex1)
     assert id(copy.vertex2) == id(copy.vertex2)
     assert memodict[id(edge)] == copy
     assert len(mapping) == 1
     assert mapping[edge] == copy
Exemplo n.º 5
0
    def test_recursive_copy(self):
        """
        References to other graph elements should lead to copies of
        these elements being made and then referenced in the new object.

        A link betwenn the old and new references should be saved in the
        mapping dict, if such is provided.
        """
        mapping = {}
        vertex1 = graph.Vertex()
        vertex2 = graph.Vertex()
        edge = graph.Edge(vertex1, vertex2)
        copy = edge.recursive_copy(mapping)
        assert id(copy) != id(edge)
        assert id(copy.vertex1) != id(edge.vertex1)
        assert id(copy.vertex2) != id(edge.vertex2)
        assert mapping[edge] == copy
        assert mapping[vertex1] == copy.vertex1
        assert mapping[vertex2] == copy.vertex2
        assert len(mapping) == 3
Exemplo n.º 6
0
 def test_deepcopy_with_edges(self):
     """
     Makes a copy of the Vertex where connections remain unchanged,
     i.e. uncopied, even if it exists in the memodict.
     """
     v = graph.Vertex()
     v2 = graph.Vertex()
     e = graph.Edge(v, v2)
     e2 = graph.Edge(v, v2)
     v.edges.add(e)
     memodict = {id(e): e2}
     mapping = {}
     assert e in v.edges
     assert e2 not in v.edges
     copy = v.__deepcopy__(memodict, mapping)
     assert e in copy.edges
     assert len(copy.edges) == 1
     memodict = {}
     copy = v.__deepcopy__(memodict, mapping)
     assert e in copy.edges
     assert len(copy.edges) == 1
Exemplo n.º 7
0
 def test_from_yaml_already_created(self):
     """
     If the Vertex object has already been created (it being present
     in mapping) then that existing object should be returned and no
     new instance created.
     """
     vertex = graph.Vertex()
     data = {'id': 1,
             'attr': {'a': 1, 3: 'test'}}
     mapping = {1: vertex}
     result = graph.Vertex.from_yaml(data, mapping)
     assert id(vertex) == id(result)
     assert len(mapping) == 1
Exemplo n.º 8
0
    def test_deepcopy_no_edges(self):
        """
        A deepcopy of a Vertex should make a copy of the vertex and save the
        old id to new object mapping into the memodict and if the mapping
        argument is supplied also save a mapping from old object to new object
        in the provided dictionary.


        """
        memodict = {}
        mapping = {}
        v = graph.Vertex()
        copy = v.__deepcopy__(memodict, mapping)
        assert id(v) != id(copy)
        assert memodict[id(v)] == copy
        assert mapping[v] == copy
Exemplo n.º 9
0
    def test_replace_connection_invalid_func(self):
        """
        If the replacement function does not return a object of a fitting
        class, e.g. returning a Vertex instead of an edge, then an error
        is to be thrown.
        """
        v = graph.Vertex()
        e = graph.Edge()
        v.edges = {e}

        def func(_):
            return ''
        with pytest.raises(ValueError):
            v.replace_connection(func)

        def func2(_):
            return graph.Vertex()
        with pytest.raises(ValueError):
            v.replace_connection(func2)
Exemplo n.º 10
0
    def test_recursive_copy(self):
        """
        Recursive copy should make a copy of all referenced elements
        and reference those in the copy of the original vertex.

        A mapping between the original referenced element and its copy
        shall be saved in a dict if it is provided as an argument.
        """
        mapping = {}
        vertex = graph.Vertex()
        edge = graph.Edge(vertex, None)
        vertex.edges.add(edge)
        copy = vertex.recursive_copy(mapping)
        copy_edge = copy.edges.pop()
        assert id(copy) != id(vertex)
        assert id(copy_edge) != id(edge)
        assert mapping[vertex] == copy
        assert mapping[edge] == copy_edge
        assert len(mapping) == 2
Exemplo n.º 11
0
    def test_replace_connection_valid_calls(self):
        """
        If the replacement function returns an element it is to be a
        replacement for the connected element, if the function returns None
        then no change is to be made.
        """
        v = graph.Vertex()
        e1 = graph.Edge()
        e2 = graph.Edge()
        v.edges = {e1}

        def func(x):
            if x == e1:
                return e2
            else:
                return None
        v.replace_connection(func)
        assert v.edges == {e2}
        v.replace_connection(func)
        assert v.edges == {e2}
Exemplo n.º 12
0
 def test_matches_vertex(self):
     edge = graph.Edge()
     vertex = graph.Vertex()
     edge.attr = {'a': 1}
     vertex.attr = {'a': 1}
     assert edge.matches(vertex) is False
Exemplo n.º 13
0
 def func2(_):
     return graph.Vertex()
Exemplo n.º 14
0
 def test_matches_vertex(self, attr1, attr2, result):
     v = graph.Vertex()
     v.attr = attr1
     v2 = graph.Vertex()
     v2.attr = attr2
     assert v.matches(v2) is result
Exemplo n.º 15
0
 def test_matches_non_vertex(self):
     v = graph.Vertex()
     v2 = graph.Vertex()
     e = graph.Edge(v, v2)
     assert v.matches(e) is False