Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 28d19c3

Browse files
Introduce conftest.py for File Related Tests (python-telegram-bot#4488)
Co-authored-by: Harshil <[email protected]>
1 parent e314e78 commit 28d19c3

14 files changed

+263
-263
lines changed

tests/_files/conftest.py

+189
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
#!/usr/bin/env python
2+
#
3+
# A library that provides a Python interface to the Telegram Bot API
4+
# Copyright (C) 2015-2024
5+
# Leandro Toledo de Souza <[email protected]>
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+
"""Module to provide fixtures most of which are used in test_inputmedia.py."""
20+
import pytest
21+
22+
from telegram.error import BadRequest
23+
from tests.auxil.files import data_file
24+
from tests.auxil.networking import expect_bad_request
25+
26+
27+
@pytest.fixture(scope="session")
28+
async def animation(bot, chat_id):
29+
with data_file("game.gif").open("rb") as f, data_file("thumb.jpg").open("rb") as thumb:
30+
return (
31+
await bot.send_animation(chat_id, animation=f, read_timeout=50, thumbnail=thumb)
32+
).animation
33+
34+
35+
@pytest.fixture
36+
def animation_file():
37+
with data_file("game.gif").open("rb") as f:
38+
yield f
39+
40+
41+
@pytest.fixture(scope="module")
42+
async def animated_sticker(bot, chat_id):
43+
with data_file("telegram_animated_sticker.tgs").open("rb") as f:
44+
return (await bot.send_sticker(chat_id, sticker=f, read_timeout=50)).sticker
45+
46+
47+
@pytest.fixture
48+
def animated_sticker_file():
49+
with data_file("telegram_animated_sticker.tgs").open("rb") as f:
50+
yield f
51+
52+
53+
@pytest.fixture
54+
async def animated_sticker_set(bot):
55+
ss = await bot.get_sticker_set(f"animated_test_by_{bot.username}")
56+
if len(ss.stickers) > 100:
57+
try:
58+
for i in range(1, 50):
59+
await bot.delete_sticker_from_set(ss.stickers[-i].file_id)
60+
except BadRequest as e:
61+
if e.message == "Stickerset_not_modified":
62+
return ss
63+
raise Exception("stickerset is growing too large.") from None
64+
return ss
65+
66+
67+
@pytest.fixture(scope="session")
68+
async def audio(bot, chat_id):
69+
with data_file("telegram.mp3").open("rb") as f, data_file("thumb.jpg").open("rb") as thumb:
70+
return (await bot.send_audio(chat_id, audio=f, read_timeout=50, thumbnail=thumb)).audio
71+
72+
73+
@pytest.fixture
74+
def audio_file():
75+
with data_file("telegram.mp3").open("rb") as f:
76+
yield f
77+
78+
79+
@pytest.fixture(scope="session")
80+
async def document(bot, chat_id):
81+
with data_file("telegram.png").open("rb") as f:
82+
return (await bot.send_document(chat_id, document=f, read_timeout=50)).document
83+
84+
85+
@pytest.fixture
86+
def document_file():
87+
with data_file("telegram.png").open("rb") as f:
88+
yield f
89+
90+
91+
@pytest.fixture(scope="session")
92+
def photo(photolist):
93+
return photolist[-1]
94+
95+
96+
@pytest.fixture
97+
def photo_file():
98+
with data_file("telegram.jpg").open("rb") as f:
99+
yield f
100+
101+
102+
@pytest.fixture(scope="session")
103+
async def photolist(bot, chat_id):
104+
async def func():
105+
with data_file("telegram.jpg").open("rb") as f:
106+
return (await bot.send_photo(chat_id, photo=f, read_timeout=50)).photo
107+
108+
return await expect_bad_request(
109+
func, "Type of file mismatch", "Telegram did not accept the file."
110+
)
111+
112+
113+
@pytest.fixture(scope="module")
114+
async def sticker(bot, chat_id):
115+
with data_file("telegram.webp").open("rb") as f:
116+
sticker = (await bot.send_sticker(chat_id, sticker=f, read_timeout=50)).sticker
117+
# necessary to properly test needs_repainting
118+
with sticker._unfrozen():
119+
sticker.needs_repainting = True
120+
return sticker
121+
122+
123+
@pytest.fixture
124+
def sticker_file():
125+
with data_file("telegram.webp").open("rb") as file:
126+
yield file
127+
128+
129+
@pytest.fixture
130+
async def sticker_set(bot):
131+
ss = await bot.get_sticker_set(f"test_by_{bot.username}")
132+
if len(ss.stickers) > 100:
133+
try:
134+
for i in range(1, 50):
135+
await bot.delete_sticker_from_set(ss.stickers[-i].file_id)
136+
except BadRequest as e:
137+
if e.message == "Stickerset_not_modified":
138+
return ss
139+
raise Exception("stickerset is growing too large.") from None
140+
return ss
141+
142+
143+
@pytest.fixture
144+
def sticker_set_thumb_file():
145+
with data_file("sticker_set_thumb.png").open("rb") as file:
146+
yield file
147+
148+
149+
@pytest.fixture(scope="session")
150+
def thumb(photolist):
151+
return photolist[0]
152+
153+
154+
@pytest.fixture(scope="session")
155+
async def video(bot, chat_id):
156+
with data_file("telegram.mp4").open("rb") as f:
157+
return (await bot.send_video(chat_id, video=f, read_timeout=50)).video
158+
159+
160+
@pytest.fixture
161+
def video_file():
162+
with data_file("telegram.mp4").open("rb") as f:
163+
yield f
164+
165+
166+
@pytest.fixture
167+
def video_sticker_file():
168+
with data_file("telegram_video_sticker.webm").open("rb") as f:
169+
yield f
170+
171+
172+
@pytest.fixture(scope="module")
173+
def video_sticker(bot, chat_id):
174+
with data_file("telegram_video_sticker.webm").open("rb") as f:
175+
return bot.send_sticker(chat_id, sticker=f, timeout=50).sticker
176+
177+
178+
@pytest.fixture
179+
async def video_sticker_set(bot):
180+
ss = await bot.get_sticker_set(f"video_test_by_{bot.username}")
181+
if len(ss.stickers) > 100:
182+
try:
183+
for i in range(1, 50):
184+
await bot.delete_sticker_from_set(ss.stickers[-i].file_id)
185+
except BadRequest as e:
186+
if e.message == "Stickerset_not_modified":
187+
return ss
188+
raise Exception("stickerset is growing too large.") from None
189+
return ss

tests/_files/test_animation.py

-14
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,6 @@
3737
from tests.auxil.slots import mro_slots
3838

3939

40-
@pytest.fixture
41-
def animation_file():
42-
with data_file("game.gif").open("rb") as f:
43-
yield f
44-
45-
46-
@pytest.fixture(scope="module")
47-
async def animation(bot, chat_id):
48-
with data_file("game.gif").open("rb") as f, data_file("thumb.jpg").open("rb") as thumb:
49-
return (
50-
await bot.send_animation(chat_id, animation=f, read_timeout=50, thumbnail=thumb)
51-
).animation
52-
53-
5440
class AnimationTestBase:
5541
animation_file_id = "CgADAQADngIAAuyVeEez0xRovKi9VAI"
5642
animation_file_unique_id = "adc3145fd2e84d95b64d68eaa22aa33e"

tests/_files/test_audio.py

-12
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,6 @@
3737
from tests.auxil.slots import mro_slots
3838

3939

40-
@pytest.fixture
41-
def audio_file():
42-
with data_file("telegram.mp3").open("rb") as f:
43-
yield f
44-
45-
46-
@pytest.fixture(scope="module")
47-
async def audio(bot, chat_id):
48-
with data_file("telegram.mp3").open("rb") as f, data_file("thumb.jpg").open("rb") as thumb:
49-
return (await bot.send_audio(chat_id, audio=f, read_timeout=50, thumbnail=thumb)).audio
50-
51-
5240
class AudioTestBase:
5341
caption = "Test *audio*"
5442
performer = "Leandro Toledo"

tests/_files/test_document.py

-12
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,6 @@
3737
from tests.auxil.slots import mro_slots
3838

3939

40-
@pytest.fixture
41-
def document_file():
42-
with data_file("telegram.png").open("rb") as f:
43-
yield f
44-
45-
46-
@pytest.fixture(scope="module")
47-
async def document(bot, chat_id):
48-
with data_file("telegram.png").open("rb") as f:
49-
return (await bot.send_document(chat_id, document=f, read_timeout=50)).document
50-
51-
5240
class DocumentTestBase:
5341
caption = "DocumentTest - *Caption*"
5442
document_file_url = "https://python-telegram-bot.org/static/testfiles/telegram.gif"

0 commit comments

Comments
 (0)