[Debtags-commits] [SCM] Debian Data Export - A tool to publish Debian information branch, werkzeug, updated. 700393b475fbd483117202e1877e53e97eb53116

Enrico Zini enrico at enricozini.org
Mon Nov 29 10:16:59 UTC 2010


The following commit has been merged in the werkzeug branch:
commit 6e6a1c7bcfef62b0b8d91f2aa4e82579fb9cccca
Author: Enrico Zini <enrico at enricozini.org>
Date:   Mon Nov 29 10:15:53 2010 +0000

    Library of test utility functions

diff --git a/tests/test_base_functions.py b/tests/test_base_functions.py
index f78d1cf..adb339c 100644
--- a/tests/test_base_functions.py
+++ b/tests/test_base_functions.py
@@ -1,80 +1,23 @@
 import unittest
 import dde
 import dde.util
+import testutils
 import fnmatch, sys, random
 from urllib import quote_plus
 
 #
-# Test module we can query in many ways to test all sorts of functions and
-# rendering
-#
-
-def make_module():
-    module = dde.Module("test")
-
-    # Autodoc from list method
-    @module.list("/")
-    def do_index():
-        "List of test methods"
-        return ["string", "list", "dict", "gen"]
-
-    # Autodoc from data method
-    @module.data("/string")
-    def do_string():
-        "Return a string value"
-        return "test string"
-
-    # Combined data and list method, with autodoc
-    @module.data("/list")
-    @module.list("/list")
-    def do_list():
-        "Return a list of ints"
-        return [1, 2, 3]
-
-    # Explicit doc method
-    @module.data("/dict")
-    def do_dict():
-        return dict(foo="bar")
-    @module.doc("/dict")
-    def do_dict_doc():
-        return "Return a dict"
-
-    # Return a generator
-    @module.data("/gen")
-    def do_gen():
-        "Return a generator"
-        yield "foo"
-        yield ["bar", "baz"]
-        yield dict(antani="blinda")
-
-    return module
-
-#
 # Test data
 #
 
-class TestBaseFunctions(unittest.TestCase):
+class TestBaseFunctions(unittest.TestCase, testutils.DDETest):
     def __init__(self, *args, **kw):
         super(TestBaseFunctions, self).__init__(*args, **kw)
         self.backend = dde.DDE()
         # Do not load plugins
         # Register our test module
-        module = make_module()
+        module = testutils.make_dde_test_module()
         self.backend.register_module(module)
 
-    def serialise(self, fmt, val, **kw):
-        "Serialise a value with the given format"
-        renderer = dde.TYPE_INFO[fmt]
-        r = renderer(**kw)
-        if dde.util.isgen(val):
-            return "".join(r.render_generator(val))
-        else:
-            return r.render_value(val)
-
-    def assertIn(self, needle, haystack):
-        if not needle in haystack:
-            self.fail("%s is not in %s" % (repr(needle), repr(haystack)))
-
     def testDDERoot(self):
         "Test the behaviour of the DDE root node"
         self.assertIn("test", self.backend.get_list("/"))
@@ -91,6 +34,7 @@ class TestBaseFunctions(unittest.TestCase):
         "Test the behaviour of the string node"
         self.assertEquals(self.backend.get_list("/test/string"), [])
         self.assert_(self.backend.get_doc("/test/string") is not None)
+        self.assertReachable("/test/string")
 
         val = self.backend.get_data("/test/string")
         # Serialise in all formats
diff --git a/tests/testutils.py b/tests/testutils.py
new file mode 100644
index 0000000..cfaa564
--- /dev/null
+++ b/tests/testutils.py
@@ -0,0 +1,77 @@
+import dde
+import dde.util
+
+def make_dde_test_module():
+    """
+    Make a test module we can query in many ways to test all sorts of functions
+    and rendering
+    """
+    module = dde.Module("test")
+
+    # Autodoc from list method
+    @module.list("/")
+    def do_index():
+        "List of test methods"
+        return ["string", "list", "dict", "gen"]
+
+    # Autodoc from data method
+    @module.data("/string")
+    def do_string():
+        "Return a string value"
+        return "test string"
+
+    # Combined data and list method, with autodoc
+    @module.data("/list")
+    @module.list("/list")
+    def do_list():
+        "Return a list of ints"
+        return [1, 2, 3]
+
+    # Explicit doc method
+    @module.data("/dict")
+    def do_dict():
+        return dict(foo="bar")
+    @module.doc("/dict")
+    def do_dict_doc():
+        return "Return a dict"
+
+    # Return a generator
+    @module.data("/gen")
+    def do_gen():
+        "Return a generator"
+        yield "foo"
+        yield ["bar", "baz"]
+        yield dict(antani="blinda")
+
+    return module
+
+class DDETest:
+    """
+    Mixin for unit tests to add useful utility functions
+    """
+    def serialise(self, fmt, val, **kw):
+        "Serialise a value with the given format"
+        renderer = dde.TYPE_INFO[fmt]
+        r = renderer(**kw)
+        if dde.util.isgen(val):
+            return "".join(r.render_generator(val))
+        else:
+            return r.render_value(val)
+
+    def assertIn(self, needle, haystack):
+        if not needle in haystack:
+            self.fail("%s is not in %s" % (repr(needle), repr(haystack)))
+
+    def assertReachable(self, target):
+        path = target.strip("/")
+        if not path: return
+        path = path.split("/")
+        lead = []
+        while path:
+            node = "/" + "/".join(lead)
+            children = self.backend.get_list(node)
+            if children is None:
+                self.fail("trying to reach %s: %s list method returns None" % (target, node))
+            if path[0] not in children:
+                self.fail("trying to reach %s: %s list method does not contain \"%s\"" % (target, node, path[0]))
+            lead.append(path.pop(0))

-- 
Debian Data Export - A tool to publish Debian information



More information about the Debtags-commits mailing list