hallo folks,
I have been trying to get rid of deprecation warnings by making the tests use
the new API (maybe some tests should be added to test the old one?), I attached
the patches to this mail because the trac site seems out of order.
1) no-deprecated-test.diff
Trivial changes to switch most tests to new API, it also reenables some Redland
tests in graph2_2.py that were shadowed by the ones in store_performace.py.
2) no-deprecated-context-test.diff
Switches test/context.py to new API and adds one test. Also enables context
tests on MySQL and Redland stores.
3) fix-redland.diff
Fixes Redland store so it passes the above tests (probably suboptimal
implementation).
4) fix-mysql-len-and-contexts.diff
Fix a typo and replace the optimized version of __len__ with a non-optimized
one that passes tests.
After applying all four patches the tests are 92 (instead of 67) and are all
passing without deprecation warnings.
I am thinking about including the DAWG SPARQL test suite in the tests, has
anybody already given it a try?
It would probably require the tests to run from a compiled installation instead
of sources so it should be made optional, like the other "non-quick" tests in
the suite?
ciao
ste
Index: test/store_performace.py
===================================================================
--- test/store_performace.py (revisione 863)
+++ test/store_performace.py (copia locale)
@@ -33,7 +33,7 @@
MySQL().destroy(path)
else:
path = a_tmp_dir = mkdtemp()
- self.graph.open(path)
+ self.graph.open(path, create=True)
self.input = input = Graph()
input.parse("http://eikeon.com")
Index: test/rdf.py
===================================================================
--- test/rdf.py (revisione 863)
+++ test/rdf.py (copia locale)
@@ -1,6 +1,7 @@
import unittest
from rdflib import *
+from rdflib.Graph import Graph
from rdflib import RDF
from rdflib.StringInputSource import StringInputSource
@@ -27,7 +28,7 @@
path = 'store'
def setUp(self):
- self.store = Graph(backend=self.backend)
+ self.store = Graph(store=self.backend)
self.store.open(self.path)
self.store.bind("dc", "http://http://purl.org/dc/elements/1.1/")
self.store.bind("foaf", "http://xmlns.com/foaf/0.1/")
Index: test/seq.py
===================================================================
--- test/seq.py (revisione 863)
+++ test/seq.py (copia locale)
@@ -1,6 +1,7 @@
import unittest
from rdflib import *
+from rdflib.Graph import Graph
from rdflib.StringInputSource import StringInputSource
class SeqTestCase(unittest.TestCase):
@@ -8,7 +9,7 @@
path = 'store'
def setUp(self):
- store = self.store = Graph(backend=self.backend)
+ store = self.store = Graph(store=self.backend)
store.open(self.path)
store.parse(StringInputSource(s))
Index: test/rules.py
===================================================================
--- test/rules.py (revisione 863)
+++ test/rules.py (copia locale)
@@ -2,6 +2,7 @@
from tempfile import mkdtemp
from rdflib import *
+from rdflib.Graph import Graph
LOG = Namespace("http://www.w3.org/2000/10/swap/log#")
@@ -36,7 +37,7 @@
class PychinkoTestCase(unittest.TestCase):
backend = 'default'
def setUp(self):
- self.g = Graph(backend=self.backend)
+ self.g = Graph(store=self.backend)
self.g.open(configuration=mkdtemp())
self.g.parse("test/a.n3", format="n3")
Index: test/parser.py
===================================================================
--- test/parser.py (revisione 863)
+++ test/parser.py (copia locale)
@@ -1,6 +1,6 @@
import unittest
-from rdflib import Graph
+from rdflib.Graph import Graph
from rdflib import URIRef, BNode, Literal, RDF, RDFS
from rdflib.StringInputSource import StringInputSource
@@ -10,7 +10,7 @@
path = 'store'
def setUp(self):
- self.graph = Graph(backend=self.backend)
+ self.graph = Graph(store=self.backend)
self.graph.open(self.path)
def tearDown(self):
Index: test/graph2_2.py
===================================================================
--- test/graph2_2.py (revisione 863)
+++ test/graph2_2.py (copia locale)
@@ -3,14 +3,14 @@
from tempfile import mkdtemp
from rdflib import URIRef, BNode, Literal, RDF
-from rdflib import Graph
+from rdflib.Graph import Graph
class Graph22TestCase(unittest.TestCase):
backend_name = 'default'
path = None
def setUp(self):
- self.graph = Graph(backend=self.backend_name)
+ self.graph = Graph(store=self.backend_name)
a_tmp_dir = mkdtemp()
self.path = self.path or a_tmp_dir
self.graph.open(self.path)
@@ -154,7 +154,7 @@
try:
import RDF
# If we can import RDF then test Redland backend
- class RedLandTestCase(Graph22TestCase):
+ class RedlandGraph22TestCase(Graph22TestCase):
backend_name = "Redland"
except ImportError, e:
print "Can not test Redland backend:", e
Index: test/triple_store.py
===================================================================
--- test/triple_store.py (revisione 863)
+++ test/triple_store.py (copia locale)
@@ -1,6 +1,7 @@
import unittest
-from rdflib import Graph, URIRef, BNode, Literal, RDFS
+from rdflib import URIRef, BNode, Literal, RDFS
+from rdflib.Graph import Graph
class GraphTest(unittest.TestCase):
@@ -8,7 +9,7 @@
path = 'store'
def setUp(self):
- self.store = Graph(backend=self.backend)
+ self.store = Graph(store=self.backend)
self.store.open(self.path)
self.remove_me = (BNode(), RDFS.label, Literal("remove_me"))
self.store.add(self.remove_me)
Index: test/nt.py
===================================================================
--- test/nt.py (revisione 863)
+++ test/nt.py (copia locale)
@@ -1,6 +1,7 @@
import unittest
from rdflib import *
+from rdflib.Graph import Graph
class NTTestCase(unittest.TestCase):
Index: test/n3_quoting.py
===================================================================
--- test/n3_quoting.py (revisione 863)
+++ test/n3_quoting.py (copia locale)
@@ -1,7 +1,8 @@
import unittest
#import sys
#sys.path[:0]=[".."]
-from rdflib import Graph, Literal, Namespace, StringInputSource
+from rdflib import Literal, Namespace, StringInputSource
+from rdflib.Graph import Graph
cases = ['no quotes',
"single ' quote",
Index: rdflib/store/Redland.py
===================================================================
--- rdflib/store/Redland.py (revisione 863)
+++ rdflib/store/Redland.py (copia locale)
@@ -1,22 +1,29 @@
import rdflib
+from rdflib.Graph import Graph
+from rdflib.URIRef import URIRef
+from rdflib.Node import Node
+from rdflib.BNode import BNode
+from rdflib.Literal import Literal
import RDF
from rdflib.store import Store
def _t(i):
if isinstance(i, rdflib.URIRef):
- return RDF.Uri(unicode(i))
+ return RDF.Node(RDF.Uri(unicode(i)))
if isinstance(i, rdflib.BNode):
return RDF.Node(blank=str(i))
if isinstance(i, rdflib.Literal):
return RDF.Node(literal=str(i))
+ if isinstance(i, Graph):
+ return _t(i.identifier)
if i is None:
return None
raise TypeError, 'Cannot convert %s' % `i`
def _c(i):
- return RDF.Node(uri=RDF.Uri(str(id(i))))
+ return _t(i)
def _f(i):
@@ -24,25 +31,32 @@
return rdflib.URIRef(i)
if isinstance(i, RDF.Node):
if i.is_blank():
- return rdflib.BNode(i)
+ return rdflib.BNode(i.blank_identifier)
+ elif i.is_literal():
+ return rdflib.Literal(i)
else:
- return rdflib.Literal(i)
+ return URIRef(i.uri)
if i is None:
return None
raise TypeError, 'Cannot convert %s' % `i`
class Redland(Store):
+ context_aware = True
def __init__(self, model=None):
super(Redland, self).__init__()
if model is None:
- model = RDF.Model()
+ model = RDF.Model(RDF.MemoryStorage(options_string="contexts='yes'"))
self.model = model
def __len__(self, context=None):
""" Return number of triples (statements in librdf). """
- return self.model.size()
+ count = 0
+ for triple, cg in self.triples((None, None, None), context):
+ count += 1
+ return count
+
def add(self, (subject, predicate, object), context=None, quoted=False):
"""\
Add a triple to the store of triples.
@@ -53,19 +67,33 @@
self.model.append(RDF.Statement(_t(subject), _t(predicate), _t(object)))
def remove(self, (subject, predicate, object), context, quoted=False):
- if context is not None:
- del self.model[RDF.Statement(_t(subject), _t(predicate), _t(object)), _t(context)]
+ if context is None:
+ contexts = self.contexts()
else:
- del self.model[RDF.Statement(_t(subject), _t(predicate), _t(object))]
+ contexts = [context]
+ for context in contexts:
+ if subject is None and predicate is None and object is None:
+ self.model.remove_statements_with_context(_c(context))
+ else:
+ del self.model[RDF.Statement(_t(subject), _t(predicate), _t(object)), _c(context)]
def triples(self, (subject, predicate, object), context=None):
"""A generator over all the triples matching """
- for statement in self.model.find_statements(RDF.Statement(_t(subject), _t(predicate), _t(object)), _t(context)):
- yield _f(statement.subject), _f(statement.predicate), _f(statement.object)
+ cgraph = RDF.Model()
+ triple = RDF.Statement(_t(subject), _t(predicate), _t(object))
+ for statement, c in self.model.find_statements_context(triple):
+ if context is None or _f(c) == context.identifier:
+ cgraph.append(statement)
+ for statement in cgraph.find_statements(triple):
+ ret = []
+ for c in self.model.get_contexts():
+ if self.model.contains_statement_context(statement, _c(context)):
+ ret.append(c)
+ yield (_f(statement.subject), _f(statement.predicate), _f(statement.object)), iter(ret)
def contexts(self, triple=None): # TODO: have Graph support triple?
for context in self.model.get_contexts():
- yield URIRef(context)
+ yield Graph(self, _f(context))
def bind(self, prefix, namespace):
pass
Index: test/context.py
===================================================================
--- test/context.py (revisione 863)
+++ test/context.py (copia locale)
@@ -2,14 +2,22 @@
from tempfile import mkdtemp
from rdflib import *
+from rdflib.Graph import Graph
class ContextTestCase(unittest.TestCase):
#backend = 'Memory'
backend = 'default'
def setUp(self):
- self.graph = Graph(backend=self.backend)
- self.graph.open(mkdtemp())
+ self.graph = ConjunctiveGraph(store=self.backend)
+ if self.backend == "MySQL":
+ from test.mysql import configString
+ from rdflib.store.MySQL import MySQL
+ path=configString
+ MySQL().destroy(path)
+ else:
+ path = a_tmp_dir = mkdtemp()
+ self.graph.open(path, create=True)
self.michel = URIRef(u'michel')
self.tarek = URIRef(u'tarek')
self.bob = URIRef(u'bob')
@@ -27,6 +35,11 @@
def tearDown(self):
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
@@ -36,14 +49,15 @@
pizza = self.pizza
cheese = self.cheese
c1 = self.c1
+ graph = Graph(self.graph.store, c1)
- self.graph.add((tarek, likes, pizza), c1)
- self.graph.add((tarek, likes, cheese), c1)
- self.graph.add((michel, likes, pizza), c1)
- self.graph.add((michel, likes, cheese), c1)
- self.graph.add((bob, likes, cheese), c1)
- self.graph.add((bob, hates, pizza), c1)
- self.graph.add((bob, hates, michel), c1) # gasp!
+ 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)) # gasp!
def removeStuff(self):
tarek = self.tarek
@@ -54,14 +68,15 @@
pizza = self.pizza
cheese = self.cheese
c1 = self.c1
+ graph = Graph(self.graph.store, c1)
- self.graph.remove((tarek, likes, pizza), c1)
- self.graph.remove((tarek, likes, cheese), c1)
- self.graph.remove((michel, likes, pizza), c1)
- self.graph.remove((michel, likes, cheese), c1)
- self.graph.remove((bob, likes, cheese), c1)
- self.graph.remove((bob, hates, pizza), c1)
- self.graph.remove((bob, hates, michel), c1) # gasp!
+ 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)) # gasp!
def addStuffInMultipleContexts(self):
c1 = self.c1
@@ -71,10 +86,20 @@
# add to default context
self.graph.add(triple)
# add to context 1
- self.graph.add(triple, c1)
+ graph = Graph(self.graph.store, c1)
+ graph.add(triple)
# add to context 2
- self.graph.add(triple, c2)
+ 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(self.graph), len(graph))
+
def testAdd(self):
self.addStuff()
@@ -84,24 +109,28 @@
def testLenInOneContext(self):
c1 = self.c1
- oldLen = len(self.graph)
# make sure context is empty
- self.graph.remove_context(c1)
+ self.graph.remove_context(self.get_context(c1))
+ graph = Graph(self.graph.store, c1)
+ oldLen = len(self.graph)
for i in range(0, 10):
- self.graph.add((BNode(), self.hates, self.hates), c1)
- self.assertEquals(len(self.graph), oldLen + 10)
- self.assertEquals(len(self.graph.get_context(c1)), oldLen + 10)
- self.graph.remove_context(c1)
+ 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()
- # TODO: what should the length of the graph now be? 1 or 3?
- #self.assertEquals(len(self.graph), oldLen + 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
@@ -111,9 +140,11 @@
# triple should be still in store after removing it from c1 + c2
self.assert_(triple in self.graph)
- self.graph.remove(triple, c1)
+ graph = Graph(self.graph.store, c1)
+ graph.remove(triple)
self.assert_(triple in self.graph)
- self.graph.remove(triple, c2)
+ graph = Graph(self.graph.store, c2)
+ graph.remove(triple)
self.assert_(triple in self.graph)
self.graph.remove(triple)
# now gone!
@@ -128,10 +159,12 @@
triple = (self.pizza, self.hates, self.tarek) # revenge!
self.addStuffInMultipleContexts()
- self.assert_(self.c1 in self.graph.contexts())
- self.assert_(self.c2 in self.graph.contexts())
+ def cid(c):
+ return c.identifier
+ self.assert_(self.c1 in map(cid, self.graph.contexts()))
+ self.assert_(self.c2 in map(cid, self.graph.contexts()))
- contextList = list(self.graph.contexts(triple))
+ contextList = map(cid, list(self.graph.contexts(triple)))
self.assert_(self.c1 in contextList)
self.assert_(self.c2 in contextList)
@@ -139,9 +172,10 @@
c1 = self.c1
self.addStuffInMultipleContexts()
- self.assertEquals(self.graph.__len__(context=c1), 1)
+ self.assertEquals(len(Graph(self.graph.store, c1)), 1)
+ self.assertEquals(len(self.get_context(c1)), 1)
- self.graph.remove_context(c1)
+ self.graph.remove_context(self.get_context(c1))
self.assert_(self.c1 not in self.graph.contexts())
def testRemoveAny(self):
@@ -162,15 +196,17 @@
asserte = self.assertEquals
triples = self.graph.triples
graph = self.graph
+ c1graph = Graph(self.graph.store, c1)
+ c1triples = c1graph.triples
Any = None
self.addStuff()
# unbound subjects with context
- asserte(len(list(triples((Any, likes, pizza), c1))), 2)
- asserte(len(list(triples((Any, hates, pizza), c1))), 1)
- asserte(len(list(triples((Any, likes, cheese), c1))), 3)
- asserte(len(list(triples((Any, hates, cheese), c1))), 0)
+ asserte(len(list(c1triples((Any, likes, pizza)))), 2)
+ asserte(len(list(c1triples((Any, hates, pizza)))), 1)
+ asserte(len(list(c1triples((Any, likes, cheese)))), 3)
+ asserte(len(list(c1triples((Any, hates, cheese)))), 0)
# unbound subjects without context, same results!
asserte(len(list(triples((Any, likes, pizza)))), 2)
@@ -179,10 +215,10 @@
asserte(len(list(triples((Any, hates, cheese)))), 0)
# unbound objects with context
- asserte(len(list(triples((michel, likes, Any), c1))), 2)
- asserte(len(list(triples((tarek, likes, Any), c1))), 2)
- asserte(len(list(triples((bob, hates, Any), c1))), 2)
- asserte(len(list(triples((bob, likes, Any), c1))), 1)
+ asserte(len(list(c1triples((michel, likes, Any)))), 2)
+ asserte(len(list(c1triples((tarek, likes, Any)))), 2)
+ asserte(len(list(c1triples((bob, hates, Any)))), 2)
+ asserte(len(list(c1triples((bob, likes, Any)))), 1)
# unbound objects without context, same results!
asserte(len(list(triples((michel, likes, Any)))), 2)
@@ -191,10 +227,10 @@
asserte(len(list(triples((bob, likes, Any)))), 1)
# unbound predicates with context
- asserte(len(list(triples((michel, Any, cheese), c1))), 1)
- asserte(len(list(triples((tarek, Any, cheese), c1))), 1)
- asserte(len(list(triples((bob, Any, pizza), c1))), 1)
- asserte(len(list(triples((bob, Any, michel), c1))), 1)
+ asserte(len(list(c1triples((michel, Any, cheese)))), 1)
+ asserte(len(list(c1triples((tarek, Any, cheese)))), 1)
+ asserte(len(list(c1triples((bob, Any, pizza)))), 1)
+ asserte(len(list(c1triples((bob, Any, michel)))), 1)
# unbound predicates without context, same results!
asserte(len(list(triples((michel, Any, cheese)))), 1)
@@ -203,17 +239,17 @@
asserte(len(list(triples((bob, Any, michel)))), 1)
# unbound subject, objects with context
- asserte(len(list(triples((Any, hates, Any), c1))), 2)
- asserte(len(list(triples((Any, likes, Any), c1))), 5)
+ asserte(len(list(c1triples((Any, hates, Any)))), 2)
+ asserte(len(list(c1triples((Any, likes, Any)))), 5)
# unbound subject, objects without context, same results!
asserte(len(list(triples((Any, hates, Any)))), 2)
asserte(len(list(triples((Any, likes, Any)))), 5)
# unbound predicates, objects with context
- asserte(len(list(triples((michel, Any, Any), c1))), 2)
- asserte(len(list(triples((bob, Any, Any), c1))), 3)
- asserte(len(list(triples((tarek, Any, Any), c1))), 2)
+ asserte(len(list(c1triples((michel, Any, Any)))), 2)
+ asserte(len(list(c1triples((bob, Any, Any)))), 3)
+ asserte(len(list(c1triples((tarek, Any, Any)))), 2)
# unbound predicates, objects without context, same results!
asserte(len(list(triples((michel, Any, Any)))), 2)
@@ -221,9 +257,9 @@
asserte(len(list(triples((tarek, Any, Any)))), 2)
# unbound subjects, predicates with context
- asserte(len(list(triples((Any, Any, pizza), c1))), 3)
- asserte(len(list(triples((Any, Any, cheese), c1))), 3)
- asserte(len(list(triples((Any, Any, michel), c1))), 1)
+ asserte(len(list(c1triples((Any, Any, pizza)))), 3)
+ asserte(len(list(c1triples((Any, Any, cheese)))), 3)
+ asserte(len(list(c1triples((Any, Any, michel)))), 1)
# unbound subjects, predicates without context, same results!
asserte(len(list(triples((Any, Any, pizza)))), 3)
@@ -231,13 +267,12 @@
asserte(len(list(triples((Any, Any, michel)))), 1)
# all unbound with context
- asserte(len(list(triples((Any, Any, Any), c1))), 7)
+ asserte(len(list(c1triples((Any, Any, Any)))), 7)
# all unbound without context, same result!
asserte(len(list(triples((Any, Any, Any)))), 7)
- for c in [graph, graph.get_context(c1)]:
+ for c in [graph, self.get_context(c1)]:
# unbound subjects
- #c = graph.get_context(c1)
asserte(set(c.subjects(likes, pizza)), set((michel, tarek)))
asserte(set(c.subjects(hates, pizza)), set((bob,)))
asserte(set(c.subjects(likes, cheese)), set([tarek, bob, michel]))
@@ -270,7 +305,7 @@
# remove stuff and make sure the graph is empty again
self.removeStuff()
- asserte(len(list(triples((Any, Any, Any), c1))), 0)
+ asserte(len(list(c1triples((Any, Any, Any)))), 0)
asserte(len(list(triples((Any, Any, Any)))), 0)
class IOMemoryContextTestCase(ContextTestCase):
@@ -292,9 +327,26 @@
pass
try:
+ import MySQLdb
+ # If we can import RDF then test Redland store
+ class MySQLContextTestCase(ContextTestCase):
+ backend = "MySQL"
+except ImportError, e:
+ print "Can not test MySQL store:", e
+
+try:
+ import RDF
+ # If we can import RDF then test Redland store
+ class RedlandContextTestCase(ContextTestCase):
+ backend = "Redland"
+except ImportError, e:
+ print "Can not test Redland store:", e
+
+try:
import backends.Sleepycat
except ImportError:
del ContextTestCase
+
if __name__ == '__main__':
unittest.main()
Index: rdflib/store/MySQL.py
===================================================================
--- rdflib/store/MySQL.py (revisione 863)
+++ rdflib/store/MySQL.py (copia locale)
@@ -418,14 +418,18 @@
)
def __len__(self, context=None):
- rows = []
- countRows = "select count(*) from %s"
- c=self._db.cursor()
- for part in self.partitions:
- self.executeSQL(c,countRows%part)
- rowCount = c.fetchone()[0]
- rows.append(rowCount)
- return reduce(lambda x,y: x+y,rows)
+ count = 0
+ for triple, cg in self.triples((None, None, None), context):
+ count += 1
+ return count
+ #rows = []
+ #countRows = "select count(*) from %s"
+ #c=self._db.cursor()
+ #for part in self.partitions:
+ #self.executeSQL(c,countRows%part)
+ #rowCount = c.fetchone()[0]
+ #rows.append(rowCount)
+ #return reduce(lambda x,y: x+y,rows)
def contexts(self, triple=None):
c=self._db.cursor()
@@ -436,7 +440,7 @@
rt=PatternResolution((subject,predicate,obj,None),
c,
self.partitions,
- orderByContext=False,
+ orderByTriple=False,
fetchall=False,
fetchContexts=True)
while rt:
_______________________________________________
Dev mailing list
[email protected]
http://rdflib.net/mailman/listinfo/dev