Skip to content

Commit

Permalink
create basic unit test. add dummy nodes and edges and check nr of nod…
Browse files Browse the repository at this point in the history
…es and edges written to xml file
  • Loading branch information
LoesvdBiggelaar committed Apr 17, 2024
1 parent e93f1ee commit 6dd5590
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
6 changes: 6 additions & 0 deletions biocypher/write/graph/_rdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ def _write_single_edge_list_to_file(
g.add((self.namespaces["biocypher"][rdf_predicate], self.namespaces["biocypher"][key], Literal(v)))
except (SyntaxError, ValueError, TypeError):
g.add((self.namespaces["biocypher"][rdf_predicate], self.namespaces["biocypher"][key], Literal(value)))
else:
g.add((self.namespaces["biocypher"][rdf_predicate], self.namespaces["biocypher"][key], Literal(value)))

else:
g.add((self.namespaces["biocypher"][rdf_predicate], self.namespaces["biocypher"][key], Literal(value)))

Expand Down Expand Up @@ -288,6 +291,7 @@ def write_nodes(self, nodes, batch_size: int = int(1e6)):
if not passed:
logger.error('Error while writing node data.')
return False
return True

def write_edges(
self,
Expand Down Expand Up @@ -315,6 +319,8 @@ def write_edges(
if not passed:
logger.error('Error while writing edge data.')
return False

return True

def _construct_import_call(self) -> bool:
"""
Expand Down
21 changes: 21 additions & 0 deletions test/fixtures/rdf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import os

import pytest

from biocypher.write.graph._rdf import _RDFwriter


@pytest.fixture(scope="function")
def bw_rdf(translator, deduplicator, tmp_path_session):
bw_rdf = _RDFwriter(
translator=translator,
deduplicator=deduplicator,
output_directory=tmp_path_session,
delimiter=",",
)
bw_rdf.rdf_format = "xml"
yield bw_rdf

# teardown
for f in os.listdir(tmp_path_session):
os.remove(os.path.join(tmp_path_session, f))
75 changes: 75 additions & 0 deletions test/write/graph/test_rdf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import os
import pytest
from rdflib import Graph
from biocypher.write import _batch_writer
from biocypher.write.graph._rdf import _RDFwriter
from biocypher._create import BioCypherEdge, BioCypherNode, BioCypherRelAsNode
import glob

def nodes_and_edges():
nodes_type_1 = []
nodes_type_2 = []
edges = []
number_of_items = 4
for i in range(number_of_items):
node1 = BioCypherNode(
f"EFO:{i+1}",
node_label="disease",
properties= {"name": f"disease{i+1}"}
)
node2 = BioCypherNode(
f"ensembl:ENS{i+1}",
node_label="gene",
)
edge_1 = BioCypherEdge(
source_id=f"EFO:{i+1}",
target_id=f"ensembl:ENS{i+1}",
relationship_label="gene_disease_relationship",
properties = {"score": "0.3"}
)
nodes_type_1.append(node1)
nodes_type_2.append(node2)
edges.append(edge_1)

return [[nodes_type_1, nodes_type_2], edges]

@pytest.fixture
def _get_nodes():
# return a list of nodes
return nodes_and_edges()[0]

@pytest.fixture
def _get_edges():
# return a list of edges
return nodes_and_edges()[1]


@pytest.mark.parametrize("length", [4], scope="function")
def test_rdf_write_data(bw_rdf, length, _get_nodes, _get_edges):
# writer = _get_writer
# writer.rdf_format = "xml"
nodes = _get_nodes
edges = _get_edges

node_type_1 = bw_rdf.write_nodes(nodes[0])
node_type_2 = bw_rdf.write_nodes(nodes[1])
edge_1 = bw_rdf.write_edges(edges)

# check if the writing of the nodes went okay.
assert all([node_type_1, node_type_2, edge_1])

tmp_path = bw_rdf.outdir

rdf_files_path = os.path.join(tmp_path, "*.xml")
rdf_files = glob.glob(rdf_files_path)

g = Graph()
for file in rdf_files:
print(file)
with open(file) as f:
g_temp = Graph().parse(data=f.read(), format="xml")
g += g_temp

# check if the number of nodes and relations are correct
assert len(set(g.subjects())) == 15 # generated 4 nodes per type (8 in total) and 4 'edges'. together with the class definition (+3) makes 15 in total
assert len(set(g.subject_objects())) == 47 # all the triples; between nodes, but also the converted properties of nodes and the 'edges'.

0 comments on commit 6dd5590

Please sign in to comment.