changeset 82:8a9f8dc65da8

fix existing tests. Note bytes->str on public api arg
author drewp@bigasterisk.com
date Mon, 04 Apr 2022 21:10:40 -0700
parents 8d3c3e7cfb54
children bbf4595b34ae
files rdfdb/graphfile.py rdfdb/graphfile_test.py rdfdb/patch.py rdfdb/patch_test.py tasks.py
diffstat 5 files changed, 72 insertions(+), 65 deletions(-) [+]
line wrap: on
line diff
--- a/rdfdb/graphfile.py	Mon Apr 04 11:25:05 2022 -0700
+++ b/rdfdb/graphfile.py	Mon Apr 04 21:10:40 2022 -0700
@@ -114,13 +114,13 @@
     one rdf file that we read from, write to, and notice external changes to
     """
 
-    def __init__(self, notifier: INotify, path: bytes, uri: URIRef, patch: PatchCb, getSubgraph: GetSubgraph, globalPrefixes: Dict[str, URIRef],
+    def __init__(self, notifier: INotify, path: str, uri: URIRef, patch: PatchCb, getSubgraph: GetSubgraph, globalPrefixes: Dict[str, URIRef],
                  ctxPrefixes: Dict[str, URIRef]):
         """
         uri is the context for the triples in this file. We assume
         sometimes that we're the only ones with triples in this
         context.
-        
+
         this does not include an initial reread() call
 
         Prefixes are mutable dicts. The caller may add to them later.
@@ -256,18 +256,18 @@
     def dirty(self, graph: Graph) -> None:
         """
         there are new contents to write to our file
-        
+
         graph is the rdflib.Graph that contains the contents of the
         file. It is allowed to change. Note that dirty() will probably
         do the save later when the graph might be different.
-        
+
         after a timer has passed, write it out. Any scheduling issues
         between files? i don't think so. the timer might be kind of
         huge, and then we might want to take a hint from a client that
         it's a good time to save the files that it was editing, like
         when the mouse moves out of the client's window and might be
         going towards a text file editor
-        
+
         """
         log.debug("%s dirty, needs write", self.path)
 
@@ -281,7 +281,7 @@
     def flush(self) -> None:
         self.writeCall = None
 
-        tmpOut = self.path + b".rdfdb-temp"
+        tmpOut = self.path + ".rdfdb-temp"
         f = open(tmpOut, 'wb')
         t1 = time.time()
         for p, n in (list(self.globalPrefixes.items()) + list(self.readPrefixes.items()) + list(self.ctxPrefixes.items())):
--- a/rdfdb/graphfile_test.py	Mon Apr 04 11:25:05 2022 -0700
+++ b/rdfdb/graphfile_test.py	Mon Apr 04 21:10:40 2022 -0700
@@ -1,10 +1,15 @@
 import tempfile
 import unittest
+from typing import cast
 
 import mock
 from rdflib import Graph, URIRef
+from twisted.internet.inotify import INotify
 
 from rdfdb.graphfile import GraphFile
+from _pytest.assertion import truncate
+truncate.DEFAULT_MAX_LINES = 9999
+truncate.DEFAULT_MAX_CHARS = 9999
 
 
 class TestGraphFileOutput(unittest.TestCase):
@@ -21,7 +26,7 @@
         def getSubgraph(uri):
             return Graph()
 
-        gf = GraphFile(mock.Mock(), tf.name.encode('ascii'), URIRef('uri'), mock.Mock(), getSubgraph, {}, {})
+        gf = GraphFile(cast(INotify, mock.Mock()), tf.name, URIRef('uri'), mock.Mock(), getSubgraph, {}, {})
         gf.reread()
 
         newGraph = Graph()
@@ -29,13 +34,10 @@
         gf.dirty(newGraph)
         gf.flush()
         wroteContent = open(tf.name, 'rb').read()
+        print(wroteContent)
         self.assertEqual(
             b'''@prefix : <http://example.com/> .
 @prefix n: <http://example.com/n/> .
-@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
-@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
-@prefix xml: <http://www.w3.org/XML/1998/namespace> .
-@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
 
 :boo n:two <http://example.com/other/ns> .
 ''', wroteContent)
--- a/rdfdb/patch.py	Mon Apr 04 11:25:05 2022 -0700
+++ b/rdfdb/patch.py	Mon Apr 04 21:10:40 2022 -0700
@@ -1,10 +1,8 @@
 import json
