forked from G-Node/python-odml
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_rdf_writer.py
More file actions
203 lines (170 loc) · 8.29 KB
/
Copy pathtest_rdf_writer.py
File metadata and controls
203 lines (170 loc) · 8.29 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import datetime
import os
import unittest
from os.path import dirname, abspath
import yaml
from rdflib import URIRef, Literal
from rdflib.namespace import XSD, RDF
import odml
import odml.format as format
from odml.tools.rdf_converter import RDFWriter
from test.test_samplefile import SampleFileCreator
from test.test_samplefile import parse
odmlns = format.Format.namespace()
class TestRDFWriter(unittest.TestCase):
def setUp(self):
doc = SampleFileCreator().create_document()
doc1 = SampleFileCreator().create_document()
self.doc = doc
self.doc1 = doc1
def test_convert_to_rdf(self):
w = RDFWriter([self.doc, self.doc1])
w.convert_to_rdf()
doc_subjects = w.graph.subjects(predicate=RDF.type, object=URIRef(odmlns.Document))
self.assertEqual(len(list(doc_subjects)), 2)
def test_adding_doc_to_the_hub(self):
w = RDFWriter([self.doc])
w.convert_to_rdf()
hub_hasDocument = w.graph.objects(subject=w.hub_root, predicate=odmlns.hasDocument)
self.assertEqual(len(list(hub_hasDocument)), 1)
def test_adding_repository(self):
w = RDFWriter([self.doc])
w.convert_to_rdf()
self.assertEqual(len(list(w.graph.objects(subject=w.hub_root, predicate=odmlns.hasTerminology))), 0)
self.assertEqual(len(list(w.graph.objects(subject=URIRef(odmlns + w.docs[0].id), predicate=odmlns.hasTerminology))), 0)
url = "terminology_url"
self.doc.repository = url
w = RDFWriter([self.doc])
w.convert_to_rdf()
self.assertEqual(len(list(w.graph.subjects(predicate=RDF.type, object=URIRef(url)))), 1)
self.assertEqual(len(list(w.graph.objects(subject=w.hub_root, predicate=odmlns.hasTerminology))), 1)
self.assertEqual(len(list(w.graph.objects(subject=URIRef(odmlns + w.docs[0].id), predicate=odmlns.hasTerminology))), 1)
def test_adding_sections(self):
doc = odml.Document()
w = RDFWriter([doc])
w.convert_to_rdf()
self.assertEqual(len(list(w.graph.subject_objects(predicate=odmlns.hasSection))), 0)
w = RDFWriter([self.doc])
w.convert_to_rdf()
self.assertEqual(len(list(w.graph.subject_objects(predicate=odmlns.hasSection))), 9)
w = RDFWriter([self.doc, self.doc1])
w.convert_to_rdf()
self.assertEqual(len(list(w.graph.subject_objects(predicate=odmlns.hasSection))), 18)
def test_adding_properties(self):
doc = parse("""
s1[t1]
- s11[t1]
s2[t2]
""")
w = RDFWriter([doc])
w.convert_to_rdf()
self.assertEqual(len(list(w.graph.subject_objects(predicate=odmlns.hasProperty))), 0)
w = RDFWriter([self.doc])
w.convert_to_rdf()
self.assertEqual(len(list(w.graph.subject_objects(predicate=odmlns.hasProperty))), 12)
w = RDFWriter([self.doc, self.doc1])
w.convert_to_rdf()
self.assertEqual(len(list(w.graph.subject_objects(predicate=odmlns.hasProperty))), 24)
def test_adding_values(self):
doc = parse("""
s1[t1]
""")
w = RDFWriter([doc])
w.convert_to_rdf()
self.assertEqual(len(list(w.graph.subject_objects(predicate=RDF.li))), 0)
self.assertEqual(len(list(
w.graph.subject_objects(predicate=URIRef("%s_1" % str(RDF))))), 0)
doc = parse("""
s1[t1]
- p1
""")
w = RDFWriter([doc])
w.convert_to_rdf()
self.assertEqual(len(list(w.graph.subjects(predicate=RDF.li,
object=Literal("val")))), 0)
self.assertEqual(len(list(w.graph.subjects(predicate=URIRef("%s_1" % str(RDF)),
object=Literal("val")))), 1)
doc.sections[0].properties[0].append("val2")
w = RDFWriter([doc])
w.convert_to_rdf()
self.assertEqual(len(list(w.graph.subject_objects(predicate=RDF.li))), 0)
self.assertEqual(len(list(w.graph.subjects(predicate=RDF.li, object=Literal("val")))), 0)
self.assertEqual(len(list(w.graph.subjects(predicate=RDF.li, object=Literal("val2")))), 0)
self.assertEqual(len(list(w.graph.subjects(predicate=URIRef("%s_1" % str(RDF)),
object=Literal("val")))), 1)
self.assertEqual(len(list(w.graph.subjects(predicate=URIRef("%s_2" % str(RDF)),
object=Literal("val2")))), 1)
doc = parse("""
s1[t1]
- p1
s2[t2]
- p1
- p2
""")
w = RDFWriter([doc])
w.convert_to_rdf()
self.assertEqual(len(list(w.graph.subjects(predicate=RDF.li, object=Literal("val")))), 0)
self.assertEqual(len(list(w.graph.subjects(predicate=URIRef("%s_1" % str(RDF)),
object=Literal("val")))), 3)
def test_section_subclass(self):
p = os.path.join(odml.__path__[0], 'resources', 'section_subclasses.yaml')
with open(p, "r") as f:
subclass = yaml.safe_load(f)
doc = odml.Document()
subclass_key = next(iter(subclass))
s = odml.Section("S", type=subclass_key)
doc.append(s)
w = RDFWriter(doc)
w.convert_to_rdf()
self.assertEqual(len(list(w.graph.subjects(predicate=RDF.type, object=URIRef(odmlns[subclass[subclass_key]])))), 1)
self.assertEqual(len(list(w.graph.subjects(predicate=RDF.type, object=URIRef(odmlns.Section)))), 0)
def test_adding_other_entities_properties(self):
doc = parse("""
s1[t1]
- p1
""")
version = "v1"
date = datetime.date(1979, 10, 12)
author = "nice person"
s_def = "comment"
s_ref = "reference"
p_unit = "u1"
p_name = "p1"
p_def = "p comment"
p_uncertainty = 13.0
p_dtype = "string"
p_value_origin = "value"
p_ref = "p_ref"
doc.version = version
doc.date = date
doc.author = author
doc.sections[0].definition = s_def
doc.sections[0].reference = s_ref
doc.sections[0].properties[0].name = p_name
doc.sections[0].properties[0].unit = p_unit
doc.sections[0].properties[0].definition = p_def
doc.sections[0].properties[0].uncertainty = p_uncertainty
doc.sections[0].properties[0].dtype = p_dtype
doc.sections[0].properties[0].value_origin = p_value_origin
doc.sections[0].properties[0].reference = p_ref
w = RDFWriter([doc])
w.convert_to_rdf()
self.assertEqual(len(list(w.graph.subjects(predicate=odmlns.hasDocVersion, object=Literal(version)))), 1)
self.assertEqual(len(list(w.graph.subjects(predicate=odmlns.hasDate, object=Literal(date, datatype=XSD.date)))), 1)
self.assertEqual(len(list(w.graph.subjects(predicate=odmlns.hasAuthor, object=Literal(author)))), 1)
self.assertEqual(len(list(w.graph.subjects(predicate=odmlns.hasName, object=Literal("s1")))), 1)
self.assertEqual(len(list(w.graph.subjects(predicate=odmlns.hasType, object=Literal("t1")))), 1)
self.assertEqual(len(list(w.graph.subjects(predicate=odmlns.hasDefinition, object=Literal(s_def)))), 1)
self.assertEqual(len(list(w.graph.subjects(predicate=odmlns.hasReference, object=Literal(s_ref)))), 1)
self.assertEqual(len(list(w.graph.subjects(predicate=odmlns.hasName, object=Literal(p_name)))), 1)
self.assertEqual(len(list(w.graph.subjects(predicate=odmlns.hasUnit, object=Literal(p_unit)))), 1)
self.assertEqual(len(list(w.graph.subjects(predicate=odmlns.hasDefinition, object=Literal(p_def)))), 1)
self.assertEqual(len(list(w.graph.subjects(predicate=odmlns.hasUncertainty, object=Literal(p_uncertainty)))), 1)
self.assertEqual(len(list(w.graph.subjects(predicate=odmlns.hasDtype, object=Literal(p_dtype)))), 1)
self.assertEqual(len(list(w.graph.subjects(predicate=odmlns.hasValueOrigin, object=Literal(p_value_origin)))), 1)
self.assertEqual(len(list(w.graph.subjects(predicate=odmlns.hasReference, object=Literal(p_ref)))), 1)
def test_get_rdf_string(self):
w = RDFWriter([self.doc1])
w.get_rdf_str()
with self.assertRaises(ValueError):
w.get_rdf_str("abc")