def test_issue_4(self): ident = URIRef("rdflib_test") g = Graph(store="SQLAlchemy", identifier=ident) rt = g.open(self.uri, create=True) if rt == NO_STORE: g.open(self.uri, create=True) else: assert rt == VALID_STORE, "The underlying store is corrupt" g.destroy(self.uri)
def test_issue_4(self): ident = URIRef("rdflib_test") g = Graph(store="SQLAlchemy", identifier=ident) rt = g.open(self.uri, create=True) if rt == NO_STORE: g.open(self.uri, create=True) else: assert rt == VALID_STORE, "The underlying store is corrupt" g.destroy(self.uri)
def test_issue_4(self): from rdflib.graph import ConjunctiveGraph as Graph from rdflib.store import NO_STORE, VALID_STORE from rdflib.term import URIRef ident = URIRef("rdflib_test") g = Graph(store="SQLAlchemy", identifier=ident) rt = g.open(self.uri, create=True) if rt == NO_STORE: g.open(self.uri, create=True) else: assert rt == VALID_STORE, "The underlying store is corrupt" g.destroy(self.uri)
def test_issue_4(self): from rdflib.graph import ConjunctiveGraph as Graph from rdflib.store import NO_STORE, VALID_STORE from rdflib.term import URIRef ident = URIRef("rdflib_test") g = Graph(store="SQLAlchemy", identifier=ident) rt = g.open(self.uri, create=True) if rt == NO_STORE: g.open(self.uri, create=True) else: assert rt == VALID_STORE, "The underlying store is corrupt" g.destroy(self.uri)
class ContextTest(TestCase): """ Testing different contexts. Heavily based on https://github.com/RDFLib/rdflib-postgresql/blob/master/test/context_case.py """ # noqa: E501 store_name = "Django" storetest = True path = "" create = True michel = URIRef(u'michel') tarek = URIRef(u'tarek') bob = URIRef(u'bob') likes = URIRef(u'likes') hates = URIRef(u'hates') pizza = URIRef(u'pizza') cheese = URIRef(u'cheese') c1 = URIRef(u'context-1') c2 = URIRef(u'context-2') def setUp(self): self.graph = ConjunctiveGraph(store=self.store_name) self.graph.destroy(self.path) self.graph.open(self.path, create=self.create) def tearDown(self): self.graph.destroy(self.path) self.graph.close() def get_context(self, identifier): assert isinstance(identifier, URIRef) or isinstance(identifier, BNode), type(identifier) # noqa: E501 return Graph(store=self.graph.store, identifier=identifier, namespace_manager=self) # noqa: E501 def addStuff(self): tarek = self.tarek michel = self.michel bob = self.bob likes = self.likes hates = self.hates pizza = self.pizza cheese = self.cheese c1 = self.c1 graph = Graph(self.graph.store, c1) graph.add((tarek, likes, pizza)) graph.add((tarek, likes, cheese)) graph.add((michel, likes, pizza)) graph.add((michel, likes, cheese)) graph.add((bob, likes, cheese)) graph.add((bob, hates, pizza)) graph.add((bob, hates, michel)) def removeStuff(self): tarek = self.tarek michel = self.michel bob = self.bob likes = self.likes hates = self.hates pizza = self.pizza cheese = self.cheese c1 = self.c1 graph = Graph(self.graph.store, c1) graph.remove((tarek, likes, pizza)) graph.remove((tarek, likes, cheese)) graph.remove((michel, likes, pizza)) graph.remove((michel, likes, cheese)) graph.remove((bob, likes, cheese)) graph.remove((bob, hates, pizza)) graph.remove((bob, hates, michel)) def addStuffInMultipleContexts(self): c1 = self.c1 c2 = self.c2 triple = (self.pizza, self.hates, self.tarek) # add to default context self.graph.add(triple) # add to context 1 graph = Graph(self.graph.store, c1) graph.add(triple) # add to context 2 graph = Graph(self.graph.store, c2) graph.add(triple) def testConjunction(self): self.addStuffInMultipleContexts() triple = (self.pizza, self.likes, self.pizza) # add to context 1 graph = Graph(self.graph.store, self.c1) graph.add(triple) self.assertEquals(len(graph), 2) self.assertEquals(len(self.graph), 2) def testAdd(self): self.addStuff() def testRemove(self): self.addStuff() self.removeStuff() def testLenInOneContext(self): c1 = self.c1 # make sure context is empty self.graph.remove_context(self.get_context(c1)) graph = Graph(self.graph.store, c1) oldLen = len(self.graph) for _ in range(0, 10): graph.add((BNode(), self.hates, self.hates)) self.assertEquals(len(graph), oldLen + 10) self.assertEquals(len(self.get_context(c1)), oldLen + 10) self.graph.remove_context(self.get_context(c1)) self.assertEquals(len(self.graph), oldLen) self.assertEquals(len(graph), 0) def testLenInMultipleContexts(self): oldLen = len(self.graph) self.addStuffInMultipleContexts() # addStuffInMultipleContexts is adding the same triple to # three different contexts. So it's only + 1 self.assertEquals(len(self.graph), oldLen + 1) graph = Graph(self.graph.store, self.c1) self.assertEquals(len(graph), oldLen + 1) def testRemoveInMultipleContexts(self): c1 = self.c1 c2 = self.c2 triple = (self.pizza, self.hates, self.tarek) self.addStuffInMultipleContexts() # triple should be still in store after removing it from c1 + c2 self.assertIn(triple, self.graph) graph = Graph(self.graph.store, c1) graph.remove(triple) self.assertIn(triple, self.graph) graph = Graph(self.graph.store, c2) graph.remove(triple) self.assertIn(triple, self.graph) self.graph.remove(triple) # now gone! self.assertNotIn(triple, self.graph) # add again and see if remove without context removes all triples! self.addStuffInMultipleContexts() self.graph.remove(triple) self.assertNotIn(triple, self.graph) def testContexts(self): triple = (self.pizza, self.hates, self.tarek) self.addStuffInMultipleContexts() def cid(c): if not isinstance(c, str): return c.identifier return c self.assertIn(self.c1, [cid(c) for c in self.graph.contexts()]) self.assertIn(self.c2, [cid(c) for c in self.graph.contexts()]) contextList = [cid(c) for c in self.graph.contexts(triple)] self.assertIn(self.c1, contextList) self.assertIn(self.c2, contextList) def testRemoveContext(self): c1 = self.c1 self.addStuffInMultipleContexts() self.assertEquals(len(Graph(self.graph.store, c1)), 1) self.assertEquals(len(self.get_context(c1)), 1) self.graph.remove_context(self.get_context(c1)) self.assert_(self.c1 not in self.graph.contexts()) def testRemoveAny(self): Any = None self.addStuffInMultipleContexts() self.graph.remove((Any, Any, Any)) self.assertEquals(len(self.graph), 0) def testTriples(self): triples = self.graph.triples graph = self.graph c1graph = Graph(self.graph.store, self.c1) c1triples = c1graph.triples Any = None self.addStuff() # unbound subjects with context self.assertEquals( len( list( c1triples((Any, self.likes, self.pizza)) ) ), 2 ) self.assertEquals( len( list( c1triples((Any, self.hates, self.pizza)) ) ), 1 ) self.assertEquals( len( list( c1triples((Any, self.likes, self.cheese)) ) ), 3 ) self.assertEquals( len( list( c1triples((Any, self.hates, self.cheese)) ) ), 0 ) # unbound subjects without context, same results! self.assertEquals(len(list(triples((Any, self.likes, self.pizza)))), 2) self.assertEquals(len(list(triples((Any, self.hates, self.pizza)))), 1) self.assertEquals(len(list(triples((Any, self.likes, self.cheese)))), 3) self.assertEquals(len(list(triples((Any, self.hates, self.cheese)))), 0) # unbound objects with context self.assertEquals(len(list(c1triples((self.michel, self.likes, Any)))), 2) self.assertEquals(len(list(c1triples((self.tarek, self.likes, Any)))), 2) self.assertEquals(len(list(c1triples((self.bob, self.hates, Any)))), 2) self.assertEquals(len(list(c1triples((self.bob, self.likes, Any)))), 1) # unbound objects without context, same results! self.assertEquals(len(list(triples((self.michel, self.likes, Any)))), 2) self.assertEquals(len(list(triples((self.tarek, self.likes, Any)))), 2) self.assertEquals(len(list(triples((self.bob, self.hates, Any)))), 2) self.assertEquals(len(list(triples((self.bob, self.likes, Any)))), 1) # unbound predicates with context self.assertEquals(len(list(c1triples((self.michel, Any, self.cheese)))), 1) self.assertEquals(len(list(c1triples((self.tarek, Any, self.cheese)))), 1) self.assertEquals(len(list(c1triples((self.bob, Any, self.pizza)))), 1) self.assertEquals(len(list(c1triples((self.bob, Any, self.michel)))), 1) # unbound predicates without context, same results! self.assertEquals(len(list(triples((self.michel, Any, self.cheese)))), 1) self.assertEquals(len(list(triples((self.tarek, Any, self.cheese)))), 1) self.assertEquals(len(list(triples((self.bob, Any, self.pizza)))), 1) self.assertEquals(len(list(triples((self.bob, Any, self.michel)))), 1) # unbound subject, objects with context self.assertEquals(len(list(c1triples((Any, self.hates, Any)))), 2) self.assertEquals(len(list(c1triples((Any, self.likes, Any)))), 5) # unbound subject, objects without context, same results! self.assertEquals(len(list(triples((Any, self.hates, Any)))), 2) self.assertEquals(len(list(triples((Any, self.likes, Any)))), 5) # unbound predicates, objects with context self.assertEquals(len(list(c1triples((self.michel, Any, Any)))), 2) self.assertEquals(len(list(c1triples((self.bob, Any, Any)))), 3) self.assertEquals(len(list(c1triples((self.tarek, Any, Any)))), 2) # unbound predicates, objects without context, same results! self.assertEquals(len(list(triples((self.michel, Any, Any)))), 2) self.assertEquals(len(list(triples((self.bob, Any, Any)))), 3) self.assertEquals(len(list(triples((self.tarek, Any, Any)))), 2) # unbound subjects, predicates with context self.assertEquals(len(list(c1triples((Any, Any, self.pizza)))), 3) self.assertEquals(len(list(c1triples((Any, Any, self.cheese)))), 3) self.assertEquals(len(list(c1triples((Any, Any, self.michel)))), 1) # unbound subjects, predicates without context, same results! self.assertEquals(len(list(triples((Any, Any, self.pizza)))), 3) self.assertEquals(len(list(triples((Any, Any, self.cheese)))), 3) self.assertEquals(len(list(triples((Any, Any, self.michel)))), 1) # all unbound with context self.assertEquals(len(list(c1triples((Any, Any, Any)))), 7) # all unbound without context, same result! self.assertEquals(len(list(triples((Any, Any, Any)))), 7) for c in [graph, self.get_context(self.c1)]: # unbound subjects self.assertEquals(set(c.subjects(self.likes, self.pizza)), {self.michel, self.tarek}) self.assertEquals(set(c.subjects(self.hates, self.pizza)), {self.bob}) self.assertEquals(set(c.subjects(self.likes, self.cheese)), {self.tarek, self.bob, self.michel}) self.assertEquals(set(c.subjects(self.hates, self.cheese)), set()) # unbound objects self.assertEquals(set(c.objects(self.michel, self.likes)), {self.cheese, self.pizza}) self.assertEquals(set(c.objects(self.tarek, self.likes)), {self.cheese, self.pizza}) self.assertEquals(set(c.objects(self.bob, self.hates)), {self.michel, self.pizza}) self.assertEquals(set(c.objects(self.bob, self.likes)), {self.cheese}) # unbound predicates self.assertEquals( set( c.predicates(self.michel, self.cheese) ), {self.likes} ) self.assertEquals( set( c.predicates(self.tarek, self.cheese) ), {self.likes} ) self.assertEquals(set(c.predicates(self.bob, self.pizza)), {self.hates}) self.assertEquals(set(c.predicates(self.bob, self.michel)), {self.hates}) self.assertEquals(set(c.subject_objects(self.hates)), {(self.bob, self.pizza), (self.bob, self.michel)}) self.assertEquals(set(c.subject_objects(self.likes)), {(self.tarek, self.cheese), (self.michel, self.cheese), (self.michel, self.pizza), (self.bob, self.cheese), (self.tarek, self.pizza)}) self.assertEquals(set(c.predicate_objects(self.michel)), {(self.likes, self.cheese), (self.likes, self.pizza)}) self.assertEquals(set(c.predicate_objects(self.bob)), {(self.likes, self.cheese), (self.hates, self.pizza), (self.hates, self.michel)}) self.assertEquals(set(c.predicate_objects(self.tarek)), {(self.likes, self.cheese), (self.likes, self.pizza)}) self.assertEquals(set(c.subject_predicates(self.pizza)), {(self.bob, self.hates), (self.tarek, self.likes), (self.michel, self.likes)}) self.assertEquals(set(c.subject_predicates(self.cheese)), {(self.bob, self.likes), (self.tarek, self.likes), (self.michel, self.likes)}) self.assertEquals(set(c.subject_predicates(self.michel)), {(self.bob, self.hates)}) self.assertEquals(set(c), {(self.bob, self.hates, self.michel), (self.bob, self.likes, self.cheese), (self.tarek, self.likes, self.pizza), (self.michel, self.likes, self.pizza), (self.michel, self.likes, self.cheese), (self.bob, self.hates, self.pizza), (self.tarek, self.likes, self.cheese)}) # remove stuff and make sure the graph is empty again self.removeStuff() self.assertEquals(len(list(c1triples((Any, Any, Any)))), 0) self.assertEquals(len(list(triples((Any, Any, Any)))), 0)
def testFormulaStore(store="default", configString=None): try: g = ConjunctiveGraph(store=store) except ImportError: raise SkipTest("Dependencies for store '%s' not available!" % store) if configString: g.destroy(configString) g.open(configString) else: if store == 'SQLite': _, path = mkstemp(prefix='test', dir='/tmp', suffix='.sqlite') g.open(path, create=True) else: g.open(mkdtemp(), create=True) g.parse(data=testN3, format="n3") try: for s, p, o in g.triples((None, implies, None)): formulaA = s formulaB = o assert type(formulaA) == QuotedGraph and type(formulaB) == QuotedGraph # a = URIRef('http://test/a') b = URIRef('http://test/b') c = URIRef('http://test/c') d = URIRef('http://test/d') v = Variable('y') universe = ConjunctiveGraph(g.store) #test formula as terms assert len(list(universe.triples((formulaA, implies, formulaB)))) == 1 #test variable as term and variable roundtrip assert len(list(formulaB.triples((None, None, v)))) == 1 for s, p, o in formulaB.triples((None, d, None)): if o != c: assert isinstance(o, Variable) assert o == v s = list(universe.subjects(RDF.type, RDFS.Class))[0] assert isinstance(s, BNode) assert len(list(universe.triples((None, implies, None)))) == 1 assert len(list(universe.triples((None, RDF.type, None)))) == 1 assert len(list(formulaA.triples((None, RDF.type, None)))) == 1 assert len(list(formulaA.triples((None, None, None)))) == 2 assert len(list(formulaB.triples((None, None, None)))) == 2 assert len(list(universe.triples((None, None, None)))) == 3 assert len(list(formulaB.triples( (None, URIRef('http://test/d'), None)))) == 2 assert len(list(universe.triples( (None, URIRef('http://test/d'), None)))) == 1 # #context tests # #test contexts with triple argument # assert len(list(universe.contexts((a, d, c)))) == 1, \ # [ct for ct in universe.contexts((a, d, c))] # FAIL: test.test_graph_formula.testFormulaStores('SQLite',) # -------------------------------------------------------------------- # Traceback (most recent call last): # File ".../site-packages/nose/case.py", line 197, in runTest # self.test(*self.arg) # File ".../test_graph_formula.py", line 80, in testFormulaStore # [ct for ct in universe.contexts((a, d, c))] # AssertionError: [ # <Graph identifier=N52fd4417ef7641089b2e4045ef19ad87 # (<class 'rdflib.graph.Graph'>)>, # <Graph identifier=_:Formula16 (<class 'rdflib.graph.Graph'>)> # ] #Remove test cases universe.remove((None, implies, None)) assert len(list(universe.triples((None, implies, None)))) == 0 assert len(list(formulaA.triples((None, None, None)))) == 2 assert len(list(formulaB.triples((None, None, None)))) == 2 formulaA.remove((None, b, None)) assert len(list(formulaA.triples((None, None, None)))) == 1 formulaA.remove((None, RDF.type, None)) assert len(list(formulaA.triples((None, None, None)))) == 0 universe.remove((None, RDF.type, RDFS.Class)) #remove_context tests universe.remove_context(formulaB) assert len(list(universe.triples((None, RDF.type, None)))) == 0 assert len(universe) == 1 assert len(formulaB) == 0 universe.remove((None, None, None)) assert len(universe) == 0 g.close() if store == 'SQLite': os.unlink(path) else: g.store.destroy(configString) except: g.close() if store == 'SQLite': os.unlink(path) else: g.store.destroy(configString) raise
class ContextTest(test.TestCase): """ Testing different contexts. Heavily based on https://github.com/RDFLib/rdflib-postgresql/blob/master/test/context_case.py """ store_name = "Django" storetest = True path = "" create = True michel = URIRef(u'michel') tarek = URIRef(u'tarek') bob = URIRef(u'bob') likes = URIRef(u'likes') hates = URIRef(u'hates') pizza = URIRef(u'pizza') cheese = URIRef(u'cheese') c1 = URIRef(u'context-1') c2 = URIRef(u'context-2') def setUp(self): self.graph = ConjunctiveGraph(store=self.store_name) self.graph.destroy(self.path) self.graph.open(self.path, create=self.create) def tearDown(self): self.graph.destroy(self.path) self.graph.close() def get_context(self, identifier): assert isinstance(identifier, URIRef) or isinstance(identifier, BNode), type(identifier) return Graph(store=self.graph.store, identifier=identifier, namespace_manager=self) def addStuff(self): tarek = self.tarek michel = self.michel bob = self.bob likes = self.likes hates = self.hates pizza = self.pizza cheese = self.cheese c1 = self.c1 graph = Graph(self.graph.store, c1) graph.add((tarek, likes, pizza)) graph.add((tarek, likes, cheese)) graph.add((michel, likes, pizza)) graph.add((michel, likes, cheese)) graph.add((bob, likes, cheese)) graph.add((bob, hates, pizza)) graph.add((bob, hates, michel)) def removeStuff(self): tarek = self.tarek michel = self.michel bob = self.bob likes = self.likes hates = self.hates pizza = self.pizza cheese = self.cheese c1 = self.c1 graph = Graph(self.graph.store, c1) graph.remove((tarek, likes, pizza)) graph.remove((tarek, likes, cheese)) graph.remove((michel, likes, pizza)) graph.remove((michel, likes, cheese)) graph.remove((bob, likes, cheese)) graph.remove((bob, hates, pizza)) graph.remove((bob, hates, michel)) def addStuffInMultipleContexts(self): c1 = self.c1 c2 = self.c2 triple = (self.pizza, self.hates, self.tarek) # add to default context self.graph.add(triple) # add to context 1 graph = Graph(self.graph.store, c1) graph.add(triple) # add to context 2 graph = Graph(self.graph.store, c2) graph.add(triple) def testConjunction(self): self.addStuffInMultipleContexts() triple = (self.pizza, self.likes, self.pizza) # add to context 1 graph = Graph(self.graph.store, self.c1) graph.add(triple) self.assertEquals(len(graph), 2) self.assertEquals(len(self.graph), 2) def testAdd(self): self.addStuff() def testRemove(self): self.addStuff() self.removeStuff() def testLenInOneContext(self): c1 = self.c1 # make sure context is empty self.graph.remove_context(self.get_context(c1)) graph = Graph(self.graph.store, c1) oldLen = len(self.graph) for _ in range(0, 10): graph.add((BNode(), self.hates, self.hates)) self.assertEquals(len(graph), oldLen + 10) self.assertEquals(len(self.get_context(c1)), oldLen + 10) self.graph.remove_context(self.get_context(c1)) self.assertEquals(len(self.graph), oldLen) self.assertEquals(len(graph), 0) def testLenInMultipleContexts(self): oldLen = len(self.graph) self.addStuffInMultipleContexts() # addStuffInMultipleContexts is adding the same triple to # three different contexts. So it's only + 1 self.assertEquals(len(self.graph), oldLen + 1) graph = Graph(self.graph.store, self.c1) self.assertEquals(len(graph), oldLen + 1) def testRemoveInMultipleContexts(self): c1 = self.c1 c2 = self.c2 triple = (self.pizza, self.hates, self.tarek) self.addStuffInMultipleContexts() # triple should be still in store after removing it from c1 + c2 self.assertIn(triple, self.graph) graph = Graph(self.graph.store, c1) graph.remove(triple) self.assertIn(triple, self.graph) graph = Graph(self.graph.store, c2) graph.remove(triple) self.assertIn(triple, self.graph) self.graph.remove(triple) # now gone! self.assertNotIn(triple, self.graph) # add again and see if remove without context removes all triples! self.addStuffInMultipleContexts() self.graph.remove(triple) self.assertNotIn(triple, self.graph) def testContexts(self): triple = (self.pizza, self.hates, self.tarek) self.addStuffInMultipleContexts() def cid(c): if not isinstance(c, basestring): return c.identifier return c self.assertIn(self.c1, [cid(c) for c in self.graph.contexts()]) self.assertIn(self.c2, [cid(c) for c in self.graph.contexts()]) contextList = [cid(c) for c in self.graph.contexts(triple)] self.assertIn(self.c1, contextList) self.assertIn(self.c2, contextList) def testRemoveContext(self): c1 = self.c1 self.addStuffInMultipleContexts() self.assertEquals(len(Graph(self.graph.store, c1)), 1) self.assertEquals(len(self.get_context(c1)), 1) self.graph.remove_context(self.get_context(c1)) self.assert_(self.c1 not in self.graph.contexts()) def testRemoveAny(self): Any = None self.addStuffInMultipleContexts() self.graph.remove((Any, Any, Any)) self.assertEquals(len(self.graph), 0) def testTriples(self): triples = self.graph.triples graph = self.graph c1graph = Graph(self.graph.store, self.c1) c1triples = c1graph.triples Any = None self.addStuff() # unbound subjects with context self.assertEquals(len(list(c1triples((Any, self.likes, self.pizza)))), 2) self.assertEquals(len(list(c1triples((Any, self.hates, self.pizza)))), 1) self.assertEquals(len(list(c1triples((Any, self.likes, self.cheese)))), 3) self.assertEquals(len(list(c1triples((Any, self.hates, self.cheese)))), 0) # unbound subjects without context, same results! self.assertEquals(len(list(triples((Any, self.likes, self.pizza)))), 2) self.assertEquals(len(list(triples((Any, self.hates, self.pizza)))), 1) self.assertEquals(len(list(triples((Any, self.likes, self.cheese)))), 3) self.assertEquals(len(list(triples((Any, self.hates, self.cheese)))), 0) # unbound objects with context self.assertEquals(len(list(c1triples((self.michel, self.likes, Any)))), 2) self.assertEquals(len(list(c1triples((self.tarek, self.likes, Any)))), 2) self.assertEquals(len(list(c1triples((self.bob, self.hates, Any)))), 2) self.assertEquals(len(list(c1triples((self.bob, self.likes, Any)))), 1) # unbound objects without context, same results! self.assertEquals(len(list(triples((self.michel, self.likes, Any)))), 2) self.assertEquals(len(list(triples((self.tarek, self.likes, Any)))), 2) self.assertEquals(len(list(triples((self.bob, self.hates, Any)))), 2) self.assertEquals(len(list(triples((self.bob, self.likes, Any)))), 1) # unbound predicates with context self.assertEquals(len(list(c1triples((self.michel, Any, self.cheese)))), 1) self.assertEquals(len(list(c1triples((self.tarek, Any, self.cheese)))), 1) self.assertEquals(len(list(c1triples((self.bob, Any, self.pizza)))), 1) self.assertEquals(len(list(c1triples((self.bob, Any, self.michel)))), 1) # unbound predicates without context, same results! self.assertEquals(len(list(triples((self.michel, Any, self.cheese)))), 1) self.assertEquals(len(list(triples((self.tarek, Any, self.cheese)))), 1) self.assertEquals(len(list(triples((self.bob, Any, self.pizza)))), 1) self.assertEquals(len(list(triples((self.bob, Any, self.michel)))), 1) # unbound subject, objects with context self.assertEquals(len(list(c1triples((Any, self.hates, Any)))), 2) self.assertEquals(len(list(c1triples((Any, self.likes, Any)))), 5) # unbound subject, objects without context, same results! self.assertEquals(len(list(triples((Any, self.hates, Any)))), 2) self.assertEquals(len(list(triples((Any, self.likes, Any)))), 5) # unbound predicates, objects with context self.assertEquals(len(list(c1triples((self.michel, Any, Any)))), 2) self.assertEquals(len(list(c1triples((self.bob, Any, Any)))), 3) self.assertEquals(len(list(c1triples((self.tarek, Any, Any)))), 2) # unbound predicates, objects without context, same results! self.assertEquals(len(list(triples((self.michel, Any, Any)))), 2) self.assertEquals(len(list(triples((self.bob, Any, Any)))), 3) self.assertEquals(len(list(triples((self.tarek, Any, Any)))), 2) # unbound subjects, predicates with context self.assertEquals(len(list(c1triples((Any, Any, self.pizza)))), 3) self.assertEquals(len(list(c1triples((Any, Any, self.cheese)))), 3) self.assertEquals(len(list(c1triples((Any, Any, self.michel)))), 1) # unbound subjects, predicates without context, same results! self.assertEquals(len(list(triples((Any, Any, self.pizza)))), 3) self.assertEquals(len(list(triples((Any, Any, self.cheese)))), 3) self.assertEquals(len(list(triples((Any, Any, self.michel)))), 1) # all unbound with context self.assertEquals(len(list(c1triples((Any, Any, Any)))), 7) # all unbound without context, same result! self.assertEquals(len(list(triples((Any, Any, Any)))), 7) for c in [graph, self.get_context(self.c1)]: # unbound subjects self.assertEquals(set(c.subjects(self.likes, self.pizza)), {self.michel, self.tarek}) self.assertEquals(set(c.subjects(self.hates, self.pizza)), {self.bob}) self.assertEquals(set(c.subjects(self.likes, self.cheese)), {self.tarek, self.bob, self.michel}) self.assertEquals(set(c.subjects(self.hates, self.cheese)), set()) # unbound objects self.assertEquals(set(c.objects(self.michel, self.likes)), {self.cheese, self.pizza}) self.assertEquals(set(c.objects(self.tarek, self.likes)), {self.cheese, self.pizza}) self.assertEquals(set(c.objects(self.bob, self.hates)), {self.michel, self.pizza}) self.assertEquals(set(c.objects(self.bob, self.likes)), {self.cheese}) # unbound predicates self.assertEquals(set(c.predicates(self.michel, self.cheese)), {self.likes}) self.assertEquals(set(c.predicates(self.tarek, self.cheese)), {self.likes}) self.assertEquals(set(c.predicates(self.bob, self.pizza)), {self.hates}) self.assertEquals(set(c.predicates(self.bob, self.michel)), {self.hates}) self.assertEquals(set(c.subject_objects(self.hates)), {(self.bob, self.pizza), (self.bob, self.michel)}) self.assertEquals(set(c.subject_objects(self.likes)), {(self.tarek, self.cheese), (self.michel, self.cheese), (self.michel, self.pizza), (self.bob, self.cheese), (self.tarek, self.pizza)}) self.assertEquals(set(c.predicate_objects(self.michel)), {(self.likes, self.cheese), (self.likes, self.pizza)}) self.assertEquals(set(c.predicate_objects(self.bob)), {(self.likes, self.cheese), (self.hates, self.pizza), (self.hates, self.michel)}) self.assertEquals(set(c.predicate_objects(self.tarek)), {(self.likes, self.cheese), (self.likes, self.pizza)}) self.assertEquals(set(c.subject_predicates(self.pizza)), {(self.bob, self.hates), (self.tarek, self.likes), (self.michel, self.likes)}) self.assertEquals(set(c.subject_predicates(self.cheese)), {(self.bob, self.likes), (self.tarek, self.likes), (self.michel, self.likes)}) self.assertEquals(set(c.subject_predicates(self.michel)), {(self.bob, self.hates)}) self.assertEquals(set(c), {(self.bob, self.hates, self.michel), (self.bob, self.likes, self.cheese), (self.tarek, self.likes, self.pizza), (self.michel, self.likes, self.pizza), (self.michel, self.likes, self.cheese), (self.bob, self.hates, self.pizza), (self.tarek, self.likes, self.cheese)}) # remove stuff and make sure the graph is empty again self.removeStuff() self.assertEquals(len(list(c1triples((Any, Any, Any)))), 0) self.assertEquals(len(list(triples((Any, Any, Any)))), 0)
def testN3Store(store="default", configString=None): storetest = True g = ConjunctiveGraph(store=store) if configString is not None: g.destroy(configString) g.open(configString, create=True) g.parse(data=testN3, format="n3") # op = g.serialize(format="n3") # print(op) formulaA = BNode() formulaB = BNode() try: for s,p,o in g.triples((None,implies,None)): formulaA = s formulaB = o assert type(formulaA)==QuotedGraph and type(formulaB)==QuotedGraph a = URIRef('http://test/a') b = URIRef('http://test/b') c = URIRef('http://test/c') d = URIRef('http://test/d') v = Variable('y') universe = ConjunctiveGraph(g.store) #test formula as terms assert len(list(universe.triples((formulaA,implies,formulaB))))==1 #test variable as term and variable roundtrip assert len(list(formulaB.triples((None,None,v))))==1 for s,p,o in formulaB.triples((None,d,None)): if o != c: assert isinstance(o,Variable) assert o == v s = list(universe.subjects(RDF.type, RDFS.Class))[0] assert isinstance(s,BNode) assert len(list(universe.triples((None,implies,None)))) == 1 assert len(list(universe.triples((None,RDF.type,None)))) ==1 assert len(list(formulaA.triples((None,RDF.type,None))))==1 assert len(list(formulaA.triples((None,None,None))))==2 assert len(list(formulaB.triples((None,None,None))))==2 assert len(list(universe.triples((None,None,None))))==3 assert len(list(formulaB.triples((None,URIRef('http://test/d'),None))))==2 assert len(list(universe.triples((None,URIRef('http://test/d'),None))))==1 #context tests #test contexts with triple argument assert len(list(universe.contexts((a,d,c))))==1 #Remove test cases universe.remove((None,implies,None)) assert len(list(universe.triples((None,implies,None))))==0 assert len(list(formulaA.triples((None,None,None))))==2 assert len(list(formulaB.triples((None,None,None))))==2 formulaA.remove((None,b,None)) assert len(list(formulaA.triples((None,None,None))))==1 formulaA.remove((None,RDF.type,None)) assert len(list(formulaA.triples((None,None,None))))==0 universe.remove((None,RDF.type,RDFS.Class)) #remove_context tests universe.remove_context(formulaB) assert len(list(universe.triples((None,RDF.type,None))))==0 assert len(universe)==1 assert len(formulaB)==0 universe.remove((None,None,None)) assert len(universe)==0 g.store.destroy(configString) except: g.store.destroy(configString) raise
class TestLevelDBConjunctiveGraphCore(unittest.TestCase): def setUp(self): store = "LevelDB" self.graph = ConjunctiveGraph(store=store) self.path = configString self.graph.open(self.path, create=True) def tearDown(self): self.graph.destroy(self.path) try: self.graph.close() except: pass if getattr(self, 'path', False) and self.path is not None: if os.path.exists(self.path): if os.path.isdir(self.path): for f in os.listdir(self.path): os.unlink(self.path + '/' + f) os.rmdir(self.path) elif len(self.path.split(':')) == 1: os.unlink(self.path) else: os.remove(self.path) def test_namespaces(self): self.graph.bind("dc", "http://http://purl.org/dc/elements/1.1/") self.graph.bind("foaf", "http://xmlns.com/foaf/0.1/") self.assert_(len(list(self.graph.namespaces())) == 5) self.assert_(('foaf', rdflib.term.URIRef(u'http://xmlns.com/foaf/0.1/') ) in list(self.graph.namespaces())) def test_readable_index(self): print(readable_index(111)) def test_triples_context_reset(self): michel = rdflib.URIRef(u'michel') likes = rdflib.URIRef(u'likes') pizza = rdflib.URIRef(u'pizza') cheese = rdflib.URIRef(u'cheese') self.graph.add((michel, likes, pizza)) self.graph.add((michel, likes, cheese)) self.graph.commit() ntriples = self.graph.triples((None, None, None), context=self.graph.store) self.assert_(len(list(ntriples)) == 2) def test_remove_context_reset(self): michel = rdflib.URIRef(u'michel') likes = rdflib.URIRef(u'likes') pizza = rdflib.URIRef(u'pizza') cheese = rdflib.URIRef(u'cheese') self.graph.add((michel, likes, pizza)) self.graph.add((michel, likes, cheese)) self.graph.commit() self.graph.store.remove((michel, likes, cheese), self.graph.store) self.graph.commit() self.assert_( len( list( self.graph.triples((None, None, None), context=self.graph.store))) == 1) def test_remove_db_exception(self): michel = rdflib.URIRef(u'michel') likes = rdflib.URIRef(u'likes') pizza = rdflib.URIRef(u'pizza') cheese = rdflib.URIRef(u'cheese') self.graph.add((michel, likes, pizza)) self.graph.add((michel, likes, cheese)) self.graph.commit() self.graph.store.__len__(context=self.graph.store) self.assert_( len( list( self.graph.triples((None, None, None), context=self.graph.store))) == 2)
class TestKyotoCabinetConjunctiveGraphCore(unittest.TestCase): def setUp(self): store = "KyotoCabinet" self.graph = ConjunctiveGraph(store=store) self.path = configString self.graph.open(self.path, create=True) def tearDown(self): self.graph.destroy(self.path) try: self.graph.close() except: pass if getattr(self, "path", False) and self.path is not None: if os.path.exists(self.path): if os.path.isdir(self.path): for f in os.listdir(self.path): os.unlink(self.path + "/" + f) os.rmdir(self.path) elif len(self.path.split(":")) == 1: os.unlink(self.path) else: os.remove(self.path) def test_namespaces(self): self.graph.bind("dc", "http://http://purl.org/dc/elements/1.1/") self.graph.bind("foaf", "http://xmlns.com/foaf/0.1/") self.assert_(len(list(self.graph.namespaces())) == 5) self.assert_(("foaf", rdflib.term.URIRef(u"http://xmlns.com/foaf/0.1/")) in list(self.graph.namespaces())) def test_play_journal(self): self.assertRaises(NotImplementedError, self.graph.store.play_journal, {"graph": self.graph}) def test_readable_index(self): print(readable_index(111)) def test_triples_context_reset(self): michel = rdflib.URIRef(u"michel") likes = rdflib.URIRef(u"likes") pizza = rdflib.URIRef(u"pizza") cheese = rdflib.URIRef(u"cheese") self.graph.add((michel, likes, pizza)) self.graph.add((michel, likes, cheese)) self.graph.commit() ntriples = self.graph.triples((None, None, None), context=self.graph.store) self.assert_(len(list(ntriples)) == 2) def test_remove_context_reset(self): michel = rdflib.URIRef(u"michel") likes = rdflib.URIRef(u"likes") pizza = rdflib.URIRef(u"pizza") cheese = rdflib.URIRef(u"cheese") self.graph.add((michel, likes, pizza)) self.graph.add((michel, likes, cheese)) self.graph.commit() self.graph.store.remove((michel, likes, cheese), self.graph.store) self.graph.commit() self.assert_(len(list(self.graph.triples((None, None, None), context=self.graph.store))) == 1) def test_remove_db_exception(self): michel = rdflib.URIRef(u"michel") likes = rdflib.URIRef(u"likes") pizza = rdflib.URIRef(u"pizza") cheese = rdflib.URIRef(u"cheese") self.graph.add((michel, likes, pizza)) self.graph.add((michel, likes, cheese)) self.graph.commit() self.graph.store.__len__(context=self.graph.store) self.assert_(len(list(self.graph.triples((None, None, None), context=self.graph.store))) == 2)
def testN3Store(store="default", configString=None): g = ConjunctiveGraph(store=store) if configString: g.destroy(configString) g.open(configString) g.parse(data=testN3, format="n3") print g.store try: for s, p, o in g.triples((None, implies, None)): formulaA = s formulaB = o assert type(formulaA) == QuotedGraph and type(formulaB) == QuotedGraph a = URIRef('http://test/a') b = URIRef('http://test/b') c = URIRef('http://test/c') d = URIRef('http://test/d') v = Variable('y') universe = ConjunctiveGraph(g.store) #test formula as terms assert len(list(universe.triples((formulaA, implies, formulaB)))) == 1 #test variable as term and variable roundtrip assert len(list(formulaB.triples((None, None, v)))) == 1 for s, p, o in formulaB.triples((None, d, None)): if o != c: assert isinstance(o, Variable) assert o == v s = list(universe.subjects(RDF.type, RDFS.Class))[0] assert isinstance(s, BNode) assert len(list(universe.triples((None, implies, None)))) == 1 assert len(list(universe.triples((None, RDF.type, None)))) == 1 assert len(list(formulaA.triples((None, RDF.type, None)))) == 1 assert len(list(formulaA.triples((None, None, None)))) == 2 assert len(list(formulaB.triples((None, None, None)))) == 2 assert len(list(universe.triples((None, None, None)))) == 3 assert len( list(formulaB.triples((None, URIRef('http://test/d'), None)))) == 2 assert len( list(universe.triples((None, URIRef('http://test/d'), None)))) == 1 #context tests #test contexts with triple argument assert len(list(universe.contexts((a, d, c)))) == 1 #Remove test cases universe.remove((None, implies, None)) assert len(list(universe.triples((None, implies, None)))) == 0 assert len(list(formulaA.triples((None, None, None)))) == 2 assert len(list(formulaB.triples((None, None, None)))) == 2 formulaA.remove((None, b, None)) assert len(list(formulaA.triples((None, None, None)))) == 1 formulaA.remove((None, RDF.type, None)) assert len(list(formulaA.triples((None, None, None)))) == 0 universe.remove((None, RDF.type, RDFS.Class)) #remove_context tests universe.remove_context(formulaB) assert len(list(universe.triples((None, RDF.type, None)))) == 0 assert len(universe) == 1 assert len(formulaB) == 0 universe.remove((None, None, None)) assert len(universe) == 0 g.store.destroy(configString) except: g.store.destroy(configString) raise
def testFormulaStore(store="default", configString=None): try: g = ConjunctiveGraph(store=store) except ImportError: raise SkipTest("Dependencies for store '%s' not available!"%store) if configString: g.destroy(configString) g.open(configString) else: g.open(mkdtemp(), create=True) g.parse(data=testN3, format="n3") print g.store try: for s,p,o in g.triples((None,implies,None)): formulaA = s formulaB = o assert type(formulaA)==QuotedGraph and type(formulaB)==QuotedGraph a = URIRef('http://test/a') b = URIRef('http://test/b') c = URIRef('http://test/c') d = URIRef('http://test/d') v = Variable('y') universe = ConjunctiveGraph(g.store) #test formula as terms assert len(list(universe.triples((formulaA,implies,formulaB))))==1 #test variable as term and variable roundtrip assert len(list(formulaB.triples((None,None,v))))==1 for s,p,o in formulaB.triples((None,d,None)): if o != c: assert isinstance(o,Variable) assert o == v s = list(universe.subjects(RDF.type, RDFS.Class))[0] assert isinstance(s,BNode) assert len(list(universe.triples((None,implies,None)))) == 1 assert len(list(universe.triples((None,RDF.type,None)))) ==1 assert len(list(formulaA.triples((None,RDF.type,None))))==1 assert len(list(formulaA.triples((None,None,None))))==2 assert len(list(formulaB.triples((None,None,None))))==2 assert len(list(universe.triples((None,None,None))))==3 assert len(list(formulaB.triples((None,URIRef('http://test/d'),None))))==2 assert len(list(universe.triples((None,URIRef('http://test/d'),None))))==1 #context tests #test contexts with triple argument assert len(list(universe.contexts((a,d,c))))==1 #Remove test cases universe.remove((None,implies,None)) assert len(list(universe.triples((None,implies,None))))==0 assert len(list(formulaA.triples((None,None,None))))==2 assert len(list(formulaB.triples((None,None,None))))==2 formulaA.remove((None,b,None)) assert len(list(formulaA.triples((None,None,None))))==1 formulaA.remove((None,RDF.type,None)) assert len(list(formulaA.triples((None,None,None))))==0 universe.remove((None,RDF.type,RDFS.Class)) #remove_context tests universe.remove_context(formulaB) assert len(list(universe.triples((None,RDF.type,None))))==0 assert len(universe)==1 assert len(formulaB)==0 universe.remove((None,None,None)) assert len(universe)==0 g.close() g.store.destroy(configString) except: g.close() g.store.destroy(configString) raise