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

Skip to content

Enforce the offline_bot fixture in Test*WithoutRequest #4465

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Sep 13, 2024
32 changes: 18 additions & 14 deletions tests/_files/test_animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def test_expected_values(self, animation):
assert animation.file_name.startswith("game.gif") == self.file_name.startswith("game.gif")
assert isinstance(animation.thumbnail, PhotoSize)

def test_de_json(self, bot, animation):
def test_de_json(self, offline_bot, animation):
json_dict = {
"file_id": self.animation_file_id,
"file_unique_id": self.animation_file_unique_id,
Expand All @@ -96,7 +96,7 @@ def test_de_json(self, bot, animation):
"mime_type": self.mime_type,
"file_size": self.file_size,
}
animation = Animation.de_json(json_dict, bot)
animation = Animation.de_json(json_dict, offline_bot)
assert animation.api_kwargs == {}
assert animation.file_id == self.animation_file_id
assert animation.file_unique_id == self.animation_file_unique_id
Expand Down Expand Up @@ -140,18 +140,22 @@ def test_equality(self):
assert a != e
assert hash(a) != hash(e)

async def test_send_animation_custom_filename(self, bot, chat_id, animation_file, monkeypatch):
async def test_send_animation_custom_filename(
self, offline_bot, chat_id, animation_file, monkeypatch
):
async def make_assertion(url, request_data: RequestData, *args, **kwargs):
return next(iter(request_data.multipart_data.values()))[0] == "custom_filename"

monkeypatch.setattr(bot.request, "post", make_assertion)
assert await bot.send_animation(chat_id, animation_file, filename="custom_filename")
monkeypatch.setattr(offline_bot.request, "post", make_assertion)
assert await offline_bot.send_animation(
chat_id, animation_file, filename="custom_filename"
)

@pytest.mark.parametrize("local_mode", [True, False])
async def test_send_animation_local_files(self, monkeypatch, bot, chat_id, local_mode):
async def test_send_animation_local_files(self, monkeypatch, offline_bot, chat_id, local_mode):
try:
bot._local_mode = local_mode
# For just test that the correct paths are passed as we have no local bot API set up
offline_bot._local_mode = local_mode
# For just test that the correct paths are passed as we have no local Bot API set up
test_flag = False
file = data_file("telegram.jpg")
expected = file.as_uri()
Expand All @@ -167,18 +171,18 @@ async def make_assertion(_, data, *args, **kwargs):
data.get("thumbnail"), InputFile
)

monkeypatch.setattr(bot, "_post", make_assertion)
await bot.send_animation(chat_id, file, thumbnail=file)
monkeypatch.setattr(offline_bot, "_post", make_assertion)
await offline_bot.send_animation(chat_id, file, thumbnail=file)
assert test_flag
finally:
bot._local_mode = False
offline_bot._local_mode = False

async def test_send_with_animation(self, monkeypatch, bot, chat_id, animation):
async def test_send_with_animation(self, monkeypatch, offline_bot, chat_id, animation):
async def make_assertion(url, request_data: RequestData, *args, **kwargs):
return request_data.json_parameters["animation"] == animation.file_id

monkeypatch.setattr(bot.request, "post", make_assertion)
assert await bot.send_animation(animation=animation, chat_id=chat_id)
monkeypatch.setattr(offline_bot.request, "post", make_assertion)
assert await offline_bot.send_animation(animation=animation, chat_id=chat_id)

async def test_get_file_instance_method(self, monkeypatch, animation):
async def make_assertion(*_, **kwargs):
Expand Down
28 changes: 14 additions & 14 deletions tests/_files/test_audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def test_expected_values(self, audio):
assert audio.thumbnail.width == self.thumb_width
assert audio.thumbnail.height == self.thumb_height

def test_de_json(self, bot, audio):
def test_de_json(self, offline_bot, audio):
json_dict = {
"file_id": self.audio_file_id,
"file_unique_id": self.audio_file_unique_id,
Expand All @@ -103,7 +103,7 @@ def test_de_json(self, bot, audio):
"file_size": self.file_size,
"thumbnail": audio.thumbnail.to_dict(),
}
json_audio = Audio.de_json(json_dict, bot)
json_audio = Audio.de_json(json_dict, offline_bot)
assert json_audio.api_kwargs == {}

