示例#1
0
    def test_send_callback_query_default_message(self,
                                                 mocked_next_update_id: Mock,
                                                 mocked_next_message_id: Mock,
                                                 mocked_now: Mock,
                                                 mocked_dispatcher: Mock):
        now = timezone.datetime(2000, 1, 1, tzinfo=timezone.utc)
        mocked_now.return_value = now
        mocked_next_message_id.return_value = 42
        mocked_next_update_id.return_value = 142
        test_user = MockUser(self.bot, username='******')
        message = Message.objects.create(
            chat=test_user.chat,
            date=timezone.now(),
            message_id=test_user._next_message_id(),
            text='Yes of No?',
            _reply_markup=types.InlineKeyboardMarkup(inline_keyboard=[[
                types.InlineKeyboardButton("Yes", callback_data="yes")
            ], [types.InlineKeyboardButton("No", callback_data="no")
                ]]).to_dict())

        test_user.send_callback_query(data='yes')

        user = types.User(id=START_USER_ID,
                          is_bot=False,
                          username='******',
                          first_name='u',
                          last_name='u')
        chat = types.Chat(id=START_USER_ID,
                          type='private',
                          username='******',
                          first_name='u',
                          last_name='u')
        message = types.Message(
            message_id=message.message_id,
            date=message.date,
            chat=chat,
        )
        callback_query = types.CallbackQuery(id='1',
                                             from_user=user,
                                             chat_instance='1',
                                             message=message,
                                             data='yes')

        update = types.Update(update_id=142, callback_query=callback_query)
        update_data = update.to_dict(date_as_timestamp=True)
        test_user.dispatcher.dispatch.assert_called_with(
            update_data=update_data)
示例#2
0
    def test_next_message_id(self, mocked_dispatcher: Mock):
        """chat_id + message_id must be unique"""
        mock_user = MockUser(bot=self.bot)
        another_chat = Chat.objects.create(
            bot=self.another_bot,
            chat_id=18,
            type='private',
        )
        now = timezone.now()
        Message.objects.create(message_id=7,
                               chat=mock_user.chat,
                               date=now,
                               text="message_1")
        Message.objects.create(message_id=8,
                               chat=mock_user.chat,
                               date=now,
                               text="message_2_2")
        Message.objects.create(message_id=9,
                               chat=another_chat,
                               date=now,
                               text="message_2_1")

        self.assertEqual(mock_user._next_message_id(), 9)