Debian Codegraph @

test/graph_case.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 import unittest
3
4 from rdflib import Graph, URIRef, Literal, plugin, RDF
5 from rdflib.parser import StringInputSource
6 from six import PY3
7 from rdflib.store import Store
8
9
10 class GraphTestCase(unittest.TestCase):
11 storetest = True
12 identifier = URIRef("rdflib_test")
13
14 michel = URIRef(u"michel")
15 tarek = URIRef(u"tarek")
16 bob = URIRef(u"bob")
17 likes = URIRef(u"likes")
18 hates = URIRef(u"hates")
19 pizza = URIRef(u"pizza")
20 cheese = URIRef(u"cheese")
21
22 namespace_dc = "http://purl.org/dc/elements/1.1/"
23 namespace_dct = "http://purl.org/dc/terms/"
24 namespace_saws = "http://purl.org/saws/ontology#"
25
26 def setUp(self, uri="sqlite://", storename=None):
27 store = plugin.get(storename, Store)(identifier=self.identifier)
28 self.graph = Graph(store, identifier=self.identifier)
29 self.graph.open(uri, create=True)
30
31 def tearDown(self, uri="sqlite://"):
32 self.graph.destroy(uri)
33 self.graph.close()
34
35 def addStuff(self):
36 tarek = self.tarek
37 michel = self.michel
38 bob = self.bob
39 likes = self.likes
40 hates = self.hates
41 pizza = self.pizza
42 cheese = self.cheese
43
44 self.graph.add((tarek, likes, pizza))
45 self.graph.add((tarek, likes, cheese))
46 self.graph.add((michel, likes, pizza))
47 self.graph.add((michel, likes, cheese))
48 self.graph.add((bob, likes, cheese))
49 self.graph.add((bob, hates, pizza))
50 self.graph.add((bob, hates, michel)) # gasp!
51 self.graph.commit()
52
53 def removeStuff(self):
54 tarek = self.tarek
55 michel = self.michel
56 bob = self.bob
57 likes = self.likes
58 hates = self.hates
59 pizza = self.pizza
60 cheese = self.cheese
61
62 self.graph.remove((tarek, likes, pizza))
63 self.graph.remove((tarek, likes, cheese))
64 self.graph.remove((michel, likes, pizza))
65 self.graph.remove((michel, likes, cheese))
66 self.graph.remove((bob, likes, cheese))
67 self.graph.remove((bob, hates, pizza))
68 self.graph.remove((bob, hates, michel)) # gasp!
69
70 def testAdd(self):
71 self.addStuff()
72
73 def testRemove(self):
74 self.addStuff()
75 self.removeStuff()
76
77 def testTriples(self):
78 tarek = self.tarek
79 michel = self.michel
80 bob = self.bob
81 likes = self.likes
82 hates = self.hates
83 pizza = self.pizza
84 cheese = self.cheese
85 asserte = self.assertEqual
86 triples = self.graph.triples
87 Any = None
88
89 self.addStuff()
90
91 # unbound subjects
92 asserte(len(list(triples((Any, likes, pizza)))), 2)
93 asserte(len(list(triples((Any, hates, pizza)))), 1)
94 asserte(len(list(triples((Any, likes, cheese)))), 3)
95 asserte(len(list(triples((Any, hates, cheese)))), 0)
96
97 # unbound objects
98 asserte(len(list(triples((michel, likes, Any)))), 2)
99 asserte(len(list(triples((tarek, likes, Any)))), 2)
100 asserte(len(list(triples((bob, hates, Any)))), 2)
101 asserte(len(list(triples((bob, likes, Any)))), 1)
102
103 # unbound predicates
104 asserte(len(list(triples((michel, Any, cheese)))), 1)
105 asserte(len(list(triples((tarek, Any, cheese)))), 1)
106 asserte(len(list(triples((bob, Any, pizza)))), 1)
107 asserte(len(list(triples((bob, Any, michel)))), 1)
108
109 # unbound subject, objects
110 asserte(len(list(triples((Any, hates, Any)))), 2)
111 asserte(len(list(triples((Any, likes, Any)))), 5)
112
113 # unbound predicates, objects
114 asserte(len(list(triples((michel, Any, Any)))), 2)
115 asserte(len(list(triples((bob, Any, Any)))), 3)
116 asserte(len(list(triples((tarek, Any, Any)))), 2)
117
118 # unbound subjects, predicates
119 asserte(len(list(triples((Any, Any, pizza)))), 3)
120 asserte(len(list(triples((Any, Any, cheese)))), 3)
121 asserte(len(list(triples((Any, Any, michel)))), 1)
122
123 # all unbound
124 asserte(len(list(triples((Any, Any, Any)))), 7)
125 self.removeStuff()
126 asserte(len(list(triples((Any, Any, Any)))), 0)
127
128 def testConnected(self):
129 graph = self.graph
130 self.addStuff()
131 self.assertEqual(True, graph.connected())
132
133 jeroen = URIRef("jeroen")
134 unconnected = URIRef("unconnected")
135
136 graph.add((jeroen, self.likes, unconnected))
137
138 self.assertEqual(False, graph.connected())
139
140 def testSub(self):
141 g1 = Graph()
142 g2 = Graph()
143
144 tarek = self.tarek
145 # michel = self.michel
146 bob = self.bob
147 likes = self.likes
148 # hates = self.hates
149 pizza = self.pizza
150 cheese = self.cheese
151
152 g1.add((tarek, likes, pizza))
153 g1.add((bob, likes, cheese))
154
155 g2.add((bob, likes, cheese))
156
157 g3 = g1 - g2
158
159 self.assertEqual(len(g3), 1)
160 self.assertEqual((tarek, likes, pizza) in g3, True)
161 self.assertEqual((tarek, likes, cheese) in g3, False)
162
163 self.assertEqual((bob, likes, cheese) in g3, False)
164
165 g1 -= g2
166
167 self.assertEqual(len(g1), 1)
168 self.assertEqual((tarek, likes, pizza) in g1, True)
169 self.assertEqual((tarek, likes, cheese) in g1, False)
170
171 self.assertEqual((bob, likes, cheese) in g1, False)
172
173 def testGraphAdd(self):
174 g1 = Graph()
175 g2 = Graph()
176
177 tarek = self.tarek
178 # michel = self.michel
179 bob = self.bob
180 likes = self.likes
181 # hates = self.hates
182 pizza = self.pizza
183 cheese = self.cheese
184
185 g1.add((tarek, likes, pizza))
186
187 g2.add((bob, likes, cheese))
188
189 g3 = g1 + g2
190
191 self.assertEqual(len(g3), 2)
192 self.assertEqual((tarek, likes, pizza) in g3, True)
193 self.assertEqual((tarek, likes, cheese) in g3, False)
194
195 self.assertEqual((bob, likes, cheese) in g3, True)
196
197 g1 += g2
198
199 self.assertEqual(len(g1), 2)
200 self.assertEqual((tarek, likes, pizza) in g1, True)
201 self.assertEqual((tarek, likes, cheese) in g1, False)
202
203 self.assertEqual((bob, likes, cheese) in g1, True)
204
205 def testGraphIntersection(self):
206 g1 = Graph()
207 g2 = Graph()
208
209 tarek = self.tarek
210 michel = self.michel
211 bob = self.bob
212 likes = self.likes
213 # hates = self.hates
214 pizza = self.pizza
215 cheese = self.cheese
216
217 g1.add((tarek, likes, pizza))
218 g1.add((michel, likes, cheese))
219
220 g2.add((bob, likes, cheese))
221 g2.add((michel, likes, cheese))
222
223 g3 = g1 * g2
224
225 self.assertEqual(len(g3), 1)
226 self.assertEqual((tarek, likes, pizza) in g3, False)
227 self.assertEqual((tarek, likes, cheese) in g3, False)
228
229 self.assertEqual((bob, likes, cheese) in g3, False)
230
231 self.assertEqual((michel, likes, cheese) in g3, True)
232
233 g1 *= g2
234
235 self.assertEqual(len(g1), 1)
236
237 self.assertEqual((tarek, likes, pizza) in g1, False)
238 self.assertEqual((tarek, likes, cheese) in g1, False)
239
240 self.assertEqual((bob, likes, cheese) in g1, False)
241
242 self.assertEqual((michel, likes, cheese) in g1, True)
243
244 def testStoreLiterals(self):
245 bob = self.bob
246 says = URIRef(u"says")
247 hello = Literal(u"hello", lang="en")
248 konichiwa = Literal(u"こんにちは", lang="ja")
249 something = Literal(u"something")
250
251 self.graph.add((bob, says, hello))
252 self.graph.add((bob, says, konichiwa))
253 self.graph.add((bob, says, something))
254 self.graph.commit()
255
256 objs = list(self.graph.objects(subject=bob, predicate=says))
257 for o in objs:
258 if o.value == u"hello":
259 self.assertEqual(o.language, "en")
260 elif o.value == u"こんにちは":
261 self.assertEqual(o.language, "ja")
262 elif o.value == u"something":
263 self.assertEqual(o.language, None)
264 else:
265 self.fail()
266 self.assertEqual(len(list(objs)), 3)
267
268 def testStoreLiteralsXml(self):
269 bob = self.bob
270 says = URIRef(u"http://www.rdflib.net/terms/says")
271 objects = [
272 Literal(u"I'm the one", lang="en"),
273 Literal(u"こんにちは", lang="ja"),
274 Literal(u"les garçons à Noël reçoivent des œufs", lang="fr")]
275
276 testdoc = (PY3 and bytes(xmltestdocXml, "UTF-8")) or xmltestdocXml
277
278 self.graph.parse(StringInputSource(testdoc), format="xml")
279
280 objs = list(self.graph)
281 self.assertEqual(len(objs), 3)
282 for o in objs:
283 self.assertEqual(o[0], bob)
284 self.assertEqual(o[1], says)
285 self.assertIn(o[2], objects)
286
287 def testStoreLiteralsXmlQuote(self):
288 bob = self.bob
289 says = URIRef(u"http://www.rdflib.net/terms/says")
290 imtheone = Literal(u"I'm the one", lang="en")
291
292 testdoc = (PY3 and bytes(xmltestdocXmlQuote, "UTF-8")) or xmltestdocXmlQuote
293
294 self.graph.parse(StringInputSource(testdoc), format="xml")
295
296 objs = list(self.graph)
297 self.assertEqual(len(objs), 1)
298 o = objs[0]
299 self.assertEqual(o, (bob, says, imtheone))
300
301 def testBindNamespace(self):
302 """ Check that bound namespaced with prefix (including empty ones) are correctly kept """
303 self.graph.bind("", self.namespace_dc)
304 self.graph.bind("dct", self.namespace_dct)
305 self.assertEqual(
306 self.graph.qname(self.namespace_dct + u"title"), "dct:title",
307 "Prefixed namespace should be stored and retrieved"
308 )
309 self.assertEqual(
310 self.graph.qname(self.namespace_dc + u"title"), "title",
311 "Empty prefixes for namespace should be stored and retrieved"
312 )
313 self.assertEqual(
314 self.graph.qname(self.namespace_saws + u"title"), "ns1:title",
315 "Unknown prefixes for namespace should be transformed to nsX"
316 )
317
318 def testTriplesChoices(self):
319 likes = self.likes
320 pizza = self.pizza
321 cheese = self.cheese
322 tarek = self.tarek
323 michel = self.michel
324 bob = self.bob
325 self.addStuff()
326 trips = self.graph.triples_choices((None, likes, [pizza, cheese]))
327 self.assertEqual(
328 set(trips),
329 set([(tarek, likes, pizza),
330 (tarek, likes, pizza),
331 (tarek, likes, cheese),
332 (michel, likes, pizza),
333 (michel, likes, cheese),
334 (bob, likes, cheese)])
335 )
336
337 def test_type_add(self):
338 trip = (URIRef('http://example.org#type-add'), RDF.type, URIRef('http://example.org/cra'))
339 self.graph.add(trip)
340 self.graph.add(trip)
341 # No exception raised
342
343 def test_type_addn(self):
344 quad = (URIRef('http://example.org#type-addn'), RDF.type, URIRef('http://example.org/cra'), self.graph)
345 self.graph.addN([quad, quad])
346 # No exception raised
347
348 def test_add(self):
349 trip = (URIRef('http://example.org#add'), URIRef('http://example.org/blah'), URIRef('http://example.org/cra'))
350 self.graph.add(trip)
351 self.graph.add(trip)
352 # No exception raised
353
354 def test_addn(self):
355 quad = (URIRef('http://example.org#addn'),
356 URIRef('http://example.org/blah'),
357 URIRef('http://example.org/cra'),
358 self.graph)
359 self.graph.addN([quad, quad])
360 # No exception raised
361
362 def test_namespace_change_prefix_binding(self):
363 nm = self.graph.namespace_manager
364 nm.bind('change_binding', URIRef('http://example.org/change-binding-1#'),
365 replace=True)
366 nm.bind('change_binding', URIRef('http://example.org/change-binding-2#'),
367 replace=True)
368 self.assertIn(
369 ('change_binding', URIRef('http://example.org/change-binding-2#')),
370 list(nm.namespaces())
371 )
372
373 def test_namespace_rebind_prefix(self):
374 nm = self.graph.namespace_manager
375 nm.bind('rebind', URIRef('http://example.org/rebind#'))
376 nm.bind('rebind', URIRef('http://example.org/rebind#'))
377
378 def test_add_duplicate_length(self):
379 '''
380 Test that adding duplicate triples doesn't increase the length of the graph
381 '''
382 trip = (URIRef('http://example.org#add'), URIRef('http://example.org/blah'), URIRef('http://example.org/cra'))
383 self.graph.add(trip)
384 self.graph.add(trip)
385 self.assertEqual(len(self.graph), 1)
386 # additional tests
387 # - add "duplicate" triples and query -- ensure the graph length counts only distinct
388 # triples
389 # - add duplicate triples and query -- ensure there are no duplicate entries in the
390 # query result
391 # - repeat the above two for type triples
392 # - test with quoted graphs (not even sure how that works)
393
394
395 xmltestdoc = """<?xml version="1.0" encoding="UTF-8"?>
396 <rdf:RDF
397 xmlns="http://example.org/"
398 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
399 >
400 <rdf:Description rdf:about="http://example.org/a">
401 <b rdf:resource="http://example.org/c"/>
402 </rdf:Description>
403 </rdf:RDF>
404 """
405
406 xmltestdocXml = """<?xml version="1.0" encoding="UTF-8"?>
407 <rdf:RDF
408 xmlns:ns1="http://www.rdflib.net/terms/"
409 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
410 >
411 <rdf:Description rdf:about="bob">
412 <ns1:says xml:lang="en">I'm the one</ns1:says>
413 </rdf:Description>
414 <rdf:Description rdf:about="bob">
415 <ns1:says xml:lang="ja">こんにちは</ns1:says>
416 </rdf:Description>
417 <rdf:Description rdf:about="bob">
418 <ns1:says xml:lang="fr">les garçons à Noël reçoivent des œufs</ns1:says>
419 </rdf:Description>
420 </rdf:RDF>
421 """
422
423 xmltestdocXmlQuote = """<?xml version="1.0" encoding="UTF-8"?>
424 <rdf:RDF
425 xmlns:ns1="http://www.rdflib.net/terms/"
426 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
427 >
428 <rdf:Description rdf:about="bob">
429 <ns1:says xml:lang="en">I'm the one</ns1:says>
430 </rdf:Description>
431 </rdf:RDF>
432 """
433
434
435 n3testdoc = """@prefix : <http://example.org/> .
436
437 :a :b :c .
438 """
439
440 nttestdoc = "<http://example.org/a> <http://example.org/b> <http://example.org/c> .\n"
441
442 if __name__ == "__main__":
443 unittest.main()