Changeset - 05aabe3d7b02
[Not reviewed]
default
0 3 0
Drew Perttula - 12 years ago 2013-06-04 20:28:49
drewp@bigasterisk.com
fix some minor issues with graph contexts
Ignore-this: 7fd366a4b6cbd94af6f9edb1b4b4c6fc
3 files changed with 34 insertions and 12 deletions:
0 comments (0 inline, 0 general)
bin/rdfdb
Show inline comments
 
@@ -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"
light9/rdfdb/patch.py
Show inline comments
 
@@ -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)
 
                  
light9/rdfdb/rdflibpatch.py
Show inline comments
 
@@ -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):
0 comments (0 inline, 0 general)