def test_single_route_one_view_registered_with_factory(self): from zope.interface import Interface from pyramid.interfaces import IRouteRequest from pyramid.interfaces import IViewClassifier from pyramid.interfaces import IView registry = self._makeRegistry() def view(): pass class IMyRoot(Interface): pass class IMyRoute(Interface): pass registry.registerAdapter(view, (IViewClassifier, IMyRoute, IMyRoot), IView, '') registry.registerUtility(IMyRoute, IRouteRequest, name='a') command = self._makeOne() def factory(request): pass route = dummy.DummyRoute('a', '/a', factory=factory) mapper = dummy.DummyMapper(route) command._get_mapper = lambda *arg: mapper L = [] command.out = L.append command.bootstrap = (dummy.DummyBootstrap(registry=registry), ) result = command.run() self.assertEqual(result, 0) self.assertEqual(len(L), 3) self.assertEqual(L[-1].split()[:3], ['a', '/a', '<unknown>'])
def test_single_route_one_view_registered(self): from zope.interface import Interface from pyramid.interfaces import IRouteRequest from pyramid.interfaces import IViewClassifier from pyramid.interfaces import IView registry = self._makeRegistry() def view():pass class IMyRoute(Interface): pass registry.registerAdapter(view, (IViewClassifier, IMyRoute, Interface), IView, '') registry.registerUtility(IMyRoute, IRouteRequest, name='a') command = self._makeOne() route = dummy.DummyRoute('a', '/a') mapper = dummy.DummyMapper(route) command._get_mapper = lambda *arg: mapper L = [] command.out = L.append command.bootstrap = dummy.DummyBootstrap(registry=registry) result = command.run() self.assertEqual(result, 0) self.assertEqual(len(L), 3) compare_to = L[-1].split()[:3] self.assertEqual( compare_to, ['a', '/a', 'pyramid.tests.test_scripts.test_proutes.view'] )
def test_no_routes(self): command = self._makeOne() mapper = dummy.DummyMapper() command._get_mapper = lambda *arg: mapper L = [] command.out = L.append result = command.run() self.assertEqual(result, None) self.assertEqual(L, [])
def test_bad_args(self): cmd = self._getTargetClass()([]) cmd.bootstrap = (dummy.DummyBootstrap(), ) cmd.args = ('/foo/bar/myapp.ini#myapp', 'a') route = dummy.DummyRoute('a', '/a') mapper = dummy.DummyMapper(route) cmd._get_mapper = lambda *arg: mapper self.assertRaises(ValueError, cmd.run)
def test__find_multi_routes_none_match(self): command = self._makeOne() def factory(request): pass routes = [dummy.DummyRoute('a', '/a', factory=factory), dummy.DummyRoute('b', '/a', factory=factory)] mapper = dummy.DummyMapper(*routes) request = dummy.DummyRequest({'PATH_INFO':'/a'}) result = command._find_multi_routes(mapper, request) self.assertEqual(result, [])
def test__find_multi_routes_all_match(self): command = self._makeOne() def factory(request): pass routes = [dummy.DummyRoute('a', '/a', factory=factory, matchdict={}), dummy.DummyRoute('b', '/a', factory=factory, matchdict={})] mapper = dummy.DummyMapper(*routes) request = dummy.DummyRequest({'PATH_INFO':'/a'}) result = command._find_multi_routes(mapper, request) self.assertEqual(result, [{'match':{}, 'route':routes[0]}, {'match':{}, 'route':routes[1]}])
def test_route_with_no_slash_prefix(self): command = self._makeOne() route = dummy.DummyRoute('a', 'a') mapper = dummy.DummyMapper(route) command._get_mapper = lambda *arg: mapper L = [] command.out = L.append result = command.run() self.assertEqual(result, None) self.assertEqual(len(L), 3) self.assertEqual(L[-1].split(), ['a', '/a', '<unknown>'])
def test_single_route_no_route_registered(self): command = self._makeOne() route = dummy.DummyRoute('a', '/a') mapper = dummy.DummyMapper(route) command._get_mapper = lambda *arg: mapper L = [] command.out = L.append result = command.run() self.assertEqual(result, 0) self.assertEqual(len(L), 3) self.assertEqual(L[-1].split(), ['a', '/a', '<unknown>'])
def test_bad_args(self): cmd = self._getTargetClass()([]) cmd.bootstrap = dummy.DummyBootstrap() cmd.get_config_loader = dummy.DummyLoader() cmd.args.config_uri = '/foo/bar/myapp.ini#myapp' cmd.args.config_vars = ('a',) route = dummy.DummyRoute('a', '/a') mapper = dummy.DummyMapper(route) cmd._get_mapper = lambda *arg: mapper self.assertRaises(ValueError, cmd.run)
def test_good_args(self): cmd = self._getTargetClass()([]) cmd.bootstrap = (dummy.DummyBootstrap(),) cmd.args = ('/foo/bar/myapp.ini#myapp', 'a=1') route = dummy.DummyRoute('a', '/a') mapper = dummy.DummyMapper(route) cmd._get_mapper = lambda *arg: mapper L = [] cmd.out = lambda msg: L.append(msg) cmd.run() self.assertTrue('<unknown>' in ''.join(L))
def test_route_with_no_slash_prefix(self): command = self._makeOne() route = dummy.DummyRoute('a', 'a') mapper = dummy.DummyMapper(route) command._get_mapper = lambda *arg: mapper L = [] command.out = L.append registry = self._makeRegistry() command.bootstrap = (dummy.DummyBootstrap(registry=registry), ) result = command.run() self.assertEqual(result, 0) self.assertEqual(len(L), 3) self.assertEqual(L[-1].split(), ['a', '/a', '<unknown>', '*'])
def test_good_args(self): cmd = self._getTargetClass()([]) cmd.bootstrap = dummy.DummyBootstrap() cmd.get_config_loader = dummy.DummyLoader() cmd.args.config_uri = '/foo/bar/myapp.ini#myapp' cmd.args.config_args = ('a=1',) route = dummy.DummyRoute('a', '/a') mapper = dummy.DummyMapper(route) cmd._get_mapper = lambda *arg: mapper registry = self._makeRegistry() cmd.bootstrap = dummy.DummyBootstrap(registry=registry) L = [] cmd.out = lambda msg: L.append(msg) cmd.run() self.assertTrue('<unknown>' in ''.join(L))
def test_single_route_multiview_registered(self): from zope.interface import Interface from pyramid.interfaces import IRouteRequest from pyramid.interfaces import IViewClassifier from pyramid.interfaces import IMultiView registry = self._makeRegistry() def view(): pass class IMyRoute(Interface): pass multiview1 = dummy.DummyMultiView( view, context='context', view_name='a1' ) registry.registerAdapter( multiview1, (IViewClassifier, IMyRoute, Interface), IMultiView, '' ) registry.registerUtility(IMyRoute, IRouteRequest, name='a') command = self._makeOne() route = dummy.DummyRoute('a', '/a') mapper = dummy.DummyMapper(route) command._get_mapper = lambda *arg: mapper L = [] command.out = L.append command.bootstrap = (dummy.DummyBootstrap(registry=registry),) result = command.run() self.assertEqual(result, 0) self.assertEqual(len(L), 3) compare_to = L[-1].split()[:3] view_module = 'pyramid.tests.test_scripts.dummy' view_str = '<pyramid.tests.test_scripts.dummy.DummyMultiView' final = '%s.%s' % (view_module, view_str) self.assertEqual( compare_to, ['a', '/a', final] )
def test_single_route_no_views_registered(self): from zope.interface import Interface from pyramid.interfaces import IRouteRequest registry = self._makeRegistry() def view():pass class IMyRoute(Interface): pass registry.registerUtility(IMyRoute, IRouteRequest, name='a') command = self._makeOne() route = dummy.DummyRoute('a', '/a') mapper = dummy.DummyMapper(route) command._get_mapper = lambda *arg: mapper L = [] command.out = L.append command.bootstrap = (dummy.DummyBootstrap(registry=registry),) result = command.run() self.assertEqual(result, 0) self.assertEqual(len(L), 3) self.assertEqual(L[-1].split()[:3], ['a', '/a', '<unknown>'])
def _register_mapper(self, registry, routes): from pyramid.interfaces import IRoutesMapper mapper = dummy.DummyMapper(*routes) registry.registerUtility(mapper, IRoutesMapper)