forked from python-telegram-bot/python-telegram-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_files.py
More file actions
161 lines (137 loc) · 6.15 KB
/
test_files.py
File metadata and controls
161 lines (137 loc) · 6.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env python
#
# A library that provides a Python interface to the Telegram Bot API
# Copyright (C) 2015-2022
# Leandro Toledo de Souza <[email protected]>
#
# 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 subprocess
import sys
from pathlib import Path
import pytest
import telegram._utils.datetime
import telegram._utils.files
from telegram import Animation, InputFile, MessageEntity
from tests.conftest import TEST_DATA_PATH, data_file
class TestFiles:
@pytest.mark.parametrize(
"string,expected",
[
(str(data_file("game.gif")), True),
(str(TEST_DATA_PATH), False),
(str(data_file("game.gif")), True),
(str(TEST_DATA_PATH), False),
(data_file("game.gif"), True),
(TEST_DATA_PATH, False),
("https:/api.org/file/botTOKEN/document/file_3", False),
(None, False),
],
)
def test_is_local_file(self, string, expected):
assert telegram._utils.files.is_local_file(string) == expected
@pytest.mark.parametrize(
"string,expected_local,expected_non_local",
[
(data_file("game.gif"), data_file("game.gif").as_uri(), InputFile),
(TEST_DATA_PATH, TEST_DATA_PATH, TEST_DATA_PATH),
("file://foobar", "file://foobar", ValueError),
(str(data_file("game.gif")), data_file("game.gif").as_uri(), InputFile),
(str(TEST_DATA_PATH), str(TEST_DATA_PATH), str(TEST_DATA_PATH)),
(
"https:/api.org/file/botTOKEN/document/file_3",
"https:/api.org/file/botTOKEN/document/file_3",
"https:/api.org/file/botTOKEN/document/file_3",
),
],
ids=[
"Path(local_file)",
"Path(directory)",
"file_uri",
"str-path local file",
"str-path directory",
"URL",
],
)
def test_parse_file_input_string(self, string, expected_local, expected_non_local):
assert telegram._utils.files.parse_file_input(string, local_mode=True) == expected_local
if expected_non_local is InputFile:
assert isinstance(
telegram._utils.files.parse_file_input(string, local_mode=False), InputFile
)
elif expected_non_local is ValueError:
with pytest.raises(ValueError):
telegram._utils.files.parse_file_input(string, local_mode=False)
else:
assert (
telegram._utils.files.parse_file_input(string, local_mode=False)
== expected_non_local
)
def test_parse_file_input_file_like(self):
source_file = data_file("game.gif")
with source_file.open("rb") as file:
parsed = telegram._utils.files.parse_file_input(file)
assert isinstance(parsed, InputFile)
assert parsed.filename == "game.gif"
with source_file.open("rb") as file:
parsed = telegram._utils.files.parse_file_input(file, filename="test_file")
assert isinstance(parsed, InputFile)
assert parsed.filename == "test_file"
def test_parse_file_input_bytes(self):
source_file = data_file("text_file.txt")
parsed = telegram._utils.files.parse_file_input(source_file.read_bytes())
assert isinstance(parsed, InputFile)
assert parsed.filename == "application.octet-stream"
parsed = telegram._utils.files.parse_file_input(
source_file.read_bytes(), filename="test_file"
)
assert isinstance(parsed, InputFile)
assert parsed.filename == "test_file"
def test_parse_file_input_tg_object(self):
animation = Animation("file_id", "unique_id", 1, 1, 1)
assert telegram._utils.files.parse_file_input(animation, Animation) == "file_id"
assert telegram._utils.files.parse_file_input(animation, MessageEntity) is animation
@pytest.mark.parametrize("obj", [{1: 2}, [1, 2], (1, 2)])
def test_parse_file_input_other(self, obj):
assert telegram._utils.files.parse_file_input(obj) is obj
@pytest.mark.parametrize("attach", [True, False])
def test_parse_file_input_attach(self, attach):
source_file = data_file("text_file.txt")
parsed = telegram._utils.files.parse_file_input(source_file.read_bytes(), attach=attach)
assert isinstance(parsed, InputFile)
assert bool(parsed.attach_name) is attach
def test_load_file_none(self):
assert telegram._utils.files.load_file(None) == (None, None)
@pytest.mark.parametrize("arg", [b"bytes", "string", InputFile(b"content"), Path("file/path")])
def test_load_file_no_file(self, arg):
out = telegram._utils.files.load_file(arg)
assert out[0] is None
assert out[1] is arg
def test_load_file_file_handle(self):
out = telegram._utils.files.load_file(data_file("telegram.gif").open("rb"))
assert out[0] == "telegram.gif"
assert out[1] == data_file("telegram.gif").read_bytes()
def test_load_file_subprocess_pipe(self):
png_file = data_file("telegram.png")
cmd_str = "type" if sys.platform == "win32" else "cat"
cmd = [cmd_str, str(png_file)]
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=(sys.platform == "win32"))
out = telegram._utils.files.load_file(proc.stdout)
assert out[0] is None
assert out[1] == png_file.read_bytes()
try:
proc.kill()
except ProcessLookupError:
# This exception may be thrown if the process has finished before we had the chance
# to kill it.
pass