def test_find_common_reachable_vertex():
    g = DirectedGraph()
    v0 = g.add_vertex(vertex_props={'license': 'L0', 'type': 'P'})
    v1 = g.add_vertex(vertex_props={'license': 'L1', 'type': 'WP'})

    g.add_edge(from_id=v0.id, to_id=v1.id)

    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

    list_vertices = DirectedGraph.find_common_reachable_vertices(
        input_vertices=[v0])
    assert list_vertices is not None
    assert len(list_vertices) == 2
    assert list_vertices[0] == v0
    assert list_vertices[1] == v1

    list_vertices = DirectedGraph.find_common_reachable_vertices(
        input_vertices=[v0, v1])
    assert list_vertices is not None
    assert len(list_vertices) == 1
    assert list_vertices[0] == v1

    v2 = g.add_vertex(vertex_props={'license': 'L2', 'type': 'SP'})
    g.add_edge(from_id=v2.id, to_id=v1.id)
    list_vertices = DirectedGraph.find_common_reachable_vertices(
        input_vertices=[v0, v2])
    assert list_vertices is not None
    assert len(list_vertices) == 1
    assert list_vertices[0] == v1
Ejemplo n.º 2
0
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)
Ejemplo n.º 3
0
def test_add_cycle_to_the_same_vertex():
    """Test whether method DirectedGraph.add_edge() works correctly."""
    g = DirectedGraph()
    v0 = g.add_vertex(vertex_props=None)

    # create edge from and to the same vertex
    g.add_edge(from_id=v0.id, to_id=v0.id)
    v0_neighbours = v0.get_neighbours()
    # only one neighbour
    assert len(v0_neighbours) == 1
    # neighbour has id = 1
    assert v0_neighbours[0].id == 0
def test_add_edge():
    g = DirectedGraph()
    v0 = g.add_vertex(vertex_props=None)
    v1 = g.add_vertex(vertex_props=None)

    g.add_edge(from_id=v0.id, to_id=v1.id)
    v0_neighbours = v0.get_neighbours()
    # only one neighbour
    assert len(v0_neighbours) == 1
    # neighbour has id = 1
    assert v0_neighbours[0].id == 1

    v1_neighbours = v1.get_neighbours()
    # no neighbour of v1 as it is directed graph
    assert len(v1_neighbours) == 0
Ejemplo n.º 5
0
def test_add_cycle():
    """Test whether method DirectedGraph.add_edge() works correctly."""
    g = DirectedGraph()
    v0 = g.add_vertex(vertex_props=None)
    v1 = g.add_vertex(vertex_props=None)

    g.add_edge(from_id=v0.id, to_id=v1.id)
    g.add_edge(from_id=v1.id, to_id=v0.id)
    v0_neighbours = v0.get_neighbours()
    # only one neighbour
    assert len(v0_neighbours) == 1
    # neighbour has id = 1
    assert v0_neighbours[0].id == 1

    v1_neighbours = v1.get_neighbours()
    # no neighbour of v1 as it is directed graph
    assert len(v1_neighbours) == 1
    # neighbour has id = 0
    assert v1_neighbours[0].id == 0
    def _create_graph():
        g = DirectedGraph()
        pd = g.add_vertex(vertex_props={'license': 'public domain', 'type': 'P'})
        mit = g.add_vertex(vertex_props={'license': 'mit', 'type': 'P'})
        bsd = g.add_vertex(vertex_props={'license': 'bsd-new', 'type': 'P'})
        apache = g.add_vertex(vertex_props={'license': 'apache 2.0', 'type': 'P'})
        lgpl2 = g.add_vertex(vertex_props={'license': 'lgplv2.1', 'type': 'WP'})
        lgpl22 = g.add_vertex(vertex_props={'license': 'lgplv2.1+', 'type': 'WP'})
        lgpl3 = g.add_vertex(vertex_props={'license': 'lgplv3+', 'type': 'WP'})
        mpl = g.add_vertex(vertex_props={'license': 'mpl 1.1', 'type': 'WP'})
        gpl2 = g.add_vertex(vertex_props={'license': 'gplv2', 'type': 'SP'})
        gpl22 = g.add_vertex(vertex_props={'license': 'gplv2+', 'type': 'SP'})
        gpl3 = g.add_vertex(vertex_props={'license': 'gplv3+', 'type': 'SP'})
        agpl3 = g.add_vertex(vertex_props={'license': 'agplv3', 'type': 'NP'})

        g.add_edge(pd.id, mit.id)
        g.add_edge(mit.id, bsd.id)
        g.add_edge(bsd.id, apache.id)
        g.add_edge(bsd.id, mpl.id)
        g.add_edge(bsd.id, lgpl2.id)
        g.add_edge(bsd.id, lgpl22.id)
        g.add_edge(bsd.id, lgpl3.id)
        g.add_edge(apache.id, lgpl3.id)
        g.add_edge(lgpl22.id, lgpl2.id)
        g.add_edge(lgpl22.id, lgpl3.id)
        g.add_edge(lgpl2.id, gpl2.id)
        g.add_edge(lgpl2.id, gpl22.id)
        g.add_edge(lgpl22.id, gpl22.id)
        g.add_edge(lgpl3.id, gpl3.id)
        g.add_edge(gpl22.id, gpl2.id)
        g.add_edge(gpl22.id, gpl3.id)
        g.add_edge(gpl3.id, agpl3.id)

        return g