assert json_audio.file_id == self.audio_file_id
Expand Down Expand Up @@ -147,25 +147,25 @@ def test_equality(self, audio):
assert a != e
assert hash(a) != hash(e)

async def test_send_with_audio(self, monkeypatch, bot, chat_id, audio):
async def test_send_with_audio(self, monkeypatch, offline_bot, chat_id, audio):
async def make_assertion(url, request_data: RequestData, *args, **kwargs):
return request_data.json_parameters["audio"] == audio.file_id

monkeypatch.setattr(bot.request, "post", make_assertion)
assert await bot.send_audio(audio=audio, chat_id=chat_id)
monkeypatch.setattr(offline_bot.request, "post", make_assertion)
assert await offline_bot.send_audio(audio=audio, chat_id=chat_id)

async def test_send_audio_custom_filename(self, bot, chat_id, audio_file, monkeypatch):
async def test_send_audio_custom_filename(self, offline_bot, chat_id, audio_file, monkeypatch):
async def make_assertion(url, request_data: RequestData, *args, **kwargs):
return next(iter(request_data.multipart_data.values()))[0] == "custom_filename"

monkeypatch.setattr(bot.request, "post", make_assertion)
assert await bot.send_audio(chat_id, audio_file, filename="custom_filename")
monkeypatch.setattr(offline_bot.request, "post", make_assertion)
assert await offline_bot.send_audio(chat_id, audio_file, filename="custom_filename")

@pytest.mark.parametrize("local_mode", [True, False])
async def test_send_audio_local_files(self, monkeypatch, bot, chat_id, local_mode):
async def test_send_audio_local_files(self, monkeypatch, offline_bot, chat_id, local_mode):
try:
bot._local_mode = local_mode
# For just test that the correct paths are passed as we have no local bot API set up
offline_bot._local_mode = local_mode
# For just test that the correct paths are passed as we have no local Bot API set up
test_flag = False
file = data_file("telegram.jpg")
expected = file.as_uri()
Expand All @@ -179,11 +179,11 @@ async def make_assertion(_, data, *args, **kwargs):
data.get("thumbnail"), InputFile
)

monkeypatch.setattr(bot, "_post", make_assertion)
await bot.send_audio(chat_id, file, thumbnail=file)
monkeypatch.setattr(offline_bot, "_post", make_assertion)
await offline_bot.send_audio(chat_id, file, thumbnail=file)
assert test_flag
finally:
bot._local_mode = False
offline_bot._local_mode = False

async def test_get_file_instance_method(self, monkeypatch, audio):
async def make_assertion(*_, **kwargs):
Expand Down
12 changes: 7 additions & 5 deletions tests/_files/test_chatphoto.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ def test_slot_behaviour(self, chat_photo):
assert getattr(chat_photo, attr, "err") != "err", f"got extra slot '{attr}'"
assert len(mro_slots(chat_photo)) == len(set(mro_slots(chat_photo))), "duplicate slot"

def test_de_json(self, bot, chat_photo):
def test_de_json(self, offline_bot, chat_photo):
json_dict = {
"small_file_id": self.chatphoto_small_file_id,
"big_file_id": self.chatphoto_big_file_id,
"small_file_unique_id": self.chatphoto_small_file_unique_id,
"big_file_unique_id": self.chatphoto_big_file_unique_id,
}
chat_photo = ChatPhoto.de_json(json_dict, bot)
chat_photo = ChatPhoto.de_json(json_dict, offline_bot)
assert chat_photo.api_kwargs == {}
assert chat_photo.small_file_id == self.chatphoto_small_file_id
assert chat_photo.big_file_id == self.chatphoto_big_file_id
Expand Down Expand Up @@ -121,12 +121,14 @@ def test_equality(self):
assert a != e
assert hash(a) != hash(e)

async def test_send_with_chat_photo(self, monkeypatch, bot, super_group_id, chat_photo):
async def test_send_with_chat_photo(
self, monkeypatch, offline_bot, super_group_id, chat_photo
):
async def make_assertion(url, request_data: RequestData, *args, **kwargs):
return request_data.parameters["photo"] == chat_photo.to_dict()

