diff options
| author | Chris McDonough <chrism@agendaless.com> | 2009-10-15 15:44:33 +0000 |
|---|---|---|
| committer | Chris McDonough <chrism@agendaless.com> | 2009-10-15 15:44:33 +0000 |
| commit | 17b8bc69b783be4b81a6c42818c6227845b16683 (patch) | |
| tree | fa481f95e2508b6cf57185cec3e687372fa8ad18 /repoze/bfg/tests/test_router.py | |
| parent | 083422c0c66c1aa53f9d96c6fd185e238bc51708 (diff) | |
| download | pyramid-17b8bc69b783be4b81a6c42818c6227845b16683.tar.gz pyramid-17b8bc69b783be4b81a6c42818c6227845b16683.tar.bz2 pyramid-17b8bc69b783be4b81a6c42818c6227845b16683.zip | |
Features
--------
- Add ``setUp`` and ``tearDown`` functions to the
``repoze.bfg.testing`` module. Using ``setUp`` in a test setup and
``tearDown`` in a test teardown is now the recommended way to do
component registry setup and teardown. Previously, it was
recommended that a single function named
``repoze.bfg.testing.cleanUp`` be called in both the test setup and
tear down. ``repoze.bfg.testing.cleanUp`` still exists (and will
exist "forever" due to its widespread use); it is now just an alias
for ``repoze.bfg.testing.setUp`` and is nominally deprecated.
- The BFG component registry is now available in view and event
subscriber code as an attribute of the request
ie. ``request.registry``. This fact is currently undocumented
except for this note, because BFG developers never need to interact
with the registry directly anywhere else.
- The BFG component registry now inherits from ``dict``, meaning that
it can optionally be used as a simple dictionary. *Component*
registrations performed against it via e.g. ``registerUtility``,
``registerAdapter``, and similar API methods are kept in a
completely separate namespace than its dict members, so using the
its component API methods won't effect the keys and values in the
dictionary namespace. Likewise, though the component registry
"happens to be" a dictionary, use of mutating dictionary methods
such as ``__setitem__`` will have no influence on any component
registrations made against it. In other words, the registry object
you obtain via e.g. ``repoze.bfg.threadlocal.get_current_registry``
or ``request.registry`` happens to be both a component registry and
a dictionary, but using its component-registry API won't impact data
added to it via its dictionary API and vice versa. This is a
forward compatibility move based on the goals of "marco".
Documentation
-------------
- Various tutorial test modules updated to use
``repoze.bfg.testing.setUp`` and ``repoze.bfg.testing.tearDown``
methods in order to encourage this as best practice going forward.
Backwards Incompatibilities
---------------------------
- Importing ``getSiteManager`` and ``get_registry`` from
``repoze.bfg.registry`` is no longer supported. These imports were
deprecated in repoze.bfg 1.0. Import of ``getSiteManager`` should
be done as ``from zope.component import getSiteManager``. Import of
``get_registry`` should be done as ``from repoze.bfg.threadlocal
import get_current_registry``. This was done to prevent a circular
import dependency.
- Code bases which alternately invoke both
``zope.testing.cleanup.cleanUp`` and ``repoze.bfg.testing.cleanUp``
(treating them equivalently, using them interchangeably) in the
setUp/tearDown of unit tests will begin to experience test failures
due to lack of test isolation. The "right" mechanism is
``repoze.bfg.testing.cleanUp`` (or the combination of
``repoze.bfg.testing.setUp`` and
``repoze.bfg.testing.tearDown``). but a good number of legacy
codebases will use ``zope.testing.cleanup.cleanUp`` instead. We
support ``zope.testing.cleanup.cleanUp`` but not in combination with
``repoze.bfg.testing.cleanUp`` in the same codebase. You should use
one or the other test cleanup function in a single codebase, but not
both.
Internal
--------
- Created new ``repoze.bfg.configuration`` module which assumes
responsibilities previously held by the ``repoze.bfg.registry`` and
``repoze.bfg.router`` modules (avoid a circular import dependency).
- The result of the ``zope.component.getSiteManager`` function in unit
tests set up with ``repoze.bfg.testing.cleanUp`` or
``repoze.bfg.testing.setUp`` will be an instance of
``repoze.bfg.registry.Registry`` instead of the global
``zope.component.globalregistry.base`` registry. This also means
that the threadlocal ZCA API functions such as ``getAdapter`` and
``getUtility`` as well as internal BFG machinery (such as
``model_url`` and ``route_url``) will consult this registry within
unit tests. This is a forward compatibility move based on the goals
of "marco".
- Removed ``repoze.bfg.testing.addCleanUp`` function and associated
module-scope globals. This was never an API.
Diffstat (limited to 'repoze/bfg/tests/test_router.py')
| -rw-r--r-- | repoze/bfg/tests/test_router.py | 268 |
1 files changed, 38 insertions, 230 deletions
diff --git a/repoze/bfg/tests/test_router.py b/repoze/bfg/tests/test_router.py index 5c39b65f0..151734b9b 100644 --- a/repoze/bfg/tests/test_router.py +++ b/repoze/bfg/tests/test_router.py @@ -2,7 +2,7 @@ import unittest from repoze.bfg.testing import cleanUp -class RouterTests(unittest.TestCase): +class TestRouter(unittest.TestCase): def setUp(self): from repoze.bfg.registry import Registry from zope.component import getSiteManager @@ -407,230 +407,6 @@ class RouterTests(unittest.TestCase): self.assertEqual(len(router.threadlocal_manager.pushed), 1) self.assertEqual(len(router.threadlocal_manager.popped), 1) -class MakeAppTests(unittest.TestCase): - def setUp(self): - cleanUp() - - def tearDown(self): - cleanUp() - - def _callFUT(self, *arg, **kw): - from repoze.bfg.router import make_app - return make_app(*arg, **kw) - - def test_fixtureapp_default_filename_withpackage(self): - manager = DummyRegistryManager() - from repoze.bfg.tests import fixtureapp - rootfactory = DummyRootFactory(None) - app = self._callFUT(rootfactory, fixtureapp, manager=manager) - self.assertEqual(app.registry.__name__, 'repoze.bfg.tests.fixtureapp') - from repoze.bfg.tests.fixtureapp.models import IFixture - self.failUnless(app.registry.queryUtility(IFixture)) # only in c.zcml - - def test_fixtureapp_explicit_filename(self): - manager = DummyRegistryManager() - from repoze.bfg.tests import fixtureapp - rootfactory = DummyRootFactory(None) - app = self._callFUT(rootfactory, fixtureapp, filename='another.zcml', - manager=manager) - self.assertEqual(app.registry.__name__, 'repoze.bfg.tests.fixtureapp') - from repoze.bfg.tests.fixtureapp.models import IFixture - self.failIf(app.registry.queryUtility(IFixture)) # only in c.zcml - - def test_fixtureapp_explicit_filename_in_options(self): - import os - manager = DummyRegistryManager() - rootfactory = DummyRootFactory(None) - from repoze.bfg.tests import fixtureapp - zcmlfile = os.path.join(os.path.dirname(fixtureapp.__file__), - 'another.zcml') - app = self._callFUT(rootfactory, fixtureapp, filename='configure.zcml', - options={'configure_zcml':zcmlfile}, - manager=manager) - self.assertEqual(app.registry.__name__, 'repoze.bfg.tests.fixtureapp') - from repoze.bfg.tests.fixtureapp.models import IFixture - self.failIf(app.registry.queryUtility(IFixture)) # only in c.zcml - - def test_fixtureapp_explicit_specification_in_options(self): - manager = DummyRegistryManager() - rootfactory = DummyRootFactory(None) - from repoze.bfg.tests import fixtureapp - zcmlfile = 'repoze.bfg.tests.fixtureapp.subpackage:yetanother.zcml' - app = self._callFUT(rootfactory, fixtureapp, filename='configure.zcml', - options={'configure_zcml':zcmlfile}, - manager=manager) - self.assertEqual(app.registry.__name__, - 'repoze.bfg.tests.fixtureapp.subpackage') - from repoze.bfg.tests.fixtureapp.models import IFixture - self.failIf(app.registry.queryUtility(IFixture)) # only in c.zcml - - def test_fixtureapp_filename_hascolon_isabs(self): - manager = DummyRegistryManager() - rootfactory = DummyRootFactory(None) - from repoze.bfg.tests import fixtureapp - zcmlfile = 'repoze.bfg.tests.fixtureapp.subpackage:yetanother.zcml' - class Dummy: - def isabs(self, name): - return True - os = Dummy() - os.path = Dummy() - self.assertRaises(IOError, self._callFUT, - rootfactory, - fixtureapp, - filename='configure.zcml', - options={'configure_zcml':zcmlfile}, - manager=manager, - os=os) - - def test_event(self): - manager = DummyRegistryManager() - def subscriber(event): - event.app.created = True - from repoze.bfg.interfaces import IWSGIApplicationCreatedEvent - from zope.component import getGlobalSiteManager - getGlobalSiteManager().registerHandler( - subscriber, - (IWSGIApplicationCreatedEvent,) - ) - from repoze.bfg.tests import fixtureapp - rootfactory = DummyRootFactory(None) - app = self._callFUT(rootfactory, fixtureapp, manager=manager) - assert app.created is True - - def test_custom_settings(self): - manager = DummyRegistryManager() - options= {'mysetting':True} - from repoze.bfg.tests import fixtureapp - rootfactory = DummyRootFactory(None) - app = self._callFUT(rootfactory, fixtureapp, options=options, - manager=manager) - from repoze.bfg.interfaces import ISettings - settings = app.registry.getUtility(ISettings) - self.assertEqual(settings.reload_templates, False) - self.assertEqual(settings.debug_authorization, False) - self.assertEqual(settings.mysetting, True) - - def test_registrations(self): - manager = DummyRegistryManager() - options= {'reload_templates':True, - 'debug_authorization':True} - from repoze.bfg.tests import fixtureapp - rootfactory = DummyRootFactory(None) - app = self._callFUT(rootfactory, fixtureapp, options=options, - manager=manager) - from repoze.bfg.interfaces import ISettings - from repoze.bfg.interfaces import ILogger - from repoze.bfg.interfaces import IRootFactory - settings = app.registry.getUtility(ISettings) - logger = app.registry.getUtility(ILogger, name='repoze.bfg.debug') - rootfactory = app.registry.getUtility(IRootFactory) - self.assertEqual(logger.name, 'repoze.bfg.debug') - self.assertEqual(settings.reload_templates, True) - self.assertEqual(settings.debug_authorization, True) - self.assertEqual(rootfactory, rootfactory) - self.failUnless(manager.pushed and manager.popped) - - def test_routes_in_config_with_rootfactory(self): - manager = DummyRegistryManager() - options= {'reload_templates':True, - 'debug_authorization':True} - from repoze.bfg.urldispatch import RoutesRootFactory - from repoze.bfg.tests import routesapp - rootfactory = DummyRootFactory(None) - app = self._callFUT(rootfactory, routesapp, options=options, - manager=manager) - from repoze.bfg.interfaces import ISettings - from repoze.bfg.interfaces import ILogger - from repoze.bfg.interfaces import IRootFactory - settings = app.registry.getUtility(ISettings) - logger = app.registry.getUtility(ILogger, name='repoze.bfg.debug') - effective_rootfactory = app.registry.getUtility(IRootFactory) - self.assertEqual(logger.name, 'repoze.bfg.debug') - self.assertEqual(settings.reload_templates, True) - self.assertEqual(settings.debug_authorization, True) - self.failUnless(isinstance(effective_rootfactory, RoutesRootFactory)) - self.assertEqual(effective_rootfactory.default_root_factory, - rootfactory) - self.failUnless(manager.pushed and manager.popped) - - def test_routes_in_config_no_rootfactory(self): - manager = DummyRegistryManager() - options= {'reload_templates':True, - 'debug_authorization':True} - from repoze.bfg.urldispatch import RoutesRootFactory - from repoze.bfg.router import DefaultRootFactory - from repoze.bfg.tests import routesapp - app = self._callFUT(None, routesapp, options=options, - manager=manager) - from repoze.bfg.interfaces import ISettings - from repoze.bfg.interfaces import ILogger - from repoze.bfg.interfaces import IRootFactory - settings = app.registry.getUtility(ISettings) - logger = app.registry.getUtility(ILogger, name='repoze.bfg.debug') - rootfactory = app.registry.getUtility(IRootFactory) - self.assertEqual(logger.name, 'repoze.bfg.debug') - self.assertEqual(settings.reload_templates, True) - self.assertEqual(settings.debug_authorization, True) - self.failUnless(isinstance(rootfactory, RoutesRootFactory)) - self.assertEqual(rootfactory.default_root_factory, DefaultRootFactory) - self.failUnless(manager.pushed and manager.popped) - - def test_no_routes_in_config_no_rootfactory(self): - manager = DummyRegistryManager() - from repoze.bfg.router import DefaultRootFactory - from repoze.bfg.interfaces import IRootFactory - options= {'reload_templates':True, - 'debug_authorization':True} - from repoze.bfg.tests import fixtureapp - app = self._callFUT(None, fixtureapp, options=options, - manager=manager) - rootfactory = app.registry.getUtility(IRootFactory) - self.assertEqual(rootfactory, DefaultRootFactory) - - def test_authorization_policy_no_authentication_policy(self): - manager = DummyRegistryManager() - from repoze.bfg.interfaces import IAuthorizationPolicy - authzpolicy = DummyContext() - from repoze.bfg.tests import routesapp - logger = DummyLogger() - app = self._callFUT(None, routesapp, authorization_policy=authzpolicy, - debug_logger=logger, manager=manager) - self.failIf(app.registry.queryUtility(IAuthorizationPolicy)) - self.assertEqual(logger.messages, []) - - def test_authentication_policy_no_authorization_policy(self): - manager = DummyRegistryManager() - from repoze.bfg.interfaces import IAuthorizationPolicy - from repoze.bfg.interfaces import IAuthenticationPolicy - from repoze.bfg.authorization import ACLAuthorizationPolicy - authnpolicy = DummyContext() - from repoze.bfg.tests import routesapp - logger = DummyLogger() - app = self._callFUT(None, routesapp, authentication_policy=authnpolicy, - debug_logger=logger, manager=manager) - self.assertEqual(app.registry.getUtility(IAuthenticationPolicy), - authnpolicy) - self.assertEqual( - app.registry.getUtility(IAuthorizationPolicy).__class__, - ACLAuthorizationPolicy) - self.assertEqual(len(logger.messages), 1) # deprecation warning - - def test_authentication_policy_and_authorization_policy(self): - manager = DummyRegistryManager() - from repoze.bfg.interfaces import IAuthorizationPolicy - from repoze.bfg.interfaces import IAuthenticationPolicy - authnpolicy = DummyContext() - authzpolicy = DummyContext() - from repoze.bfg.tests import routesapp - logger = DummyLogger() - app = self._callFUT(None, routesapp, authentication_policy=authnpolicy, - authorization_policy = authzpolicy, - debug_logger=logger, manager=manager) - self.assertEqual(app.registry.getUtility(IAuthenticationPolicy), - authnpolicy) - self.assertEqual(app.registry.getUtility(IAuthorizationPolicy), - authzpolicy) - self.assertEqual(len(logger.messages), 1) # deprecation warning class TestDefaultRootFactory(unittest.TestCase): def _getTargetClass(self): @@ -652,13 +428,38 @@ class TestDefaultRootFactory(unittest.TestCase): self.assertEqual(root.a, 1) self.assertEqual(root.b, 2) +class TestMakeApp(unittest.TestCase): + def setUp(self): + cleanUp() -class DummyRegistryManager: - def push(self, registry): - self.pushed = True + def tearDown(self): + cleanUp() - def pop(self): - self.popped = True + def _callFUT(self, *arg, **kw): + from repoze.bfg.router import make_app + return make_app(None, *arg, **kw) + + def test_it(self): + from repoze.bfg.interfaces import IWSGIApplicationCreatedEvent + from zope.component import getSiteManager + from repoze.bfg.tests import fixtureapp + sm = getSiteManager() + class DummyMakeRegistry(object): + def __call__(self, *arg): + self.arg = arg + return sm + def subscriber(event): + event.app.created = True + dummy_make_registry = DummyMakeRegistry() + manager = DummyRegistryManager() + sm.registerHandler(subscriber, (IWSGIApplicationCreatedEvent,)) + rootfactory = DummyRootFactory(None) + app = self._callFUT(rootfactory, fixtureapp, manager=manager, + make_registry=dummy_make_registry) + self.failUnless(app.created) + self.failUnless(manager.pushed) + self.failUnless(manager.popped) + self.assertEqual(len(dummy_make_registry.arg), 6) class DummyContext: pass @@ -720,3 +521,10 @@ class DummyLogger: warn = info debug = info +class DummyRegistryManager: + def push(self, registry): + self.pushed = True + + def pop(self): + self.popped = True + |
