From 25039950f1b762bf266ae234a896c5ef11678aea Mon Sep 17 00:00:00 2001 From: Hinrich Mahler <22366557+Bibo-Joshi@users.noreply.github.com> Date: Sat, 21 Sep 2024 17:22:37 +0200 Subject: [PATCH 1/2] Reduce Creation of HTTP Clients in Tests --- tests/auxil/build_messages.py | 5 +++-- tests/auxil/pytest_classes.py | 2 +- tests/conftest.py | 10 +++++----- tests/ext/test_basepersistence.py | 15 +++++++++++---- tests/ext/test_callbackcontext.py | 2 +- tests/test_bot.py | 2 +- 6 files changed, 22 insertions(+), 14 deletions(-) diff --git a/tests/auxil/build_messages.py b/tests/auxil/build_messages.py index 69370977efb..8664317d229 100644 --- a/tests/auxil/build_messages.py +++ b/tests/auxil/build_messages.py @@ -27,16 +27,17 @@ DATE = datetime.datetime.now() -def make_message(text, **kwargs): +def make_message(text: str, offline: bool = True, **kwargs): """ Testing utility factory to create a fake ``telegram.Message`` with reasonable defaults for mimicking a real message. :param text: (str) message text + :param offline: (bool) whether the bot should be offline :return: a (fake) ``telegram.Message`` """ bot = kwargs.pop("bot", None) if bot is None: - bot = make_bot(BOT_INFO_PROVIDER.get_info()) + bot = make_bot(BOT_INFO_PROVIDER.get_info(), offline=offline) message = Message( message_id=1, from_user=kwargs.pop("user", User(id=1, first_name="", is_bot=False)), diff --git a/tests/auxil/pytest_classes.py b/tests/auxil/pytest_classes.py index b80945b6704..f85e12ac23c 100644 --- a/tests/auxil/pytest_classes.py +++ b/tests/auxil/pytest_classes.py @@ -93,7 +93,7 @@ class PytestUpdater(Updater): pass -def make_bot(bot_info=None, offline: bool = False, **kwargs): +def make_bot(bot_info=None, offline: bool = True, **kwargs): """ Tests are executed on tg.ext.ExtBot, as that class only extends the functionality of tg.bot """ diff --git a/tests/conftest.py b/tests/conftest.py index 02f83b47555..69c8ce96037 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -152,7 +152,7 @@ def bot_info() -> Dict[str, str]: @pytest.fixture(scope="session") async def bot(bot_info): """Makes an ExtBot instance with the given bot_info""" - async with make_bot(bot_info) as _bot: + async with make_bot(bot_info, offline=False) as _bot: yield _bot @@ -168,13 +168,13 @@ async def offline_bot(bot_info): @pytest.fixture def one_time_bot(bot_info): """A function scoped bot since the session bot would shutdown when `async with app` finishes""" - return make_bot(bot_info) + return make_bot(bot_info, offline=False) @pytest.fixture(scope="session") async def cdc_bot(bot_info): """Makes an ExtBot instance with the given bot_info that uses arbitrary callback_data""" - async with make_bot(bot_info, arbitrary_callback_data=True) as _bot: + async with make_bot(bot_info, arbitrary_callback_data=True, offline=False) as _bot: yield _bot @@ -204,7 +204,7 @@ async def default_bot(request, bot_info): # If the bot is already created, return it. Else make a new one. default_bot = _default_bots.get(defaults) if default_bot is None: - default_bot = make_bot(bot_info, defaults=defaults) + default_bot = make_bot(bot_info, defaults=defaults, offline=False) await default_bot.initialize() _default_bots[defaults] = default_bot # Defaults object is hashable return default_bot @@ -216,7 +216,7 @@ async def tz_bot(timezone, bot_info): try: # If the bot is already created, return it. Saves time since get_me is not called again. return _default_bots[defaults] except KeyError: - default_bot = make_bot(bot_info, defaults=defaults) + default_bot = make_bot(bot_info, defaults=defaults, offline=False) await default_bot.initialize() _default_bots[defaults] = default_bot return default_bot diff --git a/tests/ext/test_basepersistence.py b/tests/ext/test_basepersistence.py index d3c2ef771b9..c04a5d16826 100644 --- a/tests/ext/test_basepersistence.py +++ b/tests/ext/test_basepersistence.py @@ -24,6 +24,7 @@ import logging import sys import time +from http import HTTPStatus from pathlib import Path from typing import NamedTuple, Optional @@ -43,8 +44,9 @@ PersistenceInput, filters, ) +from telegram.request import HTTPXRequest from telegram.warnings import PTBUserWarning -from tests.auxil.build_messages import make_message_update +from tests.auxil.build_messages import make_message, make_message_update from tests.auxil.pytest_classes import PytestApplication, make_bot from tests.auxil.slots import mro_slots @@ -245,9 +247,9 @@ def build_papp( persistence = TrackingPersistence(store_data=store_data, fill_data=fill_data) if bot_info is not None: - bot = make_bot(bot_info, arbitrary_callback_data=True) + bot = make_bot(bot_info, arbitrary_callback_data=True, offline=False) else: - bot = make_bot(token=token, arbitrary_callback_data=True) + bot = make_bot(token=token, arbitrary_callback_data=True, offline=False) return ( ApplicationBuilder() .bot(bot) @@ -262,7 +264,7 @@ def build_conversation_handler(name: str, persistent: bool = True) -> BaseHandle @pytest.fixture -def papp(request, bot_info) -> Application: +def papp(request, bot_info, monkeypatch) -> Application: papp_input = request.param store_data = {} if papp_input.bot_data is not None: @@ -274,6 +276,11 @@ def papp(request, bot_info) -> Application: if papp_input.callback_data is not None: store_data["callback_data"] = papp_input.callback_data + async def do_request(*args, **kwargs): + return HTTPStatus.OK, make_message(text="text") + + monkeypatch.setattr(HTTPXRequest, "do_request", do_request) + app = build_papp( bot_info=bot_info, store_data=store_data, diff --git a/tests/ext/test_callbackcontext.py b/tests/ext/test_callbackcontext.py index 9a5f64e6f21..429d28a6ff6 100644 --- a/tests/ext/test_callbackcontext.py +++ b/tests/ext/test_callbackcontext.py @@ -211,7 +211,7 @@ def test_drop_callback_data_exception(self, bot, app, raw_bot): app.bot = bot async def test_drop_callback_data(self, bot, chat_id): - new_bot = make_bot(token=bot.token, arbitrary_callback_data=True) + new_bot = make_bot(token=bot.token, arbitrary_callback_data=True, offline=False) app = ApplicationBuilder().bot(new_bot).build() update = Update( diff --git a/tests/test_bot.py b/tests/test_bot.py index 54fa28f11cb..3a963a73ae4 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -1564,7 +1564,7 @@ async def post(url, request_data: RequestData, *args, **kwargs): [(True, 1024), (False, 1024), (0, 0), (None, None)], ) async def test_callback_data_maxsize(self, bot_info, acd_in, maxsize): - async with make_bot(bot_info, arbitrary_callback_data=acd_in) as acd_bot: + async with make_bot(bot_info, arbitrary_callback_data=acd_in, offline=True) as acd_bot: if acd_in is not False: assert acd_bot.callback_data_cache.maxsize == maxsize else: From 6d3d61561886cd05ed97f3e9ed3d9b2d5e979a7f Mon Sep 17 00:00:00 2001 From: Hinrich Mahler <22366557+Bibo-Joshi@users.noreply.github.com> Date: Sat, 21 Sep 2024 17:27:38 +0200 Subject: [PATCH 2/2] fewer Bot initializations --- tests/test_bot.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_bot.py b/tests/test_bot.py index 3a963a73ae4..eac10c71236 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -361,13 +361,13 @@ async def test_equality(self): "link", ], ) - async def test_get_me_and_properties_not_initialized(self, offline_bot: Bot, attribute): - offline_bot = Bot(token=offline_bot.token) + async def test_get_me_and_properties_not_initialized(self, attribute): + bot = make_bot(offline=True, token="randomtoken") try: with pytest.raises(RuntimeError, match="not properly initialized"): - offline_bot[attribute] + bot[attribute] finally: - await offline_bot.shutdown() + await bot.shutdown() async def test_get_me_and_properties(self, offline_bot): get_me_bot = await ExtBot(offline_bot.token).get_me()
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: