Debian Codegraph @

test/context_case.py

raw symbols SCIP
1 import unittest
2
3 from rdflib import BNode
4 from rdflib import ConjunctiveGraph
5 from rdflib import Graph
6 from rdflib import URIRef
7 from rdflib import plugin
8 from rdflib.store import Store
9 from six import string_types
10
11
12 class ContextTestCase(unittest.TestCase):
13 storetest = True
14 identifier = URIRef("rdflib_test")
15
16 michel = URIRef(u"michel")
17 tarek = URIRef(u"tarek")
18 bob = URIRef(u"bob")
19 likes = URIRef(u"likes")
20 hates = URIRef(u"hates")
21 pizza = URIRef(u"pizza")
22 cheese = URIRef(u"cheese")
23 c1 = URIRef(u"context-1")
24 c2 = URIRef(u"context-2")
25
26 def setUp(self, uri="sqlite://", storename=None):
27 store = plugin.get(storename, Store)(identifier=self.identifier)
28 self.graph = ConjunctiveGraph(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 get_context(self, identifier):
36 assert isinstance(identifier, URIRef) or \
37 isinstance(identifier, BNode), type(identifier)
38 return Graph(store=self.graph.store, identifier=identifier,
39 namespace_manager=self)
40
41 def addStuff(self):
42 tarek = self.tarek
43 michel = self.michel
44 bob = self.bob
45 likes = self.likes
46 hates = self.hates
47 pizza = self.pizza
48 cheese = self.cheese
49 c1 = self.c1
50 graph = Graph(self.graph.store, c1)
51
52 graph.add((tarek, likes, pizza))
53 graph.add((tarek, likes, cheese))
54 graph.add((michel, likes, pizza))
55 graph.add((michel, likes, cheese))
56 graph.add((bob, likes, cheese))
57 graph.add((bob, hates, pizza))
58 graph.add((bob, hates, michel)) # gasp!
59
60 def removeStuff(self):
61 tarek = self.tarek
62 michel = self.michel
63 bob = self.bob
64 likes = self.likes
65 hates = self.hates
66 pizza = self.pizza
67 cheese = self.cheese
68 c1 = self.c1
69 graph = Graph(self.graph.store, c1)
70
71 graph.remove((tarek, likes, pizza))
72 graph.remove((tarek, likes, cheese))
73 graph.remove((michel, likes, pizza))
74 graph.remove((michel, likes, cheese))
75 graph.remove((bob, likes, cheese))
76 graph.remove((bob, hates, pizza))
77 graph.remove((bob, hates, michel)) # gasp!
78
79 def addStuffInMultipleContexts(self):
80 c1 = self.c1
81 c2 = self.c2
82 triple = (self.pizza, self.hates, self.tarek) # revenge!
83
84 # add to default context
85 self.graph.add(triple)
86 # add to context 1
87 graph = Graph(self.graph.store, c1)
88 graph.add(triple)
89 # add to context 2
90 graph = Graph(self.graph.store, c2)
91 graph.add(triple)
92
93 def testConjunction(self):
94 self.addStuffInMultipleContexts()
95 triple = (self.pizza, self.likes, self.pizza)
96 # add to context 1
97 graph = Graph(self.graph.store, self.c1)
98 graph.add(triple)
99 self.assertEqual(len(self.graph.store), len(graph.store))
100
101 def testAdd(self):
102 self.addStuff()
103
104 def testRemove(self):
105 self.addStuff()
106 self.removeStuff()
107
108 def testLenInOneContext(self):
109
110 c1 = self.c1
111 # make sure context is empty
112
113 self.graph.remove_context(self.get_context(c1))
114 graph = Graph(self.graph.store, c1)
115 oldLen = len(self.graph)
116
117 for i in range(0, 10):
118 graph.add((BNode(), self.hates, self.hates))
119 self.assertEqual(len(graph), oldLen + 10)
120 self.assertEqual(len(self.get_context(c1)), oldLen + 10)
121 self.graph.remove_context(self.get_context(c1))
122 self.assertEqual(len(self.graph), oldLen)
123 self.assertEqual(len(graph), 0)
124
125 def testLenInMultipleContexts(self):
126 oldLen = len(self.graph.store)
127 self.addStuffInMultipleContexts()
128 # addStuffInMultipleContexts is adding the same triple to
129 # three different contexts. So it's only + 1
130 self.assertEqual(len(self.graph.store), oldLen + 1,
131 [self.graph.store, oldLen + 1])
132
133 graph = Graph(self.graph.store, self.c1)
134 self.assertEqual(len(graph.store), oldLen + 1,
135 [graph.store, oldLen + 1])
136
137 def testRemoveInMultipleContexts(self):
138 c1 = self.c1
139 c2 = self.c2
140 triple = (self.pizza, self.hates, self.tarek) # revenge!
141
142 self.addStuffInMultipleContexts()
143
144 # triple should be still in store after removing it from c1 + c2
145 self.assertIn(triple, self.graph)
146 graph = Graph(self.graph.store, c1)
147 graph.remove(triple)
148 self.assertIn(triple, self.graph)
149 graph = Graph(self.graph.store, c2)
150 graph.remove(triple)
151 self.assertIn(triple, self.graph)
152 self.graph.remove(triple)
153 # now gone!
154 self.assertNotIn(triple, self.graph)
155
156 # add again and see if remove without context removes all triples!
157 self.addStuffInMultipleContexts()
158 self.graph.remove(triple)
159 self.assertNotIn(triple, self.graph)
160
161 def testContexts(self):
162 triple = (self.pizza, self.hates, self.tarek) # revenge!
163
164 self.addStuffInMultipleContexts()
165
166 def cid(c):
167 if not isinstance(c, string_types):
168 return c.identifier
169 return c
170 self.assertIn(self.c1, list(map(cid, self.graph.contexts())))
171 self.assertIn(self.c2, list(map(cid, self.graph.contexts())))
172
173 contextList = list(map(cid, list(self.graph.contexts(triple))))
174 self.assertIn(self.c1, contextList)
175 self.assertIn(self.c2, contextList)
176
177 def testRemoveContext(self):
178 c1 = self.c1
179
180 self.addStuffInMultipleContexts()
181 self.assertEqual(len(Graph(self.graph.store, c1)), 1)
182 self.assertEqual(len(self.get_context(c1)), 1)
183
184 self.graph.remove_context(self.get_context(c1))
185 self.assertNotIn(self.c1, self.graph.contexts())
186
187 def testRemoveAny(self):
188 Any = None
189 self.addStuffInMultipleContexts()
190 self.graph.remove((Any, Any, Any))
191 self.assertEqual(len(self.graph), 0)
192
193 def testTriples(self):
194 tarek = self.tarek
195 michel = self.michel
196 bob = self.bob
197 likes = self.likes
198 hates = self.hates
199 pizza = self.pizza
200 cheese = self.cheese
201 c1 = self.c1
202 asserte = self.assertEqual
203 triples = self.graph.triples
204 graph = self.graph
205 c1graph = Graph(self.graph.store, c1)
206 c1triples = c1graph.triples
207 Any = None
208
209 self.addStuff()
210
211 # unbound subjects with context
212 asserte(len(list(c1triples((Any, likes, pizza)))), 2)
213 asserte(len(list(c1triples((Any, hates, pizza)))), 1)
214 asserte(len(list(c1triples((Any, likes, cheese)))), 3)
215 asserte(len(list(c1triples((Any, hates, cheese)))), 0)
216
217 # unbound subjects without context, same results!
218 asserte(len(list(triples((Any, likes, pizza)))), 2)
219 asserte(len(list(triples((Any, hates, pizza)))), 1)
220 asserte(len(list(triples((Any, likes, cheese)))), 3)
221 asserte(len(list(triples((Any, hates, cheese)))), 0)
222
223 # unbound objects with context
224 asserte(len(list(c1triples((michel, likes, Any)))), 2)
225 asserte(len(list(c1triples((tarek, likes, Any)))), 2)
226 asserte(len(list(c1triples((bob, hates, Any)))), 2)
227 asserte(len(list(c1triples((bob, likes, Any)))), 1)
228
229 # unbound objects without context, same results!
230 asserte(len(list(triples((michel, likes, Any)))), 2)
231 asserte(len(list(triples((tarek, likes, Any)))), 2)
232 asserte(len(list(triples((bob, hates, Any)))), 2)
233 asserte(len(list(triples((bob, likes, Any)))), 1)
234
235 # unbound predicates with context
236 asserte(len(list(c1triples((michel, Any, cheese)))), 1)
237 asserte(len(list(c1triples((tarek, Any, cheese)))), 1)
238 asserte(len(list(c1triples((bob, Any, pizza)))), 1)
239 asserte(len(list(c1triples((bob, Any, michel)))), 1)
240
241 # unbound predicates without context, same results!
242 asserte(len(list(triples((michel, Any, cheese)))), 1)
243 asserte(len(list(triples((tarek, Any, cheese)))), 1)
244 asserte(len(list(triples((bob, Any, pizza)))), 1)
245 asserte(len(list(triples((bob, Any, michel)))), 1)
246
247 # unbound subject, objects with context
248 asserte(len(list(c1triples((Any, hates, Any)))), 2)
249 asserte(len(list(c1triples((Any, likes, Any)))), 5)
250
251 # unbound subject, objects without context, same results!
252 asserte(len(list(triples((Any, hates, Any)))), 2)
253 asserte(len(list(triples((Any, likes, Any)))), 5)
254
255 # unbound predicates, objects with context
256 asserte(len(list(c1triples((michel, Any, Any)))), 2)
257 asserte(len(list(c1triples((bob, Any, Any)))), 3)
258 asserte(len(list(c1triples((tarek, Any, Any)))), 2)
259
260 # unbound predicates, objects without context, same results!
261 asserte(len(list(triples((michel, Any, Any)))), 2)
262 asserte(len(list(triples((bob, Any, Any)))), 3)
263 asserte(len(list(triples((tarek, Any, Any)))), 2)
264
265 # unbound subjects, predicates with context
266 asserte(len(list(c1triples((Any, Any, pizza)))), 3)
267 asserte(len(list(c1triples((Any, Any, cheese)))), 3)
268 asserte(len(list(c1triples((Any, Any, michel)))), 1)
269
270 # unbound subjects, predicates without context, same results!
271 asserte(len(list(triples((Any, Any, pizza)))), 3)
272 asserte(len(list(triples((Any, Any, cheese)))), 3)
273 asserte(len(list(triples((Any, Any, michel)))), 1)
274
275 # all unbound with context
276 asserte(len(list(c1triples((Any, Any, Any)))), 7)
277 # all unbound without context, same result!
278 asserte(len(list(triples((Any, Any, Any)))), 7)
279
280 for c in [graph, self.get_context(c1)]:
281 # unbound subjects
282 asserte(set(c.subjects(likes, pizza)), set((michel, tarek)))
283 asserte(set(c.subjects(hates, pizza)), set((bob,)))
284 asserte(set(c.subjects(likes, cheese)), set([tarek, bob, michel]))
285 asserte(set(c.subjects(hates, cheese)), set())
286
287 # unbound objects
288 asserte(set(c.objects(michel, likes)), set([cheese, pizza]))
289 asserte(set(c.objects(tarek, likes)), set([cheese, pizza]))
290 asserte(set(c.objects(bob, hates)), set([michel, pizza]))
291 asserte(set(c.objects(bob, likes)), set([cheese]))
292
293 # unbound predicates
294 asserte(set(c.predicates(michel, cheese)), set([likes]))
295 asserte(set(c.predicates(tarek, cheese)), set([likes]))
296 asserte(set(c.predicates(bob, pizza)), set([hates]))
297 asserte(set(c.predicates(bob, michel)), set([hates]))
298
299 asserte(set(
300 c.subject_objects(hates)), set([(bob, pizza), (bob, michel)]))
301 asserte(set(c.subject_objects(likes)),
302 set([(tarek, cheese), (michel, cheese),
303 (michel, pizza), (bob, cheese), (tarek, pizza)]))
304
305 asserte(set(c.predicate_objects(
306 michel)), set([(likes, cheese), (likes, pizza)]))
307 asserte(set(c.predicate_objects(bob)), set([(likes,
308 cheese), (hates, pizza), (hates, michel)]))
309 asserte(set(c.predicate_objects(
310 tarek)), set([(likes, cheese), (likes, pizza)]))
311
312 asserte(set(c.subject_predicates(
313 pizza)), set([(bob, hates), (tarek, likes), (michel, likes)]))
314 asserte(set(c.subject_predicates(cheese)), set([(
315 bob, likes), (tarek, likes), (michel, likes)]))
316 asserte(set(c.subject_predicates(michel)), set([(bob, hates)]))
317
318 asserte(set(c), set([
319 (bob, hates, michel), (bob, likes, cheese),
320 (tarek, likes, pizza), (michel, likes, pizza),
321 (michel, likes, cheese), (bob, hates, pizza),
322 (tarek, likes, cheese)]))
323
324 # remove stuff and make sure the graph is empty again
325 self.removeStuff()
326 asserte(len(list(c1triples((Any, Any, Any)))), 0)
327 asserte(len(list(triples((Any, Any, Any)))), 0)
328
329
330 if __name__ == "__main__":
331 unittest.main()