Commits

Chris McDonough  committed 87e701f

test urlgenerator; rename context property to ctx (it will never be callable)

  • Participants
  • Parent commits 9dcca90

Comments (0)

Files changed (5)

File akhet/tests/test.py

-import os
-import shutil
-import tempfile
-import unittest
-
-import pyramid.view
-import sqlalchemy as sa
-from sqlalchemy.engine.base import Engine
-
-import akhet
-import akhet.static
-
-class TestAddStaticRoute(unittest.TestCase):
-    def _callFUT(self, config, package, subdir, cache_max_age=3600,
-                 **add_route_args):
-        return akhet.static.add_static_route(config, package, subdir,
-                                cache_max_age=cache_max_age, **add_route_args)
-
-    def test_pattern_is_bad_arg(self):
-        self.assertRaises(TypeError, self._callFUT,
-                          None, None, None, pattern="foo")
-
-    def test_view_is_bad_arg(self):
-        self.assertRaises(TypeError, self._callFUT,
-                          None, None, None, view="foo")
-
-    def test_has_name(self):
-        config = DummyConfig()
-        self._callFUT(config, "package", "subdir", name="myname")
-        self.assertEqual(len(config.routes), 1)
-        route = config.routes[0]
-        self.assertEqual(route["pattern"], "/*subpath")
-        self.assertEqual(route["name"], "myname")
-        self.assertEqual(route["kw"]["custom_predicates"][0].__class__,
-            akhet.static.StaticViewPredicate)
-        self.assertEqual(route["kw"]["view"].__class__,
-            pyramid.view.static)
-
-    def test_has_no_name(self):
-        config = DummyConfig()
-        self._callFUT(config, "package", "subdir")
-        self.assertEqual(len(config.routes), 1)
-        route = config.routes[0]
-        self.assertEqual(route["pattern"], "/*subpath")
-        self.assertEqual(route["name"], "static")
-
-class TestStaticViewPredicate(unittest.TestCase):
-    def _makeOne(self, package, subdir):
-        return akhet.static.StaticViewPredicate(package, subdir)
-
-    def test___call___has_no_subpath(self):
-        inst = self._makeOne("package", "subdir")
-        self.assertEqual(inst({"match":{"subpath":()}}, None), False)
-
-    def test___call___resource_exists(self):
-        inst = self._makeOne("akhet", "tests")
-        self.assertEqual(inst({"match":{"subpath":("test.py",)}}, None), True)
-
-    def test___call___resource_doesnt_exist(self):
-        inst = self._makeOne("akhet", "tests")
-        self.assertEqual(inst({"match":{"subpath":("wont.py",)}}, None), False)
-
-class Test_includeme(unittest.TestCase):
-    def _callFUT(self, config):
-        return akhet.includeme(config)
-
-    def test_it(self):
-        from akhet.static import add_static_route
-        config = DummyConfig()
-        self._callFUT(config)
-        self.assertEqual(config.directives["add_static_route"],
-                         add_static_route)
-
-
-class DummyConfig(object):
-    def __init__(self):
-        self.routes = []
-        self.directives = {}
-
-    def add_route(self, name, pattern, **kw):
-        self.routes.append({"name":name, "pattern":pattern, "kw":kw})
-
-    def add_directive(self, name, value):
-        self.directives[name] = value

File akhet/tests/test_init.py

+import unittest
+import akhet
+
+class Test_includeme(unittest.TestCase):
+    def _callFUT(self, config):
+        return akhet.includeme(config)
+
+    def test_it(self):
+        from akhet.static import add_static_route
+        config = DummyConfig()
+        self._callFUT(config)
+        self.assertEqual(config.directives["add_static_route"],
+                         add_static_route)
+
+
+class DummyConfig(object):
+    def __init__(self):
+        self.routes = []
+        self.directives = {}
+
+    def add_directive(self, name, value):
+        self.directives[name] = value

File akhet/tests/test_static.py

