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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
|
from __future__ import print_function
import sys
from encodings.utf_8 import StreamWriter
import unittest
import os
import os.path
from six.moves.urllib.request import url2pathname, urlopen
from rdflib import RDF, RDFS, URIRef, BNode, Literal, Namespace, Graph
from rdflib.exceptions import ParserError
from rdflib.util import first
import logging
_logger = logging.getLogger("parser_rdfcore")
verbose = 0
sw = StreamWriter(sys.stdout)
def write(msg):
_logger.info(msg + "\n")
# sw.write(msg+"\n")
class TestStore(Graph):
__test__ = False
def __init__(self, expected):
super(TestStore, self).__init__()
self.expected = expected
def add(self, spo):
(s, p, o) = spo
if not isinstance(s, BNode) and not isinstance(o, BNode):
if not (s, p, o) in self.expected:
m = "Triple not in expected result: %s, %s, %s" % (
s.n3(), p.n3(), o.n3())
if verbose:
write(m)
# raise Exception(m)
super(TestStore, self).add((s, p, o))
TEST = Namespace("http://www.w3.org/2000/10/rdf-tests/rdfcore/testSchema#")
CACHE_DIR = os.path.join(os.path.dirname(__file__), "rdf")
skipped = (
# "datatypes/Manifest.rdf#test002",
# "rdf-containers-syntax-vs-schema/Manifest.rdf#test004",
# "rdfms-xml-literal-namespaces/Manifest.rdf#test001",
# "rdfms-xml-literal-namespaces/Manifest.rdf#test002",
# "rdfms-xmllang/Manifest.rdf#test001",
# "rdfms-xmllang/Manifest.rdf#test002",
# "xml-canon/Manifest.rdf#test001"
)
def cached_file(url):
fname = url2pathname(relative(url))
fpath = os.path.join(CACHE_DIR, fname)
if not os.path.exists(fpath):
print("%s does not exist, fetching from %s" % (fpath, url))
folder = os.path.dirname(fpath)
if not os.path.exists(folder):
os.makedirs(folder)
f = open(fpath, 'w')
try:
f.write(urlopen(url).read())
finally:
f.close()
return fpath
RDFCOREBASE = "http://www.w3.org/2000/10/rdf-tests/rdfcore/"
def relative(url):
return url[len(RDFCOREBASE):]
def resolve(rel):
return RDFCOREBASE + rel
def _testPositive(uri, manifest):
if verbose:
write(u"TESTING: %s" % uri)
result = 0 # 1=failed, 0=passed
inDoc = first(manifest.objects(uri, TEST["inputDocument"]))
outDoc = first(manifest.objects(uri, TEST["outputDocument"]))
expected = Graph()
if outDoc[-3:] == ".nt":
format = "nt"
else:
format = "xml"
expected.parse(cached_file(outDoc), publicID=outDoc, format=format)
store = TestStore(expected)
if inDoc[-3:] == ".nt":
format = "nt"
else:
format = "xml"
try:
store.parse(cached_file(inDoc), publicID=inDoc, format=format)
except ParserError as pe:
write("Failed '")
write(inDoc)
write("' failed with")
raise pe
try:
write(type(pe))
except:
write("sorry could not dump out error.")
result = 1
else:
if not store.isomorphic(expected):
write(u"""Failed: '%s'""" % uri)
if verbose:
write(""" In:\n""")
for s, p, o in store:
write("%s %s %s." % (repr(s), repr(p), repr(o)))
write(""" Out:\n""")
for s, p, o in expected:
write("%s %s %s." % (repr(s), repr(p), repr(o)))
result += 1
return result
def _testNegative(uri, manifest):
if verbose:
write(u"TESTING: %s" % uri)
result = 0 # 1=failed, 0=passed
inDoc = first(manifest.objects(uri, TEST["inputDocument"]))
store = Graph()
test = BNode()
results.add((test, RESULT["test"], uri))
results.add((test, RESULT["system"], system))
try:
if inDoc[-3:] == ".nt":
format = "nt"
else:
format = "xml"
store.parse(cached_file(inDoc), publicID=inDoc, format=format)
except ParserError:
results.add((test, RDF.type, RESULT["PassingRun"]))
# pass
else:
write(u"""Failed: '%s'""" % uri)
results.add((test, RDF.type, RESULT["FailingRun"]))
result = 1
return result
class ParserTestCase(unittest.TestCase):
store = 'default'
path = 'store'
slow = True
def setUp(self):
self.manifest = manifest = Graph(store=self.store)
manifest.open(self.path)
manifest.load(cached_file(
"http://www.w3.org/2000/10/rdf-tests/rdfcore/Manifest.rdf"))
def tearDown(self):
self.manifest.close()
def testNegative(self):
manifest = self.manifest
num_failed = total = 0
negs = list(manifest.subjects(RDF.type, TEST["NegativeParserTest"]))
negs.sort()
for neg in negs:
status = first(manifest.objects(neg, TEST["status"]))
if status == Literal("APPROVED"):
result = _testNegative(neg, manifest)
total += 1
num_failed += result
self.assertEqual(
num_failed, 0, "Failed: %s of %s." % (num_failed, total))
def testPositive(self):
manifest = self.manifest
uris = list(manifest.subjects(RDF.type, TEST["PositiveParserTest"]))
uris.sort()
num_failed = total = 0
for uri in uris:
status = first(manifest.objects(uri, TEST["status"]))
# Failing tests, skipped
if uri[44:] in skipped:
status = Literal("Locally DISAPPROVED")
write("Skipping %s" % uri)
if status == Literal("APPROVED"):
result = _testPositive(uri, manifest)
test = BNode()
results.add((test, RESULT["test"], uri))
results.add((test, RESULT["system"], system))
if not result:
results.add((test, RDF.type, RESULT["PassingRun"]))
else:
results.add((test, RDF.type, RESULT["FailingRun"]))
total += 1
num_failed += result
self.assertEqual(
num_failed, 0, "Failed: %s of %s." % (num_failed, total))
RESULT = Namespace("http://www.w3.org/2002/03owlt/resultsOntology#")
FOAF = Namespace("http://xmlns.com/foaf/0.1/")
results = Graph()
system = BNode("system")
results.add((system, FOAF["homepage"], URIRef("http://rdflib.net/")))
results.add((system, RDFS.label, Literal("RDFLib")))
results.add((system, RDFS.comment, Literal("")))
if __name__ == "__main__":
manifest = Graph()
manifest.load(cached_file(
"http://www.w3.org/2000/10/rdf-tests/rdfcore/Manifest.rdf"))
import sys
import getopt
try:
optlist, args = getopt.getopt(sys.argv[1:], 'h:', ["help"])
except getopt.GetoptError as msg:
write(msg)
# usage()
try:
argv = sys.argv
if len(argv) > 1:
_logger.setLevel(logging.INFO)
_logger.addHandler(logging.StreamHandler())
for arg in argv[1:]:
verbose = 1
case = URIRef(arg)
write(u"Testing: %s" % case)
if (case, RDF.type, TEST["PositiveParserTest"]) in manifest:
result = _testPositive(case, manifest)
write(u"Positive test %s" % ["PASSED", "FAILED"][result])
elif (case, RDF.type, TEST["NegativeParserTest"]) in manifest:
result = _testNegative(case, manifest)
write(u"Negative test %s" % ["PASSED", "FAILED"][result])
else:
write(u"%s not ??" % case)
if len(argv) <= 1:
unittest.main()
finally:
results.serialize("results.rdf")
|