From 2a13b0dff000c5cd766299c52d495d82e3ec83b7 Mon Sep 17 00:00:00 2001 From: Chris McDonough Date: Fri, 21 Jan 2011 04:38:14 -0500 Subject: - ``testing.setUp`` now adds a ``settings`` attribute to the registry (both when it's passed a registry without any settings and when it creates one). - The ``testing.setUp`` function now takes a ``settings`` argument, which should be a dictionary. Its values will subsequently be available on the returned ``config`` object as ``config.registry.settings``. --- CHANGES.txt | 10 ++++++++++ pyramid/renderers.py | 2 +- pyramid/testing.py | 10 +++++++++- pyramid/tests/test_settings.py | 2 +- pyramid/tests/test_testing.py | 24 ++++++++++++++++++++++++ 5 files changed, 45 insertions(+), 3 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 886f2d5ae..5b228e77a 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -13,6 +13,16 @@ Features to ``True``. If it is set to ``False``, the feature of the policy which sets a cookie with a wilcard domain will be turned off. +Bug Fixes +--------- + +- ``testing.setUp`` now adds a ``settings`` attribute to the registry (both + when it's passed a registry without any settings and when it creates one). + +- The ``testing.setUp`` function now takes a ``settings`` argument, which + should be a dictionary. Its values will subsequently be available on the + returned ``config`` object as ``config.registry.settings``. + Documentation ------------- diff --git a/pyramid/renderers.py b/pyramid/renderers.py index 2e0514b01..bc3377815 100644 --- a/pyramid/renderers.py +++ b/pyramid/renderers.py @@ -268,7 +268,7 @@ class RendererHelper(object): @reify def settings(self): - settings = self.registry.settings + settings = self.registry.settings or {} return settings @reify diff --git a/pyramid/testing.py b/pyramid/testing.py index 17012785b..f159d47f8 100644 --- a/pyramid/testing.py +++ b/pyramid/testing.py @@ -613,7 +613,8 @@ class DummyRequest(object): self.response_callbacks = [] self.response_callbacks.append(callback) -def setUp(registry=None, request=None, hook_zca=True, autocommit=True): +def setUp(registry=None, request=None, hook_zca=True, autocommit=True, + settings=None): """ Set :app:`Pyramid` registry and request thread locals for the duration of a single unit test. @@ -661,6 +662,9 @@ def setUp(registry=None, request=None, hook_zca=True, autocommit=True): :mod:`zope.component` package cannot be imported, or if ``hook_zca`` is ``False``, the hook will not be set. + If ``settings`` is not None, it must be a dictionary representing the + values passed to a Configurator as its ``settings=`` argument. + This function returns an instance of the :class:`pyramid.config.Configurator` class, which can be used for further configuration to set up an environment suitable @@ -674,6 +678,10 @@ def setUp(registry=None, request=None, hook_zca=True, autocommit=True): if registry is None: registry = Registry('testing') config = Configurator(registry=registry, autocommit=autocommit) + if settings is None: + settings = {} + if getattr(registry, 'settings', None) is None: + config._set_settings(settings) if hasattr(registry, 'registerUtility'): # Sometimes nose calls us with a non-registry object because # it thinks this function is module test setup. Likewise, diff --git a/pyramid/tests/test_settings.py b/pyramid/tests/test_settings.py index 5e28000a6..a444539e0 100644 --- a/pyramid/tests/test_settings.py +++ b/pyramid/tests/test_settings.py @@ -243,7 +243,7 @@ class TestGetSettings(unittest.TestCase): return get_settings() def test_it_nosettings(self): - self.assertEqual(self._callFUT(), None) + self.assertEqual(self._callFUT()['reload_templates'], False) def test_it_withsettings(self): settings = {'a':1} diff --git a/pyramid/tests/test_testing.py b/pyramid/tests/test_testing.py index eaaad6aef..b432928ab 100644 --- a/pyramid/tests/test_testing.py +++ b/pyramid/tests/test_testing.py @@ -586,6 +586,30 @@ class Test_setUp(unittest.TestCase): getSiteManager.reset() manager.clear() + def test_it_with_settings_passed_explicit_registry(self): + from zope.component import getSiteManager + from pyramid.threadlocal import manager + from pyramid.registry import Registry + registry = Registry() + try: + self._callFUT(registry=registry, hook_zca=False, + settings=dict(a=1)) + self.assertEqual(registry.settings['a'], 1) + finally: + getSiteManager.reset() + manager.clear() + + def test_it_with_settings_passed_implicit_registry(self): + from zope.component import getSiteManager + from pyramid.threadlocal import manager + try: + config = self._callFUT(hook_zca=False, + settings=dict(a=1)) + self.assertEqual(config.registry.settings['a'], 1) + finally: + getSiteManager.reset() + manager.clear() + class Test_cleanUp(Test_setUp): def _callFUT(self, *arg, **kw): from pyramid.testing import cleanUp -- cgit v1.2.3