|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# A library that provides a Python interface to the Telegram Bot API |
| 4 | +# Copyright (C) 2015-2018 |
| 5 | +# Leandro Toledo de Souza <devs@python-telegram-bot.org> |
| 6 | +# |
| 7 | +# This program is free software: you can redistribute it and/or modify |
| 8 | +# it under the terms of the GNU Lesser Public License as published by |
| 9 | +# the Free Software Foundation, either version 3 of the License, or |
| 10 | +# (at your option) any later version. |
| 11 | +# |
| 12 | +# This program is distributed in the hope that it will be useful, |
| 13 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | +# GNU Lesser Public License for more details. |
| 16 | +# |
| 17 | +# You should have received a copy of the GNU Lesser Public License |
| 18 | +# along with this program. If not, see [http://www.gnu.org/licenses/]. |
| 19 | +import logging |
| 20 | +from collections import defaultdict |
| 21 | + |
| 22 | +import pytest |
| 23 | + |
| 24 | +from telegram import Update, Message, User, Chat |
| 25 | +from telegram.ext import BasePersistence, Updater, ConversationHandler, MessageHandler, Filters |
| 26 | + |
| 27 | + |
| 28 | +@pytest.fixture(scope="function") |
| 29 | +def base_persistence(): |
| 30 | + return BasePersistence(store_chat_data=True, store_user_data=True) |
| 31 | + |
| 32 | + |
| 33 | +@pytest.fixture(scope="function") |
| 34 | +def chat_data(): |
| 35 | + return defaultdict(dict, {-12345: {'test1': 'test2'}, -67890: {'test3': 'test4'}}) |
| 36 | + |
| 37 | + |
| 38 | +@pytest.fixture(scope="function") |
| 39 | +def user_data(): |
| 40 | + return defaultdict(dict, {12345: {'test1': 'test2'}, 67890: {'test3': 'test4'}}) |
| 41 | + |
| 42 | + |
| 43 | +@pytest.fixture(scope="function") |
| 44 | +def updater(bot, base_persistence): |
| 45 | + base_persistence.store_chat_data = False |
| 46 | + base_persistence.store_user_data = False |
| 47 | + u = Updater(bot=bot, persistence=base_persistence) |
| 48 | + base_persistence.store_chat_data = True |
| 49 | + base_persistence.store_user_data = True |
| 50 | + return u |
| 51 | + |
| 52 | + |
| 53 | +class TestBasePersistence(object): |
| 54 | + |
| 55 | + def test_creation(self, base_persistence): |
| 56 | + assert base_persistence.store_chat_data |
| 57 | + assert base_persistence.store_user_data |
| 58 | + with pytest.raises(NotImplementedError): |
| 59 | + base_persistence.get_chat_data() |
| 60 | + with pytest.raises(NotImplementedError): |
| 61 | + base_persistence.get_user_data() |
| 62 | + with pytest.raises(NotImplementedError): |
| 63 | + base_persistence.get_conversations("test") |
| 64 | + with pytest.raises(NotImplementedError): |
| 65 | + base_persistence.update_chat_data(None) |
| 66 | + with pytest.raises(NotImplementedError): |
| 67 | + base_persistence.update_user_data(None) |
| 68 | + with pytest.raises(NotImplementedError): |
| 69 | + base_persistence.update_conversation(None) |
| 70 | + with pytest.raises(NotImplementedError): |
| 71 | + base_persistence.flush() |
| 72 | + |
| 73 | + def test_implementation(self, updater, base_persistence): |
| 74 | + dp = updater.dispatcher |
| 75 | + assert dp.persistence == base_persistence |
| 76 | + |
| 77 | + def test_conversationhandler_addition(self, dp, base_persistence): |
| 78 | + with pytest.raises(ValueError, match="when handler is unnamed"): |
| 79 | + ConversationHandler([], [], [], persistent=True) |
| 80 | + with pytest.raises(ValueError, match="if dispatcher has no persistence"): |
| 81 | + dp.add_handler(ConversationHandler([], {}, [], persistent=True, name="My Handler")) |
| 82 | + dp.persistence = base_persistence |
| 83 | + with pytest.raises(NotImplementedError): |
| 84 | + dp.add_handler(ConversationHandler([], {}, [], persistent=True, name="My Handler")) |
| 85 | + |
| 86 | + def test_dispatcher_integration_init(self, bot, base_persistence, chat_data, user_data): |
| 87 | + def get_user_data(): |
| 88 | + return "test" |
| 89 | + |
| 90 | + def get_chat_data(): |
| 91 | + return "test" |
| 92 | + |
| 93 | + base_persistence.get_user_data = get_user_data |
| 94 | + base_persistence.get_chat_data = get_chat_data |
| 95 | + with pytest.raises(ValueError, match="user_data must be of type defaultdict"): |
| 96 | + u = Updater(bot=bot, persistence=base_persistence) |
| 97 | + |
| 98 | + def get_user_data(): |
| 99 | + return user_data |
| 100 | + |
| 101 | + base_persistence.get_user_data = get_user_data |
| 102 | + with pytest.raises(ValueError, match="chat_data must be of type defaultdict"): |
| 103 | + u = Updater(bot=bot, persistence=base_persistence) |
| 104 | + |
| 105 | + def get_chat_data(): |
| 106 | + return chat_data |
| 107 | + |
| 108 | + base_persistence.get_chat_data = get_chat_data |
| 109 | + u = Updater(bot=bot, persistence=base_persistence) |
| 110 | + assert u.dispatcher.chat_data == chat_data |
| 111 | + assert u.dispatcher.user_data == user_data |
| 112 | + u.dispatcher.chat_data[442233]['test5'] = 'test6' |
| 113 | + assert u.dispatcher.chat_data[442233]['test5'] == 'test6' |
| 114 | + |
| 115 | + def test_dispatcher_integration_handlers(self, caplog, bot, base_persistence: BasePersistence, |
| 116 | + chat_data, user_data): |
| 117 | + def get_user_data(): |
| 118 | + return user_data |
| 119 | + |
| 120 | + def get_chat_data(): |
| 121 | + return chat_data |
| 122 | + |
| 123 | + base_persistence.get_user_data = get_user_data |
| 124 | + base_persistence.get_chat_data = get_chat_data |
| 125 | + # base_persistence.update_chat_data = lambda x: x |
| 126 | + # base_persistence.update_user_data = lambda x: x |
| 127 | + updater = Updater(bot=bot, persistence=base_persistence) |
| 128 | + dp = updater.dispatcher |
| 129 | + |
| 130 | + def callback_known_user(bot, update, user_data, chat_data): |
| 131 | + if not user_data['test1'] == 'test2': |
| 132 | + pytest.fail('user_data corrupt') |
| 133 | + |
| 134 | + def callback_known_chat(bot, update, user_data, chat_data): |
| 135 | + if not chat_data['test3'] == 'test4': |
| 136 | + pytest.fail('chat_data corrupt') |
| 137 | + |
| 138 | + def callback_unknown_user_or_chat(bot, update, user_data, chat_data): |
| 139 | + if not user_data == {}: |
| 140 | + pytest.fail('user_data corrupt') |
| 141 | + if not chat_data == {}: |
| 142 | + pytest.fail('chat_data corrupt') |
| 143 | + user_data[1] = 'test7' |
| 144 | + chat_data[2] = 'test8' |
| 145 | + |
| 146 | + known_user = MessageHandler(Filters.user(user_id=12345), callback_known_user, |
| 147 | + pass_chat_data=True, pass_user_data=True) |
| 148 | + known_chat = MessageHandler(Filters.chat(chat_id=-67890), callback_known_chat, |
| 149 | + pass_chat_data=True, pass_user_data=True) |
| 150 | + unknown = MessageHandler(Filters.all, callback_unknown_user_or_chat, pass_chat_data=True, |
| 151 | + pass_user_data=True) |
| 152 | + dp.add_handler(known_user) |
| 153 | + dp.add_handler(known_chat) |
| 154 | + dp.add_handler(unknown) |
| 155 | + user1 = User(id=12345, first_name='test user', is_bot=False) |
| 156 | + user2 = User(id=54321, first_name='test user', is_bot=False) |
| 157 | + chat1 = Chat(id=-67890, type='group') |
| 158 | + chat2 = Chat(id=-987654, type='group') |
| 159 | + m = Message(1, user1, None, chat2) |
| 160 | + u = Update(0, m) |
| 161 | + with caplog.at_level(logging.ERROR): |
| 162 | + dp.process_update(u) |
| 163 | + rec = caplog.records[-1] |
| 164 | + assert rec.msg == 'Saving user data raised an error' |
| 165 | + assert rec.levelname == 'ERROR' |
| 166 | + rec = caplog.records[-2] |
| 167 | + assert rec.msg == 'Saving chat data raised an error' |
| 168 | + assert rec.levelname == 'ERROR' |
| 169 | + |
| 170 | + m.from_user = user2 |
| 171 | + m.chat = chat1 |
| 172 | + u = Update(1, m) |
| 173 | + dp.process_update(u) |
| 174 | + m.chat = chat2 |
| 175 | + u = Update(2, m) |
| 176 | + dp.process_update(u) |
| 177 | + |
| 178 | + assert dp.user_data[54321][1] == 'test7' |
| 179 | + assert dp.chat_data[-987654][2] == 'test8' |
0 commit comments