Exemplo n.º 1
0
def test_get_vertex():
    """Check if vertex can be retrieved from directed graph."""
    g = DirectedGraph()
    g.add_vertex(vertex_props=None)
    v = g.get_vertex(vertex_id=0)
    assert v is not None
    assert v.id == 0
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
Exemplo n.º 3
0
def test_num_vertices_counter():
    """Check the num_vertices counter."""
    g = DirectedGraph()
    assert g.num_vertices == 0
    g.add_vertex(vertex_props=None)
    assert g.num_vertices == 1
    g.add_vertex(vertex_props=None)
    assert g.num_vertices == 2
def test_find_vertex():
    g = DirectedGraph()
    v0 = g.add_vertex(vertex_props={'license': 'L0', 'type': 'P'})
    v1 = g.add_vertex(vertex_props={'license': 'L1', 'type': 'WP'})

    v = g.find_vertex(prop_name='license', prop_value='L1')
    assert v != v0
    assert v == v1

    v = g.find_vertex(prop_name='license', prop_value='L2')
    assert v is None
def test_find_vertex():
    """Test if method DirectedGraph.find_vertex() works correctly."""
    g = DirectedGraph()
    v0 = g.add_vertex(vertex_props={'license': 'L0', 'type': 'P'})
    v1 = g.add_vertex(vertex_props={'license': 'L1', 'type': 'WP'})

    v = g.find_vertex(prop_name='license', prop_value='L1')
    assert v != v0
    assert v == v1

    v = g.find_vertex(prop_name='license', prop_value='L2')
    assert v is None
Exemplo n.º 6
0
def test_get_vertices_graph_with_one_vertex():
    """Test whether method DirectedGraph.get_vertices() works correctly."""
    g = DirectedGraph()
    v0 = g.add_vertex(vertex_props=None)

    list_vertices = g.get_vertices()
    assert len(list_vertices) == 1
    assert v0 in list_vertices
Exemplo n.º 7
0
def test_get_vertex_ids_graph_with_one_vertex():
    """Test whether method DirectedGraph.get_vertex_ids() works correctly for graph with vertex."""
    g = DirectedGraph()
    v0 = g.add_vertex(vertex_props=None)

    list_ids = g.get_vertex_ids()
    assert len(list_ids) == 1
    assert v0.id in list_ids
def test_get_vertex_ids():
    g = DirectedGraph()
    v0 = g.add_vertex(vertex_props=None)
    v1 = g.add_vertex(vertex_props=None)

    list_ids = g.get_vertex_ids()
    assert len(list_ids) == 2
    assert v0.id in list_ids
    assert v1.id in list_ids
def test_get_vertices():
    g = DirectedGraph()
    v0 = g.add_vertex(vertex_props=None)
    v1 = g.add_vertex(vertex_props=None)

    list_vertices = g.get_vertices()
    assert len(list_vertices) == 2
    assert v0 in list_vertices
    assert v1 in list_vertices
def test_get_vertex_ids():
    """Test whether method DirectedGraph.get_vertex_ids() works correctly."""
    g = DirectedGraph()
    v0 = g.add_vertex(vertex_props=None)
    v1 = g.add_vertex(vertex_props=None)

    list_ids = g.get_vertex_ids()
    assert len(list_ids) == 2
    assert v0.id in list_ids
    assert v1.id in list_ids
Exemplo n.º 11
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
Exemplo n.º 12
0
def test_find_vertex_graph_with_one_vertex():
    """Test if method DirectedGraph.find_vertex() works correctly."""
    g = DirectedGraph()
    v0 = g.add_vertex(vertex_props={'license': 'L0', 'type': 'P'})

    v = g.find_vertex(prop_name='license', prop_value='L0')
    assert v is not None
    assert v == v0

    v = g.find_vertex(prop_name='license', prop_value='something_else')
    assert v is None

    with pytest.raises(Exception):
        v = g.find_vertex(prop_name='unknown_property', prop_value='L0')
Exemplo n.º 13
0
def test_iterator():
    """Test that the iterator over all vertexes works correctly."""
    g = DirectedGraph()
    items = [item for item in g]
    assert not items

    v0 = g.add_vertex(vertex_props={'license': 'L0', 'type': 'P'})
    items = [item for item in g]
    assert len(items) == 1
    assert items[0] == v0

    v1 = g.add_vertex(vertex_props={'license': 'L1', 'type': 'WP'})
    items = [item for item in g]
    assert len(items) == 2
    assert v0 in items
    assert v1 in items

    v2 = g.add_vertex(vertex_props={})
    items = [item for item in g]
    assert len(items) == 3
    assert v0 in items
    assert v1 in items
    assert v2 in items
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
Exemplo n.º 15
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 test_add_vertex():
    g = DirectedGraph()
    v0 = g.add_vertex(vertex_props=None)
    assert v0.id >= 0
Exemplo n.º 17
0
def test_get_nonexistent_vertex():
    """Check if non existent vertex can not be retrieved from directed graph."""
    g = DirectedGraph()
    g.add_vertex(vertex_props=None)
    v = g.get_vertex(vertex_id=42)
    assert v is None
def test_get_vertex():
    g = DirectedGraph()
    g.add_vertex(vertex_props=None)
    v = g.get_vertex(vertex_id=0)
    assert v is not None
    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
def test_add_vertex():
    """Check that new vertex can be added to directed graph."""
    g = DirectedGraph()
    v0 = g.add_vertex(vertex_props=None)
    assert v0.id >= 0