2021-09-22 16:49:10 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# A library that provides a Python interface to the Telegram Bot API
|
2022-01-03 09:09:03 +01:00
|
|
|
# Copyright (C) 2015-2022
|
2021-09-22 16:49:10 +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 pytest
|
|
|
|
|
2021-10-10 15:10:21 +02:00
|
|
|
import telegram._utils.datetime
|
|
|
|
import telegram._utils.files
|
2022-05-05 09:27:54 +02:00
|
|
|
from telegram import Animation, InputFile, MessageEntity
|
2021-10-13 08:12:48 +02:00
|
|
|
from tests.conftest import TEST_DATA_PATH, data_file
|
2021-09-22 16:49:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TestFiles:
|
|
|
|
@pytest.mark.parametrize(
|
2022-05-05 17:40:22 +02:00
|
|
|
"string,expected",
|
2021-09-22 16:49:10 +02:00
|
|
|
[
|
2022-05-05 17:40:22 +02:00
|
|
|
(str(data_file("game.gif")), True),
|
2021-10-13 08:12:48 +02:00
|
|
|
(str(TEST_DATA_PATH), False),
|
2022-05-05 17:40:22 +02:00
|
|
|
(str(data_file("game.gif")), True),
|
2021-10-13 08:12:48 +02:00
|
|
|
(str(TEST_DATA_PATH), False),
|
2022-05-05 17:40:22 +02:00
|
|
|
(data_file("game.gif"), True),
|
2021-10-13 08:12:48 +02:00
|
|
|
(TEST_DATA_PATH, False),
|
2022-05-05 17:40:22 +02:00
|
|
|
("https:/api.org/file/botTOKEN/document/file_3", False),
|
2021-09-22 16:49:10 +02:00
|
|
|
(None, False),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_is_local_file(self, string, expected):
|
2021-10-10 15:10:21 +02:00
|
|
|
assert telegram._utils.files.is_local_file(string) == expected
|
2021-09-22 16:49:10 +02:00
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
2022-05-05 17:40:22 +02:00
|
|
|
"string,expected",
|
2021-09-22 16:49:10 +02:00
|
|
|
[
|
2022-05-05 17:40:22 +02:00
|
|
|
(data_file("game.gif"), data_file("game.gif").as_uri()),
|
2021-10-13 08:12:48 +02:00
|
|
|
(TEST_DATA_PATH, TEST_DATA_PATH),
|
2022-05-05 17:40:22 +02:00
|
|
|
("file://foobar", "file://foobar"),
|
|
|
|
(str(data_file("game.gif")), data_file("game.gif").as_uri()),
|
2021-10-13 08:12:48 +02:00
|
|
|
(str(TEST_DATA_PATH), str(TEST_DATA_PATH)),
|
2022-05-05 17:40:22 +02:00
|
|
|
(data_file("game.gif"), data_file("game.gif").as_uri()),
|
2021-10-13 08:12:48 +02:00
|
|
|
(TEST_DATA_PATH, TEST_DATA_PATH),
|
2021-09-22 16:49:10 +02:00
|
|
|
(
|
2022-05-05 17:40:22 +02:00
|
|
|
"https:/api.org/file/botTOKEN/document/file_3",
|
|
|
|
"https:/api.org/file/botTOKEN/document/file_3",
|
2021-09-22 16:49:10 +02:00
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_parse_file_input_string(self, string, expected):
|
2021-10-10 15:10:21 +02:00
|
|
|
assert telegram._utils.files.parse_file_input(string) == expected
|
2021-09-22 16:49:10 +02:00
|
|
|
|
|
|
|
def test_parse_file_input_file_like(self):
|
2022-05-05 17:40:22 +02:00
|
|
|
source_file = data_file("game.gif")
|
|
|
|
with source_file.open("rb") as file:
|
2021-10-10 15:10:21 +02:00
|
|
|
parsed = telegram._utils.files.parse_file_input(file)
|
2021-09-22 16:49:10 +02:00
|
|
|
|
|
|
|
assert isinstance(parsed, InputFile)
|
2022-05-05 17:40:22 +02:00
|
|
|
assert parsed.filename == "game.gif"
|
2021-09-22 16:49:10 +02:00
|
|
|
|
2022-05-05 17:40:22 +02:00
|
|
|
with source_file.open("rb") as file:
|
|
|
|
parsed = telegram._utils.files.parse_file_input(file, filename="test_file")
|
2021-09-22 16:49:10 +02:00
|
|
|
|
|
|
|
assert isinstance(parsed, InputFile)
|
2022-05-05 17:40:22 +02:00
|
|
|
assert parsed.filename == "test_file"
|
2021-09-22 16:49:10 +02:00
|
|
|
|
|
|
|
def test_parse_file_input_bytes(self):
|
2022-05-05 17:40:22 +02:00
|
|
|
source_file = data_file("text_file.txt")
|
2021-10-10 15:10:21 +02:00
|
|
|
parsed = telegram._utils.files.parse_file_input(source_file.read_bytes())
|
2021-09-22 16:49:10 +02:00
|
|
|
|
|
|
|
assert isinstance(parsed, InputFile)
|
2022-05-05 17:40:22 +02:00
|
|
|
assert parsed.filename == "application.octet-stream"
|
2021-09-22 16:49:10 +02:00
|
|
|
|
2021-10-10 15:10:21 +02:00
|
|
|
parsed = telegram._utils.files.parse_file_input(
|
2022-05-05 17:40:22 +02:00
|
|
|
source_file.read_bytes(), filename="test_file"
|
2021-10-05 19:50:11 +02:00
|
|
|
)
|
2021-09-22 16:49:10 +02:00
|
|
|
|
|
|
|
assert isinstance(parsed, InputFile)
|
2022-05-05 17:40:22 +02:00
|
|
|
assert parsed.filename == "test_file"
|
2021-09-22 16:49:10 +02:00
|
|
|
|
|
|
|
def test_parse_file_input_tg_object(self):
|
2022-05-05 17:40:22 +02:00
|
|
|
animation = Animation("file_id", "unique_id", 1, 1, 1)
|
|
|
|
assert telegram._utils.files.parse_file_input(animation, Animation) == "file_id"
|
2021-10-10 15:10:21 +02:00
|
|
|
assert telegram._utils.files.parse_file_input(animation, MessageEntity) is animation
|
2021-09-22 16:49:10 +02:00
|
|
|
|
2022-05-05 17:40:22 +02:00
|
|
|
@pytest.mark.parametrize("obj", [{1: 2}, [1, 2], (1, 2)])
|
2021-09-22 16:49:10 +02:00
|
|
|
def test_parse_file_input_other(self, obj):
|
2021-10-10 15:10:21 +02:00
|
|
|
assert telegram._utils.files.parse_file_input(obj) is obj
|
2022-04-24 12:38:09 +02:00
|
|
|
|
2022-05-05 17:40:22 +02:00
|
|
|
@pytest.mark.parametrize("attach", [True, False])
|
2022-04-24 12:38:09 +02:00
|
|
|
def test_parse_file_input_attach(self, attach):
|
2022-05-05 17:40:22 +02:00
|
|
|
source_file = data_file("text_file.txt")
|
2022-04-24 12:38:09 +02:00
|
|
|
parsed = telegram._utils.files.parse_file_input(source_file.read_bytes(), attach=attach)
|
|
|
|
|
|
|
|
assert isinstance(parsed, InputFile)
|
|
|
|
assert bool(parsed.attach_name) is attach
|