Skip to content

Commit 59014be

Browse files
eldbudBibo-Joshi
authored andcommitted
Make Tests Agnostic of the CWD (python-telegram-bot#2727)
1 parent 0cb8d50 commit 59014be

19 files changed

Lines changed: 216 additions & 164 deletions

tests/conftest.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import os
2424
import re
2525
from collections import defaultdict
26+
from pathlib import Path
2627
from queue import Queue
2728
from threading import Thread, Event
2829
from time import sleep
@@ -217,16 +218,24 @@ def updater(bot):
217218
up.stop()
218219

219220

221+
PROJECT_ROOT_PATH = Path(__file__).parent.parent.resolve()
222+
TEST_DATA_PATH = Path(__file__).parent.resolve() / "data"
223+
224+
225+
def data_file(filename: str):
226+
return TEST_DATA_PATH / filename
227+
228+
220229
@pytest.fixture(scope='function')
221230
def thumb_file():
222-
f = open('tests/data/thumb.jpg', 'rb')
231+
f = data_file('thumb.jpg').open('rb')
223232
yield f
224233
f.close()
225234

226235

227236
@pytest.fixture(scope='class')
228237
def class_thumb_file():
229-
f = open('tests/data/thumb.jpg', 'rb')
238+
f = data_file('thumb.jpg').open('rb')
230239
yield f
231240
f.close()
232241

tests/test_animation.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,27 @@
2525
from telegram import PhotoSize, Animation, Voice, MessageEntity, Bot
2626
from telegram.error import BadRequest, TelegramError
2727
from telegram.helpers import escape_markdown
28-
from tests.conftest import check_shortcut_call, check_shortcut_signature, check_defaults_handling
28+
from tests.conftest import (
29+
check_shortcut_call,
30+
check_shortcut_signature,
31+
check_defaults_handling,
32+
data_file,
33+
)
2934

3035

3136
@pytest.fixture(scope='function')
3237
def animation_file():
33-
f = Path('tests/data/game.gif').open('rb')
38+
f = data_file('game.gif').open('rb')
3439
yield f
3540
f.close()
3641

3742

3843
@pytest.fixture(scope='class')
3944
def animation(bot, chat_id):
40-
with Path('tests/data/game.gif').open('rb') as f:
45+
with data_file('game.gif').open('rb') as f:
46+
thumb = data_file('thumb.jpg')
4147
return bot.send_animation(
42-
chat_id, animation=f, timeout=50, thumb=open('tests/data/thumb.jpg', 'rb')
48+
chat_id, animation=f, timeout=50, thumb=thumb.open('rb')
4349
).animation
4450

4551

@@ -193,8 +199,8 @@ def test_send_animation_default_parse_mode_3(self, default_bot, chat_id, animati
193199
def test_send_animation_local_files(self, monkeypatch, bot, chat_id):
194200
# For just test that the correct paths are passed as we have no local bot API set up
195201
test_flag = False
196-
expected = (Path.cwd() / 'tests/data/telegram.jpg/').as_uri()
197-
file = 'tests/data/telegram.jpg'
202+
file = data_file('telegram.jpg')
203+
expected = file.as_uri()
198204

199205
def make_assertion(_, data, *args, **kwargs):
200206
nonlocal test_flag

tests/test_audio.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,26 @@
2525
from telegram import Audio, Voice, MessageEntity, Bot
2626
from telegram.error import TelegramError
2727
from telegram.helpers import escape_markdown
28-
from tests.conftest import check_shortcut_call, check_shortcut_signature, check_defaults_handling
28+
from tests.conftest import (
29+
check_shortcut_call,
30+
check_shortcut_signature,
31+
check_defaults_handling,
32+
data_file,
33+
)
2934

3035

3136
@pytest.fixture(scope='function')
3237
def audio_file():
33-
f = Path('tests/data/telegram.mp3').open('rb')
38+
f = data_file('telegram.mp3').open('rb')
3439
yield f
3540
f.close()
3641

3742

3843
@pytest.fixture(scope='class')
3944
def audio(bot, chat_id):
40-
with Path('tests/data/telegram.mp3').open('rb') as f:
41-
return bot.send_audio(
42-
chat_id, audio=f, timeout=50, thumb=Path('tests/data/thumb.jpg').open('rb')
43-
).audio
45+
with data_file('telegram.mp3').open('rb') as f:
46+
thumb = data_file('thumb.jpg')
47+
return bot.send_audio(chat_id, audio=f, timeout=50, thumb=thumb.open('rb')).audio
4448

4549

4650
class TestAudio:
@@ -215,8 +219,8 @@ def test_send_audio_default_parse_mode_3(self, default_bot, chat_id, audio_file,
215219
def test_send_audio_local_files(self, monkeypatch, bot, chat_id):
216220
# For just test that the correct paths are passed as we have no local bot API set up
217221
test_flag = False
218-
expected = (Path.cwd() / 'tests/data/telegram.jpg/').as_uri()
219-
file = 'tests/data/telegram.jpg'
222+
file = data_file('telegram.jpg')
223+
expected = file.as_uri()
220224

221225
def make_assertion(_, data, *args, **kwargs):
222226
nonlocal test_flag

tests/test_bot.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import time
2222
import datetime as dtm
2323
from collections import defaultdict
24-
from pathlib import Path
2524
from platform import python_implementation
2625

2726
import pytest
@@ -59,9 +58,15 @@
5958
from telegram.ext import ExtBot, InvalidCallbackData
6059
from telegram.error import BadRequest, InvalidToken, NetworkError, RetryAfter, TelegramError
6160
from telegram._utils.datetime import from_timestamp, to_timestamp
62-
from telegram.helpers import escape_markdown
6361
from telegram._utils.defaultvalue import DefaultValue
64-
from tests.conftest import expect_bad_request, check_defaults_handling, GITHUB_ACTION, build_kwargs
62+
from telegram.helpers import escape_markdown
63+
from tests.conftest import (
64+
expect_bad_request,
65+
check_defaults_handling,
66+
GITHUB_ACTION,
67+
build_kwargs,
68+
data_file,
69+
)
6570
from tests.bots import FALLBACKS
6671

6772

@@ -99,7 +104,7 @@ def message(bot, chat_id):
99104

100105
@pytest.fixture(scope='class')
101106
def media_message(bot, chat_id):
102-
with Path('tests/data/telegram.ogg').open('rb') as f:
107+
with data_file('telegram.ogg').open('rb') as f:
103108
return bot.send_voice(chat_id, voice=f, caption='my caption', timeout=10)
104109

105110

@@ -1039,7 +1044,7 @@ def test_get_one_user_profile_photo(self, bot, chat_id):
10391044
# get_file is tested multiple times in the test_*media* modules.
10401045
# Here we only test the behaviour for bot apis in local mode
10411046
def test_get_file_local_mode(self, bot, monkeypatch):
1042-
path = str(Path.cwd() / 'tests' / 'data' / 'game.gif')
1047+
path = str(data_file('game.gif'))
10431048

10441049
def _post(*args, **kwargs):
10451050
return {
@@ -1924,14 +1929,14 @@ def test_set_chat_photo(self, bot, channel_id):
19241929
def func():
19251930
assert bot.set_chat_photo(channel_id, f)
19261931

1927-
with Path('tests/data/telegram_test_channel.jpg').open('rb') as f:
1932+
with data_file('telegram_test_channel.jpg').open('rb') as f:
19281933
expect_bad_request(func, 'Type of file mismatch', 'Telegram did not accept the file.')
19291934

19301935
def test_set_chat_photo_local_files(self, monkeypatch, bot, chat_id):
19311936
# For just test that the correct paths are passed as we have no local bot API set up
19321937
test_flag = False
1933-
expected = (Path.cwd() / 'tests/data/telegram.jpg/').as_uri()
1934-
file = 'tests/data/telegram.jpg'
1938+
file = data_file('telegram.jpg')
1939+
expected = file.as_uri()
19351940

19361941
def make_assertion(_, data, *args, **kwargs):
19371942
nonlocal test_flag
@@ -2008,7 +2013,7 @@ def request_wrapper(*args, **kwargs):
20082013

20092014
# Test file uploading
20102015
with pytest.raises(OkException):
2011-
bot.send_photo(chat_id, open('tests/data/telegram.jpg', 'rb'), timeout=TIMEOUT)
2016+
bot.send_photo(chat_id, data_file('telegram.jpg').open('rb'), timeout=TIMEOUT)
20122017

20132018
# Test JSON submission
20142019
with pytest.raises(OkException):
@@ -2032,7 +2037,7 @@ def request_wrapper(*args, **kwargs):
20322037

20332038
# Test file uploading
20342039
with pytest.raises(OkException):
2035-
bot.send_photo(chat_id, open('tests/data/telegram.jpg', 'rb'))
2040+
bot.send_photo(chat_id, data_file('telegram.jpg').open('rb'))
20362041

20372042
@flaky(3, 1)
20382043
def test_send_message_entities(self, bot, chat_id):

tests/test_chatphoto.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,13 @@
2929
check_shortcut_call,
3030
check_shortcut_signature,
3131
check_defaults_handling,
32+
data_file,
3233
)
3334

3435

3536
@pytest.fixture(scope='function')
3637
def chatphoto_file():
37-
f = open('tests/data/telegram.jpg', 'rb')
38+
f = data_file('telegram.jpg').open('rb')
3839
yield f
3940
f.close()
4041

@@ -68,23 +69,24 @@ def func():
6869

6970
@flaky(3, 1)
7071
def test_get_and_download(self, bot, chat_photo):
72+
jpg_file = Path('telegram.jpg')
7173
new_file = bot.get_file(chat_photo.small_file_id)
7274

7375
assert new_file.file_id == chat_photo.small_file_id
7476
assert new_file.file_path.startswith('https://')
7577

76-
new_file.download('telegram.jpg')
78+
new_file.download(jpg_file)
7779

78-
assert Path('telegram.jpg').is_file()
80+
assert jpg_file.is_file()
7981

8082
new_file = bot.get_file(chat_photo.big_file_id)
8183

8284
assert new_file.file_id == chat_photo.big_file_id
8385
assert new_file.file_path.startswith('https://')
8486

85-
new_file.download('telegram.jpg')
87+
new_file.download(jpg_file)
8688

87-
assert Path('telegram.jpg').is_file()
89+
assert jpg_file.is_file()
8890

8991
def test_send_with_chat_photo(self, monkeypatch, bot, super_group_id, chat_photo):
9092
def test(url, data, **kwargs):

tests/test_constants.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@
1616
#
1717
# You should have received a copy of the GNU Lesser Public License
1818
# along with this program. If not, see [http://www.gnu.org/licenses/].
19-
from pathlib import Path
20-
2119
import pytest
2220
from flaky import flaky
2321

2422
from telegram import constants
2523
from telegram.error import BadRequest
24+
from tests.conftest import data_file
2625

2726

2827
class TestConstants:
@@ -39,13 +38,11 @@ def test_max_message_length(self, bot, chat_id):
3938
@flaky(3, 1)
4039
def test_max_caption_length(self, bot, chat_id):
4140
good_caption = 'a' * constants.MAX_CAPTION_LENGTH
42-
with Path('tests/data/telegram.png').open('rb') as f:
41+
with data_file('telegram.png').open('rb') as f:
4342
good_msg = bot.send_photo(photo=f, caption=good_caption, chat_id=chat_id)
4443
assert good_msg.caption == good_caption
4544

4645
bad_caption = good_caption + 'Z'
47-
with pytest.raises(
48-
BadRequest,
49-
match="Media_caption_too_long",
50-
), open('tests/data/telegram.png', 'rb') as f:
46+
match = "Media_caption_too_long"
47+
with pytest.raises(BadRequest, match=match), data_file('telegram.png').open('rb') as f:
5148
bot.send_photo(photo=f, caption=bad_caption, chat_id=chat_id)

tests/test_document.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,24 @@
2525
from telegram import Document, PhotoSize, Voice, MessageEntity, Bot
2626
from telegram.error import BadRequest, TelegramError
2727
from telegram.helpers import escape_markdown
28-
from tests.conftest import check_shortcut_signature, check_shortcut_call, check_defaults_handling
28+
from tests.conftest import (
29+
check_shortcut_signature,
30+
check_shortcut_call,
31+
check_defaults_handling,
32+
data_file,
33+
)
2934

3035

3136
@pytest.fixture(scope='function')
3237
def document_file():
33-
f = open('tests/data/telegram.png', 'rb')
38+
f = data_file('telegram.png').open('rb')
3439
yield f
3540
f.close()
3641

3742

3843
@pytest.fixture(scope='class')
3944
def document(bot, chat_id):
40-
with Path('tests/data/telegram.png').open('rb') as f:
45+
with data_file('telegram.png').open('rb') as f:
4146
return bot.send_document(chat_id, document=f, timeout=50).document
4247

4348

@@ -239,8 +244,8 @@ def test_send_document_default_allow_sending_without_reply(
239244
def test_send_document_local_files(self, monkeypatch, bot, chat_id):
240245
# For just test that the correct paths are passed as we have no local bot API set up
241246
test_flag = False
242-
expected = (Path.cwd() / 'tests/data/telegram.jpg/').as_uri()
243-
file = 'tests/data/telegram.jpg'
247+
file = data_file('telegram.jpg')
248+
expected = file.as_uri()
244249

245250
def make_assertion(_, data, *args, **kwargs):
246251
nonlocal test_flag

tests/test_file.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
from telegram import File, Voice
2727
from telegram.error import TelegramError
28+
from tests.conftest import data_file
2829

2930

3031
@pytest.fixture(scope='class')
@@ -43,7 +44,7 @@ def local_file(bot):
4344
return File(
4445
TestFile.file_id,
4546
TestFile.file_unique_id,
46-
file_path=str(Path.cwd() / 'tests' / 'data' / 'local_file.txt'),
47+
file_path=str(data_file('local_file.txt')),
4748
file_size=TestFile.file_size,
4849
bot=bot,
4950
)

0 commit comments

Comments
 (0)