예제 #1
0
def test_docnetdb_search_edge_parameters(tmp_path):
    """Test if the DocNetDB search_edge parameters are working."""
    db = DocNetDB(tmp_path / "db.db")
    v1, v2, v3 = Vertex(), Vertex(), Vertex()
    for vertex in v1, v2, v3:
        db.insert(vertex)

    db.insert_edge(Edge(v1, v2, label="name", has_direction=False))
    db.insert_edge(Edge(v2, v3, has_direction=True))

    assert list(db.search_edge(v2, direction="all")) == [
        Edge.from_anchor(anchor=v2, other=v1, label="name", direction="none"),
        Edge.from_anchor(anchor=v2, other=v3, label="", direction="out"),
    ]
    assert list(db.search_edge(v2, direction="none")) == [
        Edge.from_anchor(anchor=v2, other=v1, label="name", direction="none")
    ]
    assert list(db.search_edge(v2, direction="in")) == []
    assert list(db.search_edge(v2, direction="out")) == [
        Edge.from_anchor(anchor=v2, other=v3, label="", direction="out")
    ]

    assert list(db.search_edge(v2, label="name")) == [
        Edge.from_anchor(anchor=v2, other=v1, label="name", direction="none")
    ]
    assert list(db.search_edge(v2, label="")) == [
        Edge.from_anchor(anchor=v2, other=v3, label="", direction="out")
    ]

    assert list(db.search_edge(v2, label="another_name")) == []

    assert list(db.search_edge(v3, direction="in")) == [
        Edge.from_anchor(anchor=v3, other=v2, label="", direction="in")
    ]
    assert list(db.search_edge(v3, direction="out")) == []
예제 #2
0
def test_docnetdb_remove_with_edges(tmp_path):
    """Test if the DocNetDB remove fails with vertices connected to edges."""
    db = DocNetDB(tmp_path / "db.db")
    v1, v2 = Vertex(), Vertex()
    db.insert(v1)
    db.insert(v2)
    db.insert_edge(Edge(v1, v2))
    with pytest.raises(ValueError):
        db.remove(v1)
예제 #3
0
def test_docnetdb_load_edges(tmp_path):
    """Test if the DocNetDB load restores all the edges in the object."""
    path = tmp_path / "db.db"
    db1 = DocNetDB(path)
    v1, v2, v3 = Vertex(), Vertex(), Vertex()
    db1.insert(v1)
    db1.insert(v2)
    db1.insert(v3)
    db1.insert_edge(Edge(v1, v2, "edge1", False))
    db1.insert_edge(Edge(v3, v2, "edge2", True))
    db1.save()

    db2 = DocNetDB(path)
    assert list(db2.search_edge(db2[2])) == [
        Edge(db2[1], db2[2], "edge1", False),
        Edge(db2[3], db2[2], "edge2", True),
    ]
예제 #4
0
def test_docnetdb_load_no_duplication(tmp_path):
    """Test if calling DocNetDB load two times doesn't duplicate anything.

    Like the vertices or the edges.
    """
    path = tmp_path / "db.db"
    db1 = DocNetDB(path)
    v1, v2 = Vertex(), Vertex()
    db1.insert(v1)
    db1.insert(v2)
    db1.insert_edge(Edge(v1, v2, "my_edge", True))
    db1.save()

    # The file is loaded into the same database.
    db1.load()

    assert len(db1) == 2
    assert len(list(db1.search_edge(db1[1]))) == 1
예제 #5
0
def test_docnetdb_edges(tmp_path):
    """Test if the DocNetDB edges method returns all the contained edges.

    In an Iterable.
    """
    db = DocNetDB(tmp_path / "db.db")
    db.insert(Vertex())
    db.insert(Vertex())
    db.insert(Vertex())
    db.insert_edge(Edge(db[1], db[2]))
    db.insert_edge(Edge(db[2], db[3]))

    edges = db.edges()

    # Test if the type is right
    assert isinstance(edges, Iterator) is True

    # Test if the content is right
    assert list(edges) == [Edge(db[1], db[2]), Edge(db[2], db[3])]
예제 #6
0
def test_docnetdb_search_edge(tmp_path):
    """Test if the DocNetDB search_edge returns the right edges."""
    db = DocNetDB(tmp_path / "db.db")
    v1, v2, v3 = Vertex(), Vertex(), Vertex()
    for vertex in v1, v2, v3:
        db.insert(vertex)

    db.insert_edge(Edge(v1, v2, label="name", has_direction=False))
    db.insert_edge(Edge(v2, v3, has_direction=True))

    assert list(db.search_edge(v1)) == [
        Edge.from_anchor(anchor=v1, other=v2, label="name", direction="none")
    ]
    assert list(db.search_edge(v2)) == [
        Edge.from_anchor(anchor=v2, other=v1, label="name", direction="none"),
        Edge.from_anchor(anchor=v2, other=v3, label="", direction="out"),
    ]
    assert list(db.search_edge(v3)) == [
        Edge.from_anchor(anchor=v3, other=v2, label="", direction="in")
    ]
예제 #7
0
def test_docnetdb_remove_edge(tmp_path):
    """Test if the DocNetDB remove_edge removes one corresponding edge.

    And only one.
    """
    db = DocNetDB(tmp_path / "db.db")
    v1, v2, v3 = Vertex(), Vertex(), Vertex()
    for vertex in v1, v2, v3:
        db.insert(vertex)

    db.insert_edge(Edge(v1, v2, label="name", has_direction=False))
    db.insert_edge(Edge(v1, v2, label="name", has_direction=False))
    db.insert_edge(Edge(v2, v3, has_direction=True))

    db.remove_edge(Edge(v1, v2, label="name", has_direction=False))
    db.remove_edge(Edge(v2, v3, label="", has_direction=True))

    assert list(db.search_edge(v2)) == [
        Edge.from_anchor(anchor=v2, other=v1, label="name", direction="none")
    ]
    assert list(db.search_edge(v3)) == []
예제 #8
0
def test_docnetdb_insert_edge_exception(tmp_path):
    """Test if the DocNetDB insert_edge raises exceptions.

    When vertices are not inserted in the database.
    """
    db = DocNetDB(tmp_path / "db.db")
    wrong_db = DocNetDB(tmp_path / "Wrong.db")
    v1, v2 = Vertex(), Vertex()
    wrong_db.insert(v1)
    wrong_db.insert(v2)

    new_edge = Edge(v1, v2, label="", has_direction=False)

    with pytest.raises(VertexInsertionException):
        db.insert_edge(new_edge)

    wrong_db.remove(v1)
    wrong_db.remove(v2)
    db.insert(v1)
    db.insert(v2)

    db.insert_edge(new_edge)