mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2024-11-22 15:17:00 +01:00
7e554584b1
* Handle promise states in conversation timeout Signed-off-by: starry69 <starry369126@outlook.com> * warn if nested conversation & timeout Signed-off-by: starry69 <starry369126@outlook.com> * Add notes and test for conversation_timeout Signed-off-by: starry69 <starry369126@outlook.com> * Try to fix pre-commit Signed-off-by: starry69 <starry369126@outlook.com> * Test promise exception Signed-off-by: starry69 <starry369126@outlook.com> * Welp Signed-off-by: starry69 <starry369126@outlook.com> * improve docs Signed-off-by: starry69 <starry369126@outlook.com> * typo Signed-off-by: starry69 <starry369126@outlook.com> * try to fix codecov Signed-off-by: starry69 <starry369126@outlook.com> * refactor timeout logic with promise.add_done_cb Signed-off-by: starry69 <starry369126@outlook.com> * small fix Signed-off-by: starry69 <starry369126@outlook.com> * Address review Signed-off-by: starry69 <starry369126@outlook.com> * Fix some type hinting * Few fixes Signed-off-by: starry69 <starry369126@outlook.com> * fix tests Signed-off-by: starry69 <starry369126@outlook.com> * minor nitpick Signed-off-by: starry69 <starry369126@outlook.com> Co-authored-by: Hinrich Mahler <hinrich.mahler@freenet.de>
129 lines
3.8 KiB
Python
129 lines
3.8 KiB
Python
#!/usr/bin/env python
|
|
#
|
|
# A library that provides a Python interface to the Telegram Bot API
|
|
# Copyright (C) 2015-2021
|
|
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU Lesser Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU Lesser Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU Lesser Public License
|
|
# along with this program. If not, see [http://www.gnu.org/licenses/].
|
|
import logging
|
|
import pytest
|
|
|
|
from telegram import TelegramError
|
|
from telegram.ext.utils.promise import Promise
|
|
|
|
|
|
class TestPromise:
|
|
"""
|
|
Here we just test the things that are not covered by the other tests anyway
|
|
"""
|
|
|
|
test_flag = False
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def reset(self):
|
|
self.test_flag = False
|
|
|
|
def test_call(self):
|
|
def callback():
|
|
self.test_flag = True
|
|
|
|
promise = Promise(callback, [], {})
|
|
promise()
|
|
|
|
assert promise.done
|
|
assert self.test_flag
|
|
|
|
def test_run_with_exception(self):
|
|
def callback():
|
|
raise TelegramError('Error')
|
|
|
|
promise = Promise(callback, [], {})
|
|
promise.run()
|
|
|
|
assert promise.done
|
|
assert not self.test_flag
|
|
assert isinstance(promise.exception, TelegramError)
|
|
|
|
def test_wait_for_exception(self):
|
|
def callback():
|
|
raise TelegramError('Error')
|
|
|
|
promise = Promise(callback, [], {})
|
|
promise.run()
|
|
|
|
with pytest.raises(TelegramError, match='Error'):
|
|
promise.result()
|
|
|
|
def test_done_cb_after_run(self):
|
|
def callback():
|
|
return "done!"
|
|
|
|
def done_callback(_):
|
|
self.test_flag = True
|
|
|
|
promise = Promise(callback, [], {})
|
|
promise.run()
|
|
promise.add_done_callback(done_callback)
|
|
assert promise.result() == "done!"
|
|
assert self.test_flag is True
|
|
|
|
def test_done_cb_after_run_excp(self):
|
|
def callback():
|
|
return "done!"
|
|
|
|
def done_callback(_):
|
|
raise Exception("Error!")
|
|
|
|
promise = Promise(callback, [], {})
|
|
promise.run()
|
|
assert promise.result() == "done!"
|
|
with pytest.raises(Exception) as err:
|
|
promise.add_done_callback(done_callback)
|
|
assert str(err) == "Error!"
|
|
|
|
def test_done_cb_before_run(self):
|
|
def callback():
|
|
return "done!"
|
|
|
|
def done_callback(_):
|
|
self.test_flag = True
|
|
|
|
promise = Promise(callback, [], {})
|
|
promise.add_done_callback(done_callback)
|
|
assert promise.result(0) != "done!"
|
|
assert self.test_flag is False
|
|
promise.run()
|
|
assert promise.result() == "done!"
|
|
assert self.test_flag is True
|
|
|
|
def test_done_cb_before_run_excp(self, caplog):
|
|
def callback():
|
|
return "done!"
|
|
|
|
def done_callback(_):
|
|
raise Exception("Error!")
|
|
|
|
promise = Promise(callback, [], {})
|
|
promise.add_done_callback(done_callback)
|
|
assert promise.result(0) != "done!"
|
|
caplog.clear()
|
|
with caplog.at_level(logging.WARNING):
|
|
promise.run()
|
|
assert len(caplog.records) == 2
|
|
assert caplog.records[0].message == (
|
|
"`done_callback` of a Promise raised the following exception."
|
|
" The exception won't be handled by error handlers."
|
|
)
|
|
assert caplog.records[1].message.startswith("Full traceback:")
|
|
assert promise.result() == "done!"
|