| | |
| | | class TestPRoutesCommand(unittest.TestCase): |
| | | def _getTargetClass(self): |
| | | from pyramid.scripts.proutes import PRoutesCommand |
| | | |
| | | return PRoutesCommand |
| | | |
| | | def _makeOne(self): |
| | |
| | | |
| | | def _makeRegistry(self): |
| | | from pyramid.registry import Registry |
| | | |
| | | registry = Registry() |
| | | registry.introspector = DummyIntrospector() |
| | | return registry |
| | | |
| | | def _makeConfig(self, *arg, **kw): |
| | | from pyramid.config import Configurator |
| | | |
| | | config = Configurator(*arg, **kw) |
| | | return config |
| | | |
| | |
| | | |
| | | def test_no_mapper(self): |
| | | command = self._makeOne() |
| | | command._get_mapper = lambda *arg:None |
| | | command._get_mapper = lambda *arg: None |
| | | L = [] |
| | | command.out = L.append |
| | | result = command.run() |
| | |
| | | def test_single_route_no_views_registered(self): |
| | | from zope.interface import Interface |
| | | from pyramid.interfaces import IRouteRequest |
| | | |
| | | registry = self._makeRegistry() |
| | | |
| | | def view():pass |
| | | def view(): |
| | | pass |
| | | |
| | | class IMyRoute(Interface): |
| | | pass |
| | | |
| | | registry.registerUtility(IMyRoute, IRouteRequest, name='a') |
| | | command = self._makeOne() |
| | | route = dummy.DummyRoute('a', '/a') |
| | |
| | | from pyramid.interfaces import IRouteRequest |
| | | from pyramid.interfaces import IViewClassifier |
| | | from pyramid.interfaces import IView |
| | | |
| | | registry = self._makeRegistry() |
| | | |
| | | def view():pass |
| | | def view(): |
| | | pass |
| | | |
| | | class IMyRoute(Interface): |
| | | pass |
| | | registry.registerAdapter(view, |
| | | (IViewClassifier, IMyRoute, Interface), |
| | | IView, '') |
| | | |
| | | registry.registerAdapter( |
| | | view, (IViewClassifier, IMyRoute, Interface), IView, '' |
| | | ) |
| | | registry.registerUtility(IMyRoute, IRouteRequest, name='a') |
| | | command = self._makeOne() |
| | | route = dummy.DummyRoute('a', '/a') |
| | |
| | | self.assertEqual(len(L), 3) |
| | | compare_to = L[-1].split()[:3] |
| | | self.assertEqual( |
| | | compare_to, |
| | | ['a', '/a', 'tests.test_scripts.test_proutes.view'] |
| | | compare_to, ['a', '/a', 'tests.test_scripts.test_proutes.view'] |
| | | ) |
| | | |
| | | def test_one_route_with_long_name_one_view_registered(self): |
| | |
| | | from pyramid.interfaces import IRouteRequest |
| | | from pyramid.interfaces import IViewClassifier |
| | | from pyramid.interfaces import IView |
| | | |
| | | registry = self._makeRegistry() |
| | | |
| | | def view():pass |
| | | def view(): |
| | | pass |
| | | |
| | | class IMyRoute(Interface): |
| | | pass |
| | | |
| | | registry.registerAdapter( |
| | | view, |
| | | (IViewClassifier, IMyRoute, Interface), |
| | | IView, '' |
| | | view, (IViewClassifier, IMyRoute, Interface), IView, '' |
| | | ) |
| | | |
| | | registry.registerUtility(IMyRoute, IRouteRequest, |
| | | name='very_long_name_123') |
| | | registry.registerUtility( |
| | | IMyRoute, IRouteRequest, name='very_long_name_123' |
| | | ) |
| | | |
| | | command = self._makeOne() |
| | | route = dummy.DummyRoute( |
| | | 'very_long_name_123', |
| | | '/and_very_long_pattern_as_well' |
| | | 'very_long_name_123', '/and_very_long_pattern_as_well' |
| | | ) |
| | | mapper = dummy.DummyMapper(route) |
| | | command._get_mapper = lambda *arg: mapper |
| | |
| | | compare_to = L[-1].split()[:3] |
| | | self.assertEqual( |
| | | compare_to, |
| | | ['very_long_name_123', |
| | | '/and_very_long_pattern_as_well', |
| | | 'tests.test_scripts.test_proutes.view'] |
| | | [ |
| | | 'very_long_name_123', |
| | | '/and_very_long_pattern_as_well', |
| | | 'tests.test_scripts.test_proutes.view', |
| | | ], |
| | | ) |
| | | |
| | | def test_class_view(self): |
| | |
| | | view=dummy.DummyView, |
| | | attr='view', |
| | | renderer=nr, |
| | | request_method='POST' |
| | | request_method='POST', |
| | | ) |
| | | |
| | | command = self._makeOne() |
| | |
| | | self.assertEqual(len(L), 3) |
| | | compare_to = L[-1].split() |
| | | expected = [ |
| | | 'foo', '/a/b', |
| | | 'tests.test_scripts.dummy.DummyView.view', 'POST' |
| | | 'foo', |
| | | '/a/b', |
| | | 'tests.test_scripts.dummy.DummyView.view', |
| | | 'POST', |
| | | ] |
| | | self.assertEqual(compare_to, expected) |
| | | |
| | |
| | | from pyramid.interfaces import IRouteRequest |
| | | from pyramid.interfaces import IViewClassifier |
| | | from pyramid.interfaces import IView |
| | | |
| | | registry = self._makeRegistry() |
| | | |
| | | def view():pass |
| | | def view(): |
| | | pass |
| | | |
| | | class IMyRoot(Interface): |
| | | pass |
| | | |
| | | class IMyRoute(Interface): |
| | | pass |
| | | registry.registerAdapter(view, |
| | | (IViewClassifier, IMyRoute, IMyRoot), |
| | | IView, '') |
| | | |
| | | registry.registerAdapter( |
| | | view, (IViewClassifier, IMyRoute, IMyRoot), IView, '' |
| | | ) |
| | | registry.registerUtility(IMyRoute, IRouteRequest, name='a') |
| | | command = self._makeOne() |
| | | def factory(request): pass |
| | | |
| | | def factory(request): |
| | | pass |
| | | |
| | | route = dummy.DummyRoute('a', '/a', factory=factory) |
| | | mapper = dummy.DummyMapper(route) |
| | | command._get_mapper = lambda *arg: mapper |
| | |
| | | |
| | | registry = self._makeRegistry() |
| | | |
| | | def view(): pass |
| | | def view(): |
| | | pass |
| | | |
| | | class IMyRoute(Interface): |
| | | pass |
| | | |
| | | multiview1 = dummy.DummyMultiView( |
| | | view, context='context', |
| | | view_name='a1' |
| | | view, context='context', view_name='a1' |
| | | ) |
| | | |
| | | registry.registerAdapter( |
| | | multiview1, |
| | | (IViewClassifier, IMyRoute, Interface), |
| | | IMultiView, '' |
| | | multiview1, (IViewClassifier, IMyRoute, Interface), IMultiView, '' |
| | | ) |
| | | registry.registerUtility(IMyRoute, IRouteRequest, name='a') |
| | | command = self._makeOne() |
| | |
| | | view_str = '<tests.test_scripts.dummy.DummyMultiView' |
| | | final = '%s.%s' % (view_module, view_str) |
| | | |
| | | self.assertEqual( |
| | | compare_to, |
| | | ['a', '/a', final] |
| | | ) |
| | | self.assertEqual(compare_to, ['a', '/a', final]) |
| | | |
| | | def test__get_mapper(self): |
| | | from pyramid.urldispatch import RoutesMapper |
| | | |
| | | command = self._makeOne() |
| | | registry = self._makeRegistry() |
| | | |
| | |
| | | def test_one_route_all_methods_view_only_post(self): |
| | | from pyramid.renderers import null_renderer as nr |
| | | |
| | | def view1(context, request): return 'view1' |
| | | def view1(context, request): |
| | | return 'view1' |
| | | |
| | | config = self._makeConfig(autocommit=True) |
| | | config.add_route('foo', '/a/b') |
| | | config.add_view( |
| | | route_name='foo', |
| | | view=view1, |
| | | renderer=nr, |
| | | request_method='POST' |
| | | route_name='foo', view=view1, renderer=nr, request_method='POST' |
| | | ) |
| | | |
| | | command = self._makeOne() |
| | |
| | | self.assertEqual(len(L), 3) |
| | | compare_to = L[-1].split() |
| | | expected = [ |
| | | 'foo', '/a/b', |
| | | 'tests.test_scripts.test_proutes.view1', 'POST' |
| | | 'foo', |
| | | '/a/b', |
| | | 'tests.test_scripts.test_proutes.view1', |
| | | 'POST', |
| | | ] |
| | | self.assertEqual(compare_to, expected) |
| | | |
| | | def test_one_route_only_post_view_all_methods(self): |
| | | from pyramid.renderers import null_renderer as nr |
| | | |
| | | def view1(context, request): return 'view1' |
| | | def view1(context, request): |
| | | return 'view1' |
| | | |
| | | config = self._makeConfig(autocommit=True) |
| | | config.add_route('foo', '/a/b', request_method='POST') |
| | | config.add_view( |
| | | route_name='foo', |
| | | view=view1, |
| | | renderer=nr, |
| | | ) |
| | | config.add_view(route_name='foo', view=view1, renderer=nr) |
| | | |
| | | command = self._makeOne() |
| | | L = [] |
| | |
| | | self.assertEqual(len(L), 3) |
| | | compare_to = L[-1].split() |
| | | expected = [ |
| | | 'foo', '/a/b', |
| | | 'tests.test_scripts.test_proutes.view1', 'POST' |
| | | 'foo', |
| | | '/a/b', |
| | | 'tests.test_scripts.test_proutes.view1', |
| | | 'POST', |
| | | ] |
| | | self.assertEqual(compare_to, expected) |
| | | |
| | | def test_one_route_only_post_view_post_and_get(self): |
| | | from pyramid.renderers import null_renderer as nr |
| | | |
| | | def view1(context, request): return 'view1' |
| | | def view1(context, request): |
| | | return 'view1' |
| | | |
| | | config = self._makeConfig(autocommit=True) |
| | | config.add_route('foo', '/a/b', request_method='POST') |
| | |
| | | route_name='foo', |
| | | view=view1, |
| | | renderer=nr, |
| | | request_method=('POST', 'GET') |
| | | request_method=('POST', 'GET'), |
| | | ) |
| | | |
| | | command = self._makeOne() |
| | |
| | | self.assertEqual(len(L), 3) |
| | | compare_to = L[-1].split() |
| | | expected = [ |
| | | 'foo', '/a/b', |
| | | 'tests.test_scripts.test_proutes.view1', 'POST' |
| | | 'foo', |
| | | '/a/b', |
| | | 'tests.test_scripts.test_proutes.view1', |
| | | 'POST', |
| | | ] |
| | | self.assertEqual(compare_to, expected) |
| | | |
| | | def test_route_request_method_mismatch(self): |
| | | from pyramid.renderers import null_renderer as nr |
| | | |
| | | def view1(context, request): return 'view1' |
| | | def view1(context, request): |
| | | return 'view1' |
| | | |
| | | config = self._makeConfig(autocommit=True) |
| | | config.add_route('foo', '/a/b', request_method='POST') |
| | | config.add_view( |
| | | route_name='foo', |
| | | view=view1, |
| | | renderer=nr, |
| | | request_method='GET' |
| | | route_name='foo', view=view1, renderer=nr, request_method='GET' |
| | | ) |
| | | |
| | | command = self._makeOne() |
| | |
| | | self.assertEqual(len(L), 3) |
| | | compare_to = L[-1].split() |
| | | expected = [ |
| | | 'foo', '/a/b', |
| | | 'foo', |
| | | '/a/b', |
| | | 'tests.test_scripts.test_proutes.view1', |
| | | '<route', 'mismatch>' |
| | | '<route', |
| | | 'mismatch>', |
| | | ] |
| | | self.assertEqual(compare_to, expected) |
| | | |
| | | def test_route_static_views(self): |
| | | from pyramid.renderers import null_renderer as nr |
| | | |
| | | config = self._makeConfig(autocommit=True) |
| | | config.add_static_view('static', 'static', cache_max_age=3600) |
| | | path2 = os.path.normpath('/var/www/static') |
| | | config.add_static_view(name='static2', path=path2) |
| | | config.add_static_view( |
| | | name='pyramid_scaffold', |
| | | path='pyramid:scaffolds/starter/+package+/static' |
| | | path='pyramid:scaffolds/starter/+package+/static', |
| | | ) |
| | | |
| | | command = self._makeOne() |
| | |
| | | self.assertEqual(len(L), 5) |
| | | |
| | | expected = [ |
| | | ['__static/', '/static/*subpath', |
| | | 'tests.test_scripts:static/', '*'], |
| | | [ |
| | | '__static/', |
| | | '/static/*subpath', |
| | | 'tests.test_scripts:static/', |
| | | '*', |
| | | ], |
| | | ['__static2/', '/static2/*subpath', path2 + os.sep, '*'], |
| | | ['__pyramid_scaffold/', '/pyramid_scaffold/*subpath', |
| | | 'pyramid:scaffolds/starter/+package+/static/', '*'], |
| | | [ |
| | | '__pyramid_scaffold/', |
| | | '/pyramid_scaffold/*subpath', |
| | | 'pyramid:scaffolds/starter/+package+/static/', |
| | | '*', |
| | | ], |
| | | ] |
| | | |
| | | for index, line in enumerate(L[2:]): |
| | |
| | | |
| | | def test_route_no_view(self): |
| | | from pyramid.renderers import null_renderer as nr |
| | | |
| | | config = self._makeConfig(autocommit=True) |
| | | config.add_route('foo', '/a/b', request_method='POST') |
| | | |
| | |
| | | self.assertEqual(result, 0) |
| | | self.assertEqual(len(L), 3) |
| | | compare_to = L[-1].split() |
| | | expected = [ |
| | | 'foo', '/a/b', |
| | | '<unknown>', |
| | | 'POST', |
| | | ] |
| | | expected = ['foo', '/a/b', '<unknown>', 'POST'] |
| | | self.assertEqual(compare_to, expected) |
| | | |
| | | def test_route_as_wsgiapp(self): |
| | | from pyramid.wsgi import wsgiapp2 |
| | | |
| | | config1 = self._makeConfig(autocommit=True) |
| | | def view1(context, request): return 'view1' |
| | | |
| | | def view1(context, request): |
| | | return 'view1' |
| | | |
| | | config1.add_route('foo', '/a/b', request_method='POST') |
| | | config1.add_view(view=view1, route_name='foo') |
| | | |
| | | config2 = self._makeConfig(autocommit=True) |
| | | config2.add_route('foo', '/a/b', request_method='POST') |
| | | config2.add_view( |
| | | wsgiapp2(config1.make_wsgi_app()), |
| | | route_name='foo', |
| | | ) |
| | | config2.add_view(wsgiapp2(config1.make_wsgi_app()), route_name='foo') |
| | | |
| | | command = self._makeOne() |
| | | L = [] |
| | |
| | | self.assertEqual(result, 0) |
| | | self.assertEqual(len(L), 3) |
| | | compare_to = L[-1].split() |
| | | expected = [ |
| | | 'foo', '/a/b', |
| | | '<wsgiapp>', |
| | | 'POST', |
| | | ] |
| | | expected = ['foo', '/a/b', '<wsgiapp>', 'POST'] |
| | | self.assertEqual(compare_to, expected) |
| | | |
| | | def test_route_is_get_view_request_method_not_post(self): |
| | | from pyramid.renderers import null_renderer as nr |
| | | from pyramid.config import not_ |
| | | |
| | | def view1(context, request): return 'view1' |
| | | def view1(context, request): |
| | | return 'view1' |
| | | |
| | | config = self._makeConfig(autocommit=True) |
| | | config.add_route('foo', '/a/b', request_method='GET') |
| | |
| | | route_name='foo', |
| | | view=view1, |
| | | renderer=nr, |
| | | request_method=not_('POST') |
| | | request_method=not_('POST'), |
| | | ) |
| | | |
| | | command = self._makeOne() |
| | |
| | | self.assertEqual(len(L), 3) |
| | | compare_to = L[-1].split() |
| | | expected = [ |
| | | 'foo', '/a/b', |
| | | 'foo', |
| | | '/a/b', |
| | | 'tests.test_scripts.test_proutes.view1', |
| | | 'GET' |
| | | 'GET', |
| | | ] |
| | | self.assertEqual(compare_to, expected) |
| | | |
| | |
| | | from pyramid.renderers import null_renderer as nr |
| | | from pyramid.config import not_ |
| | | |
| | | def view1(context, request): return 'view1' |
| | | def view1(context, request): |
| | | return 'view1' |
| | | |
| | | config = self._makeConfig(autocommit=True) |
| | | config.add_route('foo', '/a/b') |
| | |
| | | route_name='foo', |
| | | view=view1, |
| | | renderer=nr, |
| | | request_method=not_('POST') |
| | | request_method=not_('POST'), |
| | | ) |
| | | |
| | | command = self._makeOne() |
| | |
| | | self.assertEqual(len(L), 3) |
| | | compare_to = L[-1].split() |
| | | expected = [ |
| | | 'foo', '/a/b', |
| | | 'foo', |
| | | '/a/b', |
| | | 'tests.test_scripts.test_proutes.view1', |
| | | '!POST,*' |
| | | '!POST,*', |
| | | ] |
| | | self.assertEqual(compare_to, expected) |
| | | |
| | |
| | | from pyramid.renderers import null_renderer as nr |
| | | from pyramid.config import not_ |
| | | |
| | | def view1(context, request): return 'view1' |
| | | def view2(context, request): return 'view2' |
| | | def view1(context, request): |
| | | return 'view1' |
| | | |
| | | def view2(context, request): |
| | | return 'view2' |
| | | |
| | | config = self._makeConfig(autocommit=True) |
| | | config.add_route('foo', '/a/b') |
| | |
| | | route_name='foo', |
| | | view=view1, |
| | | renderer=nr, |
| | | request_method=not_('POST') |
| | | request_method=not_('POST'), |
| | | ) |
| | | |
| | | config.add_route('bar', '/b/a') |
| | |
| | | route_name='bar', |
| | | view=view2, |
| | | renderer=nr, |
| | | request_method=not_('POST') |
| | | request_method=not_('POST'), |
| | | ) |
| | | |
| | | command = self._makeOne() |
| | |
| | | self.assertEqual(len(L), 3) |
| | | compare_to = L[-1].split() |
| | | expected = [ |
| | | 'foo', '/a/b', |
| | | 'foo', |
| | | '/a/b', |
| | | 'tests.test_scripts.test_proutes.view1', |
| | | '!POST,*' |
| | | '!POST,*', |
| | | ] |
| | | self.assertEqual(compare_to, expected) |
| | | |
| | |
| | | from pyramid.renderers import null_renderer as nr |
| | | from pyramid.config import not_ |
| | | |
| | | def view1(context, request): return 'view1' |
| | | def view1(context, request): |
| | | return 'view1' |
| | | |
| | | config = self._makeConfig(autocommit=True) |
| | | config.add_route('foo', '/a/b') |
| | |
| | | route_name='foo', |
| | | view=view1, |
| | | renderer=nr, |
| | | request_method=not_('POST') |
| | | request_method=not_('POST'), |
| | | ) |
| | | |
| | | command = self._makeOne() |
| | |
| | | from pyramid.renderers import null_renderer as nr |
| | | from pyramid.config import not_ |
| | | |
| | | def view1(context, request): return 'view1' |
| | | def view1(context, request): |
| | | return 'view1' |
| | | |
| | | config = self._makeConfig(autocommit=True) |
| | | config.add_route('foo', '/a/b') |
| | |
| | | route_name='foo', |
| | | view=view1, |
| | | renderer=nr, |
| | | request_method=not_('POST') |
| | | request_method=not_('POST'), |
| | | ) |
| | | |
| | | command = self._makeOne() |
| | |
| | | from pyramid.renderers import null_renderer as nr |
| | | from pyramid.config import not_ |
| | | |
| | | def view1(context, request): return 'view1' |
| | | def view1(context, request): |
| | | return 'view1' |
| | | |
| | | config = self._makeConfig(autocommit=True) |
| | | config.add_route('foo', '/a/b') |
| | |
| | | route_name='foo', |
| | | view=view1, |
| | | renderer=nr, |
| | | request_method=not_('POST') |
| | | request_method=not_('POST'), |
| | | ) |
| | | |
| | | command = self._makeOne() |
| | |
| | | command.out = L.append |
| | | command.bootstrap = dummy.DummyBootstrap(registry=config.registry) |
| | | command.get_config_loader = dummy.DummyLoader( |
| | | {'proutes': {'format': 'method\nname'}}) |
| | | {'proutes': {'format': 'method\nname'}} |
| | | ) |
| | | |
| | | result = command.run() |
| | | self.assertEqual(result, 0) |
| | |
| | | from pyramid.renderers import null_renderer as nr |
| | | from pyramid.config import not_ |
| | | |
| | | def view1(context, request): return 'view1' |
| | | def view1(context, request): |
| | | return 'view1' |
| | | |
| | | config = self._makeConfig(autocommit=True) |
| | | config.add_route('foo', '/a/b') |
| | |
| | | route_name='foo', |
| | | view=view1, |
| | | renderer=nr, |
| | | request_method=not_('POST') |
| | | request_method=not_('POST'), |
| | | ) |
| | | |
| | | command = self._makeOne() |
| | |
| | | command.out = L.append |
| | | command.bootstrap = dummy.DummyBootstrap(registry=config.registry) |
| | | command.get_config_loader = dummy.DummyLoader( |
| | | {'proutes': {'format': 'method name'}}) |
| | | {'proutes': {'format': 'method name'}} |
| | | ) |
| | | |
| | | result = command.run() |
| | | self.assertEqual(result, 0) |
| | |
| | | from pyramid.renderers import null_renderer as nr |
| | | from pyramid.config import not_ |
| | | |
| | | def view1(context, request): return 'view1' |
| | | def view1(context, request): |
| | | return 'view1' |
| | | |
| | | config = self._makeConfig(autocommit=True) |
| | | config.add_route('foo', '/a/b') |
| | |
| | | route_name='foo', |
| | | view=view1, |
| | | renderer=nr, |
| | | request_method=not_('POST') |
| | | request_method=not_('POST'), |
| | | ) |
| | | |
| | | command = self._makeOne() |
| | |
| | | command.out = L.append |
| | | command.bootstrap = dummy.DummyBootstrap(registry=config.registry) |
| | | command.get_config_loader = dummy.DummyLoader( |
| | | {'proutes': {'format': 'method,name'}}) |
| | | {'proutes': {'format': 'method,name'}} |
| | | ) |
| | | |
| | | result = command.run() |
| | | self.assertEqual(result, 0) |
| | |
| | | self.assertEqual(result, 0) |
| | | self.assertEqual(len(L), 3) |
| | | compare_to = L[-1].split() |
| | | expected = [ |
| | | 'foo', 'http://example.com/bar.aspx', |
| | | '<unknown>', '*', |
| | | ] |
| | | expected = ['foo', 'http://example.com/bar.aspx', '<unknown>', '*'] |
| | | self.assertEqual(compare_to, expected) |
| | | |
| | | |
| | | class Test_main(unittest.TestCase): |
| | | def _callFUT(self, argv): |
| | | from pyramid.scripts.proutes import main |
| | | |
| | | return main(argv, quiet=True) |
| | | |
| | | def test_it(self): |