def test_one_label(self, conversation): label = "label" sticker = telegram_factories.StickerFactory() conversation.sticker = sticker user = telegram_factories.UserFactory() conversation.user = user update = telegram_factories.MessageUpdateFactory( message__text=label, message__from_user=user) with mock.patch(*get_or_create(conversation)): run_handler(stickertaggerbot.handlers.labels.create_labels_handler, update) assert message.Message.set_content.call_count == 2 assert message.Message.send.call_count == 2 first_call_contents, second_call_contents = \ message.Message.set_content.call_args_list assert first_call_contents[0][1] == sticker assert second_call_contents[0][1] == \ message.Text.Instruction.CONFIRM.value + label
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)
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)
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)
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)
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)
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)
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)
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)
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)