2022-04-24 12:38:09 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# A library that provides a Python interface to the Telegram Bot API
|
2024-02-19 20:06:25 +01:00
|
|
|
# Copyright (C) 2015-2024
|
2022-04-24 12:38:09 +02:00
|
|
|
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Lesser Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Lesser Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser Public License
|
|
|
|
# along with this program. If not, see [http://www.gnu.org/licenses/].
|
|
|
|
import datetime
|
2023-01-01 14:24:30 +01:00
|
|
|
from typing import Sequence
|
2022-04-24 12:38:09 +02:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2023-03-25 11:47:26 +01:00
|
|
|
from telegram import InputFile, InputMediaPhoto, InputMediaVideo, InputSticker, MessageEntity
|
2022-04-24 12:38:09 +02:00
|
|
|
from telegram.constants import ChatType
|
|
|
|
from telegram.request._requestparameter import RequestParameter
|
2023-02-22 20:19:46 +01:00
|
|
|
from tests.auxil.files import data_file
|
|
|
|
from tests.auxil.slots import mro_slots
|
2022-04-24 12:38:09 +02:00
|
|
|
|
|
|
|
|
2023-02-11 10:45:17 +01:00
|
|
|
class TestRequestParameterWithoutRequest:
|
2023-02-22 20:19:46 +01:00
|
|
|
def test_slot_behaviour(self):
|
2023-02-11 10:45:17 +01:00
|
|
|
inst = RequestParameter("name", "value", [1, 2])
|
|
|
|
for attr in inst.__slots__:
|
|
|
|
assert getattr(inst, attr, "err") != "err", f"got extra slot '{attr}'"
|
|
|
|
assert len(mro_slots(inst)) == len(set(mro_slots(inst))), "duplicate slot"
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
def test_init(self):
|
2022-05-05 17:40:22 +02:00
|
|
|
request_parameter = RequestParameter("name", "value", [1, 2])
|
|
|
|
assert request_parameter.name == "name"
|
|
|
|
assert request_parameter.value == "value"
|
2022-04-24 12:38:09 +02:00
|
|
|
assert request_parameter.input_files == [1, 2]
|
|
|
|
|
2022-05-05 17:40:22 +02:00
|
|
|
request_parameter = RequestParameter("name", "value", None)
|
|
|
|
assert request_parameter.name == "name"
|
|
|
|
assert request_parameter.value == "value"
|
2022-04-24 12:38:09 +02:00
|
|
|
assert request_parameter.input_files is None
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
2023-03-25 19:18:04 +01:00
|
|
|
("value", "expected"),
|
2022-04-24 12:38:09 +02:00
|
|
|
[
|
2022-05-05 17:40:22 +02:00
|
|
|
(1, "1"),
|
|
|
|
("one", "one"),
|
|
|
|
(True, "true"),
|
2022-04-24 12:38:09 +02:00
|
|
|
(None, None),
|
2022-05-05 17:40:22 +02:00
|
|
|
([1, "1"], '[1, "1"]'),
|
2022-04-24 12:38:09 +02:00
|
|
|
({True: None}, '{"true": null}'),
|
2022-05-05 17:40:22 +02:00
|
|
|
((1,), "[1]"),
|
2022-04-24 12:38:09 +02:00
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_json_value(self, value, expected):
|
2022-05-05 17:40:22 +02:00
|
|
|
request_parameter = RequestParameter("name", value, None)
|
2022-04-24 12:38:09 +02:00
|
|
|
assert request_parameter.json_value == expected
|
|
|
|
|
|
|
|
def test_multiple_multipart_data(self):
|
2022-05-05 17:40:22 +02:00
|
|
|
assert RequestParameter("name", "value", []).multipart_data is None
|
2022-04-24 12:38:09 +02:00
|
|
|
|
2022-05-05 17:40:22 +02:00
|
|
|
input_file_1 = InputFile("data1", attach=True)
|
|
|
|
input_file_2 = InputFile("data2", filename="custom")
|
2022-04-24 12:38:09 +02:00
|
|
|
request_parameter = RequestParameter(
|
2022-05-05 17:40:22 +02:00
|
|
|
value="value", name="name", input_files=[input_file_1, input_file_2]
|
2022-04-24 12:38:09 +02:00
|
|
|
)
|
|
|
|
files = request_parameter.multipart_data
|
|
|
|
assert files[input_file_1.attach_name] == input_file_1.field_tuple
|
2022-05-05 17:40:22 +02:00
|
|
|
assert files["name"] == input_file_2.field_tuple
|
2022-04-24 12:38:09 +02:00
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
2022-05-05 17:40:22 +02:00
|
|
|
("value", "expected_value"),
|
2022-04-24 12:38:09 +02:00
|
|
|
[
|
|
|
|
(True, True),
|
2022-05-05 17:40:22 +02:00
|
|
|
("str", "str"),
|
2022-04-24 12:38:09 +02:00
|
|
|
({1: 1.0}, {1: 1.0}),
|
2022-05-05 17:40:22 +02:00
|
|
|
(ChatType.PRIVATE, "private"),
|
|
|
|
(MessageEntity("type", 1, 1), {"type": "type", "offset": 1, "length": 1}),
|
2022-05-05 09:27:54 +02:00
|
|
|
(datetime.datetime(2019, 11, 11, 0, 26, 16, 10**5), 1573431976),
|
2022-04-24 12:38:09 +02:00
|
|
|
(
|
|
|
|
[
|
|
|
|
True,
|
2022-05-05 17:40:22 +02:00
|
|
|
"str",
|
|
|
|
MessageEntity("type", 1, 1),
|
2022-04-24 12:38:09 +02:00
|
|
|
ChatType.PRIVATE,
|
2022-05-05 09:27:54 +02:00
|
|
|
datetime.datetime(2019, 11, 11, 0, 26, 16, 10**5),
|
2022-04-24 12:38:09 +02:00
|
|
|
],
|
2022-05-05 17:40:22 +02:00
|
|
|
[True, "str", {"type": "type", "offset": 1, "length": 1}, "private", 1573431976],
|
2022-04-24 12:38:09 +02:00
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_from_input_no_media(self, value, expected_value):
|
2022-05-05 17:40:22 +02:00
|
|
|
request_parameter = RequestParameter.from_input("key", value)
|
2022-04-24 12:38:09 +02:00
|
|
|
assert request_parameter.value == expected_value
|
|
|
|
assert request_parameter.input_files is None
|
|
|
|
|
|
|
|
def test_from_input_inputfile(self):
|
2022-05-05 17:40:22 +02:00
|
|
|
inputfile_1 = InputFile("data1", filename="inputfile_1", attach=True)
|
|
|
|
inputfile_2 = InputFile("data2", filename="inputfile_2")
|
2022-04-24 12:38:09 +02:00
|
|
|
|
2022-05-05 17:40:22 +02:00
|
|
|
request_parameter = RequestParameter.from_input("key", inputfile_1)
|
2022-04-24 12:38:09 +02:00
|
|
|
assert request_parameter.value == inputfile_1.attach_uri
|
|
|
|
assert request_parameter.input_files == [inputfile_1]
|
|
|
|
|
2022-05-05 17:40:22 +02:00
|
|
|
request_parameter = RequestParameter.from_input("key", inputfile_2)
|
2022-04-24 12:38:09 +02:00
|
|
|
assert request_parameter.value is None
|
|
|
|
assert request_parameter.input_files == [inputfile_2]
|
|
|
|
|
2022-05-05 17:40:22 +02:00
|
|
|
request_parameter = RequestParameter.from_input("key", [inputfile_1, inputfile_2])
|
2022-04-24 12:38:09 +02:00
|
|
|
assert request_parameter.value == [inputfile_1.attach_uri]
|
|
|
|
assert request_parameter.input_files == [inputfile_1, inputfile_2]
|
|
|
|
|
|
|
|
def test_from_input_input_media(self):
|
2022-05-05 17:40:22 +02:00
|
|
|
input_media_no_thumb = InputMediaPhoto(media=data_file("telegram.jpg").read_bytes())
|
2022-04-24 12:38:09 +02:00
|
|
|
input_media_thumb = InputMediaVideo(
|
2022-05-05 17:40:22 +02:00
|
|
|
media=data_file("telegram.mp4").read_bytes(),
|
2023-03-25 11:47:26 +01:00
|
|
|
thumbnail=data_file("telegram.jpg").read_bytes(),
|
2022-04-24 12:38:09 +02:00
|
|
|
)
|
|
|
|
|
2022-05-05 17:40:22 +02:00
|
|
|
request_parameter = RequestParameter.from_input("key", input_media_no_thumb)
|
2022-04-24 12:38:09 +02:00
|
|
|
expected_no_thumb = input_media_no_thumb.to_dict()
|
2022-05-05 17:40:22 +02:00
|
|
|
expected_no_thumb.update({"media": input_media_no_thumb.media.attach_uri})
|
2022-04-24 12:38:09 +02:00
|
|
|
assert request_parameter.value == expected_no_thumb
|
|
|
|
assert request_parameter.input_files == [input_media_no_thumb.media]
|
|
|
|
|
2022-05-05 17:40:22 +02:00
|
|
|
request_parameter = RequestParameter.from_input("key", input_media_thumb)
|
2022-04-24 12:38:09 +02:00
|
|
|
expected_thumb = input_media_thumb.to_dict()
|
2022-05-05 17:40:22 +02:00
|
|
|
expected_thumb.update({"media": input_media_thumb.media.attach_uri})
|
2023-03-25 11:47:26 +01:00
|
|
|
expected_thumb.update({"thumbnail": input_media_thumb.thumbnail.attach_uri})
|
2022-04-24 12:38:09 +02:00
|
|
|
assert request_parameter.value == expected_thumb
|
2023-03-25 11:47:26 +01:00
|
|
|
assert request_parameter.input_files == [
|
|
|
|
input_media_thumb.media,
|
|
|
|
input_media_thumb.thumbnail,
|
|
|
|
]
|
2022-04-24 12:38:09 +02:00
|
|
|
|
|
|
|
request_parameter = RequestParameter.from_input(
|
2022-05-05 17:40:22 +02:00
|
|
|
"key", [input_media_thumb, input_media_no_thumb]
|
2022-04-24 12:38:09 +02:00
|
|
|
)
|
|
|
|
assert request_parameter.value == [expected_thumb, expected_no_thumb]
|
|
|
|
assert request_parameter.input_files == [
|
|
|
|
input_media_thumb.media,
|
2023-03-25 11:47:26 +01:00
|
|
|
input_media_thumb.thumbnail,
|
2022-04-24 12:38:09 +02:00
|
|
|
input_media_no_thumb.media,
|
|
|
|
]
|
|
|
|
|
|
|
|
def test_from_input_inputmedia_without_attach(self):
|
|
|
|
"""This case will never happen, but we test it for completeness"""
|
|
|
|
input_media = InputMediaVideo(
|
2022-05-05 17:40:22 +02:00
|
|
|
data_file("telegram.png").read_bytes(),
|
2023-03-25 11:47:26 +01:00
|
|
|
thumbnail=data_file("telegram.png").read_bytes(),
|
2022-04-24 12:38:09 +02:00
|
|
|
parse_mode=None,
|
|
|
|
)
|
|
|
|
input_media.media.attach_name = None
|
2023-03-25 11:47:26 +01:00
|
|
|
input_media.thumbnail.attach_name = None
|
2022-05-05 17:40:22 +02:00
|
|
|
request_parameter = RequestParameter.from_input("key", input_media)
|
2022-04-24 12:38:09 +02:00
|
|
|
assert request_parameter.value == {"type": "video"}
|
2023-03-25 11:47:26 +01:00
|
|
|
assert request_parameter.input_files == [input_media.media, input_media.thumbnail]
|
|
|
|
|
|
|
|
def test_from_input_inputsticker(self):
|
|
|
|
input_sticker = InputSticker(data_file("telegram.png").read_bytes(), ["emoji"])
|
|
|
|
expected = input_sticker.to_dict()
|
|
|
|
expected.update({"sticker": input_sticker.sticker.attach_uri})
|
|
|
|
request_parameter = RequestParameter.from_input("key", input_sticker)
|
|
|
|
assert request_parameter.value == expected
|
|
|
|
assert request_parameter.input_files == [input_sticker.sticker]
|
2023-01-01 14:24:30 +01:00
|
|
|
|
|
|
|
def test_from_input_str_and_bytes(self):
|
|
|
|
input_str = "test_input"
|
|
|
|
request_parameter = RequestParameter.from_input("input", input_str)
|
|
|
|
assert request_parameter.value == input_str
|
|
|
|
assert request_parameter.name == "input"
|
|
|
|
assert request_parameter.input_files is None
|
|
|
|
|
|
|
|
input_bytes = b"test_input"
|
|
|
|
request_parameter = RequestParameter.from_input("input", input_bytes)
|
|
|
|
assert request_parameter.value == input_bytes
|
|
|
|
assert request_parameter.name == "input"
|
|
|
|
assert request_parameter.input_files is None
|
|
|
|
|
|
|
|
def test_from_input_different_sequences(self):
|
|
|
|
input_list = ["item1", "item2"]
|
|
|
|
request_parameter = RequestParameter.from_input("input", input_list)
|
|
|
|
assert request_parameter.value == input_list
|
|
|
|
assert request_parameter.name == "input"
|
|
|
|
assert request_parameter.input_files is None
|
|
|
|
|
|
|
|
input_tuple = tuple(input_list)
|
|
|
|
request_parameter = RequestParameter.from_input("input", input_tuple)
|
|
|
|
assert request_parameter.value == input_list
|
|
|
|
assert request_parameter.name == "input"
|
|
|
|
assert request_parameter.input_files is None
|
|
|
|
|
|
|
|
class CustomSequence(Sequence):
|
|
|
|
def __init__(self, items):
|
|
|
|
self.items = items
|
|
|
|
|
|
|
|
def __getitem__(self, index):
|
|
|
|
return self.items[index]
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return len(self.items)
|
|
|
|
|
|
|
|
input_custom_sequence = CustomSequence(input_list)
|
|
|
|
request_parameter = RequestParameter.from_input("input", input_custom_sequence)
|
|
|
|
assert request_parameter.value == input_list
|
|
|
|
assert request_parameter.name == "input"
|
|
|
|
assert request_parameter.input_files is None
|