def test_edge_from_pack(db_3_vertices): """Test if the from_pack method generate the same edges that the init().""" db, v1, v2, v3 = db_3_vertices pack1 = (2, 3, "", True) assert Edge.from_pack(pack1, db) == Edge(v2, v3) pack2 = (1, 3, "edge", False) assert Edge.from_pack(pack2, db) == Edge(v3, v1, "edge", False)
def test_edge_change_anchor_valuerror(db_3_vertices): """Test if the change_anchor method raises an exception. When the given anchor doesn't belong the the edge. """ db, v1, v2, v3 = db_3_vertices e1 = Edge(v1, v2) with pytest.raises(ValueError): e1.change_anchor(v3)
def test_edge_init_no_direction(db_3_vertices): """Test if vertices are sorted by place when the edge is not oriented.""" db, v1, v2, v3 = db_3_vertices e1 = Edge(v1, v2, has_direction=False) assert e1.start is v1 assert e1.end is v2 e2 = Edge(v3, v2, has_direction=False) assert e2.start is v2 assert e2.end is v3
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")) == []
def test_edge_init(db_3_vertices): """Test if the init works and use the correct defaults.""" db, v1, v2, v3 = db_3_vertices e1 = Edge(v1, v2) assert e1.label == "" assert e1.has_direction is True assert e1.is_inserted is False
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), ]
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)
def test_edge_from_anchor_valueerror(db_3_vertices): """Test if the from_anchor mathod raises an exception. With incorrect direction. """ db, v1, v2, v3 = db_3_vertices with pytest.raises(ValueError): e1 = Edge.from_anchor(v1, v2, direction="incorrect") del e1
def test_edge_has_vertex(db_3_vertices): """Test if the has_vertex method works.""" db, v1, v2, v3 = db_3_vertices e1 = Edge(v1, v2) assert e1.has_vertex(v1) is True assert e1.has_vertex(v2) is True assert e1.has_vertex(v3) is False e2 = Edge(v1, v3) assert e2.has_vertex(v1) is True assert e2.has_vertex(v2) is False assert e2.has_vertex(v3) is True
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])]
def test_edge_pack(db_3_vertices): """Test if the pack method return the correct pack.""" db, v1, v2, v3 = db_3_vertices e1 = Edge(v2, v3, label="edge", has_direction=True) assert e1.pack() == (2, 3, "edge", True) e2 = Edge(v2, v1, has_direction=False) assert e2.pack() == (1, 2, "", False)
def test_edge_properties_read_only(db_3_vertices): """Test if modifiying a read-only property raises an exception.""" db, v1, v2, v3 = db_3_vertices edge = Edge(v1, v2) def wrapper(prop_name): """Test if it raises an AttributeError.""" with pytest.raises(AttributeError): setattr(edge, prop_name, 1) wrapper("start") wrapper("end") wrapper("label") wrapper("has_direction") wrapper("anchor") wrapper("other") wrapper("direction")
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
def test_edge_from_anchor(db_3_vertices): """Test if the from_anchor method works works properly.""" db, v1, v2, v3 = db_3_vertices # Test if the defaults are the same as in the __init__. e1 = Edge.from_anchor(v1, v2) assert e1 == Edge(v1, v2) # Test if it works for "none" direction. e2 = Edge.from_anchor(v3, v2, direction="none") assert e2 == Edge(v2, v3, has_direction=False) # Test if it works for 'in" direction. e3 = Edge.from_anchor(v3, v2, direction="in") assert e3 == Edge(v2, v3)
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)
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") ]
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)) == []
def test_edge_change_anchor(db_3_vertices): """Test if the change_anchor method changes the right attributes.""" db, v1, v2, v3 = db_3_vertices e1 = Edge(v1, v2) e1.change_anchor(v1) assert e1.anchor is v1 assert e1.other is v2 assert e1.direction == "out" e1.change_anchor(v2) assert e1.anchor is v2 assert e1.other is v1 assert e1.direction == "in" e2 = Edge(v2, v3, has_direction=False) e2.change_anchor(v2) assert e2.anchor is v2 assert e2.other is v3 assert e2.direction == "none" e2.change_anchor(v3) assert e2.anchor is v3 assert e2.other is v2 assert e2.direction == "none"
def test_edge_init_vertexinsertionexception(): """Test if the Edge init with non-inserted vertices raises an exception.""" v1, v2 = Vertex(), Vertex() with pytest.raises(VertexInsertionException): edge = Edge(v1, v2) del edge