forked from monarch-initiative/dipper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestUtils.py
73 lines (63 loc) · 2.34 KB
/
TestUtils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import logging
import io
from pathlib import Path
from rdflib import URIRef, RDF
from dipper.graph.RDFGraph import RDFGraph
LOG = logging.getLogger(__name__)
class TestUtils:
@staticmethod
def test_graph_equality(turtlish, graph):
"""
:param turtlish: file path or string of triples in turtle
format without prefix header
:param graph: Graph object to test against
:return: Boolean, True if graphs contain same
set of triples
"""
turtle_graph = RDFGraph()
turtle_graph.bind_all_namespaces()
prefixes = "\n".join(
["@prefix {}: <{}> .".format(
n[0], n[1]) for n in turtle_graph.namespace_manager.namespaces()]
)
headless_ttl = ''
try:
if Path(turtlish).exists():
headless_ttl = Path(turtlish).read_text()
else:
raise OSError
except OSError:
if isinstance(turtlish, str):
headless_ttl = turtlish
else:
raise ValueError("turtlish must be filepath or string")
turtle_string = prefixes + headless_ttl
mock_file = io.StringIO(turtle_string)
turtle_graph.parse(mock_file, format="turtle")
TestUtils.remove_ontology_axioms(graph)
turtle_triples = set(list(turtle_graph))
ref_triples = set(list(graph))
equality = turtle_triples == ref_triples
if not equality:
LOG.warning(
"Triples do not match\n"
"\tLeft hand difference: %s\n"
"\tRight hand difference: %s",
sorted(turtle_triples - ref_triples),
sorted(ref_triples - turtle_triples)
)
return equality
@staticmethod
def remove_ontology_axioms(graph):
"""
Given an rdflib graph, remove any triples
connected to an ontology node:
{} a owl:Ontology
:param graph: RDFGraph
:return: None
"""
ontology_iri = URIRef("http://www.w3.org/2002/07/owl#Ontology")
for subject in graph.subjects(RDF.type, ontology_iri):
for predicate, obj in graph.predicate_objects(subject):
graph.remove((subject, predicate, obj))
graph.remove((subject, RDF.type, ontology_iri))