changeset 85:7d894e977615

clean up rdflibpatch (some of which might now be unused)
author drewp@bigasterisk.com
date Mon, 04 Apr 2022 22:08:41 -0700
parents 36f4318442f2
children 5b6e90a708ce
files rdfdb/rdflibpatch.py rdfdb/rdflibpatch_test.py
diffstat 2 files changed, 136 insertions(+), 143 deletions(-) [+]
line wrap: on
line diff
--- a/rdfdb/rdflibpatch.py	Mon Apr 04 22:00:45 2022 -0700
+++ b/rdfdb/rdflibpatch.py	Mon Apr 04 22:08:41 2022 -0700
@@ -1,16 +1,5 @@
-"""
-this is a proposal for a ConjunctiveGraph method in rdflib
-"""
-import sys
-
-if sys.path[0] == '/usr/lib/python2.7/dist-packages':
-    # nosetests puts this in
-    sys.path = sys.path[1:]
-
-import unittest
-
-from rdflib import ConjunctiveGraph, Graph, Literal
-from rdflib import URIRef as U
+from rdflib import ConjunctiveGraph, Graph, Literal, URIRef
+from rdflib.plugins.serializers.nt import _quoteLiteral
 
 
 def patchQuads(graph, deleteQuads, addQuads, perfect=False):
@@ -49,7 +38,7 @@
 
 
 def fixContextToUri(spoc):
-    if not isinstance(spoc[3], U):
+    if not isinstance(spoc[3], URIRef):
         return spoc[:3] + (spoc[3].identifier,)
     return spoc
 
@@ -77,9 +66,11 @@
 # some of the following workarounds may be fixed in https://github.com/RDFLib/rdflib/issues/299
 def graphFromQuads(q):
     g = ConjunctiveGraph()
-    #g.addN(q) # no effect on nquad output
+    # Ought to be just:
+    #   g.addN(q)
+    # but that doesn't work.
     for s, p, o, c in q:
-        #g.get_context(c).add((s,p,o)) # kind of works with broken rdflib nquad serializer code; you need this for json_ld serialize to work :(
+        # g.get_context(c).add((s,p,o)) # kind of works with broken rdflib nquad serializer code; you need this for json_ld serialize to work :(
         g.store.add((s, p, o), c)  # no effect on nquad output
     return g
 
@@ -91,9 +82,6 @@
     return g1
 
 
