M4R774 / bobweb2

Telegram botti piristämään elämää
MIT License
8 stars 1 forks source link

Better test tools for chats and bot interaction #185

Closed Latemus closed 1 year ago

Latemus commented 1 year ago

Problem:

Mock classes defined in test_utils.py are inconsistent with the way that python-telegram-bot api works. The initial work on the mocks is based on wrong assumption on which later additions have been added. For example Current MockUpdate is mostly used in a way, where each message from user shares same update even though in the real python-telegram-bot api each message is contained in it's own message. This causes weird situtations when testing more complex systems and need for better test framework came when DailyQuestion was implemented.

For example a snippet from DailyQuestion related test:

    def test_reply_to_daily_question_is_saved_as_answer(self):
        populate_season_with_dq_and_answer()
        TelegramUser.objects.create(id=3, username='3')

        chat = Chat.objects.get(id=1337)
        user3 = TelegramUser.objects.get(id=3)
        mock_dq_message = MockMessage(chat)
        mock_dq_message.message_id = 1
        message = MockMessage(chat)
        message.from_user = user3
        update = MockUpdate(message)
        update.effective_user = user3
        update.effective_message.reply_to_message = mock_dq_message
        update.send_text("a3")

        answers = list(DailyQuestionAnswer.objects.filter(answer_author__id=3))
        self.assertEqual(1, len(answers))
        self.assertEqual('a3', answers[0].content)

When it could be something like:

    def test_reply_to_daily_question_is_saved_as_answer_v2(self):
        chat = simple_mock_chat().with_user()
        chat.create_season()
        dq = chat.create_dq()

        chat.get_user().send_reply_to(dq.get_tg_msg(), 'a3')

        answers = list(DailyQuestionAnswer.objects.filter(answer_author__id=chat.get_user().id))
        self.assertEqual(1, len(answers))
        self.assertEqual('a3', answers[0].content)

So at the moment the tests are messy and hard to upkeep and they do not provide enough abstraction to make it easy to write tests without having exact knowledge of the python-telegram-bot or implementation of current message_handler.

Solution

Easy to use test tools that follows api provided by python-telegram-bot framework

Requirements: