Commits

Anonymous committed df3a9a1

[tests][s]: refactor test_handler and test_pt to use a temp directory for pairtree and have proper setup and teardown (similar to cset:47807b410c50).

* All tests passing now, hooray: Ran 34 tests in 7.982s! (Initially: Ran 14 tests in 0.614s FAILED (errors=5, failures=2)

  • Participants
  • Parent commits 9e29de0

Comments (0)

Files changed (2)

ordf/tests/test_handler.py

+import os
+import tempfile
+import shutil
+
 from ordf.handler import Handler, HandlerPlugin
 from ordf.namespace import RDF, Namespace
 from ordf.graph import Graph
 from ordf.tests import our_test_graph, identifier
 
-ptree_cls = HandlerPlugin.find("pairtree")
-ptree = ptree_cls("data/ordf_test")
-handler = Handler(namespace=Namespace("http://example.org/changesets/"))
-handler.register_reader(ptree)
-handler.register_writer(ptree)
+class TestHandler:
+    @classmethod
+    def setup_class(self):
+        self.tmpdir = os.path.join(tempfile.gettempdir(), self.__name__)
+        ptree_cls = HandlerPlugin.find("pairtree")
+        ptree = ptree_cls(self.tmpdir)
+        self.handler = Handler(namespace=Namespace("http://example.org/changesets/"))
+        self.handler.register_reader(ptree)
+        self.handler.register_writer(ptree)
 
-def test_01_find():
-    assert HandlerPlugin.find("null") is not None
+    @classmethod
+    def teardown_class(self):
+        shutil.rmtree(self.tmpdir)
 
-def test_02_register():
-    h = Handler()
+    def test_01_find(self):
+        assert HandlerPlugin.find("null") is not None
 
-    # test  direct registering of an instance
-    r = HandlerPlugin()
-    h.register_reader(r)
+    def test_02_register(self):
+        h = Handler()
 
-    # test registering by named plugin
-    h.register_writer("null")
+        # test  direct registering of an instance
+        r = HandlerPlugin()
+        h.register_reader(r)
 
+        # test registering by named plugin
+        h.register_writer("null")
 
-def test_03_context():
-    ctx = handler.context("test_handler", "create some data")
-    ctx.add(our_test_graph())
-    ctx.commit()
 
-    g = our_test_graph()
-    g.remove((None, RDF.type, None))
-    ctx = handler.context("test_handler", "delete some data")
-    ctx.add(g)
-    ctx.commit()
+    def test_03_context(self):
+        ctx = self.handler.context("test_handler", "create some data")
+        ctx.add(our_test_graph())
+        ctx.commit()
 
-def test_04_undo():
-    ## undo the last change (remove the types again)
-    constructed = handler.get(identifier)
-    ver = constructed.version()
-    cs = handler.changeset(ver)
-    cs.undo(constructed)
+        g = our_test_graph()
+        g.remove((None, RDF.type, None))
+        ctx = self.handler.context("test_handler", "delete some data")
+        ctx.add(g)
+        ctx.commit()
 
-    ctx = handler.context("test_handler", "put data back")
-    ctx.add(our_test_graph())
-    ctx.commit()
+    def test_04_undo(self):
+        ## undo the last change (remove the types again)
+        constructed = self.handler.get(identifier)
+        ver = constructed.version()
+        cs = self.handler.changeset(ver)
+        cs.undo(constructed)
 
-def test_05_construct():
-    ## construct head from changes in previous step
-    constructed = handler.construct(identifier)
-    assert len(constructed) == len(our_test_graph()) + 1
+        ctx = self.handler.context("test_handler", "put data back")
+        ctx.add(our_test_graph())
+        ctx.commit()
+
+    def test_05_construct(self):
+        ## construct head from changes in previous step
+        constructed = self.handler.construct(identifier)
+        assert len(constructed) == len(our_test_graph()) + 1
+

ordf/tests/test_pt.py

 """
 Test Pairtree storage of models
 """
+import os
+import tempfile
+import shutil
+
 from ordf.tests import our_test_graph, identifier
 from ordf.handler import Handler
 from ordf.handler.pt import PairTree
 
-ptree = PairTree("data/ordf_test")
 
-def test_01_create():
-	ptree[identifier] = our_test_graph()
+class TestPairtreeStorage:
+    @classmethod
+    def setup_class(self):
+        self.tmpdir = os.path.join(tempfile.gettempdir(), self.__name__)
+        self.ptree = PairTree(self.tmpdir)
 
-def test_02_read():
-	fsobj = ptree[identifier]
+    @classmethod
+    def teardown_class(self):
+        shutil.rmtree(self.tmpdir)
 
-def test_03_delete():
-    del ptree[identifier]
+    def test_01_create(self):
+        self.ptree[identifier] = our_test_graph()
 
-def test_04_graphops():
-    ptree.put(graph=our_test_graph())
-    ptree.get(our_test_graph())
-    ptree.remove(our_test_graph())
+    def test_02_read(self):
+        fsobj = self.ptree[identifier]
 
-def test_05_handler():
-    handler = Handler()
-    handler.register_reader(ptree)
-    handler.register_writer(ptree)
-    ctx = handler.context("ptree_test", "ptree_test")
-    ctx.add(our_test_graph())
-    ctx.commit()
+    def test_03_delete(self):
+        del self.ptree[identifier]
 
-    for cs in handler.history(identifier):
-        ptree.remove(cs)
-    ptree.remove(our_test_graph())
+    def test_04_graphops(self):
+        self.ptree.put(graph=our_test_graph())
+        self.ptree.get(our_test_graph())
+        self.ptree.remove(our_test_graph())
+
+    def test_05_handler(self):
+        handler = Handler()
+        handler.register_reader(self.ptree)
+        handler.register_writer(self.ptree)
+        ctx = handler.context("ptree_test", "ptree_test")
+        ctx.add(our_test_graph())
+        ctx.commit()
+
+        for cs in handler.history(identifier):
+            self.ptree.remove(cs)
+        self.ptree.remove(our_test_graph())
+