-from rdflib.plugins.serializers.nt import _quoteLiteral
-
-
 def serializeQuad(g):
     """
     replacement for graph.serialize(format='nquads')
@@ -125,127 +113,3 @@
 
 def contextsForStatement(graph, triple):
     return [q[3] for q in graph.quads(triple)]
-
-
-A = U("http://a")
-B = U("http://b")
-
-
-class TestInContext(unittest.TestCase):
-
-    def testResultHasQuads(self):
-        g = inContext([(A, A, A)], B)
-        self.assertEqual(list(g.quads())[0], (A, A, A, B))
-
-
-class TestContextsForStatement(unittest.TestCase):
-
-    def testNotFound(self):
-        g = graphFromQuads([(A, A, A, A)])
-        self.assertEqual(contextsForStatement(g, (B, B, B)), [])
-
-    def testOneContext(self):
-        g = graphFromQuads([(A, A, A, A), (A, A, B, B)])
-        self.assertEqual(contextsForStatement(g, (A, A, A)), [A])
-
-    def testTwoContexts(self):
-        g = graphFromQuads([(A, A, A, A), (A, A, A, B)])
-        self.assertEqual(sorted(contextsForStatement(g, (A, A, A))), sorted([A, B]))
-
-    # There's a case where contextsForStatement was returning a Graph
-    # with identifier, which I've fixed without a test
-
-
-class TestInGraph(unittest.TestCase):
-
-    def testSimpleMatch(self):
-        g = graphFromQuads([(A, A, A, A)])
-        self.assertTrue(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):
-        n = U("http://example.com/")
-        g = graphFromQuads([(n, n, n, n)])
-        out = serializeQuad(g)
-        self.assertEqual(out.strip(), self.nqOut.strip())
-
-    def testNquadParserSerializes(self):
-        g = graphFromNQuad(self.nqOut)
-        self.assertEqual(len(g), 1)
-        out = serializeQuad(g)
-        self.assertEqual(out.strip(), self.nqOut.strip())
-
-
-A = U("http://a")
-B = U("http://b")
-C = U("http://c")
-CTX1 = U('http://ctx1')
-CTX2 = U('http://ctx2')
-stmt1 = A, B, C, CTX1
-stmt2 = A, B, C, CTX2
-
-
-class TestPatchQuads(unittest.TestCase):
-
-    def testAddsToNewContext(self):
-        g = ConjunctiveGraph()
-        patchQuads(g, [], [stmt1])
-        self.assertEqual(len(g), 1)
-        quads = list(g.quads((None, None, None)))
-        self.assertEqual(quads, [(A, B, C, Graph(identifier=CTX1))])
-
-    def testDeletes(self):
-        g = ConjunctiveGraph()
-        patchQuads(g, [], [stmt1])
-        patchQuads(g, [stmt1], [])
-        quads = list(g.quads((None, None, None)))
-        self.assertEqual(quads, [])
-
-    def testDeleteRunsBeforeAdd(self):
-        g = ConjunctiveGraph()
-        patchQuads(g, [stmt1], [stmt1])
-        quads = list(g.quads((None, None, None)))
-        self.assertEqual(quads, [(A, B, C, Graph(identifier=CTX1))])
-
-    def testPerfectAddRejectsExistingStmt(self):
-        g = ConjunctiveGraph()
-        patchQuads(g, [], [stmt1])
-        self.assertRaises(ValueError, patchQuads, g, [], [stmt1], perfect=True)
-
-    def testPerfectAddAllowsExistingStmtInNewContext(self):
-        g = ConjunctiveGraph()
-        patchQuads(g, [], [stmt1])
-        patchQuads(g, [], [stmt2], perfect=True)
-        self.assertEqual(len(list(g.quads((None, None, None)))), 2)
-
-    def testPerfectDeleteRejectsAbsentStmt(self):
-        g = ConjunctiveGraph()
-        self.assertRaises(ValueError, patchQuads, g, [stmt1], [], perfect=True)
-
-    def testPerfectDeleteRejectsStmtFromOtherGraph(self):
-        g = ConjunctiveGraph()
-        patchQuads(g, [], [stmt2])
-        self.assertRaises(ValueError, patchQuads, g, [stmt1], [], perfect=True)
-
-    def testPerfectDeleteAllowsRemovalOfStmtInMultipleContexts(self):
-        g = ConjunctiveGraph()
-        patchQuads(g, [], [stmt1, stmt2])
-        patchQuads(g, [stmt1], [], perfect=True)
-
-    def testRedundantStmtOkForAddOrDelete(self):
-        g = ConjunctiveGraph()
-        patchQuads(g, [], [stmt1, stmt1], perfect=True)
-        patchQuads(g, [stmt1, stmt1], [], perfect=True)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rdfdb/rdflibpatch_test.py	Mon Apr 04 22:08:41 2022 -0700
@@ -0,0 +1,129 @@
+import unittest
+
+from rdflib import ConjunctiveGraph, Graph
+from rdflib import URIRef as U
+
+from rdfdb.rdflibpatch import (contextsForStatement, graphFromNQuad, graphFromQuads, inContext, inGraph, patchQuads, serializeQuad)
+
+A = U("http://a")
+B = U("http://b")
+
+
+class TestInContext(unittest.TestCase):
+
+    def testResultHasQuads(self):
+        g = inContext([(A, A, A)], B)
+        self.assertEqual(list(g.quads())[0], (A, A, A, B))
+
+
+class TestContextsForStatement(unittest.TestCase):
+
+    def testNotFound(self):
+        g = graphFromQuads([(A, A, A, A)])
+        self.assertEqual(contextsForStatement(g, (B, B, B)), [])
+
+    def testOneContext(self):
+        g = graphFromQuads([(A, A, A, A), (A, A, B, B)])
+        self.assertEqual(contextsForStatement(g, (A, A, A)), [A])
+
+    def testTwoContexts(self):
+        g = graphFromQuads([(A, A, A, A), (A, A, A, B)])
+        self.assertEqual(sorted(contextsForStatement(g, (A, A, A))), sorted([A, B]))
+
+    # There's a case where contextsForStatement was returning a Graph
+    # with identifier, which I've fixed without a test
+
+
+class TestInGraph(unittest.TestCase):
+
+    def testSimpleMatch(self):
+        g = graphFromQuads([(A, A, A, A)])
+        self.assertTrue(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):
+        n = U("http://example.com/")
+        g = graphFromQuads([(n, n, n, n)])
+        out = serializeQuad(g)
+        self.assertEqual(out.strip(), self.nqOut.strip())
+
+    def testNquadParserSerializes(self):
+        g = graphFromNQuad(self.nqOut)
+        self.assertEqual(len(g), 1)
+        out = serializeQuad(g)
+        self.assertEqual(out.strip(), self.nqOut.strip())
+
+
+A = U("http://a")
+B = U("http://b")
+C = U("http://c")
+CTX1 = U('http://ctx1')
+CTX2 = U('http://ctx2')
+stmt1 = A, B, C, CTX1
+stmt2 = A, B, C, CTX2
+
+
+class TestPatchQuads(unittest.TestCase):
+
+    def testAddsToNewContext(self):
+        g = ConjunctiveGraph()
+        patchQuads(g, [], [stmt1])
+        self.assertEqual(len(g), 1)
+        quads = list(g.quads((None, None, None)))
+        self.assertEqual(quads, [(A, B, C, Graph(identifier=CTX1))])
+
+    def testDeletes(self):
+        g = ConjunctiveGraph()
+        patchQuads(g, [], [stmt1])
+        patchQuads(g, [stmt1], [])
+        quads = list(g.quads((None, None, None)))
+        self.assertEqual(quads, [])
+
+    def testDeleteRunsBeforeAdd(self):
+        g = ConjunctiveGraph()
+        patchQuads(g, [stmt1], [stmt1])
+        quads = list(g.quads((None, None, None)))
+        self.assertEqual(quads, [(A, B, C, Graph(identifier=CTX1))])
+
+    def testPerfectAddRejectsExistingStmt(self):
+        g = ConjunctiveGraph()
+        patchQuads(g, [], [stmt1])
+        self.assertRaises(ValueError, patchQuads, g, [], [stmt1], perfect=True)
+
+    def testPerfectAddAllowsExistingStmtInNewContext(self):
+        g = ConjunctiveGraph()
+        patchQuads(g, [], [stmt1])
+        patchQuads(g, [], [stmt2], perfect=True)
+        self.assertEqual(len(list(g.quads((None, None, None)))), 2)
+
+    def testPerfectDeleteRejectsAbsentStmt(self):
+        g = ConjunctiveGraph()
+        self.assertRaises(ValueError, patchQuads, g, [stmt1], [], perfect=True)
+
+    def testPerfectDeleteRejectsStmtFromOtherGraph(self):
+        g = ConjunctiveGraph()
+        patchQuads(g, [], [stmt2])
+        self.assertRaises(ValueError, patchQuads, g, [stmt1], [], perfect=True)
+
+    def testPerfectDeleteAllowsRemovalOfStmtInMultipleContexts(self):
+        g = ConjunctiveGraph()
+        patchQuads(g, [], [stmt1, stmt2])
+        patchQuads(g, [stmt1], [], perfect=True)
+
+    def testRedundantStmtOkForAddOrDelete(self):
+        g = ConjunctiveGraph()
+        patchQuads(g, [], [stmt1, stmt1], perfect=True)
+        patchQuads(g, [stmt1, stmt1], [], perfect=True)