async def test_basic(self, app, command): """Test whether a command handler responds to its command and not to others, or badly formatted commands""" handler = self.make_default_handler() app.add_handler(handler) assert await self.response(app, make_command_update(command, bot=app.bot)) assert not is_match(handler, make_command_update(command[1:], bot=app.bot)) assert not is_match( handler, make_command_update(f"/not{command[1:]}", bot=app.bot)) assert not is_match( handler, make_command_update(f"not {command} at start", bot=app.bot)) assert not is_match( handler, make_message_update(bot=app.bot, message=None, caption="caption")) handler = CommandHandler(["FOO", "bAR"], callback=self.callback) assert isinstance(handler.commands, frozenset) assert handler.commands == {"foo", "bar"} handler = CommandHandler(["FOO"], callback=self.callback) assert isinstance(handler.commands, frozenset) assert handler.commands == {"foo"}
def test_with_filter(self, command): """Test that a CH with a (generic) filter responds iff its filters match""" handler = self.make_default_handler(filters=Filters.group) assert is_match( handler, make_command_update(command, chat=Chat(-23, Chat.GROUP))) assert not is_match( handler, make_command_update(command, chat=Chat(23, Chat.PRIVATE)))
def test_directed_commands(self, bot, command): """Test recognition of commands with a mention to the bot""" handler = self.make_default_handler() assert is_match( handler, make_command_update(command + '@' + bot.username, bot=bot)) assert not is_match( handler, make_command_update(command + '@otherbot', bot=bot))
def test_pass_args(self, dp, bot, command): """Test the passing of arguments alongside a command""" handler = self.make_default_handler(self.ch_callback_args, pass_args=True) dp.add_handler(handler) at_command = '{}@{}'.format(command, bot.username) assert self.response(dp, make_command_update(command)) assert self.response(dp, make_command_update(command + ' one two')) assert self.response(dp, make_command_update(at_command, bot=bot)) assert self.response(dp, make_command_update(at_command + ' one two', bot=bot))
def test_basic(self, dp, command): """Test whether a command handler responds to its command and not to others, or badly formatted commands""" handler = self.make_default_handler() dp.add_handler(handler) assert self.response(dp, make_command_update(command)) assert not is_match(handler, make_command_update(command[1:])) assert not is_match(handler, make_command_update('/not{}'.format(command[1:]))) assert not is_match(handler, make_command_update('not {} at start'.format(command)))
def _test_edited(self, message, handler_edited, handler_not_edited): """ Assert whether a handler that should accept edited messages and a handler that shouldn't work correctly. :param message: ``telegram.Message`` to check against the handlers :param handler_edited: handler that should accept edited messages :param handler_not_edited: handler that should not accept edited messages """ update = make_command_update(message) edited_update = make_command_update(message, edited=True) assert is_match(handler_edited, update) assert is_match(handler_edited, edited_update) assert is_match(handler_not_edited, update) assert not is_match(handler_not_edited, edited_update)
def test_newline(self, dp, command): """Assert that newlines don't interfere with a command handler matching a message""" handler = self.make_default_handler() dp.add_handler(handler) update = make_command_update(command + '\nfoobar') assert is_match(handler, update) assert self.response(dp, update)
async def test_basic(self, app, prefix, command): """Test the basic expected response from a prefix handler""" handler = self.make_default_handler() app.add_handler(handler) text = prefix + command assert await self.response(app, make_message_update(text)) assert not is_match(handler, make_message_update(command)) assert not is_match(handler, make_message_update(prefix + "notacommand")) assert not is_match(handler, make_command_update(f"not {text} at start")) assert not is_match( handler, make_message_update(bot=app.bot, message=None, caption="caption")) handler = PrefixHandler(prefix=["!", "#"], command="cmd", callback=self.callback) assert isinstance(handler.commands, frozenset) assert handler.commands == {"!cmd", "#cmd"} handler = PrefixHandler(prefix="#", command={"cmd", "bmd"}, callback=self.callback) assert isinstance(handler.commands, frozenset) assert handler.commands == {"#cmd", "#bmd"}
async def test_newline(self, app, command): """Assert that newlines don't interfere with a command handler matching a message""" handler = self.make_default_handler() app.add_handler(handler) update = make_command_update(command + "\nfoobar", bot=app.bot) async with app: assert is_match(handler, update) assert await self.response(app, update)
def test_basic(self, dp, prefix, command): """Test the basic expected response from a prefix handler""" handler = self.make_default_handler() dp.add_handler(handler) text = prefix + command assert self.response(dp, make_message_update(text)) assert not is_match(handler, make_message_update(command)) assert not is_match(handler, make_message_update(prefix + 'notacommand')) assert not is_match(handler, make_command_update('not {} at start'.format(text)))
def test_not_choose(monkeypatch): def reply_text(*args, **kwargs): pass def guess_answer(*args, **kwargs): pass def guess_edit_message_text(*args, **kwargs): if step == "do": assert ":未参与" in kwargs['text'] # guess_cmd guessupdate = make_command_update("/guess") monkeypatch.setattr(guessupdate.message, 'reply_text', reply_text) # 加入游戏button addupdate = make_callback_query_update("","guess_start:add") monkeypatch.setattr(addupdate.callback_query, 'answer', guess_answer) monkeypatch.setattr(addupdate.callback_query, 'edit_message_text', guess_edit_message_text) # 开始button startupdate = make_callback_query_update("","guess_start:start") monkeypatch.setattr(startupdate.callback_query, 'answer', guess_answer) monkeypatch.setattr(startupdate.callback_query, 'edit_message_text', guess_edit_message_text) # 大按钮 dupdate = make_callback_query_update("","guess_play:d") monkeypatch.setattr(dupdate.callback_query, 'answer', guess_answer) monkeypatch.setattr(dupdate.callback_query, 'edit_message_text', guess_edit_message_text) # 小按钮 xupdate = make_callback_query_update("","guess_play:x") monkeypatch.setattr(xupdate.callback_query, 'answer', guess_answer) monkeypatch.setattr(xupdate.callback_query, 'edit_message_text', guess_edit_message_text) # 结算按钮 doupdate = make_callback_query_update("","guess_play:do") monkeypatch.setattr(doupdate.callback_query, 'answer', guess_answer) monkeypatch.setattr(doupdate.callback_query, 'edit_message_text', guess_edit_message_text) step = "" # 开始 guesscmd.guess_start(guessupdate,None) # 加入游戏 guesscmd.guess_start_callback(addupdate,None) # 开始游戏 guesscmd.guess_start_callback(startupdate,None) # 直接结束 step = "do" guesscmd.guess_play_callback(doupdate,None)
def test_filters_for_wrong_command(self, mock_filter): """Filters should not be executed if the command does not match the handler""" handler = self.make_default_handler(filters=mock_filter) assert not is_match(handler, make_command_update('/star')) assert not mock_filter.tested
def _test_context_args_or_regex(self, cdp, handler, text): cdp.add_handler(handler) update = make_command_update(text) assert not self.response(cdp, update) update.message.text += ' one two' assert self.response(cdp, update)
async def _test_context_args_or_regex(self, app, handler, text): app.add_handler(handler) update = make_command_update(text, bot=app.bot) assert not await self.response(app, update) update.message.text += " one two" assert await self.response(app, update)
def test_command_list(self, bot): """A command handler with multiple commands registered should respond to all of them.""" handler = CommandHandler(["test", "star"], self.callback_basic) assert is_match(handler, make_command_update("/test", bot=bot)) assert is_match(handler, make_command_update("/star", bot=bot)) assert not is_match(handler, make_command_update("/stop", bot=bot))
def test_command_list(self): """A command handler with multiple commands registered should respond to all of them.""" handler = CommandHandler(['test', 'star'], self.callback_basic) assert is_match(handler, make_command_update('/test')) assert is_match(handler, make_command_update('/star')) assert not is_match(handler, make_command_update('/stop'))
def test_start(monkeypatch): # 测试发出/start命令 def reply_text(*args, **kwargs): assert "猜大小" in args[0] assert kwargs['reply_markup'] == guesscmd.init_replay_markup(guesscmd.start_buttons) update = make_command_update("/guess") monkeypatch.setattr(update.message, 'reply_text', reply_text) guesscmd.guess_start(update,None) # 测试点add按钮 def guess_start_add_answer(*args, **kwargs): # print(f"args:{args}\nkwargs:{kwargs}\n\n") nonlocal step if step == "start": assert args[0] == "加入游戏成功!You joined the game successfully!" step = "join" elif step == "join": assert args[0] == "你已经加入游戏了!You're in the game!" step = "playing" else: assert args[0] == "开局啦" def guess_start_add_edit_message_text(*args, **kwargs): # print(f"args:{args}\nkwargs:{kwargs}\n\n") if step == "playing": assert "first_name:🔴未完成" in kwargs['text'] assert kwargs['reply_markup'] == guesscmd.init_replay_markup(guesscmd.play_buttons) else: assert kwargs['reply_markup'] == guesscmd.init_replay_markup(guesscmd.start_buttons) assert "玩家列表:\nfirst_name" in kwargs['text'] step = "start" addupdate = make_callback_query_update("","guess_start:add") monkeypatch.setattr(addupdate.callback_query, 'answer', guess_start_add_answer) monkeypatch.setattr(addupdate.callback_query, 'edit_message_text', guess_start_add_edit_message_text) # 点第一次add按钮 guesscmd.guess_start_callback(addupdate,None) # 点第二次add按钮 guesscmd.guess_start_callback(addupdate,None) startupdate = make_callback_query_update("","guess_start:start") monkeypatch.setattr(startupdate.callback_query, 'answer', guess_start_add_answer) monkeypatch.setattr(startupdate.callback_query, 'edit_message_text', guess_start_add_edit_message_text) # 点一下start按钮 guesscmd.guess_start_callback(startupdate,None) # 测试点add按钮 def guess_play_answer(*args, **kwargs): # print(f"args:{args}\nkwargs:{kwargs}\n\n") nonlocal step if step == "d": assert args[0] == "你选择了大" elif step == "dd": assert args[0] == "你已经选择了大" elif step == "x": assert args[0] == "你选择了小" elif step == "xx": assert args[0] == "你已经选择了小" elif step == "do": assert args[0] == "结算结果" def guess_play_edit_message_text(*args, **kwargs): # print(f"args:{args}\nkwargs:{kwargs}\n\n") if step == "d": assert "first_name:🔼大" in kwargs['text'] assert kwargs['reply_markup'] == guesscmd.init_replay_markup(guesscmd.play_buttons) elif step == "x": assert "first_name:🔽小" in kwargs['text'] assert kwargs['reply_markup'] == guesscmd.init_replay_markup(guesscmd.play_buttons) elif step == "do": assert "结算结果" in kwargs['text'] # 大按钮 dupdate = make_callback_query_update("","guess_play:d") monkeypatch.setattr(dupdate.callback_query, 'answer', guess_play_answer) monkeypatch.setattr(dupdate.callback_query, 'edit_message_text', guess_play_edit_message_text) # 小按钮 xupdate = make_callback_query_update("","guess_play:x") monkeypatch.setattr(xupdate.callback_query, 'answer', guess_play_answer) monkeypatch.setattr(xupdate.callback_query, 'edit_message_text', guess_play_edit_message_text) # 结算按钮 doupdate = make_callback_query_update("","guess_play:do") monkeypatch.setattr(doupdate.callback_query, 'answer', guess_play_answer) monkeypatch.setattr(doupdate.callback_query, 'edit_message_text', guess_play_edit_message_text) step="d" guesscmd.guess_play_callback(dupdate,None) step="dd" guesscmd.guess_play_callback(dupdate,None) step="x" guesscmd.guess_play_callback(xupdate,None) step="xx" guesscmd.guess_play_callback(xupdate,None) step="do" guesscmd.guess_play_callback(doupdate,None)
def command_update(self, command_message): return make_command_update(command_message)