diff --git a/bin/rdfdb b/bin/rdfdb --- a/bin/rdfdb +++ b/bin/rdfdb @@ -205,6 +205,7 @@ class Db(object): return URIRef(self.topUri + filename) def fileForUri(self, ctx): + assert isinstance(ctx, URIRef), ctx if not ctx.startswith(self.topUri): raise ValueError("don't know what filename to use for %s" % ctx) return ctx[len(self.topUri):] + ".n3" diff --git a/light9/rdfdb/patch.py b/light9/rdfdb/patch.py --- a/light9/rdfdb/patch.py +++ b/light9/rdfdb/patch.py @@ -49,19 +49,17 @@ class Patch(object): @property def addQuads(self): if self._addQuads is None: - if self._addGraph is not None: - self._addQuads = list(self._addGraph.quads(ALLSTMTS)) - else: - raise + if self._addGraph is None: + return [] + self._addQuads = list(self._addGraph.quads(ALLSTMTS)) return self._addQuads @property def delQuads(self): if self._delQuads is None: - if self._delGraph is not None: - self._delQuads = list(self._delGraph.quads(ALLSTMTS)) - else: - raise + if self._delGraph is None: + return [] + self._delQuads = list(self._delGraph.quads(ALLSTMTS)) return self._delQuads @property @@ -125,6 +123,9 @@ class Patch(object): if ctx != q[3]: raise ValueError("patch applies to multiple contexts, at least %r and %r" % (ctx, q[3])) + if ctx is None: + raise ValueError("patch affects no contexts") + assert isinstance(ctx, URIRef), ctx return ctx stmt1 = U('http://a'), U('http://b'), U('http://c'), U('http://ctx1') @@ -156,3 +157,17 @@ class TestPatchFromDiff(unittest.TestCas self.assertEqual(p.delQuads, [stmt1]) +class TestPatchGetContext(unittest.TestCase): + def testEmptyPatchCantGiveContext(self): + p = Patch() + self.assertRaises(ValueError, p.getContext) + + def testSimplePatchReturnsContext(self): + p = Patch(addQuads=[stmt1]) + self.assertEqual(p.getContext(), U('http://ctx1')) + + def testMultiContextPatchFailsToReturnContext(self): + p = Patch(addQuads=[stmt1[:3] + (U('http://ctx1'),), + stmt1[:3] + (U('http://ctx2'),)]) + self.assertRaises(ValueError, p.getContext) + diff --git a/light9/rdfdb/rdflibpatch.py b/light9/rdfdb/rdflibpatch.py --- a/light9/rdfdb/rdflibpatch.py +++ b/light9/rdfdb/rdflibpatch.py @@ -7,7 +7,7 @@ if sys.path[0] == '/usr/lib/python2.7/di sys.path = sys.path[1:] import unittest -from rdflib import ConjunctiveGraph, URIRef as U +from rdflib import ConjunctiveGraph, Graph, URIRef as U def patchQuads(graph, deleteQuads, addQuads, perfect=False): """ @@ -66,10 +66,16 @@ def serializeQuad(g): """replacement for graph.serialize(format='nquads')""" out = "" for s,p,o,c in g.quads((None,None,None)): + if isinstance(c, Graph): + # still not sure why this is Graph sometimes, + # already URIRef other times + c = c.identifier + if '[' in c.n3(): + import ipdb;ipdb.set_trace() out += u"%s %s %s %s .\n" % (s.n3(), - p.n3(), - _xmlcharref_encode(o.n3()), - c.n3()) + p.n3(), + _xmlcharref_encode(o.n3()), + c.n3()) return out def inContext(graph, newContext):