monkeypatch.setattr(bot.request, "post", make_assertion)
message = await bot.set_chat_photo(photo=chat_photo, chat_id=super_group_id)
monkeypatch.setattr(offline_bot.request, "post", make_assertion)
message = await offline_bot.set_chat_photo(photo=chat_photo, chat_id=super_group_id)
assert message

async def test_get_small_file_instance_method(self, monkeypatch, chat_photo):
Expand Down
22 changes: 11 additions & 11 deletions tests/_files/test_contact.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,22 @@ def test_slot_behaviour(self, contact):
assert getattr(contact, attr, "err") != "err", f"got extra slot '{attr}'"
assert len(mro_slots(contact)) == len(set(mro_slots(contact))), "duplicate slot"

def test_de_json_required(self, bot):
def test_de_json_required(self, offline_bot):
json_dict = {"phone_number": self.phone_number, "first_name": self.first_name}
contact = Contact.de_json(json_dict, bot)
contact = Contact.de_json(json_dict, offline_bot)
assert contact.api_kwargs == {}

assert contact.phone_number == self.phone_number
assert contact.first_name == self.first_name

def test_de_json_all(self, bot):
def test_de_json_all(self, offline_bot):
json_dict = {
"phone_number": self.phone_number,
"first_name": self.first_name,
"last_name": self.last_name,
"user_id": self.user_id,
}
contact = Contact.de_json(json_dict, bot)
contact = Contact.de_json(json_dict, offline_bot)
assert contact.api_kwargs == {}

assert contact.phone_number == self.phone_number
Expand Down Expand Up @@ -104,29 +104,29 @@ def test_equality(self):
assert a != e
assert hash(a) != hash(e)

async def test_send_contact_without_required(self, bot, chat_id):
async def test_send_contact_without_required(self, offline_bot, chat_id):
with pytest.raises(ValueError, match="Either contact or phone_number and first_name"):
await bot.send_contact(chat_id=chat_id)
await offline_bot.send_contact(chat_id=chat_id)