-import unittest
 from typing import Optional
 
 from rdflib import ConjunctiveGraph, Graph, Literal, Namespace
 from rdflib import URIRef
-from rdflib import URIRef as U
 
 XSD = Namespace("http://www.w3.org/2001/XMLSchema#")
 
@@ -186,54 +184,3 @@
     def isNoop(self):
         return set(self.addQuads) == set(self.delQuads)
 
-
-stmt1 = U('http://a'), U('http://b'), U('http://c'), U('http://ctx1')
-
-
-class TestPatchFromDiff(unittest.TestCase):
-
-    def testEmpty(self):
-        g = ConjunctiveGraph()
-        p = Patch.fromDiff(g, g)
-        self.assertTrue(not p)
-
-    def testNonEmpty(self):
-        g1 = ConjunctiveGraph()
-        g2 = graphFromQuads([stmt1])
-        p = Patch.fromDiff(g1, g2)
-        self.assertTrue(p)
-
-    def testNoticesAdds(self):
-        g1 = ConjunctiveGraph()
-        g2 = graphFromQuads([stmt1])
-        p = Patch.fromDiff(g1, g2)
-        self.assertEqual(p.addQuads, [stmt1])
-        self.assertEqual(p.delQuads, [])
-
-    def testNoticesDels(self):
-        g1 = graphFromQuads([stmt1])
-        g2 = ConjunctiveGraph()
-        p = Patch.fromDiff(g1, g2)
-        self.assertEqual(p.addQuads, [])
-        self.assertEqual(p.delQuads, [stmt1])
-
-    def testQuadSequenceOkInsteadOfGraph(self):
-        p = Patch.fromDiff([stmt1], ConjunctiveGraph())
-        self.assertEqual(p.delQuads, [stmt1])
-        p = Patch.fromDiff(ConjunctiveGraph(), [stmt1])
-        self.assertEqual(p.addQuads, [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)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rdfdb/patch_test.py	Mon Apr 04 21:10:40 2022 -0700
@@ -0,0 +1,58 @@
+import unittest
+
+from rdflib import ConjunctiveGraph
+from rdflib import URIRef as U
+
+from rdfdb.patch import Patch
+from rdfdb.rdflibpatch import graphFromQuads
+
+stmt1 = U('http://a'), U('http://b'), U('http://c'), U('http://ctx1')
+
+
+class TestPatchFromDiff(unittest.TestCase):
+
+    def testEmpty(self):
+        g = ConjunctiveGraph()
+        p = Patch.fromDiff(g, g)
+        self.assertTrue(not p)
+
+    def testNonEmpty(self):
+        g1 = ConjunctiveGraph()
+        g2 = graphFromQuads([stmt1])
+        p = Patch.fromDiff(g1, g2)
+        self.assertTrue(p)
+
+    def testNoticesAdds(self):
+        g1 = ConjunctiveGraph()
+        g2 = graphFromQuads([stmt1])
+        p = Patch.fromDiff(g1, g2)
+        self.assertEqual(p.addQuads, [stmt1])
+        self.assertEqual(p.delQuads, [])
+
+    def testNoticesDels(self):
+        g1 = graphFromQuads([stmt1])
+        g2 = ConjunctiveGraph()
+        p = Patch.fromDiff(g1, g2)
+        self.assertEqual(p.addQuads, [])
+        self.assertEqual(p.delQuads, [stmt1])
+
+    def testQuadSequenceOkInsteadOfGraph(self):
+        p = Patch.fromDiff([stmt1], ConjunctiveGraph())
+        self.assertEqual(p.delQuads, [stmt1])
+        p = Patch.fromDiff(ConjunctiveGraph(), [stmt1])
+        self.assertEqual(p.addQuads, [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/tasks.py	Mon Apr 04 11:25:05 2022 -0700
+++ b/tasks.py	Mon Apr 04 21:10:40 2022 -0700
@@ -19,7 +19,7 @@
 def test_forever(ctx, forever=True):
     watchdir = 'rdfdb'
     testdir = 'rdfdb'
-    ctx.run(f'pdm run pytest {testdir}', pty=True)  # ptw doesn't do an initial run
+    ctx.run(f'pdm run pytest {testdir}', pty=True, warn=True)  # ptw doesn't do an initial run
     if forever:
         ctx.run(f'pdm run ptw {watchdir} {testdir}', pty=True)