changeset 21:ad6b5804deb1

serious bugs with inGraph. the code is not consistent (still) about whether context is a URIRef or Graph-with-identifier Ignore-this: 3a750a3f9c40b1ec3e2a8fb0c96830
author drewp@bigasterisk.com
date Wed, 17 Apr 2019 09:23:34 +0000
parents 919c23ed90ec
children 2748dac8195b
files rdfdb/grapheditapi.py rdfdb/rdflibpatch.py
diffstat 2 files changed, 34 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/rdfdb/grapheditapi.py	Fri Jan 04 23:17:30 2019 +0000
+++ b/rdfdb/grapheditapi.py	Wed Apr 17 09:23:34 2019 +0000
@@ -19,14 +19,11 @@
         """
 
         existing = []
-        for spo in self._graph.triples((subject, predicate, None),
-                                     context=context):
-            existing.append(spo+(context,))
-        # what layer is supposed to cull out no-op changes?
-        return Patch(
-            delQuads=existing,
-            addQuads=([(subject, predicate, newObject, context)]
-                      if newObject is not None else []))
+        for spoc in quadsWithContextUris(self._graph.quads((subject, predicate, None, context))):
+            existing.append(spoc)
+        toAdd = ([(subject, predicate, newObject, context)]
+                      if newObject is not None else [])
+        return Patch(delQuads=existing, addQuads=toAdd).simplify()
 
     def patchObject(self, context, subject, predicate, newObject):
         p = self.getObjectPatch(context, subject, predicate, newObject)
--- a/rdfdb/rdflibpatch.py	Fri Jan 04 23:17:30 2019 +0000
+++ b/rdfdb/rdflibpatch.py	Wed Apr 17 09:23:34 2019 +0000
@@ -53,11 +53,19 @@
     c is just a URIRef.
     Workaround for https://github.com/RDFLib/rdflib/issues/398
     """
-    spoi = spoc[:3] + (Graph(identifier=spoc[3]),)
-    if spoi not in graph:
-        # this is a huge speedup, avoid many whole-graph scans
-        return False
-    return spoi in graph.quads()
+
+    c = spoc[3]
+    if isinstance(c, Graph):
+        c = c.identifier
+    
+    for spoc2 in graph.quads(spoc[:3]):
+        if spoc[:3] == spoc2[:3]:
+            c2 = spoc2[3]
+            if isinstance(c2, Graph):
+                c2 = c2.identifier
+            if c == c2:
+                return True
+    return False
 
 # some of the following workarounds may be fixed in https://github.com/RDFLib/rdflib/issues/299
 def graphFromQuads(q):
@@ -129,6 +137,22 @@
     # with identifier, which I've fixed without a test
 
 
+class TestInGraph(unittest.TestCase):
+    def testSimpleMatch(self):
+        g = graphFromQuads([(A,A,A,A)])
+        self.assert_(inGraph((A,A,A,A), g))
+
+    def testDontMatchDifferentStatement(self):
+        g = graphFromQuads([(A,A,A,A)])
+        self.assertFalse(inGraph((B,B,B,B), g))
+        
+    def testDontMatchStatementInAnotherContext(self):
+        g = graphFromQuads([(A,A,A,A)])
+        self.assertFalse(inGraph((A,A,A,B), g))
+        
+        self.assertFalse(inGraph((A,A,A,Graph(identifier=B)), g))
+    
+
 class TestGraphFromQuads(unittest.TestCase):
     nqOut = '<http://example.com/> <http://example.com/> <http://example.com/> <http://example.com/> .\n'
     def testSerializes(self):