- Notifications
You must be signed in to change notification settings - Fork5.7k
Make Tests fortelegram.ext
Independent of Networking#4454
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
7 commits Select commitHold shift + click to select a range
778cc0c
Make Tests for `telegram.ext` Independent of Networking
Bibo-Joshi708d04c
Merge branch 'master' into no-request-tests
Bibo-Joshi63256eb
Fix Tests and try speeding up a bit
Bibo-Joshi321b2d4
Review
Bibo-Joshi017925a
Way more test fixing …
Bibo-Joshi98d0890
fix unix-only tests
Bibo-Joshic25ee5e
Reduce code duplication
Bibo-JoshiFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
29 changes: 29 additions & 0 deletionstests/auxil/monkeypatch.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#!/usr/bin/env python | ||
# | ||
# A library that provides a Python interface to the Telegram Bot API | ||
# Copyright (C) 2015-2024 | ||
# 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 asyncio | ||
async def return_true(*args, **kwargs): | ||
return True | ||
async def empty_get_updates(*args, **kwargs): | ||
# The `await` gives the event loop a chance to run other tasks | ||
await asyncio.sleep(0) | ||
return [] |
31 changes: 29 additions & 2 deletionstests/auxil/networking.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
11 changes: 7 additions & 4 deletionstests/auxil/pytest_classes.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
53 changes: 11 additions & 42 deletionstests/conftest.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletionstests/ext/conftest.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import asyncio | ||
import pytest | ||
from telegram.ext import ApplicationBuilder, Updater | ||
from telegram.ext.filters import MessageFilter, UpdateFilter | ||
from tests.auxil.constants import PRIVATE_KEY | ||
from tests.auxil.envvars import TEST_WITH_OPT_DEPS | ||
from tests.auxil.monkeypatch import return_true | ||
from tests.auxil.networking import OfflineRequest | ||
from tests.auxil.pytest_classes import PytestApplication, PytestBot, make_bot | ||
# This module overrides the bot fixtures defined in the global conftest.py to use the offline bot. | ||
# We don't want the tests on telegram.ext to depend on the network, so we use the offline bot | ||
# instead. | ||
@pytest.fixture(scope="session") | ||
async def bot(bot_info, offline_bot): | ||
return offline_bot | ||
@pytest.fixture | ||
async def app(bot_info, monkeypatch): | ||
# We build a new bot each time so that we use `app` in a context manager without problems | ||
application = ( | ||
ApplicationBuilder() | ||
.bot(make_bot(bot_info, offline=True)) | ||
.application_class(PytestApplication) | ||
.build() | ||
) | ||
monkeypatch.setattr(application.bot, "delete_webhook", return_true) | ||
monkeypatch.setattr(application.bot, "set_webhook", return_true) | ||
yield application | ||
if application.running: | ||
await application.stop() | ||
await application.shutdown() | ||
@pytest.fixture | ||
async def updater(bot_info, monkeypatch): | ||
# We build a new bot each time so that we use `updater` in a context manager without problems | ||
up = Updater(bot=make_bot(bot_info, offline=True), update_queue=asyncio.Queue()) | ||
monkeypatch.setattr(up.bot, "delete_webhook", return_true) | ||
monkeypatch.setattr(up.bot, "set_webhook", return_true) | ||
yield up | ||
if up.running: | ||
await up.stop() | ||
await up.shutdown() | ||
@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, offline=True) | ||
@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, offline=True) as _bot: | ||
yield _bot | ||
@pytest.fixture(scope="session") | ||
async def raw_bot(bot_info): | ||
"""Makes an regular Bot instance with the given bot_info""" | ||
async with PytestBot( | ||
bot_info["token"], | ||
private_key=PRIVATE_KEY if TEST_WITH_OPT_DEPS else None, | ||
request=OfflineRequest(), | ||
get_updates_request=OfflineRequest(), | ||
) as _bot: | ||
yield _bot | ||
@pytest.fixture | ||
async def one_time_raw_bot(bot_info): | ||
"""Makes an regular Bot instance with the given bot_info""" | ||
return PytestBot( | ||
bot_info["token"], | ||
private_key=PRIVATE_KEY if TEST_WITH_OPT_DEPS else None, | ||
request=OfflineRequest(), | ||
get_updates_request=OfflineRequest(), | ||
) | ||
@pytest.fixture( | ||
scope="class", | ||
params=[{"class": MessageFilter}, {"class": UpdateFilter}], | ||
ids=["MessageFilter", "UpdateFilter"], | ||
) | ||
def mock_filter(request): | ||
class MockFilter(request.param["class"]): | ||
def __init__(self): | ||
super().__init__() | ||
self.tested = False | ||
def filter(self, _): | ||
self.tested = True | ||
return MockFilter() |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.