def test_simple_config(self): module = ModuleType('module') module.handler1 = lambda: sentinel.a2 module.handler2 = lambda a1, a2: (a1, a2, sentinel.result) config = ''' index /{a1:\d+} (a1: int): handler1 (a2) handler2 ''' router = configparser.parse_config(config.splitlines(), module) self.assertEqual(router.reverse('index', a1=37), '/37') ctx = Context(path_info='/42') self.assertEqual( ctx.inject(router), (42, sentinel.a2, sentinel.result) )
def test_complex_config(self): module = ModuleType('module') module.exc1 = type('exc1', (Exception,), {}) module.exc2 = type('exc2', (Exception,), {}) module.exc3 = type('exc3', (Exception,), {}) module.handler1 = lambda: sentinel.a2 module.handler2 = lambda a1, a2: (a1, a2, sentinel.result) module.handler3 = lambda: Mock(side_effect=module.exc2)() module.handler4 = lambda: sentinel.a4 module.handler5 = lambda: sentinel.a5 module.handler6 = lambda r1: (r1, sentinel.a6) module.handler7 = lambda: sentinel.a7 module.handler8 = lambda: sentinel.a8 module.handler9 = lambda r1, r2: (r1, r2, sentinel.a9) config = """ index /: * GET, HEAD: handler1 (a2) handler2 * POST: handler1 todo /{todo_id:\d+} (todo_id: int): * GET, HEAD: handler3 (r1): exc1, exc2: handler4 exc3: handler5 handler6 other /foo: handler7 (r1) * DELETE: handler8 (r2) handler9 """ router = configparser.parse_config(config.splitlines(), module) ctx = Context(path_info='/42', request_method='GET') self.assertEqual(ctx.inject(router), (sentinel.a4, sentinel.a6)) ctx = Context(path_info='/foo', request_method='DELETE') self.assertEqual( ctx.inject(router), (sentinel.a7, sentinel.a8, sentinel.a9) )
def test_medium_config(self): module = ModuleType('module') module.handler1 = lambda: sentinel.a2 module.handler2 = lambda a1, a2: (a1, a2, sentinel.result) config = ''' index /{a1:\d+} (a1: int): * GET, HEAD: handler1 (a2) handler2 * POST: handler1 ''' router = configparser.parse_config(config.splitlines(), module) self.assertEqual(router.reverse('index', a1=37), '/37') ctx = Context(path_info='/42', request_method='GET') self.assertEqual( ctx.inject(router), (42, sentinel.a2, sentinel.result) ) ctx = Context(path_info='/42', request_method='POST') self.assertIs(ctx.inject(router), sentinel.a2)