Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
331c4f5
Temporarily enable tests for the v14 branch
Bibo-Joshi Jul 14, 2021
9dc30cf
Move and Rename TelegramDecryptionError to telegram.error.PassportDec…
harshil21 Aug 11, 2021
b089cc9
Add Code Comment Guidelines to Contribution Guide (#2612)
Poolitzer Aug 11, 2021
3a19383
Improve Type Hinting for CallbackContext (#2587)
revolter Aug 12, 2021
414a18c
Add Custom pytest Marker to Ease Development (#2628)
Poolitzer Aug 12, 2021
8edb49d
Make BasePersistence Methods Abstract (#2624)
starry-shivam Aug 12, 2021
cce9c18
Refactor Initialization of Persistence Classes (#2604)
Bibo-Joshi Aug 13, 2021
dc0207a
Remove `__dict__` from `__slots__` and drop Python 3.6 (#2619, #2636)
harshil21 Aug 19, 2021
4af252f
Remove day_is_strict argument of JobQueue.run_monthly (#2634)
iota-008 Aug 26, 2021
b6e4b54
Drop Non-CallbackContext API (#2617)
Poolitzer Aug 29, 2021
4359812
Fix Signatures and Improve test_official (#2643)
harshil21 Aug 29, 2021
dbdd35e
Remove Deprecated Functionality (#2644)
Bibo-Joshi Aug 30, 2021
a7c7c82
Add User Friendly Type Check For Init Of {Inline, Reply}KeyboardMarku…
harshil21 Sep 15, 2021
8ac65fc
Refine Dispatcher.dispatch_error (#2660)
Bibo-Joshi Sep 17, 2021
66f0604
Warnings Overhaul (#2662)
harshil21 Sep 20, 2021
4f21c06
Clear Up Import Policy (#2671)
Bibo-Joshi Sep 22, 2021
5542a46
Make InlineQuery.answer Raise ValueError (#2675)
Bibo-Joshi Sep 24, 2021
2cf9311
Doc Fixes (#2597)
Bibo-Joshi Oct 1, 2021
bcc62bb
Merge remote-tracking branch 'upstream/v14' into v14
eldbud Oct 3, 2021
9ed515a
Merge remote-tracking branch 'upstream/v14' into v14
eldbud Oct 7, 2021
53e9b0e
create test_data_path fixture
eldbud Oct 8, 2021
a2b01a5
on it
eldbud Oct 8, 2021
46b5a7c
in the process of making tests use the newly created data_file and cl…
eldbud Oct 9, 2021
0745488
added PROJECT_ROOT_PATH and TEST_DATA_PATH constants, and data_file f…
eldbud Oct 11, 2021
d82473a
Merge remote-tracking branch 'upstream/v14' into v14
eldbud Oct 11, 2021
c581f27
Merge branch 'v14' into Make_file_paths_in_tests_agnostic_to_executio…
eldbud Oct 11, 2021
dd0bead
removed leftover print
eldbud Oct 11, 2021
fb41e1b
closes #2701
eldbud Oct 11, 2021
3193994
closes #2701
eldbud Oct 11, 2021
870e9d3
fixed test_updater failure, now tests pass from any working dir
eldbud Oct 12, 2021
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
13 changes: 11 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import os
import re
from collections import defaultdict
from pathlib import Path
from queue import Queue
from threading import Thread, Event
from time import sleep
Expand Down Expand Up @@ -218,16 +219,24 @@ def updater(bot):
up.stop()


PROJECT_ROOT_PATH = Path(__file__).parent.parent.resolve()
TEST_DATA_PATH = Path(__file__).parent.resolve() / "data"


def data_file(filename: str):
return TEST_DATA_PATH / filename


@pytest.fixture(scope='function')
def thumb_file():
f = open('tests/data/thumb.jpg', 'rb')
f = data_file('thumb.jpg').open('rb')
yield f
f.close()


@pytest.fixture(scope='class')
def class_thumb_file():
f = open('tests/data/thumb.jpg', 'rb')
f = data_file('thumb.jpg').open('rb')
yield f
f.close()

Expand Down
18 changes: 12 additions & 6 deletions tests/test_animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,27 @@
from telegram import PhotoSize, Animation, Voice, MessageEntity, Bot
from telegram.error import BadRequest, TelegramError
from telegram.helpers import escape_markdown
from tests.conftest import check_shortcut_call, check_shortcut_signature, check_defaults_handling
from tests.conftest import (
check_shortcut_call,
check_shortcut_signature,
check_defaults_handling,
data_file,
)


@pytest.fixture(scope='function')
def animation_file():
f = Path('tests/data/game.gif').open('rb')
f = data_file('game.gif').open('rb')
yield f
f.close()


@pytest.fixture(scope='class')
def animation(bot, chat_id):
with Path('tests/data/game.gif').open('rb') as f:
with data_file('game.gif').open('rb') as f:
thumb = data_file('thumb.jpg')
return bot.send_animation(
chat_id, animation=f, timeout=50, thumb=open('tests/data/thumb.jpg', 'rb')
chat_id, animation=f, timeout=50, thumb=thumb.open('rb')
).animation


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

def make_assertion(_, data, *args, **kwargs):
nonlocal test_flag
Expand Down
20 changes: 12 additions & 8 deletions tests/test_audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,26 @@
from telegram import Audio, Voice, MessageEntity, Bot
from telegram.error import TelegramError
from telegram.helpers import escape_markdown
from tests.conftest import check_shortcut_call, check_shortcut_signature, check_defaults_handling
from tests.conftest import (
check_shortcut_call,
check_shortcut_signature,
check_defaults_handling,
data_file,
)


@pytest.fixture(scope='function')
def audio_file():
f = Path('tests/data/telegram.mp3').open('rb')
f = data_file('telegram.mp3').open('rb')
yield f
f.close()


@pytest.fixture(scope='class')
def audio(bot, chat_id):
with Path('tests/data/telegram.mp3').open('rb') as f:
return bot.send_audio(
chat_id, audio=f, timeout=50, thumb=Path('tests/data/thumb.jpg').open('rb')
).audio
with data_file('telegram.mp3').open('rb') as f:
thumb = data_file('thumb.jpg')
return bot.send_audio(chat_id, audio=f, timeout=50, thumb=thumb.open('rb')).audio


class TestAudio:
Expand Down Expand Up @@ -213,8 +217,8 @@ def test_send_audio_default_parse_mode_3(self, default_bot, chat_id, audio_file,
def test_send_audio_local_files(self, monkeypatch, bot, chat_id):
# For just test that the correct paths are passed as we have no local bot API set up
test_flag = False
expected = (Path.cwd() / 'tests/data/telegram.jpg/').as_uri()
file = 'tests/data/telegram.jpg'
file = data_file('telegram.jpg')
expected = file.as_uri()

def make_assertion(_, data, *args, **kwargs):
nonlocal test_flag
Expand Down
25 changes: 15 additions & 10 deletions tests/test_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import time
import datetime as dtm
from collections import defaultdict
from pathlib import Path
from platform import python_implementation

import pytest
Expand Down Expand Up @@ -59,9 +58,15 @@
from telegram.ext import ExtBot, InvalidCallbackData
from telegram.error import BadRequest, InvalidToken, NetworkError, RetryAfter, TelegramError
from telegram._utils.datetime import from_timestamp, to_timestamp
from telegram.helpers import escape_markdown
from telegram._utils.defaultvalue import DefaultValue
from tests.conftest import expect_bad_request, check_defaults_handling, GITHUB_ACTION, build_kwargs
from telegram.helpers import escape_markdown
from tests.conftest import (
expect_bad_request,
check_defaults_handling,
GITHUB_ACTION,
build_kwargs,
data_file,
)
from tests.bots import FALLBACKS


Expand Down Expand Up @@ -99,7 +104,7 @@ def message(bot, chat_id):

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


Expand Down Expand Up @@ -1012,7 +1017,7 @@ def test_get_one_user_profile_photo(self, bot, chat_id):
# get_file is tested multiple times in the test_*media* modules.
# Here we only test the behaviour for bot apis in local mode
def test_get_file_local_mode(self, bot, monkeypatch):
path = str(Path.cwd() / 'tests' / 'data' / 'game.gif')
path = str(data_file('game.gif'))

def _post(*args, **kwargs):
return {
Expand Down Expand Up @@ -1795,14 +1800,14 @@ def test_set_chat_photo(self, bot, channel_id):
def func():
assert bot.set_chat_photo(channel_id, f)

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

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

def make_assertion(_, data, *args, **kwargs):
nonlocal test_flag
Expand Down Expand Up @@ -1879,7 +1884,7 @@ def request_wrapper(*args, **kwargs):

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

# Test JSON submission
with pytest.raises(OkException):
Expand All @@ -1903,7 +1908,7 @@ def request_wrapper(*args, **kwargs):

# Test file uploading
with pytest.raises(OkException):
bot.send_photo(chat_id, open('tests/data/telegram.jpg', 'rb'))
bot.send_photo(chat_id, data_file('telegram.jpg').open('rb'))

@flaky(3, 1)
def test_send_message_entities(self, bot, chat_id):
Expand Down
12 changes: 7 additions & 5 deletions tests/test_chatphoto.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@
check_shortcut_call,
check_shortcut_signature,
check_defaults_handling,
data_file,
)


@pytest.fixture(scope='function')
def chatphoto_file():
f = open('tests/data/telegram.jpg', 'rb')
f = data_file('telegram.jpg').open('rb')
yield f
f.close()

Expand Down Expand Up @@ -68,23 +69,24 @@ def func():

@flaky(3, 1)
def test_get_and_download(self, bot, chat_photo):
jpg_file = Path('telegram.jpg')
new_file = bot.get_file(chat_photo.small_file_id)

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

new_file.download('telegram.jpg')
new_file.download(jpg_file)

assert Path('telegram.jpg').is_file()
assert jpg_file.is_file()

new_file = bot.get_file(chat_photo.big_file_id)

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

new_file.download('telegram.jpg')
new_file.download(jpg_file)

assert Path('telegram.jpg').is_file()
assert jpg_file.is_file()

def test_send_with_chat_photo(self, monkeypatch, bot, super_group_id, chat_photo):
def test(url, data, **kwargs):
Expand Down
11 changes: 4 additions & 7 deletions tests/test_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@
#
# You should have received a copy of the GNU Lesser Public License
# along with this program. If not, see [http://www.gnu.org/licenses/].
from pathlib import Path

import pytest
from flaky import flaky

from telegram import constants
from telegram.error import BadRequest
from tests.conftest import data_file


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

bad_caption = good_caption + 'Z'
with pytest.raises(
BadRequest,
match="Media_caption_too_long",
), open('tests/data/telegram.png', 'rb') as f:
match = "Media_caption_too_long"
with pytest.raises(BadRequest, match=match), data_file('telegram.png').open('rb') as f:
bot.send_photo(photo=f, caption=bad_caption, chat_id=chat_id)
15 changes: 10 additions & 5 deletions tests/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,24 @@
from telegram import Document, PhotoSize, Voice, MessageEntity, Bot
from telegram.error import BadRequest, TelegramError
from telegram.helpers import escape_markdown
from tests.conftest import check_shortcut_signature, check_shortcut_call, check_defaults_handling
from tests.conftest import (
check_shortcut_signature,
check_shortcut_call,
check_defaults_handling,
data_file,
)


@pytest.fixture(scope='function')
def document_file():
f = open('tests/data/telegram.png', 'rb')
f = data_file('telegram.png').open('rb')
yield f
f.close()


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


Expand Down Expand Up @@ -237,8 +242,8 @@ def test_send_document_default_allow_sending_without_reply(
def test_send_document_local_files(self, monkeypatch, bot, chat_id):
# For just test that the correct paths are passed as we have no local bot API set up
test_flag = False
expected = (Path.cwd() / 'tests/data/telegram.jpg/').as_uri()
file = 'tests/data/telegram.jpg'
file = data_file('telegram.jpg')
expected = file.as_uri()

def make_assertion(_, data, *args, **kwargs):
nonlocal test_flag
Expand Down
3 changes: 2 additions & 1 deletion tests/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

from telegram import File, Voice
from telegram.error import TelegramError
from tests.conftest import data_file


@pytest.fixture(scope='class')
Expand All @@ -43,7 +44,7 @@ def local_file(bot):
return File(
TestFile.file_id,
TestFile.file_unique_id,
file_path=str(Path.cwd() / 'tests' / 'data' / 'local_file.txt'),
file_path=str(data_file('local_file.txt')),
file_size=TestFile.file_size,
bot=bot,
)
Expand Down
Loading