def test_reply(self, mocked_send_message: Mock): bot = Bot.objects.create( name="bot", token="token", ) chat = Chat.objects.create( bot=bot, chat_id=142, type='private', ) incoming_message = Message.objects.create( message_id=42, date=timezone.datetime(2000, 1, 1, tzinfo=timezone.utc), chat=chat, ) mocked_send_message.return_value = TelegramMessage( message_id=43, date=timezone.datetime(1999, 12, 31, tzinfo=timezone.utc), from_user=TelegramUser(id=1, is_bot=True), chat=TelegramChat(id=142, type="private"), text='Reply', reply_to_message=TelegramMessage( message_id=42, chat=TelegramChat(id=142, type="private"), date=timezone.datetime(2000, 1, 1, tzinfo=timezone.utc), ), ) incoming_message.reply( text="Reply", reply=True, ) mocked_send_message.assert_called_with( chat_id=chat.chat_id, text="Reply", parse_mode=None, reply_to_message_id=42, ) user = User.objects.first() message = Message.objects.last() self.assertEqual(message.direction, Message.DIRECTION_OUT) self.assertEqual(message.message_id, 43) self.assertEqual(message.date, timezone.datetime(1999, 12, 31, tzinfo=timezone.utc)) self.assertEqual(message.chat, chat) self.assertEqual(message.from_user, user) self.assertEqual(message.reply_to_message, incoming_message) self.assertEqual(incoming_message.reply_message, message)
def test_from_telegram(self): telegram_callback_query = TelegramCallbackQuery( id="4382bfdwdsb323b2d9", data="Data from button callback", chat_instance="42a", from_user=TelegramUser( id=1111111, is_bot=False, username="******", first_name="Test Firstname", last_name="Test Lastname", ), message=TelegramMessage( message_id=42, date=timezone.datetime(2000, 1, 1, tzinfo=timezone.utc), chat=TelegramChat(id=42, type="private"), from_user=TelegramUser(id=40, is_bot=False), ) ) CallbackQuery.objects.from_telegram( bot=self.bot, telegram_callback_query=telegram_callback_query, ) callback_query = CallbackQuery.objects.first() user = User.objects.get(user_id=1111111) message = Message.objects.first() self.assertEqual( callback_query.callback_query_id, telegram_callback_query.id) self.assertEqual(callback_query.from_user, user) self.assertEqual(callback_query.message, message) self.assertEqual(callback_query.chat.chat_id, 42) self.assertEqual(callback_query.text, "Data from button callback")
def test_from_telegram__edited_channel_post(self): telegram_update = TelegramUpdate( update_id=40, edited_channel_post=TelegramMessage( message_id=41, chat=TelegramChat(id=-42, type='channel', title='the_channel'), date=timezone.datetime(2000, 1, 1, tzinfo=timezone.utc), sender_chat=TelegramChat( id=-42, type='channel', title='the_channel' ), text='post' ) ) Update.objects.from_telegram( bot=self.bot, telegram_update=telegram_update, ) update = Update.objects.first() self.assertEqual(update.update_id, telegram_update.update_id) self.assertEqual(update.type, Update.TYPE_EDITED_CHANNEL_POST)
def test_from_telegram(self): photo = ChatPhoto( small_file_id='small_file_id', small_file_unique_id='small_file_unique_id', big_file_id='big_file_id', big_file_unique_id='big_file_unique_id', ) permissions = ChatPermissions(can_send_messages=True) location = ChatLocation( location=Location(longitude=10.5, latitude=62.8), address='address', ) telegram_chat = TelegramChat( id=1, type='private', title='title', username='******', first_name='first_name', last_name='last_name', photo=photo, bio='bio', description='description', invite_link='invite_link', permissions=permissions, slow_mode_delay=1, sticker_set_name='sticker_set_name', can_set_sticker_set=True, linked_chat_id=1, location=location, ) Chat.objects.from_telegram( telegram_chat=telegram_chat, bot=self.bot ) chat = Chat.objects.first() self.assertEqual(chat.chat_id, telegram_chat.id) self.assertEqual(chat.type, telegram_chat.type) self.assertEqual(chat.username, telegram_chat.username) self.assertEqual(chat.first_name, telegram_chat.first_name) self.assertEqual(chat.last_name, telegram_chat.last_name) self.assertEqual(chat.photo, telegram_chat.photo) self.assertEqual(chat.bio, telegram_chat.bio) self.assertEqual(chat.description, telegram_chat.description) self.assertEqual(chat.invite_link, telegram_chat.invite_link) self.assertEqual(chat.permissions, telegram_chat.permissions) self.assertEqual(chat.slow_mode_delay, telegram_chat.slow_mode_delay) self.assertEqual(chat.sticker_set_name, telegram_chat.sticker_set_name) self.assertEqual(chat.can_set_sticker_set, telegram_chat.can_set_sticker_set) self.assertEqual(chat.linked_chat_id, telegram_chat.linked_chat_id) self.assertEqual(chat.location, telegram_chat.location)
def test_edit(self, mocked_edit_message_text: Mock): old_text = "old text" new_text = "new text" old_markup = InlineKeyboardMarkup(inline_keyboard=[[ InlineKeyboardButton("Yes", callback_data="yes"), InlineKeyboardButton("No", callback_data="no"), ]]) new_markup = InlineKeyboardMarkup(inline_keyboard=[[ InlineKeyboardButton("Yes and yes", callback_data="yes"), ]]) bot = Bot.objects.create( name="bot", token="token", ) chat = Chat.objects.create( bot=bot, chat_id=142, type="private", ) message = Message.objects.create( message_id=42, date=timezone.datetime(1999, 12, 31, tzinfo=timezone.utc), chat=chat, text=old_text, _reply_markup=old_markup.to_dict(), ) mocked_edit_message_text.return_value = TelegramMessage( message_id=42, date=timezone.datetime(1999, 12, 31, tzinfo=timezone.utc), chat=TelegramChat(id=142, type="private"), text=new_text, reply_markup=new_markup, ) returned = message.edit( text=new_text, reply_markup=new_markup, ) mocked_edit_message_text.assert_called_with( text=new_text, chat_id=chat.chat_id, message_id=message.message_id, parse_mode=None, entities=None, disable_web_page_preview=None, reply_markup=new_markup, ) message.refresh_from_db() self.assertEqual(message.reply_markup, new_markup) self.assertEqual(message.text, new_text) self.assertEqual(message, returned)
def test_from_telegram(self): photo = ChatPhoto( small_file_id="small_file_id", small_file_unique_id="small_file_unique_id", big_file_id="big_file_id", big_file_unique_id="big_file_unique_id", ) permissions = ChatPermissions(can_send_messages=True) location = ChatLocation( location=Location(longitude=10.5, latitude=62.8), address="address", ) telegram_chat = TelegramChat( id=1, type="private", title="title", username="******", first_name="first_name", last_name="last_name", photo=photo, bio="bio", description="description", invite_link="invite_link", permissions=permissions, slow_mode_delay=1, sticker_set_name="sticker_set_name", can_set_sticker_set=True, linked_chat_id=1, location=location, ) Chat.objects.from_telegram(telegram_chat=telegram_chat, bot=self.bot) chat = Chat.objects.first() self.assertEqual(chat.chat_id, telegram_chat.id) self.assertEqual(chat.type, telegram_chat.type) self.assertEqual(chat.username, telegram_chat.username) self.assertEqual(chat.first_name, telegram_chat.first_name) self.assertEqual(chat.last_name, telegram_chat.last_name) self.assertEqual(chat.photo, telegram_chat.photo) self.assertEqual(chat.bio, telegram_chat.bio) self.assertEqual(chat.description, telegram_chat.description) self.assertEqual(chat.invite_link, telegram_chat.invite_link) self.assertEqual(chat.permissions, telegram_chat.permissions) self.assertEqual(chat.slow_mode_delay, telegram_chat.slow_mode_delay) self.assertEqual(chat.sticker_set_name, telegram_chat.sticker_set_name) self.assertEqual(chat.can_set_sticker_set, telegram_chat.can_set_sticker_set) self.assertEqual(chat.linked_chat_id, telegram_chat.linked_chat_id) self.assertEqual(chat.location, telegram_chat.location)
def test_from_telegram(self): animation = Animation( file_id="1", file_unique_id="1", width=1, height=1, duration=1, ) reply_markup = InlineKeyboardMarkup( inline_keyboard=[[InlineKeyboardButton(text="button")]] ) telegram_message = TelegramMessage( message_id=42, date=timezone.datetime(2000, 1, 1, tzinfo=timezone.utc), chat=TelegramChat(id=42, type="private"), from_user=TelegramUser(id=40, is_bot=False), animation=animation, reply_markup=reply_markup, left_chat_member=TelegramUser(id=41, is_bot=False), new_chat_members=[ TelegramUser(id=42, is_bot=False), TelegramUser(id=43, is_bot=False), ] ) direction = Message.DIRECTION_OUT Message.objects.from_telegram( bot=self.bot, telegram_message=telegram_message, direction=direction ) chat = Chat.objects.first() self.assertEqual(chat.chat_id, 42) self.assertEqual(chat.type, "private") user = User.objects.get(user_id=40) self.assertEqual(user.is_bot, False) message = Message.objects.first() self.assertEqual(message.direction, Message.DIRECTION_OUT) self.assertEqual(message.message_id, 42) self.assertEqual(message.chat, chat) self.assertEqual(message.from_user, user) self.assertEqual(message.animation, animation) self.assertEqual(message.reply_markup, reply_markup) self.assertEqual(message.left_chat_member.user_id, 41) new_chat_member_1 = User.objects.get(user_id=42) new_chat_member_2 = User.objects.get(user_id=43) self.assertIn(new_chat_member_1, message.new_chat_members.all()) self.assertIn(new_chat_member_2, message.new_chat_members.all())
def test_get_message__wrong_chat_id(self): chat = Chat.objects.create(bot=self.bot, chat_id=42, type="private") wanted = Message.objects.create( message_id=42, date=timezone.datetime(2000, 1, 1, tzinfo=timezone.utc), chat=chat, ) found = Message.objects.get_message(telegram_message=TelegramMessage( message_id=wanted.message_id, date=timezone.datetime(1999, 12, 31, tzinfo=timezone.utc), chat=TelegramChat(id=999, type="private"), )) self.assertEqual(found, None)
def test_edit_reply_markup(self, mocked_edit_message_reply_markup: Mock): old_markup = InlineKeyboardMarkup(inline_keyboard=[[ InlineKeyboardButton("Yes", callback_data="yes"), InlineKeyboardButton("No", callback_data="no"), ]]) new_markup = InlineKeyboardMarkup(inline_keyboard=[[ InlineKeyboardButton("Yes and yes", callback_data="yes"), ]]) bot = Bot.objects.create( name="bot", token="token", ) chat = Chat.objects.create( bot=bot, chat_id=142, type="private", ) message = Message.objects.create( message_id=42, date=timezone.datetime(1999, 12, 31, tzinfo=timezone.utc), chat=chat, _reply_markup=old_markup.to_dict(), ) mocked_edit_message_reply_markup.return_value = TelegramMessage( message_id=42, date=timezone.datetime(1999, 12, 31, tzinfo=timezone.utc), chat=TelegramChat(id=142, type="private"), reply_markup=new_markup, ) returned = message.edit_reply_markup(reply_markup=new_markup, ) mocked_edit_message_reply_markup.assert_called_with( chat_id=chat.chat_id, message_id=message.message_id, reply_markup=new_markup) message.refresh_from_db() self.assertEqual(message.reply_markup, new_markup) self.assertEqual(message, returned)
def test_from_telegram__message(self): telegram_update = TelegramUpdate( update_id=40, message=TelegramMessage( message_id=41, chat=TelegramChat(id=42, type='private'), date=timezone.datetime(2000, 1, 1, tzinfo=timezone.utc), ) ) Update.objects.from_telegram( telegram_update=telegram_update, bot=self.bot, ) update = Update.objects.first() chat = Chat.objects.first() message = Message.objects.first() self.assertEqual(update.update_id, telegram_update.update_id) self.assertEqual(update.message, message) self.assertEqual(message.chat, chat)