summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris McDonough <chrism@plope.com>2011-08-31 22:27:34 -0400
committerChris McDonough <chrism@plope.com>2011-08-31 22:27:34 -0400
commita0b2c6cf3c1585d89d15f8b919ed15410d8e6765 (patch)
tree47b9b1ef79bd63bf1bcc0d4afe6021ae475544ce
parenteb614467e425d1a012572ce921d60f40c49a0424 (diff)
downloadpyramid-a0b2c6cf3c1585d89d15f8b919ed15410d8e6765.tar.gz
pyramid-a0b2c6cf3c1585d89d15f8b919ed15410d8e6765.tar.bz2
pyramid-a0b2c6cf3c1585d89d15f8b919ed15410d8e6765.zip
move add_route tests to test_routes
-rw-r--r--pyramid/tests/test_config/__init__.py3
-rw-r--r--pyramid/tests/test_config/test_init.py190
-rw-r--r--pyramid/tests/test_config/test_routes.py223
-rw-r--r--pyramid/tests/test_config/test_testing.py4
4 files changed, 227 insertions, 193 deletions
diff --git a/pyramid/tests/test_config/__init__.py b/pyramid/tests/test_config/__init__.py
index d1e7117b0..2f9f516ae 100644
--- a/pyramid/tests/test_config/__init__.py
+++ b/pyramid/tests/test_config/__init__.py
@@ -20,6 +20,9 @@ def dummy_include2(config):
includeme = dummy_include
+class DummyContext:
+ pass
+
class DummyFactory(object):
implements(IFactory)
def __call__(self):
diff --git a/pyramid/tests/test_config/test_init.py b/pyramid/tests/test_config/test_init.py
index 5a47418be..b955dae22 100644
--- a/pyramid/tests/test_config/test_init.py
+++ b/pyramid/tests/test_config/test_init.py
@@ -19,6 +19,7 @@ from pyramid.tests.test_config import dummy_view
from pyramid.tests.test_config import dummy_extend
from pyramid.tests.test_config import dummy_extend2
from pyramid.tests.test_config import IDummy
+from pyramid.tests.test_config import DummyContext
try:
import __pypy__
@@ -786,192 +787,6 @@ pyramid.tests.test_config.dummy_include2""",
newconfig = config.with_context(ctx)
self.assertEqual(newconfig._ctx, ctx)
- def test_get_routes_mapper_not_yet_registered(self):
- config = self._makeOne()
- mapper = config.get_routes_mapper()
- self.assertEqual(mapper.routelist, [])
-
- def test_get_routes_mapper_already_registered(self):
- from pyramid.interfaces import IRoutesMapper
- config = self._makeOne()
- mapper = object()
- config.registry.registerUtility(mapper, IRoutesMapper)
- result = config.get_routes_mapper()
- self.assertEqual(result, mapper)
-
- def test_add_route_defaults(self):
- config = self._makeOne(autocommit=True)
- config.add_route('name', 'path')
- self._assertRoute(config, 'name', 'path')
-
- def test_add_route_with_route_prefix(self):
- config = self._makeOne(autocommit=True)
- config.route_prefix = 'root'
- config.add_route('name', 'path')
- self._assertRoute(config, 'name', 'root/path')
-
- def test_add_route_discriminator(self):
- config = self._makeOne()
- config.add_route('name', 'path')
- self.assertEqual(config._ctx.actions[-1][0], ('route', 'name'))
-
- def test_add_route_with_factory(self):
- config = self._makeOne(autocommit=True)
- factory = object()
- config.add_route('name', 'path', factory=factory)
- route = self._assertRoute(config, 'name', 'path')
- self.assertEqual(route.factory, factory)
-
- def test_add_route_with_static(self):
- config = self._makeOne(autocommit=True)
- config.add_route('name', 'path/{foo}', static=True)
- mapper = config.get_routes_mapper()
- self.assertEqual(len(mapper.get_routes()), 0)
- self.assertEqual(mapper.generate('name', {"foo":"a"}), '/path/a')
-
- def test_add_route_with_factory_dottedname(self):
- config = self._makeOne(autocommit=True)
- config.add_route(
- 'name', 'path',
- factory='pyramid.tests.test_config.dummyfactory')
- route = self._assertRoute(config, 'name', 'path')
- self.assertEqual(route.factory, dummyfactory)
-
- def test_add_route_with_xhr(self):
- config = self._makeOne(autocommit=True)
- config.add_route('name', 'path', xhr=True)
- route = self._assertRoute(config, 'name', 'path', 1)
- predicate = route.predicates[0]
- request = self._makeRequest(config)
- request.is_xhr = True
- self.assertEqual(predicate(None, request), True)
- request = self._makeRequest(config)
- request.is_xhr = False
- self.assertEqual(predicate(None, request), False)
-
- def test_add_route_with_request_method(self):
- config = self._makeOne(autocommit=True)
- config.add_route('name', 'path', request_method='GET')
- route = self._assertRoute(config, 'name', 'path', 1)
- predicate = route.predicates[0]
- request = self._makeRequest(config)
- request.method = 'GET'
- self.assertEqual(predicate(None, request), True)
- request = self._makeRequest(config)
- request.method = 'POST'
- self.assertEqual(predicate(None, request), False)
-
- def test_add_route_with_path_info(self):
- config = self._makeOne(autocommit=True)
- config.add_route('name', 'path', path_info='/foo')
- route = self._assertRoute(config, 'name', 'path', 1)
- predicate = route.predicates[0]
- request = self._makeRequest(config)
- request.path_info = '/foo'
- self.assertEqual(predicate(None, request), True)
- request = self._makeRequest(config)
- request.path_info = '/'
- self.assertEqual(predicate(None, request), False)
-
- def test_add_route_with_request_param(self):
- config = self._makeOne(autocommit=True)
- config.add_route('name', 'path', request_param='abc')
- route = self._assertRoute(config, 'name', 'path', 1)
- predicate = route.predicates[0]
- request = self._makeRequest(config)
- request.params = {'abc':'123'}
- self.assertEqual(predicate(None, request), True)
- request = self._makeRequest(config)
- request.params = {}
- self.assertEqual(predicate(None, request), False)
-
- def test_add_route_with_custom_predicates(self):
- config = self._makeOne(autocommit=True)
- def pred1(context, request): pass
- def pred2(context, request): pass
- config.add_route('name', 'path', custom_predicates=(pred1, pred2))
- route = self._assertRoute(config, 'name', 'path', 2)
- self.assertEqual(route.predicates, [pred1, pred2])
-
- def test_add_route_with_header(self):
- config = self._makeOne(autocommit=True)
- config.add_route('name', 'path', header='Host')
- route = self._assertRoute(config, 'name', 'path', 1)
- predicate = route.predicates[0]
- request = self._makeRequest(config)
- request.headers = {'Host':'example.com'}
- self.assertEqual(predicate(None, request), True)
- request = self._makeRequest(config)
- request.headers = {}
- self.assertEqual(predicate(None, request), False)
-
- def test_add_route_with_accept(self):
- config = self._makeOne(autocommit=True)
- config.add_route('name', 'path', accept='text/xml')
- route = self._assertRoute(config, 'name', 'path', 1)
- predicate = route.predicates[0]
- request = self._makeRequest(config)
- request.accept = ['text/xml']
- self.assertEqual(predicate(None, request), True)
- request = self._makeRequest(config)
- request.accept = ['text/html']
- self.assertEqual(predicate(None, request), False)
-
- def test_add_route_no_pattern_with_path(self):
- config = self._makeOne(autocommit=True)
- config.add_route('name', path='path')
- self._assertRoute(config, 'name', 'path')
-
- def test_add_route_no_path_no_pattern(self):
- from pyramid.exceptions import ConfigurationError
- config = self._makeOne()
- self.assertRaises(ConfigurationError, config.add_route, 'name')
-
- def test_add_route_with_pregenerator(self):
- config = self._makeOne(autocommit=True)
- config.add_route('name', 'pattern', pregenerator='123')
- route = self._assertRoute(config, 'name', 'pattern')
- self.assertEqual(route.pregenerator, '123')
-
- def test_add_route_no_view_with_view_attr(self):
- config = self._makeOne(autocommit=True)
- from pyramid.exceptions import ConfigurationError
- try:
- config.add_route('name', '/pattern', view_attr='abc')
- except ConfigurationError:
- pass
- else: # pragma: no cover
- raise AssertionError
-
- def test_add_route_no_view_with_view_context(self):
- config = self._makeOne(autocommit=True)
- from pyramid.exceptions import ConfigurationError
- try:
- config.add_route('name', '/pattern', view_context=DummyContext)
- except ConfigurationError:
- pass
- else: # pragma: no cover
- raise AssertionError
-
- def test_add_route_no_view_with_view_permission(self):
- config = self._makeOne(autocommit=True)
- from pyramid.exceptions import ConfigurationError
- try:
- config.add_route('name', '/pattern', view_permission='edit')
- except ConfigurationError:
- pass
- else: # pragma: no cover
- raise AssertionError
-
- def test_add_route_no_view_with_view_renderer(self):
- config = self._makeOne(autocommit=True)
- from pyramid.exceptions import ConfigurationError
- try:
- config.add_route('name', '/pattern', view_renderer='json')
- except ConfigurationError:
- pass
- else: # pragma: no cover
- raise AssertionError
def test_derive_view_function(self):
from pyramid.renderers import null_renderer
@@ -2025,9 +1840,6 @@ class DummyRequest:
def get_response(self, app):
return app
-class DummyContext:
- pass
-
class DummyPackage:
def __init__(self, name):
self.__name__ = name
diff --git a/pyramid/tests/test_config/test_routes.py b/pyramid/tests/test_config/test_routes.py
new file mode 100644
index 000000000..6eea92a70
--- /dev/null
+++ b/pyramid/tests/test_config/test_routes.py
@@ -0,0 +1,223 @@
+import unittest
+
+from pyramid.tests.test_config import dummyfactory
+from pyramid.tests.test_config import DummyContext
+
+class RoutesConfiguratorMixinTests(unittest.TestCase):
+ def _makeOne(self, *arg, **kw):
+ from pyramid.config import Configurator
+ config = Configurator(*arg, **kw)
+ return config
+
+ def _assertRoute(self, config, name, path, num_predicates=0):
+ from pyramid.interfaces import IRoutesMapper
+ mapper = config.registry.getUtility(IRoutesMapper)
+ routes = mapper.get_routes()
+ route = routes[0]
+ self.assertEqual(len(routes), 1)
+ self.assertEqual(route.name, name)
+ self.assertEqual(route.path, path)
+ self.assertEqual(len(routes[0].predicates), num_predicates)
+ return route
+
+ def _makeRequest(self, config):
+ request = DummyRequest()
+ request.registry = config.registry
+ return request
+
+ def test_get_routes_mapper_not_yet_registered(self):
+ config = self._makeOne()
+ mapper = config.get_routes_mapper()
+ self.assertEqual(mapper.routelist, [])
+
+ def test_get_routes_mapper_already_registered(self):
+ from pyramid.interfaces import IRoutesMapper
+ config = self._makeOne()
+ mapper = object()
+ config.registry.registerUtility(mapper, IRoutesMapper)
+ result = config.get_routes_mapper()
+ self.assertEqual(result, mapper)
+
+ def test_add_route_defaults(self):
+ config = self._makeOne(autocommit=True)
+ config.add_route('name', 'path')
+ self._assertRoute(config, 'name', 'path')
+
+ def test_add_route_with_route_prefix(self):
+ config = self._makeOne(autocommit=True)
+ config.route_prefix = 'root'
+ config.add_route('name', 'path')
+ self._assertRoute(config, 'name', 'root/path')
+
+ def test_add_route_discriminator(self):
+ config = self._makeOne()
+ config.add_route('name', 'path')
+ self.assertEqual(config._ctx.actions[-1][0], ('route', 'name'))
+
+ def test_add_route_with_factory(self):
+ config = self._makeOne(autocommit=True)
+ factory = object()
+ config.add_route('name', 'path', factory=factory)
+ route = self._assertRoute(config, 'name', 'path')
+ self.assertEqual(route.factory, factory)
+
+ def test_add_route_with_static(self):
+ config = self._makeOne(autocommit=True)
+ config.add_route('name', 'path/{foo}', static=True)
+ mapper = config.get_routes_mapper()
+ self.assertEqual(len(mapper.get_routes()), 0)
+ self.assertEqual(mapper.generate('name', {"foo":"a"}), '/path/a')
+
+ def test_add_route_with_factory_dottedname(self):
+ config = self._makeOne(autocommit=True)
+ config.add_route(
+ 'name', 'path',
+ factory='pyramid.tests.test_config.dummyfactory')
+ route = self._assertRoute(config, 'name', 'path')
+ self.assertEqual(route.factory, dummyfactory)
+
+ def test_add_route_with_xhr(self):
+ config = self._makeOne(autocommit=True)
+ config.add_route('name', 'path', xhr=True)
+ route = self._assertRoute(config, 'name', 'path', 1)
+ predicate = route.predicates[0]
+ request = self._makeRequest(config)
+ request.is_xhr = True
+ self.assertEqual(predicate(None, request), True)
+ request = self._makeRequest(config)
+ request.is_xhr = False
+ self.assertEqual(predicate(None, request), False)
+
+ def test_add_route_with_request_method(self):
+ config = self._makeOne(autocommit=True)
+ config.add_route('name', 'path', request_method='GET')
+ route = self._assertRoute(config, 'name', 'path', 1)
+ predicate = route.predicates[0]
+ request = self._makeRequest(config)
+ request.method = 'GET'
+ self.assertEqual(predicate(None, request), True)
+ request = self._makeRequest(config)
+ request.method = 'POST'
+ self.assertEqual(predicate(None, request), False)
+
+ def test_add_route_with_path_info(self):
+ config = self._makeOne(autocommit=True)
+ config.add_route('name', 'path', path_info='/foo')
+ route = self._assertRoute(config, 'name', 'path', 1)
+ predicate = route.predicates[0]
+ request = self._makeRequest(config)
+ request.path_info = '/foo'
+ self.assertEqual(predicate(None, request), True)
+ request = self._makeRequest(config)
+ request.path_info = '/'
+ self.assertEqual(predicate(None, request), False)
+
+ def test_add_route_with_request_param(self):
+ config = self._makeOne(autocommit=True)
+ config.add_route('name', 'path', request_param='abc')
+ route = self._assertRoute(config, 'name', 'path', 1)
+ predicate = route.predicates[0]
+ request = self._makeRequest(config)
+ request.params = {'abc':'123'}
+ self.assertEqual(predicate(None, request), True)
+ request = self._makeRequest(config)
+ request.params = {}
+ self.assertEqual(predicate(None, request), False)
+
+ def test_add_route_with_custom_predicates(self):
+ config = self._makeOne(autocommit=True)
+ def pred1(context, request): pass
+ def pred2(context, request): pass
+ config.add_route('name', 'path', custom_predicates=(pred1, pred2))
+ route = self._assertRoute(config, 'name', 'path', 2)
+ self.assertEqual(route.predicates, [pred1, pred2])
+
+ def test_add_route_with_header(self):
+ config = self._makeOne(autocommit=True)
+ config.add_route('name', 'path', header='Host')
+ route = self._assertRoute(config, 'name', 'path', 1)
+ predicate = route.predicates[0]
+ request = self._makeRequest(config)
+ request.headers = {'Host':'example.com'}
+ self.assertEqual(predicate(None, request), True)
+ request = self._makeRequest(config)
+ request.headers = {}
+ self.assertEqual(predicate(None, request), False)
+
+ def test_add_route_with_accept(self):
+ config = self._makeOne(autocommit=True)
+ config.add_route('name', 'path', accept='text/xml')
+ route = self._assertRoute(config, 'name', 'path', 1)
+ predicate = route.predicates[0]
+ request = self._makeRequest(config)
+ request.accept = ['text/xml']
+ self.assertEqual(predicate(None, request), True)
+ request = self._makeRequest(config)
+ request.accept = ['text/html']
+ self.assertEqual(predicate(None, request), False)
+
+ def test_add_route_no_pattern_with_path(self):
+ config = self._makeOne(autocommit=True)
+ config.add_route('name', path='path')
+ self._assertRoute(config, 'name', 'path')
+
+ def test_add_route_no_path_no_pattern(self):
+ from pyramid.exceptions import ConfigurationError
+ config = self._makeOne()
+ self.assertRaises(ConfigurationError, config.add_route, 'name')
+
+ def test_add_route_with_pregenerator(self):
+ config = self._makeOne(autocommit=True)
+ config.add_route('name', 'pattern', pregenerator='123')
+ route = self._assertRoute(config, 'name', 'pattern')
+ self.assertEqual(route.pregenerator, '123')
+
+ def test_add_route_no_view_with_view_attr(self):
+ config = self._makeOne(autocommit=True)
+ from pyramid.exceptions import ConfigurationError
+ try:
+ config.add_route('name', '/pattern', view_attr='abc')
+ except ConfigurationError:
+ pass
+ else: # pragma: no cover
+ raise AssertionError
+
+ def test_add_route_no_view_with_view_context(self):
+ config = self._makeOne(autocommit=True)
+ from pyramid.exceptions import ConfigurationError
+ try:
+ config.add_route('name', '/pattern', view_context=DummyContext)
+ except ConfigurationError:
+ pass
+ else: # pragma: no cover
+ raise AssertionError
+
+ def test_add_route_no_view_with_view_permission(self):
+ config = self._makeOne(autocommit=True)
+ from pyramid.exceptions import ConfigurationError
+ try:
+ config.add_route('name', '/pattern', view_permission='edit')
+ except ConfigurationError:
+ pass
+ else: # pragma: no cover
+ raise AssertionError
+
+ def test_add_route_no_view_with_view_renderer(self):
+ config = self._makeOne(autocommit=True)
+ from pyramid.exceptions import ConfigurationError
+ try:
+ config.add_route('name', '/pattern', view_renderer='json')
+ except ConfigurationError:
+ pass
+ else: # pragma: no cover
+ raise AssertionError
+
+class DummyRequest:
+ subpath = ()
+ matchdict = None
+ def __init__(self, environ=None):
+ if environ is None:
+ environ = {}
+ self.environ = environ
+ self.params = {}
+ self.cookies = {}
diff --git a/pyramid/tests/test_config/test_testing.py b/pyramid/tests/test_config/test_testing.py
index 4127a9039..494a2b099 100644
--- a/pyramid/tests/test_config/test_testing.py
+++ b/pyramid/tests/test_config/test_testing.py
@@ -179,8 +179,4 @@ class DummyRequest:
self.environ = environ
self.params = {}
self.cookies = {}
- def copy(self):
- return self
- def get_response(self, app):
- return app