changeset 826:05aabe3d7b02

fix some minor issues with graph contexts Ignore-this: 7fd366a4b6cbd94af6f9edb1b4b4c6fc
author Drew Perttula <drewp@bigasterisk.com>
date Tue, 04 Jun 2013 20:28:49 +0000
parents a8942364e4ee
children 9816e249ce2e
files bin/rdfdb light9/rdfdb/patch.py light9/rdfdb/rdflibpatch.py
diffstat 3 files changed, 34 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/bin/rdfdb	Tue Jun 04 19:31:39 2013 +0000
+++ b/bin/rdfdb	Tue Jun 04 20:28:49 2013 +0000
@@ -205,6 +205,7 @@
         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"
--- a/light9/rdfdb/patch.py	Tue Jun 04 19:31:39 2013 +0000
+++ b/light9/rdfdb/patch.py	Tue Jun 04 20:28:49 2013 +0000
@@ -49,19 +49,17 @@
     @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 @@
 
             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 @@
         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)
+                  
--- a/light9/rdfdb/rdflibpatch.py	Tue Jun 04 19:31:39 2013 +0000
+++ b/light9/rdfdb/rdflibpatch.py	Tue Jun 04 20:28:49 2013 +0000
@@ -7,7 +7,7 @@
     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 @@
     """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):