|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# A library that provides a Python interface to the Telegram Bot API |
| 4 | +# Copyright (C) 2015-2017 |
| 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 | + |
| 20 | +import pytest |
| 21 | + |
| 22 | +from telegram.ext import Handler |
| 23 | + |
| 24 | + |
| 25 | +class TestHandler(object): |
| 26 | + test_flag = False |
| 27 | + |
| 28 | + @pytest.fixture(autouse=True) |
| 29 | + def reset(self): |
| 30 | + self.test_flag = False |
| 31 | + |
| 32 | + def callback_basic(self, bot, update): |
| 33 | + pass |
| 34 | + |
| 35 | + def callback_some_passable(self, bot, update, update_queue, chat_data): |
| 36 | + pass |
| 37 | + |
| 38 | + def callback_all_passable(self, bot, update, update_queue, job_queue, chat_data, user_data): |
| 39 | + pass |
| 40 | + |
| 41 | + def test_set_autowired_flags_all(self): |
| 42 | + handler = Handler(self.callback_all_passable, autowire=True) |
| 43 | + assert handler._autowire_initialized is False |
| 44 | + assert handler.pass_update_queue is False |
| 45 | + assert handler.pass_job_queue is False |
| 46 | + assert handler.pass_chat_data is False |
| 47 | + assert handler.pass_user_data is False |
| 48 | + |
| 49 | + handler.set_autowired_flags() |
| 50 | + |
| 51 | + assert handler._autowire_initialized is True |
| 52 | + assert handler.pass_update_queue is True |
| 53 | + assert handler.pass_job_queue is True |
| 54 | + assert handler.pass_chat_data is True |
| 55 | + assert handler.pass_user_data is True |
| 56 | + |
| 57 | + def test_set_autowired_flags_some(self): |
| 58 | + handler = Handler(self.callback_some_passable, autowire=True) |
| 59 | + assert handler.pass_update_queue is False |
| 60 | + assert handler.pass_chat_data is False |
| 61 | + |
| 62 | + handler.set_autowired_flags() |
| 63 | + |
| 64 | + assert handler._autowire_initialized is True |
| 65 | + assert handler.pass_update_queue is True |
| 66 | + assert handler.pass_chat_data is True |
| 67 | + |
| 68 | + def test_set_autowired_flags_wrong(self): |
| 69 | + handler = Handler(self.callback_all_passable, autowire=True) |
| 70 | + with pytest.raises(UserWarning): |
| 71 | + handler.set_autowired_flags({'kektus'}) |
| 72 | + with pytest.raises(UserWarning): |
| 73 | + handler.set_autowired_flags({'chat_data', 'kektus'}) |
| 74 | + with pytest.raises(UserWarning): |
| 75 | + handler.set_autowired_flags({'bot', 'update'}) |
| 76 | + |
| 77 | + def test_autowire_and_pass(self): |
| 78 | + handler = Handler(self.callback_all_passable, autowire=True, pass_chat_data=True) |
| 79 | + with pytest.raises(UserWarning): |
| 80 | + handler.set_autowired_flags() |
| 81 | + |
| 82 | + def test_not_autowired_set_flags(self): |
| 83 | + handler = Handler(self.callback_all_passable, autowire=False) |
| 84 | + with pytest.raises(ValueError): |
| 85 | + handler.set_autowired_flags() |
| 86 | + |
| 87 | + def test_autowire_reinitialize(self): |
| 88 | + handler = Handler(self.callback_all_passable, autowire=True) |
| 89 | + assert handler._autowire_initialized is False |
| 90 | + assert handler.pass_update_queue is False |
| 91 | + assert handler.pass_job_queue is False |
| 92 | + assert handler.pass_chat_data is False |
| 93 | + assert handler.pass_user_data is False |
| 94 | + |
| 95 | + handler.set_autowired_flags() |
| 96 | + |
| 97 | + assert handler._autowire_initialized is True |
| 98 | + assert handler.pass_update_queue is True |
| 99 | + assert handler.pass_job_queue is True |
| 100 | + assert handler.pass_chat_data is True |
| 101 | + assert handler.pass_user_data is True |
| 102 | + |
| 103 | + handler.callback = self.callback_some_passable |
| 104 | + handler.set_autowired_flags() |
| 105 | + |
| 106 | + assert handler._autowire_initialized is True |
| 107 | + assert handler.pass_update_queue is True |
| 108 | + assert handler.pass_job_queue is False |
| 109 | + assert handler.pass_chat_data is True |
| 110 | + assert handler.pass_user_data is False |
| 111 | + |
| 112 | + def test_get_available_pass_flags(self): |
| 113 | + handler = Handler(self.callback_all_passable, autowire=True) |
| 114 | + assert handler.pass_update_queue is False |
| 115 | + assert handler.pass_job_queue is False |
| 116 | + assert handler.pass_chat_data is False |
| 117 | + assert handler.pass_user_data is False |
| 118 | + |
| 119 | + handler.set_autowired_flags() |
| 120 | + |
| 121 | + assert set(handler._get_available_pass_flags()) == {'pass_update_queue', 'pass_job_queue', 'pass_chat_data', |
| 122 | + 'pass_user_data'} |
0 commit comments