Skip to content

Commit 91c963d

Browse files
authored
Merge pull request #373 from fschrader1992/rdf
RDF Reader Test and Namespace
2 parents 7e9299e + d343279 commit 91c963d

File tree

2 files changed

+141
-3
lines changed

2 files changed

+141
-3
lines changed

odml/rdf/query_creator.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@
88
import re
99
from abc import ABCMeta, abstractmethod
1010

11-
from rdflib import Namespace, RDF
11+
from rdflib import RDF
1212
from rdflib.plugins.sparql import prepareQuery
1313

1414
from ..format import Document
1515
from ..format import Property
1616
from ..format import Section
17+
from ..format import Format
18+
19+
odmlns = Format.namespace()
1720

1821

1922
class BaseQueryCreator:
@@ -271,7 +274,7 @@ def get_query(self, q_str=None, q_parser=None):
271274
self.q_dict = q_parser.parse_query_string(q_str)
272275
self._prepare_query()
273276

274-
use_ns = {"odml": Namespace("https://g-node.org/odml-rdf#"), "rdf": RDF}
277+
use_ns = {"odml": odmlns, "rdf": RDF}
275278
return prepareQuery(self.query, initNs=use_ns)
276279

277280
def _prepare_query(self):
@@ -281,7 +284,7 @@ def _prepare_query(self):
281284
:return: string representing rdflib query.
282285
"""
283286

284-
odml_uri = "https://g-node.org/odml-rdf#"
287+
odml_uri = str(odmlns)
285288
self.query = "SELECT * WHERE {\n"
286289

287290
if "Doc" in self.q_dict.keys():

test/test_rdf_reader.py

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
import datetime
2+
import unittest
3+
4+
from rdflib import Literal
5+
import odml.format as format
6+
7+
from odml import Property, Section, Document
8+
from odml.tools.rdf_converter import RDFWriter, RDFReader
9+
from odml.tools.parser_utils import ParserException
10+
11+
odmlns = format.Format.namespace()
12+
13+
14+
class TestRDFReader(unittest.TestCase):
15+
16+
def setUp(self):
17+
doc = Document()
18+
sec = Section(name="sec1", type="test", parent=doc)
19+
Section(name="sec2", type="test", parent=sec)
20+
Property(name="prop1", values=[1.3], parent=sec)
21+
22+
self.doc = doc
23+
24+
def test_rdf_formats(self):
25+
"""
26+
Test if document gets correctly converted to odml for turtle, xml and n3.
27+
"""
28+
w = RDFWriter(self.doc).get_rdf_str()
29+
r = RDFReader().from_string(w, "turtle")
30+
self.assertEqual(len(r[0].sections), 1)
31+
self.assertEqual(len(r[0].sections[0].sections), 1)
32+
self.assertEqual(len(r[0].sections[0].properties), 1)
33+
34+
w = RDFWriter(self.doc).get_rdf_str("xml")
35+
r = RDFReader().from_string(w, "xml")
36+
self.assertEqual(len(r[0].sections), 1)
37+
self.assertEqual(len(r[0].sections[0].sections), 1)
38+
self.assertEqual(len(r[0].sections[0].properties), 1)
39+
40+
w = RDFWriter(self.doc).get_rdf_str("n3")
41+
r = RDFReader().from_string(w, "n3")
42+
self.assertEqual(len(r[0].sections), 1)
43+
self.assertEqual(len(r[0].sections[0].sections), 1)
44+
self.assertEqual(len(r[0].sections[0].properties), 1)
45+
46+
def test_doc(self):
47+
"""
48+
Test if a document and its attributes get converted correctly from rdf to odml.
49+
"""
50+
doc = Document()
51+
doc.author = "D. N. Adams"
52+
doc.version = 42
53+
doc.date = datetime.date(1979, 10, 12)
54+
55+
w = RDFWriter(doc).get_rdf_str()
56+
r = RDFReader().from_string(w, "turtle")
57+
58+
self.assertEqual(r[0].author, "D. N. Adams")
59+
self.assertEqual(r[0].version, "42")
60+
self.assertEqual(r[0].date, datetime.date(1979, 10, 12))
61+
62+
def test_section(self):
63+
"""
64+
Test if a section and its attributes get converted correctly from rdf to odml.
65+
"""
66+
doc = Document()
67+
sec1 = Section(name="sec1", type="test", parent=doc, definition="Interesting stuff.",
68+
reference="The Journal")
69+
Section(name="sec2", type="test", parent=sec1)
70+
71+
w = RDFWriter(doc).get_rdf_str()
72+
r = RDFReader().from_string(w, "turtle")
73+
74+
self.assertEqual(r[0].sections[0].name, "sec1")
75+
self.assertEqual(r[0].sections[0].type, "test")
76+
self.assertEqual(r[0].sections[0].id, sec1.id)
77+
self.assertEqual(r[0].sections[0].definition, "Interesting stuff.")
78+
self.assertEqual(r[0].sections[0].reference, "The Journal")
79+
self.assertEqual(r[0].sections[0].parent, r[0])
80+
self.assertEqual(len(r[0].sections[0].sections), 1)
81+
82+
def test_property(self):
83+
"""
84+
Test if a property and its attributes get converted correctly from rdf to odml.
85+
"""
86+
doc = Document()
87+
sec1 = Section(name="sec1", type="test", parent=doc)
88+
prop2 = Property(name="numbers", definition="any number", dtype="float", parent=sec1,
89+
values=[1, 3.4, 67.8, -12], unit="meter", uncertainty=0.8,
90+
value_origin="force", reference="Experiment 1")
91+
92+
w = RDFWriter(doc).get_rdf_str()
93+
r = RDFReader().from_string(w, "turtle")
94+
95+
prop = r[0].sections[0].properties["numbers"]
96+
97+
self.assertEqual(prop.name, "numbers")
98+
self.assertEqual(prop.dtype, "float")
99+
self.assertEqual(prop.id, prop2.id)
100+
self.assertEqual(prop.parent, r[0].sections[0])
101+
self.assertEqual(len(prop.values), 4)
102+
self.assertEqual(prop.values, [1, 3.4, 67.8, -12])
103+
self.assertEqual(prop.definition, "any number")
104+
self.assertEqual(prop.unit, "meter")
105+
self.assertEqual(prop.uncertainty, "0.8")
106+
self.assertEqual(prop.value_origin, "force")
107+
self.assertEqual(prop.reference, "Experiment 1")
108+
109+
def test_mandatory_attrs_section(self):
110+
"""
111+
Test if ParserError is thrown if mandatory attributes are missing for section.
112+
"""
113+
w = RDFWriter([self.doc])
114+
w.convert_to_rdf()
115+
for rdf_sec in w.graph.subjects(predicate=odmlns.hasName, object=Literal("sec1")):
116+
w.graph.remove((rdf_sec, odmlns.hasName, Literal("sec1")))
117+
118+
new_graph = w.graph.serialize(format="turtle").decode("utf-8")
119+
120+
with self.assertRaises(ParserException):
121+
RDFReader().from_string(new_graph, "turtle")
122+
123+
def test_mandatory_attrs_property(self):
124+
"""
125+
Test if ParserError is thrown if mandatory attributes are missing for section.
126+
"""
127+
w = RDFWriter([self.doc])
128+
w.convert_to_rdf()
129+
for rdf_sec in w.graph.subjects(predicate=odmlns.hasName, object=Literal("prop1")):
130+
w.graph.remove((rdf_sec, odmlns.hasName, Literal("prop1")))
131+
132+
new_graph = w.graph.serialize(format="turtle").decode("utf-8")
133+
134+
with self.assertRaises(ParserException):
135+
RDFReader().from_string(new_graph, "turtle")

0 commit comments

Comments
 (0)