Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changes/unreleased/5304.6rKY3k6kGsCdnZFP8jdsQ2.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
internal = "Make `*WithoutRequest` tests actually not use the network"
[[pull_requests]]
uid = "5304"
author_uids = ["harshil21"]
closes_threads = ["4829"]
6 changes: 4 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ tests = [
"pytest-asyncio==0.21.2",
# xdist runs tests in parallel
"pytest-xdist==3.8.0",
# no_req tests must be fully offline, including during higher-scoped fixture setup
"pytest-socket==0.8.0",
# Used for flaky tests (flaky decorator)
"flaky>=3.8.1",
# used in test_official for parsing tg docs
Expand Down Expand Up @@ -173,7 +175,7 @@ typing-extensions = false
ignore = ["PLR2004", "PLR0911", "PLR0912", "PLR0913", "PLR0915", "PERF203", "ASYNC240"]
select = ["E", "F", "I", "PL", "UP", "RUF", "PTH", "C4", "B", "PIE", "SIM", "RET", "RSE",
"G", "ISC", "PT", "ASYNC", "TCH", "SLOT", "PERF", "PYI", "FLY", "AIR", "RUF022",
"RUF023", "Q", "INP", "W", "YTT", "DTZ", "ARG", "T20", "FURB", "DOC", "TRY",
"RUF023", "Q", "INP", "W", "YTT", "DTZ", "ARG", "T20", "FURB", "DOC", "TRY",
"D100", "D101", "D102", "D103", "D300", "D418", "D419", "S"]
# Add "A (flake8-builtins)" after we drop pylint

Expand Down Expand Up @@ -212,7 +214,7 @@ exclude-protected = ["_unfrozen"]
[tool.pytest.ini_options]
testpaths = ["tests"]
pythonpath = ["src"]
addopts = "--no-success-flaky-report -rX"
addopts = "--no-flaky-report -rX --allow-unix-socket"
filterwarnings = [
"error",
"ignore::DeprecationWarning",
Expand Down
1 change: 1 addition & 0 deletions tests/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ are a few conventions that you should follow:
tooling can help you as well. You can look at the existing tests for examples and inspiration.

- New fixtures should go into ``conftest.py``. New auxiliary functions and classes, used either directly in the tests or in the fixtures, should go into the ``tests/auxil`` directory.
Offline fixtures must be prefixed with `offline_*`.

If you have made some API changes, you may want to run ``test_official`` to validate that the changes are
complete and correct. To run it, export an environment variable first:
Expand Down
147 changes: 146 additions & 1 deletion tests/_files/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,49 @@

import pytest

from telegram import Animation, Audio, Document, PhotoSize, Sticker, Video
from tests.auxil.files import data_file
from tests.auxil.networking import expect_bad_request

FILE_ID = "5a3128a4d2a04750b5b58397f3b5e812"
FILE_UNIQUE_ID = "adc3145fd2e84d95b64d68eaa22aa33e"


def _with_bot(file, bot):
file.set_bot(bot)
return file


def _thumbnail(bot, *, width, height, file_size):
return _with_bot(
PhotoSize(
file_id=f"thumbnail-{FILE_ID}",
file_unique_id=f"thumbnail-{FILE_UNIQUE_ID}",
width=width,
height=height,
file_size=file_size,
),
bot,
)


@pytest.fixture(scope="session")
def offline_animation(offline_bot):
return _with_bot(
Animation(
file_id=FILE_ID,
file_unique_id=FILE_UNIQUE_ID,
width=320,
height=180,
duration=1,
file_name="game.gif.webm",
mime_type="video/mp4",
file_size=5859,
thumbnail=_thumbnail(offline_bot, width=320, height=180, file_size=5859),
),
offline_bot,
)


@pytest.fixture(scope="session")
async def animation(bot, chat_id):
Expand Down Expand Up @@ -50,6 +90,22 @@ def animated_sticker_file():
yield f


@pytest.fixture(scope="session")
def offline_audio(offline_bot):
return _with_bot(
Audio(
file_id=FILE_ID,
file_unique_id=FILE_UNIQUE_ID,
duration=3,
file_name="telegram.mp3",
mime_type="audio/mpeg",
file_size=122920,
thumbnail=_thumbnail(offline_bot, width=50, height=50, file_size=1427),
),
offline_bot,
)


@pytest.fixture(scope="session")
async def audio(bot, chat_id):
with data_file("telegram.mp3").open("rb") as f, data_file("thumb.jpg").open("rb") as thumb:
Expand All @@ -62,6 +118,21 @@ def audio_file():
yield f


@pytest.fixture(scope="session")
def offline_document(offline_bot):
return _with_bot(
Document(
file_id=FILE_ID,
file_unique_id=FILE_UNIQUE_ID,
file_name="telegram.png",
mime_type="image/png",
file_size=12948,
thumbnail=_thumbnail(offline_bot, width=300, height=300, file_size=8090),
),
offline_bot,
)


@pytest.fixture(scope="session")
async def document(bot, chat_id):
with data_file("telegram.png").open("rb") as f:
Expand All @@ -74,6 +145,11 @@ def document_file():
yield f


@pytest.fixture(scope="session")
def offline_photo(offline_photolist):
return offline_photolist[-1]


@pytest.fixture(scope="session")
def photo(photolist):
return photolist[-1]
Expand All @@ -85,6 +161,32 @@ def photo_file():
yield f


@pytest.fixture(scope="session")
def offline_photolist(offline_bot):
return (
_with_bot(
PhotoSize(
file_id=f"small-{FILE_ID}",
file_unique_id=f"small-{FILE_UNIQUE_ID}",
width=90,
height=90,
file_size=1474,
),
offline_bot,
),
_with_bot(
PhotoSize(
file_id=FILE_ID,
file_unique_id=FILE_UNIQUE_ID,
width=800,
height=800,
file_size=29176,
),
offline_bot,
),
)


@pytest.fixture(scope="session")
async def photolist(bot, chat_id):
async def func():
Expand All @@ -96,6 +198,25 @@ async def func():
)


@pytest.fixture(scope="module")
def offline_sticker(offline_bot):
return _with_bot(
Sticker(
file_id=FILE_ID,
file_unique_id=FILE_UNIQUE_ID,
width=510,
height=512,
is_animated=False,
is_video=False,
type=Sticker.REGULAR,
file_size=39518,
thumbnail=_thumbnail(offline_bot, width=319, height=320, file_size=21448),
needs_repainting=True,
),
offline_bot,
)


@pytest.fixture(scope="module")
async def sticker(bot, chat_id):
with data_file("telegram.webp").open("rb") as f:
Expand All @@ -118,15 +239,39 @@ def sticker_set_thumb_file():
yield file


@pytest.fixture(scope="session")
def offline_thumb(offline_photolist):
return offline_photolist[0]


@pytest.fixture(scope="session")
def thumb(photolist):
return photolist[0]


@pytest.fixture(scope="session")
def offline_video(offline_bot):
return _with_bot(
Video(
file_id=FILE_ID,
file_unique_id=FILE_UNIQUE_ID,
width=360,
height=640,
duration=5,
file_name="telegram.mp4",
mime_type="video/mp4",
file_size=326534,
thumbnail=_thumbnail(offline_bot, width=180, height=320, file_size=1767),
start_timestamp=3,
),
offline_bot,
)


@pytest.fixture(scope="session")
async def video(bot, chat_id):
with data_file("telegram.mp4").open("rb") as f:
return (await bot.send_video(chat_id, video=f, read_timeout=50)).video
return (await bot.send_video(chat_id, video=f, start_timestamp=3, read_timeout=50)).video


@pytest.fixture
Expand Down
Loading
Loading