+import unittest
+
+import pyramid.view
+
+import akhet
+import akhet.static
+
+class TestAddStaticRoute(unittest.TestCase):
+    def _callFUT(self, config, package, subdir, cache_max_age=3600,
+                 **add_route_args):
+        return akhet.static.add_static_route(config, package, subdir,
+                                cache_max_age=cache_max_age, **add_route_args)
+
+    def test_pattern_is_bad_arg(self):
+        self.assertRaises(TypeError, self._callFUT,
+                          None, None, None, pattern="foo")
+
+    def test_view_is_bad_arg(self):
+        self.assertRaises(TypeError, self._callFUT,
+                          None, None, None, view="foo")
+
+    def test_has_name(self):
+        config = DummyConfig()
+        self._callFUT(config, "package", "subdir", name="myname")
+        self.assertEqual(len(config.routes), 1)
+        route = config.routes[0]
+        self.assertEqual(route["pattern"], "/*subpath")
+        self.assertEqual(route["name"], "myname")
+        self.assertEqual(route["kw"]["custom_predicates"][0].__class__,
+            akhet.static.StaticViewPredicate)
+        self.assertEqual(route["kw"]["view"].__class__,
+            pyramid.view.static)
+
+    def test_has_no_name(self):
+        config = DummyConfig()
+        self._callFUT(config, "package", "subdir")
+        self.assertEqual(len(config.routes), 1)
+        route = config.routes[0]
+        self.assertEqual(route["pattern"], "/*subpath")
+        self.assertEqual(route["name"], "static")
+
+class TestStaticViewPredicate(unittest.TestCase):
+    def _makeOne(self, package, subdir):
+        return akhet.static.StaticViewPredicate(package, subdir)
+
+    def test___call___has_no_subpath(self):
+        inst = self._makeOne("package", "subdir")
+        self.assertEqual(inst({"match":{"subpath":()}}, None), False)
+
+    def test___call___resource_exists(self):
+        inst = self._makeOne("akhet", "tests")
+        self.assertEqual(
+            inst({"match":{"subpath":("test_static.py",)}}, None), True)
+
+    def test___call___resource_doesnt_exist(self):
+        inst = self._makeOne("akhet", "tests")
+        self.assertEqual(inst({"match":{"subpath":("wont.py",)}}, None), False)
+
+class DummyConfig(object):
+    def __init__(self):
+        self.routes = []
+        self.directives = {}
+
+    def add_route(self, name, pattern, **kw):
+        self.routes.append({"name":name, "pattern":pattern, "kw":kw})
+

File akhet/tests/test_urlgenerator.py

+import unittest
+
+from akhet import urlgenerator
+
+from pyramid import testing
+
+class TestURLGenerator(unittest.TestCase):
+    def setUp(self):
+        self.config = testing.setUp()
+
+    def tearDown(self):
+        testing.tearDown()
+        
+    def _makeOne(self, context, request, qualified=False):
+        return urlgenerator.URLGenerator(context, request, qualified=qualified)
+
+    def test_ctor(self):
+        context = testing.DummyResource()
+        request = testing.DummyRequest()
+        inst = self._makeOne(context, request, True)
+        self.assertEqual(inst.context, context)
+        self.assertEqual(inst.request, request)
+        self.assertEqual(inst.qualified, True)
+        
+    def test_ctx(self):
+        context = testing.DummyResource()
+        request = testing.DummyRequest()
+        inst = self._makeOne(context, request, True)
+        result = inst.ctx
+        self.assertEqual(result, 'http://example.com/')
+
+    def test_app_qualified(self):
+        context = testing.DummyResource()
+        request = testing.DummyRequest()
+        inst = self._makeOne(context, request, True)
+        result = inst.app
+        self.assertEqual(result, 'http://example.com')
+
+    def test_app_notqualified(self):
+        context = testing.DummyResource()
+        request = testing.DummyRequest()
+        inst = self._makeOne(context, request, False)
+        result = inst.app
+        self.assertEqual(result, '/')
+
+    def test_route_qualified(self):
+        self.config.add_route('home', '/')
+        context = testing.DummyResource()
+        request = testing.DummyRequest()
+        inst = self._makeOne(context, request, True)
+        result = inst.route('home', 'a', _query={'b':'1'})
+        self.assertEqual(result, 'http://example.com/a?b=1')
+
+    def test_route_notqualified(self):
+        self.config.add_route('home', '/')
+        context = testing.DummyResource()
+        request = testing.DummyRequest()
+        inst = self._makeOne(context, request, False)
+        result = inst.route('home', 'a', _query={'b':'1'})
+        self.assertEqual(result, '/a?b=1')
+
+    def test___call__(self):
+        self.config.add_route('home', '/')
+        context = testing.DummyResource()
+        request = testing.DummyRequest()
+        inst = self._makeOne(context, request, True)
+        result = inst('home', 'a', _query={'b':'1'})
+        self.assertEqual(result, 'http://example.com/a?b=1')
+
+    def test_current(self):
+        self.config.add_route('home', '/')
+        context = testing.DummyResource()
+        request = testing.DummyRequest()
+        request.matched_route = DummyRoute()
+        inst = self._makeOne(context, request, True)
+        result = inst.current('a', _query={'b':'1'})
+        self.assertEqual(result, 'http://example.com/a?b=1')
+
+    def test_resource(self):
+        context = testing.DummyResource()
+        request = testing.DummyRequest()
+        inst = self._makeOne(context, request, True)
+        result = inst.resource('a', query={'b':'1'})
+        self.assertEqual(result, 'http://example.com/a?b=1')
+
+class DummyRoute(object):
+    name = 'home'
+    

File akhet/urlgenerator.py

         self.qualified = qualified
 
     @reify
-    def context(self):
+    def ctx(self):
         """The URL of the default view for the current context.
 
         I'm a "reified" attribute which means I start out as a property but