diff options
| author | Chris McDonough <chrism@agendaless.com> | 2009-10-10 03:58:41 +0000 |
|---|---|---|
| committer | Chris McDonough <chrism@agendaless.com> | 2009-10-10 03:58:41 +0000 |
| commit | 927acdcb1419e636a67d328084a546834aaa173d (patch) | |
| tree | 691789b788621374bf8dd5476db1e3f0bfad7321 | |
| parent | d85ac83b981cce853790c1b58bfcab0f0d7a003a (diff) | |
| download | pyramid-927acdcb1419e636a67d328084a546834aaa173d.tar.gz pyramid-927acdcb1419e636a67d328084a546834aaa173d.tar.bz2 pyramid-927acdcb1419e636a67d328084a546834aaa173d.zip | |
- Add a new ``repoze.bfg.testing`` API: ``registerRoute``, for
registering routes to satisfy calls to
e.g. ``repoze.bfg.url.route_url`` in unit tests.
| -rw-r--r-- | repoze/bfg/testing.py | 23 | ||||
| -rw-r--r-- | repoze/bfg/tests/test_testing.py | 28 |
2 files changed, 51 insertions, 0 deletions
diff --git a/repoze/bfg/testing.py b/repoze/bfg/testing.py index 2e31afce6..25a178f66 100644 --- a/repoze/bfg/testing.py +++ b/repoze/bfg/testing.py @@ -220,6 +220,28 @@ def registerTraverserFactory(traverser, for_=Interface): from repoze.bfg.interfaces import ITraverserFactory return registerAdapter(traverser, for_, ITraverserFactory) +def registerRoute(path, name, factory=None): + """ Register a new route using a path (e.g. ``:pagename``), a name + (e.g. 'home'), and an optional root factory. This is useful for + testing code that calls e.g. ``route_url``.""" + from repoze.bfg.interfaces import IRoutesMapper + from zope.component import queryUtility + from repoze.bfg.urldispatch import RoutesRootFactory + from zope.component import getSiteManager + mapper = queryUtility(IRoutesMapper) + if mapper is None: + mapper = RoutesRootFactory(DummyRootFactory) + sm = getSiteManager() + sm.registerUtility(mapper, IRoutesMapper) + mapper.connect(path, name, factory) + +class DummyRootFactory(object): + __parent__ = None + __name__ = None + def __init__(self, environ): + if 'bfg.routes.matchdict' in environ: + self.__dict__.update(environ['bfg.routes.matchdict']) + class DummySecurityPolicy: """ A standin for both an IAuthentication and IAuthorization policy """ def __init__(self, userid=None, groupids=(), permissive=True): @@ -410,6 +432,7 @@ class DummyRequest: self.headers = headers self.params = params self.cookies = cookies + self.matchdict = {} self.GET = params if post is not None: self.method = 'POST' diff --git a/repoze/bfg/tests/test_testing.py b/repoze/bfg/tests/test_testing.py index 938115ecd..05cddc980 100644 --- a/repoze/bfg/tests/test_testing.py +++ b/repoze/bfg/tests/test_testing.py @@ -259,6 +259,34 @@ class TestTestingFunctions(unittest.TestCase): testing.registerUtility(utility, iface, name='mudge') self.assertEqual(getUtility(iface, name='mudge')(), 'foo') + def test_registerRoute(self): + from repoze.bfg.url import route_url + from repoze.bfg.interfaces import IRoutesMapper + from repoze.bfg.testing import registerRoute + from zope.component import getSiteManager + class Factory: + def __init__(self, environ): + """ """ + class DummyRequest: + application_url = 'http://example.com' + registerRoute(':pagename', 'home', Factory) + sm = getSiteManager() + mapper = sm.getUtility(IRoutesMapper) + self.assertEqual(len(mapper.routelist), 1) + request = DummyRequest() + self.assertEqual(route_url('home', request, pagename='abc'), + 'http://example.com/abc') + +class TestDummyRootFactory(unittest.TestCase): + def _makeOne(self, environ): + from repoze.bfg.testing import DummyRootFactory + return DummyRootFactory(environ) + + def test_it(self): + environ = {'bfg.routes.matchdict':{'a':1}} + factory = self._makeOne(environ) + self.assertEqual(factory.a, 1) + class TestDummySecurityPolicy(unittest.TestCase): def _getTargetClass(self): from repoze.bfg.testing import DummySecurityPolicy |
