コード例 #1
0
    def test_confirm(self, conversation, sticker, callback_query_update):
        conversation.sticker = sticker
        conversation.labels = ["label1", "label2", "label3"]
        conversation.change_state = mock.MagicMock(autospec=True,
                                                   return_value=True)
        update = callback_query_update(
            conversation,
            stickertaggerbot.callback_data.CallbackData.ButtonText.CONFIRM)

        with mock.patch(base_patch_path_labels + ".User.id_exists",
                        mock.MagicMock(return_value=False)):
            database_user = \
                callbacks.labels_callback.models.User.from_telegram_user(
                    conversation.user, conversation.chat.id)

        # TODO: Use a more elegant way to do this
        with mock.patch(base_patch_path_labels + ".User.get",
                        mock.MagicMock(autospec=True,
                                       return_value=database_user)), \
             mock.patch(*get_or_create(conversation)), \
             mock.patch(base_patch_path_labels + ".Association",
                        mock.MagicMock(autospec=True)), \
             mock.patch(base_patch_path_labels + ".database.session.commit",
                        mock.MagicMock(autospec=True)):
            run_handler(callbacks.create_callback_handler, update)

        time.sleep(2)
        clear_all_tables()

        assert_sent_message_once(message.Text.Other.SUCCESS)
コード例 #2
0
    def test_empty_labels(self, conversation):
        conversation.labels = None
        update = telegram_factories.MessageUpdateFactory(message__text="")

        with mock.patch(*get_or_create(conversation)):
            run_handler(stickertaggerbot.handlers.labels.create_labels_handler,
                        update)

        assert_sent_message_once(message.Text.Error.LABEL_MISSING)
コード例 #3
0
    def test_no_conversation(self):
        update = telegram_factories.MessageUpdateFactory(
            message__text="message")

        with mock.patch(*get_or_create(None)):
            run_handler(stickertaggerbot.handlers.labels.create_labels_handler,
                        update)

        assert_sent_message_once(message.Text.Error.NOT_STARTED)
コード例 #4
0
    def test_new_user(self):
        update = telegram_factories.CommandUpdateFactory(
            message__command="start")
        user_id = update.effective_user.id

        run_handler(start.create_command_start_handler, update)

        start.models.User.get.assert_called_once_with(user_id)
        start.models.User.add_to_database.assert_called_once_with()
        assert_sent_message_once(message.Text.Instruction.START)
コード例 #5
0
    def test_cancel(self, conversation, sticker, callback_query_update):
        conversation.sticker = sticker

        update = callback_query_update(
            conversation,
            stickertaggerbot.callback_data.CallbackData.ButtonText.CANCEL)

        with mock.patch(*get_or_create(conversation)):
            run_handler(callbacks.create_callback_handler, update)

        assert_sent_message_once(message.Text.Instruction.RE_LABEL)
コード例 #6
0
    def test_interrupted_conversation(self, conversation):
        conversation.change_state = mock.Mock(
            autospec=True, side_effect=ValueError(States.IDLE))
        update = telegram_factories.MessageUpdateFactory(
            message__from_user=conversation.user)

        with mock.patch(*get_or_create(conversation)):
            run_handler(stickertaggerbot.handlers.labels.create_labels_handler,
                        update)

        assert_sent_message_once(message.Text.Error.RESTART)
コード例 #7
0
    def test_new_sticker(self, update_maker, conversation):
        update = update_maker(conversation)

        with mock.patch(*get_or_create(conversation)):
            run_handler(
                stickertaggerbot.handlers.sticker.create_sticker_handler,
                update)

        assert conversation.sticker == update.effective_message.sticker
        conversation.change_state.assert_called_once()
        assert conversation.rollback_state.call_args is None
        assert_sent_message_once(message.Text.Instruction.LABEL)
コード例 #8
0
    def test_future_timed_out(self, update_maker, conversation):
        update = update_maker(conversation)
        conversation.get_future_result = mock.MagicMock(autospec=True,
                                                        return_value=None)

        with mock.patch(*get_or_create(conversation)):
            run_handler(
                stickertaggerbot.handlers.sticker.create_sticker_handler,
                update)

        conversation.change_state.assert_called_once()
        conversation.rollback_state.assert_called_once()
        assert_sent_message_once(message.Text.Error.UNKNOWN)
コード例 #9
0
    def test_sticker_exists(self, update_maker, conversation):
        update = update_maker(conversation)

        conversation.get_future_result = mock.MagicMock(return_value=False)

        with mock.patch(*get_or_create(conversation)):
            run_handler(
                stickertaggerbot.handlers.sticker.create_sticker_handler,
                update)

        conversation.change_state.assert_called_once()
        conversation.rollback_state.assert_called_once()
        assert_sent_message_once(message.Text.Error.STICKER_EXISTS)
コード例 #10
0
    def test_interrupted_conversation(self, update_maker, conversation):
        update = update_maker(conversation)

        conversation.change_state = mock.MagicMock(autospec=True,
                                                   side_effect=ValueError(
                                                       States.IDLE))

        with mock.patch(*get_or_create(conversation)):
            run_handler(
                stickertaggerbot.handlers.sticker.create_sticker_handler,
                update)

        conversation.change_state.assert_called_once()
        assert conversation.rollback_state.call_args is None
        assert_sent_message_once(message.Text.Error.RESTART)
コード例 #11
0
    def test_help(self):
        update = telegram_factories.CommandUpdateFactory(
            message__command="help")

        run_handler(start.create_command_start_handler, update)
        assert_sent_message_once(message.Text.Instruction.HELP)