def test_unban_member(self, monkeypatch, chat, only_if_banned): def make_assertion(*_, **kwargs): chat_id = kwargs['chat_id'] == chat.id user_id = kwargs['user_id'] == 42 o_i_b = kwargs.get('only_if_banned', None) == only_if_banned return chat_id and user_id and o_i_b assert check_shortcut_signature(Chat.unban_member, Bot.unban_chat_member, ['chat_id'], []) assert check_shortcut_call(chat.unban_member, chat.bot, 'unban_chat_member') assert check_defaults_handling(chat.unban_member, chat.bot) monkeypatch.setattr(chat.bot, 'unban_chat_member', make_assertion) assert chat.unban_member(user_id=42, only_if_banned=only_if_banned)
def test_set_permissions(self, monkeypatch, chat): def make_assertion(*_, **kwargs): chat_id = kwargs['chat_id'] == chat.id permissions = kwargs['permissions'] == self.permissions return chat_id and permissions assert check_shortcut_signature(Chat.set_permissions, Bot.set_chat_permissions, ['chat_id'], []) assert check_shortcut_call(chat.set_permissions, chat.bot, 'set_chat_permissions') assert check_defaults_handling(chat.set_permissions, chat.bot) monkeypatch.setattr(chat.bot, 'set_chat_permissions', make_assertion) assert chat.set_permissions(permissions=self.permissions)
def test_answer(self, monkeypatch, inline_query): def make_assertion(*_, **kwargs): return kwargs['inline_query_id'] == inline_query.id assert check_shortcut_signature(InlineQuery.answer, Bot.answer_inline_query, ['inline_query_id'], ['auto_pagination']) assert check_shortcut_call(inline_query.answer, inline_query.bot, 'answer_inline_query') assert check_defaults_handling(inline_query.answer, inline_query.bot) monkeypatch.setattr(inline_query.bot, 'answer_inline_query', make_assertion) assert inline_query.answer(results=[])
def test_kick_member(self, monkeypatch, chat): def make_assertion(*_, **kwargs): chat_id = kwargs['chat_id'] == chat.id user_id = kwargs['user_id'] == 42 until = kwargs['until_date'] == 43 return chat_id and user_id and until assert check_shortcut_signature(Chat.kick_member, Bot.kick_chat_member, ['chat_id'], []) assert check_shortcut_call(chat.kick_member, chat.bot, 'kick_chat_member') assert check_defaults_handling(chat.kick_member, chat.bot) monkeypatch.setattr(chat.bot, 'kick_chat_member', make_assertion) assert chat.kick_member(user_id=42, until_date=43)
def test_instance_method_send_copy(self, monkeypatch, user): def make_assertion(*_, **kwargs): user_id = kwargs['chat_id'] == user.id message_id = kwargs['message_id'] == 'message_id' from_chat_id = kwargs['from_chat_id'] == 'from_chat_id' return from_chat_id and message_id and user_id assert check_shortcut_signature(User.send_copy, Bot.copy_message, ['chat_id'], []) assert check_shortcut_call(user.copy_message, user.bot, 'copy_message') assert check_defaults_handling(user.copy_message, user.bot) monkeypatch.setattr(user.bot, 'copy_message', make_assertion) assert user.send_copy(from_chat_id='from_chat_id', message_id='message_id')
def test_send_action(self, monkeypatch, chat): def make_assertion(*_, **kwargs): id_ = kwargs['chat_id'] == chat.id action = kwargs['action'] == ChatAction.TYPING return id_ and action assert check_shortcut_signature(chat.send_action, Bot.send_chat_action, ['chat_id'], []) assert check_shortcut_call(chat.send_action, chat.bot, 'send_chat_action') assert check_defaults_handling(chat.send_action, chat.bot) monkeypatch.setattr(chat.bot, 'send_chat_action', make_assertion) assert chat.send_action(action=ChatAction.TYPING) assert chat.send_action(action=ChatAction.TYPING)
def test_get_file_instance_method(self, monkeypatch, passport_file): def make_assertion(*_, **kwargs): result = kwargs['file_id'] == passport_file.file_id # we need to be a bit hacky here, b/c PF.get_file needs Bot.get_file to return a File return File(file_id=result, file_unique_id=result) assert check_shortcut_signature(PassportFile.get_file, Bot.get_file, ['file_id'], []) assert check_shortcut_call(passport_file.get_file, passport_file.bot, 'get_file') assert check_defaults_handling(passport_file.get_file, passport_file.bot) monkeypatch.setattr(passport_file.bot, 'get_file', make_assertion) assert passport_file.get_file().file_id == 'True'
def test_answer(self, monkeypatch, shipping_query): def make_assertion(*_, **kwargs): return kwargs['shipping_query_id'] == shipping_query.id assert check_shortcut_signature(ShippingQuery.answer, Bot.answer_shipping_query, ['shipping_query_id'], []) assert check_shortcut_call(shipping_query.answer, shipping_query.bot, 'answer_shipping_query') assert check_defaults_handling(shipping_query.answer, shipping_query.bot) monkeypatch.setattr(shipping_query.bot, 'answer_shipping_query', make_assertion) assert shipping_query.answer(ok=True)
def test_answer(self, monkeypatch, callback_query): def make_assertion(*_, **kwargs): return kwargs['callback_query_id'] == callback_query.id assert check_shortcut_signature( CallbackQuery.answer, Bot.answer_callback_query, ['callback_query_id'], [] ) assert check_shortcut_call( callback_query.answer, callback_query.bot, 'answer_callback_query' ) assert check_defaults_handling(callback_query.answer, callback_query.bot) monkeypatch.setattr(callback_query.bot, 'answer_callback_query', make_assertion) # TODO: PEP8 assert callback_query.answer()
def test_promote_member(self, monkeypatch, chat, is_anonymous): def make_assertion(*_, **kwargs): chat_id = kwargs['chat_id'] == chat.id user_id = kwargs['user_id'] == 42 o_i_b = kwargs.get('is_anonymous') == is_anonymous return chat_id and user_id and o_i_b assert check_shortcut_signature( Chat.promote_member, Bot.promote_chat_member, ['chat_id'], [] ) assert check_shortcut_call(chat.promote_member, chat.bot, 'promote_chat_member') assert check_defaults_handling(chat.promote_member, chat.bot) monkeypatch.setattr(chat.bot, 'promote_chat_member', make_assertion) assert chat.promote_member(user_id=42, is_anonymous=is_anonymous)
def test_instance_method_decline_join_request(self, monkeypatch, user): def make_assertion(*_, **kwargs): chat_id = kwargs['chat_id'] == 'chat_id' user_id = kwargs['user_id'] == user.id return chat_id and user_id assert check_shortcut_signature(User.decline_join_request, Bot.decline_chat_join_request, ['user_id'], []) assert check_shortcut_call(user.decline_join_request, user.bot, 'decline_chat_join_request') assert check_defaults_handling(user.decline_join_request, user.bot) monkeypatch.setattr(user.bot, 'decline_chat_join_request', make_assertion) assert user.decline_join_request(chat_id='chat_id')
def test_edit_invite_link(self, monkeypatch, chat): link = "ThisIsALink" def make_assertion(*_, **kwargs): return kwargs['chat_id'] == chat.id and kwargs[ 'invite_link'] == link assert check_shortcut_signature(Chat.edit_invite_link, Bot.edit_chat_invite_link, ['chat_id'], []) assert check_shortcut_call(chat.edit_invite_link, chat.bot, 'edit_chat_invite_link') assert check_defaults_handling(chat.edit_invite_link, chat.bot) monkeypatch.setattr(chat.bot, 'edit_chat_invite_link', make_assertion) assert chat.edit_invite_link(invite_link=link)
def test_answer(self, monkeypatch, pre_checkout_query): def make_assertion(*_, **kwargs): return kwargs['pre_checkout_query_id'] == pre_checkout_query.id assert check_shortcut_signature( PreCheckoutQuery.answer, Bot.answer_pre_checkout_query, ['pre_checkout_query_id'], [] ) assert check_shortcut_call( pre_checkout_query.answer, pre_checkout_query.bot, 'answer_pre_checkout_query', ) assert check_defaults_handling(pre_checkout_query.answer, pre_checkout_query.bot) monkeypatch.setattr(pre_checkout_query.bot, 'answer_pre_checkout_query', make_assertion) assert pre_checkout_query.answer(ok=True)
def test_decline(self, monkeypatch, chat_join_request): def make_assertion(*_, **kwargs): chat_id_test = kwargs['chat_id'] == chat_join_request.chat.id user_id_test = kwargs['user_id'] == chat_join_request.from_user.id return chat_id_test and user_id_test assert check_shortcut_signature( ChatJoinRequest.decline, Bot.decline_chat_join_request, ['chat_id', 'user_id'], [] ) assert check_shortcut_call( chat_join_request.decline, chat_join_request.bot, 'decline_chat_join_request' ) assert check_defaults_handling(chat_join_request.decline, chat_join_request.bot) monkeypatch.setattr(chat_join_request.bot, 'decline_chat_join_request', make_assertion) assert chat_join_request.decline()
def test_restrict_member(self, monkeypatch, chat): permissions = ChatPermissions(True, False, True, False, True, False, True, False) def make_assertion(*_, **kwargs): chat_id = kwargs['chat_id'] == chat.id user_id = kwargs['user_id'] == 42 o_i_b = kwargs.get('permissions') == permissions return chat_id and user_id and o_i_b assert check_shortcut_signature( Chat.restrict_member, Bot.restrict_chat_member, ['chat_id'], [] ) assert check_shortcut_call(chat.restrict_member, chat.bot, 'restrict_chat_member') assert check_defaults_handling(chat.restrict_member, chat.bot) monkeypatch.setattr(chat.bot, 'restrict_chat_member', make_assertion) assert chat.restrict_member(user_id=42, permissions=permissions)
def test_pin_message(self, monkeypatch, callback_query): if callback_query.inline_message_id: pytest.skip("Can't pin inline messages") def make_assertion(*args, **kwargs): return kwargs['chat_id'] == callback_query.message.chat_id assert check_shortcut_signature( CallbackQuery.pin_message, Bot.pin_chat_message, ['message_id', 'chat_id'], [], ) assert check_shortcut_call( callback_query.pin_message, callback_query.bot, 'pin_chat_message' ) assert check_defaults_handling(callback_query.pin_message, callback_query.bot) monkeypatch.setattr(callback_query.bot, 'pin_chat_message', make_assertion) assert callback_query.pin_message()
def test_copy_message(self, monkeypatch, callback_query): if callback_query.inline_message_id: pytest.skip("Can't copy inline messages") def make_assertion(*args, **kwargs): id_ = kwargs['from_chat_id'] == callback_query.message.chat_id chat_id = kwargs['chat_id'] == 1 message = kwargs['message_id'] == callback_query.message.message_id return id_ and message and chat_id assert check_shortcut_signature( CallbackQuery.copy_message, Bot.copy_message, ['message_id', 'from_chat_id'], [], ) assert check_shortcut_call(callback_query.copy_message, callback_query.bot, 'copy_message') assert check_defaults_handling(callback_query.copy_message, callback_query.bot) monkeypatch.setattr(callback_query.bot, 'copy_message', make_assertion) assert callback_query.copy_message(1)
def test_instance_method_send_invoice(self, monkeypatch, user): def make_assertion(*_, **kwargs): title = kwargs['title'] == 'title' description = kwargs['description'] == 'description' payload = kwargs['payload'] == 'payload' provider_token = kwargs['provider_token'] == 'provider_token' currency = kwargs['currency'] == 'currency' prices = kwargs['prices'] == 'prices' args = title and description and payload and provider_token and currency and prices return kwargs['chat_id'] == user.id and args assert check_shortcut_signature(User.send_invoice, Bot.send_invoice, ['chat_id'], []) assert check_shortcut_call(user.send_invoice, user.bot, 'send_invoice') assert check_defaults_handling(user.send_invoice, user.bot) monkeypatch.setattr(user.bot, 'send_invoice', make_assertion) assert user.send_invoice( 'title', 'description', 'payload', 'provider_token', 'currency', 'prices', )
def test_get_game_high_scores(self, monkeypatch, callback_query): def make_assertion(*_, **kwargs): user_id = kwargs.get('user_id') == 1 ids = self.check_passed_ids(callback_query, kwargs) return ids and user_id assert check_shortcut_signature( CallbackQuery.get_game_high_scores, Bot.get_game_high_scores, ['inline_message_id', 'message_id', 'chat_id'], [], ) assert check_shortcut_call( callback_query.get_game_high_scores, callback_query.bot, 'get_game_high_scores', skip_params=self.skip_params(callback_query), shortcut_kwargs=self.shortcut_kwargs(callback_query), ) assert check_defaults_handling(callback_query.get_game_high_scores, callback_query.bot) monkeypatch.setattr(callback_query.bot, 'get_game_high_scores', make_assertion) assert callback_query.get_game_high_scores(user_id=1) assert callback_query.get_game_high_scores(1)
def test_stop_message_live_location(self, monkeypatch, callback_query): def make_assertion(*_, **kwargs): ids = self.check_passed_ids(callback_query, kwargs) return ids assert check_shortcut_signature( CallbackQuery.stop_message_live_location, Bot.stop_message_live_location, ['inline_message_id', 'message_id', 'chat_id'], [], ) assert check_shortcut_call( callback_query.stop_message_live_location, callback_query.bot, 'stop_message_live_location', skip_params=self.skip_params(callback_query), shortcut_kwargs=self.shortcut_kwargs(callback_query), ) assert check_defaults_handling( callback_query.stop_message_live_location, callback_query.bot ) monkeypatch.setattr(callback_query.bot, 'stop_message_live_location', make_assertion) assert callback_query.stop_message_live_location()
def test_edit_message_media(self, monkeypatch, callback_query): def make_assertion(*_, **kwargs): message_media = kwargs.get('media') == [['1', '2']] ids = self.check_passed_ids(callback_query, kwargs) return ids and message_media assert check_shortcut_signature( CallbackQuery.edit_message_media, Bot.edit_message_media, ['inline_message_id', 'message_id', 'chat_id'], [], ) assert check_shortcut_call( callback_query.edit_message_media, callback_query.bot, 'edit_message_media', skip_params=self.skip_params(callback_query), shortcut_kwargs=self.shortcut_kwargs(callback_query), ) assert check_defaults_handling(callback_query.edit_message_media, callback_query.bot) monkeypatch.setattr(callback_query.bot, 'edit_message_media', make_assertion) assert callback_query.edit_message_media(media=[['1', '2']]) assert callback_query.edit_message_media([['1', '2']])
def test_edit_message_caption(self, monkeypatch, callback_query): def make_assertion(*_, **kwargs): caption = kwargs['caption'] == 'new caption' ids = self.check_passed_ids(callback_query, kwargs) return ids and caption assert check_shortcut_signature( CallbackQuery.edit_message_caption, Bot.edit_message_caption, ['inline_message_id', 'message_id', 'chat_id'], [], ) assert check_shortcut_call( callback_query.edit_message_caption, callback_query.bot, 'edit_message_caption', skip_params=self.skip_params(callback_query), shortcut_kwargs=self.shortcut_kwargs(callback_query), ) assert check_defaults_handling(callback_query.edit_message_caption, callback_query.bot) monkeypatch.setattr(callback_query.bot, 'edit_message_caption', make_assertion) assert callback_query.edit_message_caption(caption='new caption') assert callback_query.edit_message_caption('new caption')
def test_edit_message_text(self, monkeypatch, callback_query): def make_assertion(*_, **kwargs): text = kwargs['text'] == 'test' ids = self.check_passed_ids(callback_query, kwargs) return ids and text assert check_shortcut_signature( CallbackQuery.edit_message_text, Bot.edit_message_text, ['inline_message_id', 'message_id', 'chat_id'], [], ) assert check_shortcut_call( callback_query.edit_message_text, callback_query.bot, 'edit_message_text', skip_params=self.skip_params(callback_query), shortcut_kwargs=self.shortcut_kwargs(callback_query), ) assert check_defaults_handling(callback_query.edit_message_text, callback_query.bot) monkeypatch.setattr(callback_query.bot, 'edit_message_text', make_assertion) assert callback_query.edit_message_text(text='test') assert callback_query.edit_message_text('test')