def test_validated(route, channels, users, exp_result): request = MagicMock(user_id='U1', channel='C1', text='hello', **{'is_bot_message.return_value': False}) r = Route(route=route, handler=my_handler, channels=channels, users=users) assert r.validated(request) is exp_result
def test_validate_correct_message(route, msg, exp_result, info, match): route = Route(route, my_handler) request = MagicMock(user_id='U1', channel='C1', text=msg, match_info={}, info=None) assert route.validate_message(request) is exp_result assert request.match_info == match assert request.info == info
def test_add_routes(): # Refresh singleton instance RoutersTable.instance = None my_routes = [ Route('path1', lambda x: x), Route('path2', lambda x: x, channels=['C1'], users=['U1']) ] t = RoutersTable() t.add_routes(my_routes) assert len(t.routes) == 2 assert all([isinstance(r, Route) for r in t.routes]) assert t.routes == set(my_routes)
def test_app_add_routes(): # Refresh singleton instance RoutersTable.instance = None app = Application(TOKEN) def say_hello(request): return Response(request, text='Hi!') def do_deploy(request): current_app = request.match_info['app'] return Response(request, text=f'Start deploy {current_app}') app.add_routes([ Route('deploy {app:w}', do_deploy), Route('hello', say_hello), ]) assert len(app._routers) == 2 assert all([isinstance(r, Route) for r in app._routers.table.routes]) assert all([ r.handler in (do_deploy, say_hello) for r in app._routers.table.routes ])
def test_validate_channels_with_subscribers(channel, exp_result): r = Route('path', my_handler, ['#general']) assert r.validate_channels(MagicMock(channel=channel)) is exp_result
def test_validate_users_bot_with_subscribers(): r = Route('path', my_handler, ['#general'], users=['U1']) request = MagicMock(user_id='U1', **{'is_bot_message.return_value': True}) assert r.validate_users(request) is False
def test_validate_users_without_subscribers(user, exp_result, subscribers): r = Route('path', my_handler, ['#general'], subscribers) request = MagicMock(user_id=user, **{'is_bot_message.return_value': False}) assert r.validate_users(request) is exp_result
def test_init_require_attributes(params): with pytest.raises(TypeError): Route(**params)
def test_init_with_short_attributes(): r = Route('path', my_handler) assert r.route == 'path' assert r.handler == my_handler assert r.channels is None assert r.users is None
def test_init_with_full_attributes(): r = Route('path', my_handler, ['#general'], ['U1']) assert r.route == 'path' assert r.handler == my_handler assert r.channels == ['#general'] assert r.users == ['U1']
def test_route_str(): r = Route('path', my_handler, ['#general'], ['U1']) assert str( r ) == f"<Route 'path' handler={my_handler} channels=['#general'] users=['U1']>"
def test_validate_channels_without_subscribers(channel, exp_result, subscribers): r = Route('path', my_handler, subscribers) assert r.validate_channels(MagicMock(channel=channel)) is exp_result