Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

pep8 gardening

  • Loading branch information...
commit 245ff86d9b3aeef72093cbbf307b889ee8d83e77 1 parent 4e25d06
@reedobrien reedobrien authored
View
4 pyramid/tests/pkgs/rendererscanapp/__init__.py
@@ -1,5 +1,6 @@
from pyramid.view import view_config
+
@view_config(name='one', renderer='string')
def one(request):
return 'One!'
@@ -9,6 +10,7 @@ def one(request):
def two(request):
return 'Two!'
+
def includeme(config):
config.scan()
-
+
View
4 pyramid/tests/test_config/test_i18n.py
@@ -11,6 +11,7 @@
locale3 = os.path.abspath(
os.path.join(here, '..', 'pkgs', 'localeapp', 'locale3'))
+
class TestI18NConfiguratorMixin(unittest.TestCase):
def _makeOne(self, *arg, **kw):
from pyramid.config import Configurator
@@ -20,6 +21,7 @@ def _makeOne(self, *arg, **kw):
def test_set_locale_negotiator(self):
from pyramid.interfaces import ILocaleNegotiator
config = self._makeOne(autocommit=True)
+
def negotiator(request): pass
config.set_locale_negotiator(negotiator)
self.assertEqual(config.registry.getUtility(ILocaleNegotiator),
@@ -87,9 +89,11 @@ def test_add_translation_dirs_abspath(self):
self.assertEqual(config.registry.getUtility(ITranslationDirectories),
[locale])
+
class DummyRequest:
subpath = ()
matchdict = None
+
def __init__(self, environ=None):
if environ is None:
environ = {}
View
257 pyramid/tests/test_config/test_init.py
@@ -17,6 +17,7 @@
from pyramid.exceptions import ConfigurationExecutionError
from pyramid.exceptions import ConfigurationConflictError
+
class ConfiguratorTests(unittest.TestCase):
def _makeOne(self, *arg, **kw):
from pyramid.config import Configurator
@@ -30,7 +31,7 @@ def _getViewCallable(self, config, ctx_iface=None, request_iface=None,
from pyramid.interfaces import IView
from pyramid.interfaces import IViewClassifier
from pyramid.interfaces import IExceptionViewClassifier
- if exception_view: # pragma: no cover
+ if exception_view: # pragma: no cover
classifier = IExceptionViewClassifier
else:
classifier = IViewClassifier
@@ -43,10 +44,11 @@ def _getViewCallable(self, config, ctx_iface=None, request_iface=None,
default=None)
def _registerEventListener(self, config, event_iface=None):
- if event_iface is None: # pragma: no cover
+ if event_iface is None: # pragma: no cover
from zope.interface import Interface
event_iface = Interface
L = []
+
def subscriber(*event):
L.extend(event)
config.registry.registerHandler(subscriber, (event_iface,))
@@ -71,7 +73,7 @@ def test_ctor_no_registry(self):
self.assertTrue(config.registry.getUtility(IRendererFactory, 'string'))
if not PYPY:
self.assertTrue(config.registry.getUtility(IRendererFactory, '.pt'))
- self.assertTrue(config.registry.getUtility(IRendererFactory,'.txt'))
+ self.assertTrue(config.registry.getUtility(IRendererFactory, '.txt'))
def test_begin(self):
from pyramid.config import Configurator
@@ -80,7 +82,7 @@ def test_begin(self):
config.manager = manager
config.begin()
self.assertEqual(manager.pushed,
- {'registry':config.registry, 'request':None})
+ {'registry': config.registry, 'request': None})
self.assertEqual(manager.popped, False)
def test_begin_with_request(self):
@@ -91,7 +93,7 @@ def test_begin_with_request(self):
config.manager = manager
config.begin(request=request)
self.assertEqual(manager.pushed,
- {'registry':config.registry, 'request':request})
+ {'registry': config.registry, 'request': request})
self.assertEqual(manager.popped, False)
def test_end(self):
@@ -112,8 +114,8 @@ def test_ctor_with_package_registry(self):
def test_ctor_noreg_custom_settings(self):
from pyramid.interfaces import ISettings
- settings = {'reload_templates':True,
- 'mysetting':True}
+ settings = {'reload_templates': True,
+ 'mysetting': True}
config = self._makeOne(settings=settings)
settings = config.registry.getUtility(ISettings)
self.assertEqual(settings['reload_templates'], True)
@@ -220,7 +222,9 @@ def test_ctor_exceptionresponse_view_None(self):
def test_ctor_exceptionresponse_view_custom(self):
from pyramid.interfaces import IExceptionResponse
from pyramid.interfaces import IRequest
- def exceptionresponse_view(context, request): pass
+
+ def exceptionresponse_view(context, request):
+ pass
config = self._makeOne(exceptionresponse_view=exceptionresponse_view)
view = self._getViewCallable(config,
ctx_iface=IExceptionResponse,
@@ -311,11 +315,14 @@ def test__fix_registry_notify(self):
def test__fix_registry_queryAdapterOrSelf(self):
from zope.interface import Interface
from zope.interface import implementer
+
class IFoo(Interface):
pass
+
@implementer(IFoo)
class Foo(object):
pass
+
class Bar(object):
pass
adaptation = ()
@@ -358,9 +365,9 @@ def test_setup_registry_registers_default_exceptionresponse_view(self):
config._add_tween = lambda *arg, **kw: False
config.setup_registry()
self.assertEqual(views[0], ((default_exceptionresponse_view,),
- {'context':IExceptionResponse}))
+ {'context': IExceptionResponse}))
self.assertEqual(views[1], ((default_exceptionresponse_view,),
- {'context':WSGIHTTPException}))
+ {'context': WSGIHTTPException}))
def test_setup_registry_registers_default_webob_iresponse_adapter(self):
from webob import Response
@@ -379,7 +386,8 @@ def test_setup_registry_explicit_notfound_trumps_iexceptionresponse(self):
from pyramid.registry import Registry
reg = Registry()
config = self._makeOne(reg, autocommit=True)
- config.setup_registry() # registers IExceptionResponse default view
+ config.setup_registry() # registers IExceptionResponse default view
+
def myview(context, request):
return 'OK'
config.add_view(myview, context=HTTPNotFound, renderer=null_renderer)
@@ -393,8 +401,8 @@ def myview(context, request):
def test_setup_registry_custom_settings(self):
from pyramid.registry import Registry
from pyramid.interfaces import ISettings
- settings = {'reload_templates':True,
- 'mysetting':True}
+ settings = {'reload_templates': True,
+ 'mysetting': True}
reg = Registry()
config = self._makeOne(reg)
config.setup_registry(settings=settings)
@@ -710,19 +718,24 @@ def test_include_with_module_defaults_to_includeme_missing(self):
def test_include_with_route_prefix(self):
root_config = self._makeOne(autocommit=True)
+
def dummy_subapp(config):
self.assertEqual(config.route_prefix, 'root')
root_config.include(dummy_subapp, route_prefix='root')
def test_include_with_nested_route_prefix(self):
root_config = self._makeOne(autocommit=True, route_prefix='root')
+
def dummy_subapp2(config):
self.assertEqual(config.route_prefix, 'root/nested')
+
def dummy_subapp3(config):
self.assertEqual(config.route_prefix, 'root/nested/nested2')
config.include(dummy_subapp4)
+
def dummy_subapp4(config):
self.assertEqual(config.route_prefix, 'root/nested/nested2')
+
def dummy_subapp(config):
self.assertEqual(config.route_prefix, 'root/nested')
config.include(dummy_subapp2)
@@ -734,9 +747,11 @@ def test_include_with_missing_source_file(self):
from pyramid.exceptions import ConfigurationError
import inspect
config = self._makeOne()
+
class DummyInspect(object):
def getmodule(self, c):
return inspect.getmodule(c)
+
def getsourcefile(self, c):
return None
config.inspect = DummyInspect()
@@ -744,10 +759,10 @@ def getsourcefile(self, c):
config.include('pyramid.tests.test_config.dummy_include')
except ConfigurationError as e:
self.assertEqual(
- e.args[0],
+ e.args[0],
"No source file for module 'pyramid.tests.test_config' (.py "
"file must exist, refusing to use orphan .pyc or .pyo file).")
- else: # pragma: no cover
+ else: # pragma: no cover
raise AssertionError
def test_action_branching_kw_is_None(self):
@@ -756,7 +771,7 @@ def test_action_branching_kw_is_None(self):
def test_action_branching_kw_is_not_None(self):
config = self._makeOne(autocommit=True)
- self.assertEqual(config.action('discrim', kw={'a':1}), None)
+ self.assertEqual(config.action('discrim', kw={'a': 1}), None)
def test_action_autocommit_with_introspectables(self):
from pyramid.config.util import ActionInfo
@@ -773,14 +788,14 @@ def test_action_autocommit_with_introspectables_introspection_off(self):
intr = DummyIntrospectable()
config.action('discrim', introspectables=(intr,))
self.assertEqual(len(intr.registered), 0)
-
+
def test_action_branching_nonautocommit_with_config_info(self):
config = self._makeOne(autocommit=False)
config.info = 'abc'
state = DummyActionState()
state.autocommit = False
config.action_state = state
- config.action('discrim', kw={'a':1})
+ config.action('discrim', kw={'a': 1})
self.assertEqual(
state.actions,
[((),
@@ -800,7 +815,7 @@ def test_action_branching_nonautocommit_without_config_info(self):
state = DummyActionState()
config.action_state = state
state.autocommit = False
- config.action('discrim', kw={'a':1})
+ config.action('discrim', kw={'a': 1})
self.assertEqual(
state.actions,
[((),
@@ -837,7 +852,7 @@ def test_action_nonautocommit_with_introspectables_introspection_off(self):
config.action('discrim', introspectables=(intr,))
self.assertEqual(
state.actions[0][1]['introspectables'], ())
-
+
def test_scan_integration(self):
from zope.interface import alsoProvides
from pyramid.interfaces import IRequest
@@ -859,13 +874,13 @@ def test_scan_integration(self):
result = render_view_to_response(ctx, req, '')
self.assertEqual(result, 'grokked_post')
- result= render_view_to_response(ctx, req, 'grokked_class')
+ result = render_view_to_response(ctx, req, 'grokked_class')
self.assertEqual(result, 'grokked_class')
- result= render_view_to_response(ctx, req, 'grokked_instance')
+ result = render_view_to_response(ctx, req, 'grokked_instance')
self.assertEqual(result, 'grokked_instance')
- result= render_view_to_response(ctx, req, 'oldstyle_grokked_class')
+ result = render_view_to_response(ctx, req, 'oldstyle_grokked_class')
self.assertEqual(result, 'oldstyle_grokked_class')
req.method = 'GET'
@@ -876,13 +891,13 @@ def test_scan_integration(self):
result = render_view_to_response(ctx, req, 'another')
self.assertEqual(result, 'another_grokked_post')
- result= render_view_to_response(ctx, req, 'another_grokked_class')
+ result = render_view_to_response(ctx, req, 'another_grokked_class')
self.assertEqual(result, 'another_grokked_class')
- result= render_view_to_response(ctx, req, 'another_grokked_instance')
+ result = render_view_to_response(ctx, req, 'another_grokked_instance')
self.assertEqual(result, 'another_grokked_instance')
- result= render_view_to_response(ctx, req,
+ result = render_view_to_response(ctx, req,
'another_oldstyle_grokked_class')
self.assertEqual(result, 'another_oldstyle_grokked_class')
@@ -947,7 +962,7 @@ def test_scan_integration_with_ignore(self):
from pyramid.view import render_view_to_response
import pyramid.tests.test_config.pkgs.scannable as package
config = self._makeOne(autocommit=True)
- config.scan(package,
+ config.scan(package,
ignore='pyramid.tests.test_config.pkgs.scannable.another')
ctx = DummyContext()
@@ -962,7 +977,7 @@ def test_scan_integration_with_ignore(self):
# ignored
v = render_view_to_response(ctx, req, 'another_stacked_class2')
self.assertEqual(v, None)
-
+
def test_scan_integration_dottedname_package(self):
from zope.interface import alsoProvides
from pyramid.interfaces import IRequest
@@ -993,8 +1008,10 @@ def test_scan_integration_with_onerror(self):
path = os.path.join(here, 'path')
sys.path.append(path)
config = self._makeOne(autocommit=True)
+
class FooException(Exception):
pass
+
def onerror(name):
raise FooException
self.assertRaises(FooException, config.scan, 'scanerror',
@@ -1025,6 +1042,7 @@ def scanconflicts(e):
@skip_on('py3')
def test_hook_zca(self):
from zope.component import getSiteManager
+
def foo():
'123'
try:
@@ -1039,6 +1057,7 @@ def foo():
@skip_on('py3')
def test_unhook_zca(self):
from zope.component import getSiteManager
+
def foo():
'123'
try:
@@ -1052,16 +1071,25 @@ def foo():
def test_commit_conflict_simple(self):
config = self._makeOne()
- def view1(request): pass
- def view2(request): pass
+
+ def view1(request):
+ pass
+
+ def view2(request):
+ pass
config.add_view(view1)
config.add_view(view2)
self.assertRaises(ConfigurationConflictError, config.commit)
def test_commit_conflict_resolved_with_include(self):
config = self._makeOne()
- def view1(request): pass
- def view2(request): pass
+
+ def view1(request):
+ pass
+
+ def view2(request):
+ pass
+
def includeme(config):
config.add_view(view2)
config.add_view(view1)
@@ -1072,10 +1100,16 @@ def includeme(config):
def test_commit_conflict_with_two_includes(self):
config = self._makeOne()
- def view1(request): pass
- def view2(request): pass
+
+ def view1(request):
+ pass
+
+ def view2(request):
+ pass
+
def includeme1(config):
config.add_view(view1)
+
def includeme2(config):
config.add_view(view2)
config.include(includeme1)
@@ -1086,16 +1120,24 @@ def includeme2(config):
c1, c2 = _conflictFunctions(why)
self.assertEqual(c1, 'includeme1')
self.assertEqual(c2, 'includeme2')
- else: #pragma: no cover
+ else: # pragma: no cover
raise AssertionError
def test_commit_conflict_resolved_with_two_includes_and_local(self):
config = self._makeOne()
- def view1(request): pass
- def view2(request): pass
- def view3(request): pass
+
+ def view1(request):
+ pass
+
+ def view2(request):
+ pass
+
+ def view3(request):
+ pass
+
def includeme1(config):
config.add_view(view1)
+
def includeme2(config):
config.add_view(view2)
config.include(includeme1)
@@ -1108,9 +1150,15 @@ def includeme2(config):
def test_autocommit_no_conflicts(self):
from pyramid.renderers import null_renderer
config = self._makeOne(autocommit=True)
- def view1(request): pass
- def view2(request): pass
- def view3(request): pass
+
+ def view1(request):
+ pass
+
+ def view2(request):
+ pass
+
+ def view3(request):
+ pass
config.add_view(view1, renderer=null_renderer)
config.add_view(view2, renderer=null_renderer)
config.add_view(view3, renderer=null_renderer)
@@ -1120,8 +1168,12 @@ def view3(request): pass
def test_conflict_set_notfound_view(self):
config = self._makeOne()
- def view1(request): pass
- def view2(request): pass
+
+ def view1(request):
+ pass
+
+ def view2(request):
+ pass
config.set_notfound_view(view1)
config.set_notfound_view(view2)
try:
@@ -1130,13 +1182,17 @@ def view2(request): pass
c1, c2 = _conflictFunctions(why)
self.assertEqual(c1, 'test_conflict_set_notfound_view')
self.assertEqual(c2, 'test_conflict_set_notfound_view')
- else: # pragma: no cover
+ else: # pragma: no cover
raise AssertionError
def test_conflict_set_forbidden_view(self):
config = self._makeOne()
- def view1(request): pass
- def view2(request): pass
+
+ def view1(request):
+ pass
+
+ def view2(request):
+ pass
config.set_forbidden_view(view1)
config.set_forbidden_view(view2)
try:
@@ -1145,7 +1201,7 @@ def view2(request): pass
c1, c2 = _conflictFunctions(why)
self.assertEqual(c1, 'test_conflict_set_forbidden_view')
self.assertEqual(c2, 'test_conflict_set_forbidden_view')
- else: # pragma: no cover
+ else: # pragma: no cover
raise AssertionError
def test___getattr__missing_when_directives_exist(self):
@@ -1160,20 +1216,25 @@ def test___getattr__missing_when_directives_dont_exist(self):
def test___getattr__matches(self):
config = self._makeOne()
- def foo(config): pass
- directives = {'foo':(foo, True)}
+
+ def foo(config):
+ pass
+ directives = {'foo': (foo, True)}
config.registry._directives = directives
foo_meth = config.foo
self.assertTrue(getattr(foo_meth, im_func).__docobj__ is foo)
def test___getattr__matches_no_action_wrap(self):
config = self._makeOne()
- def foo(config): pass
- directives = {'foo':(foo, False)}
+
+ def foo(config):
+ pass
+ directives = {'foo': (foo, False)}
config.registry._directives = directives
foo_meth = config.foo
self.assertTrue(getattr(foo_meth, im_func) is foo)
+
class TestConfiguratorDeprecatedFeatures(unittest.TestCase):
def setUp(self):
@@ -1189,7 +1250,7 @@ def _makeOne(self, *arg, **kw):
config = Configurator(*arg, **kw)
config.registry._dont_resolve_responses = True
return config
-
+
def _getRouteRequestIface(self, config, name):
from pyramid.interfaces import IRouteRequest
iface = config.registry.getUtility(IRouteRequest, name)
@@ -1215,10 +1276,12 @@ def _registerRenderer(self, config, name='.txt'):
from pyramid.interfaces import IRendererFactory
from pyramid.interfaces import ITemplateRenderer
from zope.interface import implementer
+
@implementer(ITemplateRenderer)
class Renderer:
def __init__(self, info):
self.__class__.info = info
+
def __call__(self, *arg):
return 'Hello!'
config.registry.registerUtility(Renderer, IRendererFactory, name=name)
@@ -1324,9 +1387,11 @@ def test_add_route_with_view_attr(self):
from pyramid.renderers import null_renderer
config = self._makeOne(autocommit=True)
self._registerRenderer(config)
+
class View(object):
def __init__(self, context, request):
pass
+
def alt(self):
return 'OK'
config.add_route('name', 'path', view=View, view_attr='alt',
@@ -1367,8 +1432,12 @@ def test_add_route_with_view_permission_alias(self):
def test_conflict_route_with_view(self):
config = self._makeOne()
- def view1(request): pass
- def view2(request): pass
+
+ def view1(request):
+ pass
+
+ def view2(request):
+ pass
config.add_route('a', '/a', view=view1)
config.add_route('a', '/a', view=view2)
try:
@@ -1381,9 +1450,10 @@ def view2(request): pass
self.assertEqual(c4, 'test_conflict_route_with_view')
self.assertEqual(c5, 'test_conflict_route_with_view')
self.assertEqual(c6, 'test_conflict_route_with_view')
- else: # pragma: no cover
+ else: # pragma: no cover
raise AssertionError
+
class TestConfigurator_add_directive(unittest.TestCase):
def setUp(self):
@@ -1451,11 +1521,12 @@ def test_directive_persists_across_configurator_creations(self):
self.assertEqual(action['callable'], None)
self.assertEqual(action['args'], config2.package)
+
class TestActionState(unittest.TestCase):
def _makeOne(self):
from pyramid.config import ActionState
return ActionState()
-
+
def test_it(self):
c = self._makeOne()
self.assertEqual(c.actions, [])
@@ -1464,7 +1535,7 @@ def test_action_simple(self):
from pyramid.tests.test_config import dummyfactory as f
c = self._makeOne()
c.actions = []
- c.action(1, f, (1,), {'x':1})
+ c.action(1, f, (1,), {'x': 1})
self.assertEqual(
c.actions,
[{'args': (1,),
@@ -1494,7 +1565,7 @@ def test_action_simple(self):
'info': None,
'introspectables': (),
'kw': {},
- 'order': 0},])
+ 'order': 0}, ])
def test_action_with_includepath(self):
c = self._makeOne()
@@ -1577,6 +1648,7 @@ def test_processSpec(self):
def test_execute_actions_tuples(self):
output = []
+
def f(*a, **k):
output.append((a, k))
c = self._makeOne()
@@ -1591,36 +1663,38 @@ def f(*a, **k):
def test_execute_actions_dicts(self):
output = []
+
def f(*a, **k):
output.append((a, k))
c = self._makeOne()
c.actions = [
- {'discriminator':1, 'callable':f, 'args':(1,), 'kw':{},
- 'order':0, 'includepath':(), 'info':None,
- 'introspectables':()},
- {'discriminator':1, 'callable':f, 'args':(11,), 'kw':{},
- 'includepath':('x',), 'order': 0, 'info':None,
- 'introspectables':()},
- {'discriminator':2, 'callable':f, 'args':(2,), 'kw':{},
- 'order':0, 'includepath':(), 'info':None,
- 'introspectables':()},
- {'discriminator':None, 'callable':None, 'args':(), 'kw':{},
- 'order':0, 'includepath':(), 'info':None,
- 'introspectables':()},
+ {'discriminator': 1, 'callable': f, 'args': (1,), 'kw': {},
+ 'order': 0, 'includepath': (), 'info': None,
+ 'introspectables': ()},
+ {'discriminator': 1, 'callable': f, 'args': (11,), 'kw': {},
+ 'includepath': ('x',), 'order': 0, 'info': None,
+ 'introspectables': ()},
+ {'discriminator': 2, 'callable': f, 'args': (2,), 'kw': {},
+ 'order': 0, 'includepath': (), 'info': None,
+ 'introspectables': ()},
+ {'discriminator': None, 'callable': None, 'args': (), 'kw': {},
+ 'order': 0, 'includepath': (), 'info': None,
+ 'introspectables': ()},
]
c.execute_actions()
self.assertEqual(output, [((1,), {}), ((2,), {})])
def test_execute_actions_with_introspectables(self):
output = []
+
def f(*a, **k):
output.append((a, k))
c = self._makeOne()
intr = DummyIntrospectable()
c.actions = [
- {'discriminator':1, 'callable':f, 'args':(1,), 'kw':{},
- 'order':0, 'includepath':(), 'info':None,
- 'introspectables':(intr,)},
+ {'discriminator': 1, 'callable': f, 'args': (1,), 'kw': {},
+ 'order': 0, 'includepath': (), 'info': None,
+ 'introspectables': (intr,)},
]
introspector = object()
c.execute_actions(introspector=introspector)
@@ -1631,9 +1705,9 @@ def test_execute_actions_with_introspectable_no_callable(self):
c = self._makeOne()
intr = DummyIntrospectable()
c.actions = [
- {'discriminator':1, 'callable':None, 'args':(1,), 'kw':{},
- 'order':0, 'includepath':(), 'info':None,
- 'introspectables':(intr,)},
+ {'discriminator': 1, 'callable': None, 'args': (1,), 'kw': {},
+ 'order': 0, 'includepath': (), 'info': None,
+ 'introspectables': (intr,)},
]
introspector = object()
c.execute_actions(introspector=introspector)
@@ -1641,8 +1715,10 @@ def test_execute_actions_with_introspectable_no_callable(self):
def test_execute_actions_error(self):
output = []
+
def f(*a, **k):
output.append(('f', a, k))
+
def bad():
raise NotImplementedError
c = self._makeOne()
@@ -1655,6 +1731,7 @@ def bad():
self.assertRaises(ConfigurationExecutionError, c.execute_actions)
self.assertEqual(output, [('f', (1,), {}), ('f', (2,), {})])
+
class Test_resolveConflicts(unittest.TestCase):
def _callFUT(self, actions):
from pyramid.config import resolveConflicts
@@ -1868,7 +1945,7 @@ def test_it_with_actions_grouped_by_order(self):
'order': 99999}
]
)
-
+
class TestGlobalRegistriesIntegration(unittest.TestCase):
def setUp(self):
@@ -1899,9 +1976,11 @@ def test_global_registries(self):
global_registries.remove(config2.registry)
self.assertEqual(global_registries.last, config1.registry)
+
class DummyRequest:
subpath = ()
matchdict = None
+
def __init__(self, environ=None):
if environ is None:
environ = {}
@@ -1909,66 +1988,86 @@ def __init__(self, environ=None):
self.params = {}
self.cookies = {}
+
class DummyResponse:
status = '200 OK'
headerlist = ()
app_iter = ()
body = ''
+
class DummyThreadLocalManager(object):
pushed = None
popped = False
+
def push(self, d):
self.pushed = d
+
def pop(self):
self.popped = True
from zope.interface import implementer
+
+
@implementer(IDummy)
class DummyEvent:
pass
+
class DummyRegistry(object):
def __init__(self, adaptation=None):
self.utilities = []
self.adapters = []
self.adaptation = adaptation
+
def subscribers(self, events, name):
self.events = events
return events
+
def registerUtility(self, *arg, **kw):
self.utilities.append((arg, kw))
+
def registerAdapter(self, *arg, **kw):
self.adapters.append((arg, kw))
+
def queryAdapter(self, *arg, **kw):
return self.adaptation
from pyramid.interfaces import IResponse
+
+
@implementer(IResponse)
class DummyResponse(object):
pass
-
+
from zope.interface import Interface
+
+
class IOther(Interface):
pass
+
def _conflictFunctions(e):
conflicts = e._conflicts.values()
for conflict in conflicts:
for confinst in conflict:
yield confinst.function
+
class DummyActionState(object):
autocommit = False
info = ''
+
def __init__(self):
self.actions = []
+
def action(self, *arg, **kw):
self.actions.append((arg, kw))
+
class DummyIntrospectable(object):
def __init__(self):
self.registered = []
+
def register(self, introspector, action_info):
self.registered.append((introspector, action_info))
-
View
453 pyramid/tests/test_config/test_views.py
@@ -13,6 +13,7 @@
from pyramid.exceptions import ConfigurationExecutionError
from pyramid.exceptions import ConfigurationConflictError
+
class TestViewsConfigurationMixin(unittest.TestCase):
def _makeOne(self, *arg, **kw):
from pyramid.config import Configurator
@@ -42,10 +43,12 @@ def _registerRenderer(self, config, name='.txt'):
from pyramid.interfaces import IRendererFactory
from pyramid.interfaces import ITemplateRenderer
from zope.interface import implementer
+
@implementer(ITemplateRenderer)
class Renderer:
def __init__(self, info):
self.__class__.info = info
+
def __call__(self, *arg):
return 'Hello!'
config.registry.registerUtility(Renderer, IRendererFactory, name=name)
@@ -110,7 +113,8 @@ def test_add_view_view_callable_None_with_renderer(self):
self.assertTrue(b'Hello!' in view(None, None).body)
def test_add_view_wrapped_view_is_decorated(self):
- def view(request): # request-only wrapper
+
+ def view(request): # request-only wrapper
""" """
config = self._makeOne(autocommit=True)
config.add_view(view=view)
@@ -139,6 +143,7 @@ def test_add_view_with_function_callable(self):
def test_add_view_with_function_callable_requestonly(self):
from pyramid.renderers import null_renderer
+
def view(request):
return 'OK'
config = self._makeOne(autocommit=True)
@@ -168,9 +173,11 @@ def test_add_view_with_name_unicode(self):
def test_add_view_with_decorator(self):
from pyramid.renderers import null_renderer
+
def view(request):
""" ABC """
return 'OK'
+
def view_wrapper(fn):
def inner(context, request):
return fn(context, request)
@@ -188,11 +195,12 @@ def test_add_view_with_http_cache(self):
import datetime
from pyramid.response import Response
response = Response('OK')
+
def view(request):
""" ABC """
return response
config = self._makeOne(autocommit=True)
- config.add_view(view=view, http_cache=(86400, {'public':True}))
+ config.add_view(view=view, http_cache=(86400, {'public': True}))
wrapper = self._getViewCallable(config)
self.assertFalse(wrapper is view)
self.assertEqual(wrapper.__doc__, view.__doc__)
@@ -207,6 +215,7 @@ def view(request):
def test_add_view_as_instance(self):
from pyramid.renderers import null_renderer
+
class AView:
def __call__(self, context, request):
""" """
@@ -220,11 +229,12 @@ def __call__(self, context, request):
def test_add_view_as_instancemethod(self):
from pyramid.renderers import null_renderer
+
class View:
def index(self, context, request):
return 'OK'
view = View()
- config=self._makeOne(autocommit=True)
+ config = self._makeOne(autocommit=True)
config.add_view(view=view.index, renderer=null_renderer)
wrapper = self._getViewCallable(config)
result = wrapper(None, None)
@@ -232,6 +242,7 @@ def index(self, context, request):
def test_add_view_as_instance_requestonly(self):
from pyramid.renderers import null_renderer
+
class AView:
def __call__(self, request):
""" """
@@ -245,6 +256,7 @@ def __call__(self, request):
def test_add_view_as_oldstyle_class(self):
from pyramid.renderers import null_renderer
+
class view:
def __init__(self, context, request):
self.context = context
@@ -262,6 +274,7 @@ def __call__(self):
def test_add_view_as_oldstyle_class_requestonly(self):
from pyramid.renderers import null_renderer
+
class view:
def __init__(self, request):
self.request = request
@@ -281,6 +294,7 @@ def test_add_view_context_as_class(self):
from pyramid.renderers import null_renderer
from zope.interface import implementedBy
view = lambda *arg: 'OK'
+
class Foo:
pass
config = self._makeOne(autocommit=True)
@@ -320,6 +334,7 @@ def test_add_view_for_as_class(self):
from pyramid.renderers import null_renderer
from zope.interface import implementedBy
view = lambda *arg: 'OK'
+
class Foo:
pass
config = self._makeOne(autocommit=True)
@@ -342,6 +357,7 @@ def test_add_view_context_trumps_for(self):
from pyramid.renderers import null_renderer
view = lambda *arg: 'OK'
config = self._makeOne(autocommit=True)
+
class Foo:
pass
config.add_view(context=IDummy, for_=Foo, view=view,
@@ -394,6 +410,7 @@ def test_add_view_same_phash_overrides_existing_single_view(self):
config = self._makeOne(autocommit=True)
config.registry.registerAdapter(
view, (IViewClassifier, IRequest, Interface), IView, name='')
+
def newview(context, request):
return 'OK'
config.add_view(view=newview, xhr=True, renderer=null_renderer)
@@ -420,6 +437,7 @@ def test_add_view_exc_same_phash_overrides_existing_single_view(self):
view,
(IExceptionViewClassifier, IRequest, implementedBy(RuntimeError)),
IView, name='')
+
def newview(context, request):
return 'OK'
config.add_view(view=newview, xhr=True, context=RuntimeError,
@@ -442,6 +460,7 @@ def test_add_view_default_phash_overrides_no_phash(self):
config = self._makeOne(autocommit=True)
config.registry.registerAdapter(
view, (IViewClassifier, IRequest, Interface), IView, name='')
+
def newview(context, request):
return 'OK'
config.add_view(view=newview, renderer=null_renderer)
@@ -464,6 +483,7 @@ def test_add_view_exc_default_phash_overrides_no_phash(self):
view,
(IExceptionViewClassifier, IRequest, implementedBy(RuntimeError)),
IView, name='')
+
def newview(context, request):
return 'OK'
config.add_view(view=newview, context=RuntimeError,
@@ -488,6 +508,7 @@ def test_add_view_default_phash_overrides_default_phash(self):
config = self._makeOne(autocommit=True)
config.registry.registerAdapter(
view, (IViewClassifier, IRequest, Interface), IView, name='')
+
def newview(context, request):
return 'OK'
config.add_view(view=newview, renderer=null_renderer)
@@ -512,6 +533,7 @@ def test_add_view_exc_default_phash_overrides_default_phash(self):
view,
(IExceptionViewClassifier, IRequest, implementedBy(RuntimeError)),
IView, name='')
+
def newview(context, request):
return 'OK'
config.add_view(view=newview, context=RuntimeError,
@@ -616,8 +638,10 @@ def test_add_view_with_accept_multiview_replaces_existing_view(self):
from pyramid.interfaces import IView
from pyramid.interfaces import IMultiView
from pyramid.interfaces import IViewClassifier
+
def view(context, request):
return 'OK'
+
def view2(context, request):
return 'OK2'
config = self._makeOne(autocommit=True)
@@ -635,9 +659,16 @@ def view2(context, request):
def test_add_views_with_accept_multiview_replaces_existing(self):
from pyramid.renderers import null_renderer
- def view(context, request): return 'OK'
- def view2(context, request): return 'OK2'
- def view3(context, request): return 'OK3'
+
+ def view(context, request):
+ return 'OK'
+
+ def view2(context, request):
+ return 'OK2'
+
+ def view3(context, request):
+ return 'OK3'
+
config = self._makeOne(autocommit=True)
config.add_view(view=view, renderer=null_renderer)
config.add_view(view=view2, accept='text/html', renderer=null_renderer)
@@ -657,8 +688,10 @@ def test_add_view_exc_with_accept_multiview_replaces_existing_view(self):
from pyramid.interfaces import IMultiView
from pyramid.interfaces import IViewClassifier
from pyramid.interfaces import IExceptionViewClassifier
+
def view(context, request):
return 'OK'
+
def view2(context, request):
return 'OK2'
config = self._makeOne(autocommit=True)
@@ -689,8 +722,10 @@ def test_add_view_multiview_replaces_existing_view_with___accept__(self):
from pyramid.interfaces import IView
from pyramid.interfaces import IMultiView
from pyramid.interfaces import IViewClassifier
+
def view(context, request):
return 'OK'
+
def view2(context, request):
return 'OK2'
view.__accept__ = 'text/html'
@@ -716,8 +751,10 @@ def test_add_view_exc_mulview_replaces_existing_view_with___accept__(self):
from pyramid.interfaces import IMultiView
from pyramid.interfaces import IViewClassifier
from pyramid.interfaces import IExceptionViewClassifier
+
def view(context, request):
return 'OK'
+
def view2(context, request):
return 'OK2'
view.__accept__ = 'text/html'
@@ -794,8 +831,10 @@ def test_add_view_multiview_context_superclass_then_subclass(self):
from pyramid.interfaces import IView
from pyramid.interfaces import IMultiView
from pyramid.interfaces import IViewClassifier
+
class ISuper(Interface):
pass
+
class ISub(ISuper):
pass
view = lambda *arg: 'OK'
@@ -819,8 +858,10 @@ def test_add_view_multiview_exception_superclass_then_subclass(self):
from pyramid.interfaces import IMultiView
from pyramid.interfaces import IViewClassifier
from pyramid.interfaces import IExceptionViewClassifier
+
class Super(Exception):
pass
+
class Sub(Super):
pass
view = lambda *arg: 'OK'
@@ -849,18 +890,34 @@ class Sub(Super):
def test_add_view_multiview_call_ordering(self):
from pyramid.renderers import null_renderer as nr
from zope.interface import directlyProvides
- def view1(context, request): return 'view1'
- def view2(context, request): return 'view2'
- def view3(context, request): return 'view3'
- def view4(context, request): return 'view4'
- def view5(context, request): return 'view5'
- def view6(context, request): return 'view6'
- def view7(context, request): return 'view7'
- def view8(context, request): return 'view8'
+
+ def view1(context, request):
+ return 'view1'
+
+ def view2(context, request):
+ return 'view2'
+
+ def view3(context, request):
+ return 'view3'
+
+ def view4(context, request):
+ return 'view4'
+
+ def view5(context, request):
+ return 'view5'
+
+ def view6(context, request):
+ return 'view6'
+
+ def view7(context, request):
+ return 'view7'
+
+ def view8(context, request):
+ return 'view8'
config = self._makeOne(autocommit=True)
config.add_view(view=view1, renderer=nr)
config.add_view(view=view2, request_method='POST', renderer=nr)
- config.add_view(view=view3,request_param='param', renderer=nr)
+ config.add_view(view=view3, request_param='param', renderer=nr)
config.add_view(view=view4, containment=IDummy, renderer=nr)
config.add_view(view=view5, request_method='POST',
request_param='param', renderer=nr)
@@ -868,10 +925,9 @@ def view8(context, request): return 'view8'
renderer=nr)
config.add_view(view=view7, request_param='param', containment=IDummy,
renderer=nr)
- config.add_view(view=view8, request_method='POST',request_param='param',
+ config.add_view(view=view8, request_method='POST', request_param='param',
containment=IDummy, renderer=nr)
-
wrapper = self._getViewCallable(config)
ctx = DummyContext()
@@ -888,7 +944,7 @@ def view8(context, request): return 'view8'
ctx = DummyContext()
request = self._makeRequest(config)
- request.params = {'param':'1'}
+ request.params = {'param': '1'}
request.method = 'GET'
self.assertEqual(wrapper(ctx, request), 'view3')
@@ -902,7 +958,7 @@ def view8(context, request): return 'view8'
ctx = DummyContext()
request = self._makeRequest(config)
request.method = 'POST'
- request.params = {'param':'1'}
+ request.params = {'param': '1'}
self.assertEqual(wrapper(ctx, request), 'view5')
ctx = DummyContext()
@@ -916,32 +972,36 @@ def view8(context, request): return 'view8'
directlyProvides(ctx, IDummy)
request = self._makeRequest(config)
request.method = 'GET'
- request.params = {'param':'1'}
+ request.params = {'param': '1'}
self.assertEqual(wrapper(ctx, request), 'view7')
ctx = DummyContext()
directlyProvides(ctx, IDummy)
request = self._makeRequest(config)
request.method = 'POST'
- request.params = {'param':'1'}
+ request.params = {'param': '1'}
self.assertEqual(wrapper(ctx, request), 'view8')
def test_add_view_multiview___discriminator__(self):
from pyramid.renderers import null_renderer
from zope.interface import Interface
+
class IFoo(Interface):
pass
+
class IBar(Interface):
pass
+
@implementer(IFoo)
class Foo(object):
pass
+
@implementer(IBar)
class Bar(object):
pass
foo = Foo()
bar = Bar()
-
+
from pyramid.interfaces import IRequest
from pyramid.interfaces import IView
from pyramid.interfaces import IViewClassifier
@@ -968,11 +1028,13 @@ def __init__(self, context, request):
self.context = context
def __call__(self):
- return {'a':'1'}
+ return {'a': '1'}
config = self._makeOne(autocommit=True)
+
class moo(object):
def __init__(self, *arg, **kw):
pass
+
def __call__(self, *arg, **kw):
return 'moo'
config.add_renderer(None, moo)
@@ -985,6 +1047,7 @@ def __call__(self, *arg, **kw):
def test_add_view_with_request_type_as_iface(self):
from pyramid.renderers import null_renderer
from zope.interface import directlyProvides
+
def view(context, request):
return 'OK'
config = self._makeOne(autocommit=True)
@@ -1096,7 +1159,7 @@ def test_add_view_with_request_param_noval_true(self):
config.add_view(view=view, request_param='abc', renderer=null_renderer)
wrapper = self._getViewCallable(config)
request = self._makeRequest(config)
- request.params = {'abc':''}
+ request.params = {'abc': ''}
self.assertEqual(wrapper(None, request), 'OK')
def test_add_view_with_request_param_noval_false(self):
@@ -1116,7 +1179,7 @@ def test_add_view_with_request_param_val_true(self):
renderer=null_renderer)
wrapper = self._getViewCallable(config)
request = self._makeRequest(config)
- request.params = {'abc':'123'}
+ request.params = {'abc': '123'}
self.assertEqual(wrapper(None, request), 'OK')
def test_add_view_with_request_param_val_false(self):
@@ -1125,7 +1188,7 @@ def test_add_view_with_request_param_val_false(self):
config.add_view(view=view, request_param='abc=123')
wrapper = self._getViewCallable(config)
request = self._makeRequest(config)
- request.params = {'abc':''}
+ request.params = {'abc': ''}
self._assertNotFound(wrapper, None, request)
def test_add_view_with_xhr_true(self):
@@ -1160,7 +1223,7 @@ def test_add_view_with_header_noval_match(self):
config.add_view(view=view, header='Host', renderer=null_renderer)
wrapper = self._getViewCallable(config)
request = self._makeRequest(config)
- request.headers = {'Host':'whatever'}
+ request.headers = {'Host': 'whatever'}
self.assertEqual(wrapper(None, request), 'OK')
def test_add_view_with_header_noval_nomatch(self):
@@ -1169,7 +1232,7 @@ def test_add_view_with_header_noval_nomatch(self):
config.add_view(view=view, header='Host')
wrapper = self._getViewCallable(config)
request = self._makeRequest(config)
- request.headers = {'NotHost':'whatever'}
+ request.headers = {'NotHost': 'whatever'}
self._assertNotFound(wrapper, None, request)
def test_add_view_with_header_val_match(self):
@@ -1179,7 +1242,7 @@ def test_add_view_with_header_val_match(self):
config.add_view(view=view, header=r'Host:\d', renderer=null_renderer)
wrapper = self._getViewCallable(config)
request = self._makeRequest(config)
- request.headers = {'Host':'1'}
+ request.headers = {'Host': '1'}
self.assertEqual(wrapper(None, request), 'OK')
def test_add_view_with_header_val_nomatch(self):
@@ -1188,7 +1251,7 @@ def test_add_view_with_header_val_nomatch(self):
config.add_view(view=view, header=r'Host:\d')
wrapper = self._getViewCallable(config)
request = self._makeRequest(config)
- request.headers = {'Host':'abc'}
+ request.headers = {'Host': 'abc'}
self._assertNotFound(wrapper, None, request)
def test_add_view_with_header_val_missing(self):
@@ -1198,7 +1261,7 @@ def test_add_view_with_header_val_missing(self):
config.add_view(view=view, header=r'Host:\d')
wrapper = self._getViewCallable(config)
request = self._makeRequest(config)
- request.headers = {'NoHost':'1'}
+ request.headers = {'NoHost': '1'}
self.assertRaises(HTTPNotFound, wrapper, None, request)
def test_add_view_with_accept_match(self):
@@ -1282,8 +1345,10 @@ def test_add_view_with_custom_predicates_match(self):
from pyramid.renderers import null_renderer
view = lambda *arg: 'OK'
config = self._makeOne(autocommit=True)
+
def pred1(context, request):
return True
+
def pred2(context, request):
return True
predicates = (pred1, pred2)
@@ -1296,8 +1361,10 @@ def pred2(context, request):
def test_add_view_with_custom_predicates_nomatch(self):
view = lambda *arg: 'OK'
config = self._makeOne(autocommit=True)
+
def pred1(context, request):
return True
+
def pred2(context, request):
return False
predicates = (pred1, pred2)
@@ -1311,6 +1378,7 @@ def test_add_view_custom_predicate_bests_standard_predicate(self):
view = lambda *arg: 'OK'
view2 = lambda *arg: 'NOT OK'
config = self._makeOne(autocommit=True)
+
def pred1(context, request):
return True
config.add_view(view=view, custom_predicates=(pred1,),
@@ -1349,10 +1417,12 @@ def test_add_view_with_permission(self):
from pyramid.renderers import null_renderer
view1 = lambda *arg: 'OK'
outerself = self
+
class DummyPolicy(object):
def effective_principals(self, r):
outerself.assertEqual(r, request)
return ['abc']
+
def permits(self, context, principals, permission):
outerself.assertEqual(context, None)
outerself.assertEqual(principals, ['abc'])
@@ -1371,10 +1441,12 @@ def test_add_view_with_default_permission_no_explicit_permission(self):
from pyramid.renderers import null_renderer
view1 = lambda *arg: 'OK'
outerself = self
+
class DummyPolicy(object):
def effective_principals(self, r):
outerself.assertEqual(r, request)
return ['abc']
+
def permits(self, context, principals, permission):
outerself.assertEqual(context, None)
outerself.assertEqual(principals, ['abc'])
@@ -1393,7 +1465,9 @@ def permits(self, context, principals, permission):
def test_add_view_with_no_default_permission_no_explicit_permission(self):
from pyramid.renderers import null_renderer
view1 = lambda *arg: 'OK'
- class DummyPolicy(object): pass # wont be called
+
+ class DummyPolicy(object):
+ pass # wont be called
policy = DummyPolicy()
config = self._makeOne(authorization_policy=policy,
authentication_policy=policy,
@@ -1405,13 +1479,17 @@ class DummyPolicy(object): pass # wont be called
def test_add_view_with_mapper(self):
from pyramid.renderers import null_renderer
+
class Mapper(object):
def __init__(self, **kw):
self.__class__.kw = kw
+
def __call__(self, view):
return view
config = self._makeOne(autocommit=True)
- def view(context, request): return 'OK'
+
+ def view(context, request):
+ return 'OK'
config.add_view(view=view, mapper=Mapper, renderer=null_renderer)
view = self._getViewCallable(config)
self.assertEqual(view(None, None), 'OK')
@@ -1421,12 +1499,15 @@ def test_add_view_with_view_defaults(self):
from pyramid.renderers import null_renderer
from pyramid.exceptions import PredicateMismatch
from zope.interface import directlyProvides
+
class view(object):
__view_defaults__ = {
- 'containment':'pyramid.tests.test_config.IDummy'
+ 'containment': 'pyramid.tests.test_config.IDummy'
}
+
def __init__(self, request):
pass
+
def __call__(self):
return 'OK'
config = self._makeOne(autocommit=True)
@@ -1475,16 +1556,18 @@ def test_add_view_with_view_defaults_viewname_is_dottedname_nonkwarg(self):
context = DummyContext()
request = self._makeRequest(config)
self.assertRaises(PredicateMismatch, wrapper, context, request)
-
+
def test_add_view_with_view_config_and_view_defaults_doesnt_conflict(self):
from pyramid.renderers import null_renderer
+
class view(object):
__view_defaults__ = {
- 'containment':'pyramid.tests.test_config.IDummy'
+ 'containment': 'pyramid.tests.test_config.IDummy'
}
+
class view2(object):
__view_defaults__ = {
- 'containment':'pyramid.tests.test_config.IFactory'
+ 'containment': 'pyramid.tests.test_config.IFactory'
}
config = self._makeOne(autocommit=False)
config.add_view(
@@ -1493,17 +1576,19 @@ class view2(object):
config.add_view(
view=view2,
renderer=null_renderer)
- config.commit() # does not raise
+ config.commit() # does not raise
def test_add_view_with_view_config_and_view_defaults_conflicts(self):
from pyramid.renderers import null_renderer
+
class view(object):
__view_defaults__ = {
- 'containment':'pyramid.tests.test_config.IDummy'
+ 'containment': 'pyramid.tests.test_config.IDummy'
}
+
class view2(object):
__view_defaults__ = {
- 'containment':'pyramid.tests.test_config.IDummy'
+ 'containment': 'pyramid.tests.test_config.IDummy'
}
config = self._makeOne(autocommit=False)
config.add_view(
@@ -1516,6 +1601,7 @@ class view2(object):
def test_derive_view_function(self):
from pyramid.renderers import null_renderer
+
def view(request):
return 'OK'
config = self._makeOne()
@@ -1534,13 +1620,16 @@ def test_derive_view_dottedname(self):
def test_derive_view_with_default_renderer_no_explicit_renderer(self):
config = self._makeOne()
+
class moo(object):
def __init__(self, view):
pass
+
def __call__(self, *arg, **kw):
return 'moo'
config.add_renderer(None, moo)
config.commit()
+
def view(request):
return 'OK'
result = config.derive_view(view)
@@ -1548,12 +1637,17 @@ def view(request):
self.assertEqual(result(None, None).body, b'moo')
def test_derive_view_with_default_renderer_with_explicit_renderer(self):
- class moo(object): pass
+
+ class moo(object):
+ pass
+
class foo(object):
def __init__(self, view):
pass
+
def __call__(self, *arg, **kw):
return 'foo'
+
def view(request):
return 'OK'
config = self._makeOne()
@@ -1615,7 +1709,7 @@ def test_add_static_view_absolute(self):
config.add_static_view('static', static_path)
self.assertEqual(info.added,
[(config, 'static', static_path, {})])
-
+
def test_add_forbidden_view(self):
from pyramid.renderers import null_renderer
from zope.interface import implementedBy
@@ -1654,8 +1748,10 @@ def test_add_notfound_view_append_slash(self):
from pyramid.httpexceptions import HTTPNotFound
config = self._makeOne(autocommit=True)
config.add_route('foo', '/foo/')
- def view(request): return Response('OK')
- config.add_notfound_view(view, renderer=null_renderer,append_slash=True)
+
+ def view(request):
+ return Response('OK')
+ config.add_notfound_view(view, renderer=null_renderer, append_slash=True)
request = self._makeRequest(config)
request.environ['PATH_INFO'] = '/foo'
request.query_string = 'a=1&b=2'
@@ -1902,10 +1998,12 @@ def __call__(self, request, foo=1, bar=2):
self.assertTrue(self._callFUT(foo), True)
def test_instance_nocall(self):
- class Foo: pass
+
+ class Foo:
+ pass
foo = Foo()
self.assertFalse(self._callFUT(foo))
-
+
def test_method_onearg_named_request(self):
class Foo:
def method(self, request):
@@ -1913,6 +2011,7 @@ def method(self, request):
foo = Foo()
self.assertTrue(self._callFUT(foo.method))
+
class Test_isexception(unittest.TestCase):
def _callFUT(self, ob):
from pyramid.config.views import isexception
@@ -1935,10 +2034,12 @@ def test_is_IException(self):
def test_is_IException_subinterface(self):
from pyramid.interfaces import IException
+
class ISubException(IException):
pass
self.assertEqual(self._callFUT(ISubException), True)
+
class TestMultiView(unittest.TestCase):
def _getTargetClass(self):
from pyramid.config.views import MultiView
@@ -2002,8 +2103,12 @@ def test_multiple_with_functions_as_views(self):
# this failed on py3 at one point, because functions aren't orderable
# and we were sorting the views via a plain sort() rather than
# sort(key=itemgetter(0)).
- def view1(request): pass
- def view2(request): pass
+
+ def view1(request):
+ pass
+
+ def view2(request):
+ pass
mv = self._makeOne()
mv.add(view1, 100, None)
self.assertEqual(mv.views, [(100, view1, None)])
@@ -2052,6 +2157,7 @@ def test_match_not_found(self):
def test_match_predicate_fails(self):
from pyramid.httpexceptions import HTTPNotFound
mv = self._makeOne()
+
def view(context, request):
""" """
view.__predicated__ = lambda *arg: False
@@ -2062,6 +2168,7 @@ def view(context, request):
def test_match_predicate_succeeds(self):
mv = self._makeOne()
+
def view(context, request):
""" """
view.__predicated__ = lambda *arg: True
@@ -2080,6 +2187,7 @@ def test_permitted_no_views(self):
def test_permitted_no_match_with__permitted__(self):
mv = self._makeOne()
+
def view(context, request):
""" """
mv.views = [(100, view, None)]
@@ -2087,6 +2195,7 @@ def view(context, request):
def test_permitted(self):
mv = self._makeOne()
+
def view(context, request):
""" """
def permitted(context, request):
@@ -2112,8 +2221,10 @@ def test___call__intermediate_not_found(self):
request = DummyRequest()
request.view_name = ''
expected_response = DummyResponse()
+
def view1(context, request):
raise PredicateMismatch
+
def view2(context, request):
return expected_response
mv.views = [(100, view1, None), (99, view2, None)]
@@ -2126,8 +2237,10 @@ def test___call__raise_not_found_isnt_interpreted_as_pred_mismatch(self):
context = DummyContext()
request = DummyRequest()
request.view_name = ''
+
def view1(context, request):
raise HTTPNotFound
+
def view2(context, request):
""" """
mv.views = [(100, view1, None), (99, view2, None)]
@@ -2139,6 +2252,7 @@ def test___call__(self):
request = DummyRequest()
request.view_name = ''
expected_response = DummyResponse()
+
def view(context, request):
return expected_response
mv.views = [(100, view, None)]
@@ -2158,8 +2272,10 @@ def test___call_permissive_has_call_permissive(self):
request = DummyRequest()
request.view_name = ''
expected_response = DummyResponse()
+
def view(context, request):
""" """
+
def permissive(context, request):
return expected_response
view.__call_permissive__ = permissive
@@ -2173,6 +2289,7 @@ def test___call_permissive_has_no_call_permissive(self):
request = DummyRequest()
request.view_name = ''
expected_response = DummyResponse()
+
def view(context, request):
return expected_response
mv.views = [(100, view, None)]
@@ -2185,6 +2302,7 @@ def test__call__with_accept_match(self):
request = DummyRequest()
request.accept = DummyAccept('text/html', 'text/xml')
expected_response = DummyResponse()
+
def view(context, request):
return expected_response
mv.views = [(100, None)]
@@ -2199,6 +2317,7 @@ def test__call__with_accept_miss(self):
request = DummyRequest()
request.accept = DummyAccept('text/plain', 'text/html')
expected_response = DummyResponse()
+
def view(context, request):
return expected_response
mv.views = [(100, view, None)]
@@ -2207,18 +2326,19 @@ def view(context, request):
response = mv(context, request)
self.assertEqual(response, expected_response)
+
class TestViewDeriver(unittest.TestCase):
def setUp(self):
self.config = testing.setUp()
def tearDown(self):
self.config = None
-
+
def _makeOne(self, **kw):
kw['registry'] = self.config.registry
from pyramid.config.views import ViewDeriver
return ViewDeriver(**kw)
-
+
def _makeRequest(self):
request = DummyRequest()
request.registry = self.config.registry
@@ -2247,18 +2367,18 @@ def view(request):
result(None, None)
except ValueError as e:
self.assertEqual(
- e.args[0],
+ e.args[0],
'Could not convert return value of the view callable function '
'pyramid.tests.test_config.test_views.view into a response '
'object. The value returned was None. You may have forgotten '
'to return a value from the view callable.'
)
- else: # pragma: no cover
+ else: # pragma: no cover
raise AssertionError
def test_function_returns_non_adaptable_dict(self):
def view(request):
- return {'a':1}
+ return {'a': 1}
deriver = self._makeOne()
result = deriver(view)
self.assertFalse(result is view)
@@ -2266,15 +2386,15 @@ def view(request):
result(None, None)
except ValueError as e:
self.assertEqual(
- e.args[0],
+ e.args[0],
"Could not convert return value of the view callable function "
"pyramid.tests.test_config.test_views.view into a response "
"object. The value returned was {'a': 1}. You may have "
"forgotten to define a renderer in the view configuration."
)
- else: # pragma: no cover
+ else: # pragma: no cover
raise AssertionError
-
+
def test_instance_returns_non_adaptable(self):
class AView(object):
def __call__(self, request):
@@ -2293,14 +2413,16 @@ def __call__(self, request):
self.assertTrue(msg.endswith(
'> into a response object. The value returned was None. You '
'may have forgotten to return a value from the view callable.'))
- else: # pragma: no cover
+ else: # pragma: no cover
raise AssertionError
def test_requestonly_default_method_returns_non_adaptable(self):
request = DummyRequest()
+
class AView(object):
def __init__(self, request):
pass
+
def __call__(self):
return None
deriver = self._makeOne()
@@ -2310,21 +2432,23 @@ def __call__(self):
result(None, request)
except ValueError as e:
self.assertEqual(
- e.args[0],
+ e.args[0],
'Could not convert return value of the view callable '
'method __call__ of '
'class pyramid.tests.test_config.test_views.AView into a '
'response object. The value returned was None. You may have '
'forgotten to return a value from the view callable.'
)
- else: # pragma: no cover
+ else: # pragma: no cover
raise AssertionError
def test_requestonly_nondefault_method_returns_non_adaptable(self):
request = DummyRequest()
+
class AView(object):
def __init__(self, request):
pass
+
def theviewmethod(self):
return None
deriver = self._makeOne(attr='theviewmethod')
@@ -2334,18 +2458,19 @@ def theviewmethod(self):
result(None, request)
except ValueError as e:
self.assertEqual(
- e.args[0],
+ e.args[0],
'Could not convert return value of the view callable '
'method theviewmethod of '
'class pyramid.tests.test_config.test_views.AView into a '
'response object. The value returned was None. You may have '
'forgotten to return a value from the view callable.'
)
- else: # pragma: no cover
+ else: # pragma: no cover
raise AssertionError
-
+
def test_requestonly_function(self):
response = DummyResponse()
+
def view(request):
return response
deriver = self._makeOne()
@@ -2355,6 +2480,7 @@ def view(request):
def test_requestonly_function_with_renderer(self):
response = DummyResponse()
+
class moo(object):
def render_view(inself, req, resp, view_inst, ctx):
self.assertEqual(req, request)
@@ -2362,6 +2488,7 @@ def render_view(inself, req, resp, view_inst, ctx):
self.assertEqual(view_inst, view)
self.assertEqual(ctx, context)
return response
+
def view(request):
return 'OK'
deriver = self._makeOne(renderer=moo())
@@ -2379,6 +2506,7 @@ def inner(value, system):
self.assertEqual(system['context'], context)
return 'moo'
return inner
+
def view(request):
return 'OK'
self.config.add_renderer('moo', moo)
@@ -2392,6 +2520,7 @@ def view(request):
def test_requestonly_function_with_renderer_request_has_view(self):
response = DummyResponse()
+
class moo(object):
def render_view(inself, req, resp, view_inst, ctx):
self.assertEqual(req, request)
@@ -2399,6 +2528,7 @@ def render_view(inself, req, resp, view_inst, ctx):
self.assertEqual(view_inst, 'view')
self.assertEqual(ctx, context)
return response
+
def view(request):
return 'OK'
deriver = self._makeOne(renderer=moo())
@@ -2413,9 +2543,11 @@ def view(request):
def test_class_without_attr(self):
response = DummyResponse()
+
class View(object):
def __init__(self, request):
pass
+
def __call__(self):
return response
deriver = self._makeOne()
@@ -2426,9 +2558,11 @@ def __call__(self):
def test_class_with_attr(self):
response = DummyResponse()
+
class View(object):
def __init__(self, request):
pass
+
def another(self):
return response
deriver = self._makeOne(attr='another')
@@ -2448,6 +2582,7 @@ def view(context, request):
def test_as_function_requestonly(self):
response = DummyResponse()
+
def view(request):
return response
deriver = self._makeOne()
@@ -2461,9 +2596,11 @@ def view(request):
def test_as_newstyle_class_context_and_request(self):
response = DummyResponse()
+
class view(object):
def __init__(self, context, request):
pass
+
def __call__(self):
return response
deriver = self._makeOne()
@@ -2479,9 +2616,11 @@ def __call__(self):
def test_as_newstyle_class_requestonly(self):
response = DummyResponse()
+
class view(object):
def __init__(self, context, request):
pass
+
def __call__(self):
return response
deriver = self._makeOne()
@@ -2497,9 +2636,11 @@ def __call__(self):
def test_as_oldstyle_class_context_and_request(self):
response = DummyResponse()
+
class view:
def __init__(self, context, request):
pass
+
def __call__(self):
return response
deriver = self._makeOne()
@@ -2515,9 +2656,11 @@ def __call__(self):
def test_as_oldstyle_class_requestonly(self):
response = DummyResponse()
+
class view:
def __init__(self, context, request):
pass
+
def __call__(self):
return response
deriver = self._makeOne()
@@ -2533,6 +2676,7 @@ def __call__(self):
def test_as_instance_context_and_request(self):
response = DummyResponse()
+
class View:
def __call__(self, context, request):
return response
@@ -2545,6 +2689,7 @@ def __call__(self, context, request):
def test_as_instance_requestonly(self):
response = DummyResponse()
+
class View:
def __call__(self, request):
return response
@@ -2811,14 +2956,16 @@ def test_secured_view_raises_forbidden_no_name(self):
except HTTPForbidden as e:
self.assertEqual(e.message,
'Unauthorized: <lambda> failed permission check')
- else: # pragma: no cover
+ else: # pragma: no cover
raise AssertionError
def test_secured_view_raises_forbidden_with_name(self):
from pyramid.interfaces import IAuthenticationPolicy
from pyramid.interfaces import IAuthorizationPolicy
from pyramid.httpexceptions import HTTPForbidden
- def myview(request): pass
+
+ def myview(request):
+ pass
self.config.registry.settings = {}
policy = DummySecurityPolicy(False)
self.config.registry.registerUtility(policy, IAuthenticationPolicy)
@@ -2833,13 +2980,14 @@ def myview(request): pass
except HTTPForbidden