Skip to content

Commit

Permalink
- testing.setUp now adds a settings attribute to the registry…
Browse files Browse the repository at this point in the history
… (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``.
  • Loading branch information
mcdonc committed Jan 21, 2011
1 parent beff12f commit 2a13b0d
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 3 deletions.
10 changes: 10 additions & 0 deletions CHANGES.txt
Expand Up @@ -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
-------------

Expand Down
2 changes: 1 addition & 1 deletion pyramid/renderers.py
Expand Up @@ -268,7 +268,7 @@ def __init__(self, name=None, package=None, registry=None):

@reify
def settings(self):
settings = self.registry.settings
settings = self.registry.settings or {}
return settings

@reify
Expand Down
10 changes: 9 additions & 1 deletion pyramid/testing.py
Expand Up @@ -613,7 +613,8 @@ def add_response_callback(self, callback):
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.
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion pyramid/tests/test_settings.py
Expand Up @@ -243,7 +243,7 @@ def _callFUT(self):
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}
Expand Down
24 changes: 24 additions & 0 deletions pyramid/tests/test_testing.py
Expand Up @@ -586,6 +586,30 @@ def test_it_with_hook_zca_false(self):
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
Expand Down

0 comments on commit 2a13b0d

Please sign in to comment.