async def test_send_mutually_exclusive(self, bot, chat_id, contact):
async def test_send_mutually_exclusive(self, offline_bot, chat_id, contact):
with pytest.raises(ValueError, match="Not both"):
await bot.send_contact(
await offline_bot.send_contact(
chat_id=chat_id,
contact=contact,
phone_number=contact.phone_number,
first_name=contact.first_name,
)

async def test_send_with_contact(self, monkeypatch, bot, chat_id, contact):
async def test_send_with_contact(self, monkeypatch, offline_bot, chat_id, contact):
async def make_assertion(url, request_data: RequestData, *args, **kwargs):
data = request_data.json_parameters
phone = data["phone_number"] == contact.phone_number
first = data["first_name"] == contact.first_name
last = data["last_name"] == contact.last_name
return phone and first and last

monkeypatch.setattr(bot.request, "post", make_assertion)
assert await bot.send_contact(contact=contact, chat_id=chat_id)
monkeypatch.setattr(offline_bot.request, "post", make_assertion)
assert await offline_bot.send_contact(contact=contact, chat_id=chat_id)

@pytest.mark.parametrize(
("default_bot", "custom"),
Expand Down
26 changes: 13 additions & 13 deletions tests/_files/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def test_expected_values(self, document):
assert document.thumbnail.width == self.thumb_width
assert document.thumbnail.height == self.thumb_height

def test_de_json(self, bot, document):
def test_de_json(self, offline_bot, document):
json_dict = {
"file_id": self.document_file_id,
"file_unique_id": self.document_file_unique_id,
Expand All @@ -92,7 +92,7 @@ def test_de_json(self, bot, document):
"mime_type": self.mime_type,
"file_size": self.file_size,
}
test_document = Document.de_json(json_dict, bot)
test_document = Document.de_json(json_dict, offline_bot)
assert test_document.api_kwargs == {}

assert test_document.file_id == self.document_file_id
Expand Down Expand Up @@ -128,13 +128,13 @@ def test_equality(self, document):
assert a != e
assert hash(a) != hash(e)

async def test_error_send_without_required_args(self, bot, chat_id):
async def test_error_send_without_required_args(self, offline_bot, chat_id):
with pytest.raises(TypeError):
await bot.send_document(chat_id=chat_id)
await offline_bot.send_document(chat_id=chat_id)

@pytest.mark.parametrize("disable_content_type_detection", [True, False, None])
async def test_send_with_document(
self, monkeypatch, bot, chat_id, document, disable_content_type_detection
self, monkeypatch, offline_bot, chat_id, document, disable_content_type_detection
):
async def make_assertion(url, request_data: RequestData, *args, **kwargs):
data = request_data.parameters
Expand All @@ -143,9 +143,9 @@ async def make_assertion(url, request_data: RequestData, *args, **kwargs):
)
return data["document"] == document.file_id and type_detection

monkeypatch.setattr(bot.request, "post", make_assertion)
monkeypatch.setattr(offline_bot.request, "post", make_assertion)

message = await bot.send_document(
message = await offline_bot.send_document(
document=document,
chat_id=chat_id,
disable_content_type_detection=disable_content_type_detection,
Expand Down Expand Up @@ -181,10 +181,10 @@ async def make_assertion(url, request_data: RequestData, *args, **kwargs):
)

@pytest.mark.parametrize("local_mode", [True, False])
async def test_send_document_local_files(self, monkeypatch, bot, chat_id, local_mode):
async def test_send_document_local_files(self, monkeypatch, offline_bot, chat_id, local_mode):
try:
bot._local_mode = local_mode
# For just test that the correct paths are passed as we have no local bot API set up
offline_bot._local_mode = local_mode
# For just test that the correct paths are passed as we have no local Bot API set up
test_flag = False
file = data_file("telegram.jpg")
expected = file.as_uri()
Expand All @@ -200,11 +200,11 @@ async def make_assertion(_, data, *args, **kwargs):
data.get("thumbnail"), InputFile
)

monkeypatch.setattr(bot, "_post", make_assertion)
await bot.send_document(chat_id, file, thumbnail=file)
monkeypatch.setattr(offline_bot, "_post", make_assertion)
await offline_bot.send_document(chat_id, file, thumbnail=file)
assert test_flag
finally:
bot._local_mode = False
offline_bot._local_mode = False

async def test_get_file_instance_method(self, monkeypatch, document):
async def make_assertion(*_, **kwargs):
Expand Down
14 changes: 7 additions & 7 deletions tests/_files/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,14 @@ def test_slot_behaviour(self, file):
assert getattr(file, attr, "err") != "err", f"got extra slot '{attr}'"
assert len(mro_slots(file)) == len(set(mro_slots(file))), "duplicate slot"

def test_de_json(self, bot):
def test_de_json(self, offline_bot):
json_dict = {
"file_id": self.file_id,
"file_unique_id": self.file_unique_id,
"file_path": self.file_path,
"file_size": self.file_size,
}
new_file = File.de_json(json_dict, bot)
new_file = File.de_json(json_dict, offline_bot)
assert new_file.api_kwargs == {}

assert new_file.file_id == self.file_id
Expand All @@ -131,11 +131,11 @@ def test_to_dict(self, file):
assert file_dict["file_path"] == file.file_path
assert file_dict["file_size"] == file.file_size

def test_equality(self, bot):
a = File(self.file_id, self.file_unique_id, bot)
b = File("", self.file_unique_id, bot)
def test_equality(self, offline_bot):
a = File(self.file_id, self.file_unique_id, offline_bot)
b = File("", self.file_unique_id, offline_bot)
c = File(self.file_id, self.file_unique_id, None)
d = File("", "", bot)
d = File("", "", offline_bot)
e = Voice(self.file_id, self.file_unique_id, 0)

assert a == b
Expand Down Expand Up @@ -223,7 +223,7 @@ async def test(*args, **kwargs):
assert buf2[len(buf) :] == buf
assert buf2[: len(buf)] == buf

async def test_download_encrypted(self, monkeypatch, bot, encrypted_file):
async def test_download_encrypted(self, monkeypatch, offline_bot, encrypted_file):
async def test(*args, **kwargs):
return data_file("image_encrypted.jpg").read_bytes()

Expand Down
Loading
Loading