-
Notifications
You must be signed in to change notification settings - Fork 6k
Expand file tree
/
Copy pathtest_reaction.py
More file actions
276 lines (217 loc) · 9.34 KB
/
test_reaction.py
File metadata and controls
276 lines (217 loc) · 9.34 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/usr/bin/env python
#
# A library that provides a Python interface to the Telegram Bot API
# Copyright (C) 2015-2024
# 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 inspect
from copy import deepcopy
import pytest
from telegram import (
BotCommand,
Dice,
ReactionCount,
ReactionType,
ReactionTypeCustomEmoji,
ReactionTypeEmoji,
)
from telegram.constants import ReactionEmoji
from tests.auxil.slots import mro_slots
ignored = ["self", "api_kwargs"]
class RTDefaults:
custom_emoji = "123custom"
normal_emoji = ReactionEmoji.THUMBS_UP
def reaction_type_custom_emoji():
return ReactionTypeCustomEmoji(RTDefaults.custom_emoji)
def reaction_type_emoji():
return ReactionTypeEmoji(RTDefaults.normal_emoji)
def make_json_dict(instance: ReactionType, include_optional_args: bool = False) -> dict:
"""Used to make the json dict which we use for testing de_json. Similar to iter_args()"""
json_dict = {"type": instance.type}
sig = inspect.signature(instance.__class__.__init__)
for param in sig.parameters.values():
if param.name in ignored: # ignore irrelevant params
continue
val = getattr(instance, param.name)
# Compulsory args-
if param.default is inspect.Parameter.empty:
if hasattr(val, "to_dict"): # convert the user object or any future ones to dict.
val = val.to_dict()
json_dict[param.name] = val
# If we want to test all args (for de_json)-
# currently not needed, keeping for completeness
elif param.default is not inspect.Parameter.empty and include_optional_args:
json_dict[param.name] = val
return json_dict
def iter_args(instance: ReactionType, de_json_inst: ReactionType, include_optional: bool = False):
"""
We accept both the regular instance and de_json created instance and iterate over them for
easy one line testing later one.
"""
yield instance.type, de_json_inst.type # yield this here cause it's not available in sig.
sig = inspect.signature(instance.__class__.__init__)
for param in sig.parameters.values():
if param.name in ignored:
continue
inst_at, json_at = getattr(instance, param.name), getattr(de_json_inst, param.name)
if (
param.default is not inspect.Parameter.empty and include_optional
) or param.default is inspect.Parameter.empty:
yield inst_at, json_at
@pytest.fixture()
def reaction_type(request):
return request.param()
@pytest.mark.parametrize(
"reaction_type",
[
reaction_type_custom_emoji,
reaction_type_emoji,
],
indirect=True,
)
class TestReactionTypesWithoutRequest:
def test_slot_behaviour(self, reaction_type):
inst = reaction_type
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"
def test_de_json_required_args(self, bot, reaction_type):
cls = reaction_type.__class__
assert cls.de_json(None, bot) is None
json_dict = make_json_dict(reaction_type)
const_reaction_type = ReactionType.de_json(json_dict, bot)
assert const_reaction_type.api_kwargs == {}
assert isinstance(const_reaction_type, ReactionType)
assert isinstance(const_reaction_type, cls)
for reaction_type_at, const_reaction_type_at in iter_args(
reaction_type, const_reaction_type
):
assert reaction_type_at == const_reaction_type_at
def test_de_json_all_args(self, bot, reaction_type):
json_dict = make_json_dict(reaction_type, include_optional_args=True)
const_reaction_type = ReactionType.de_json(json_dict, bot)
assert const_reaction_type.api_kwargs == {}
assert isinstance(const_reaction_type, ReactionType)
assert isinstance(const_reaction_type, reaction_type.__class__)
for c_mem_type_at, const_c_mem_at in iter_args(reaction_type, const_reaction_type, True):
assert c_mem_type_at == const_c_mem_at
def test_de_json_invalid_type(self, bot, reaction_type):
json_dict = {"type": "invalid"}
reaction_type = ReactionType.de_json(json_dict, bot)
assert type(reaction_type) is ReactionType
assert reaction_type.type == "invalid"
def test_de_json_subclass(self, reaction_type, bot, chat_id):
"""This makes sure that e.g. ReactionTypeEmoji(data, bot) never returns a
ReactionTypeCustomEmoji instance."""
cls = reaction_type.__class__
json_dict = make_json_dict(reaction_type, True)
assert type(cls.de_json(json_dict, bot)) is cls
def test_to_dict(self, reaction_type):
reaction_type_dict = reaction_type.to_dict()
assert isinstance(reaction_type_dict, dict)
assert reaction_type_dict["type"] == reaction_type.type
if reaction_type.type == ReactionType.EMOJI:
assert reaction_type_dict["emoji"] == reaction_type.emoji
else:
assert reaction_type_dict["custom_emoji_id"] == reaction_type.custom_emoji_id
for slot in reaction_type.__slots__: # additional verification for the optional args
assert getattr(reaction_type, slot) == reaction_type_dict[slot]
def test_reaction_type_api_kwargs(self, reaction_type):
json_dict = make_json_dict(reaction_type_custom_emoji())
json_dict["custom_arg"] = "wuhu"
reaction_type_custom_emoji_instance = ReactionType.de_json(json_dict, None)
assert reaction_type_custom_emoji_instance.api_kwargs == {
"custom_arg": "wuhu",
}
def test_equality(self, reaction_type):
a = ReactionTypeEmoji(emoji=RTDefaults.normal_emoji)
b = ReactionTypeEmoji(emoji=RTDefaults.normal_emoji)
c = ReactionTypeCustomEmoji(custom_emoji_id=RTDefaults.custom_emoji)
d = ReactionTypeCustomEmoji(custom_emoji_id=RTDefaults.custom_emoji)
e = ReactionTypeEmoji(emoji=ReactionEmoji.RED_HEART)
f = ReactionTypeCustomEmoji(custom_emoji_id="1234custom")
g = deepcopy(a)
h = deepcopy(c)
i = Dice(4, "emoji")
assert a == b
assert hash(a) == hash(b)
assert a != c
assert hash(a) != hash(c)
assert a != e
assert hash(a) != hash(e)
assert a == g
assert hash(a) == hash(g)
assert a != i
assert hash(a) != hash(i)
assert c == d
assert hash(c) == hash(d)
assert c != e
assert hash(c) != hash(e)
assert c != f
assert hash(c) != hash(f)
assert c == h
assert hash(c) == hash(h)
assert c != i
assert hash(c) != hash(i)
@pytest.fixture(scope="module")
def reaction_count():
return ReactionCount(
type=TestReactionCountWithoutRequest.type,
total_count=TestReactionCountWithoutRequest.total_count,
)
class TestReactionCountWithoutRequest:
type = ReactionTypeEmoji(ReactionEmoji.THUMBS_UP)
total_count = 42
def test_slot_behaviour(self, reaction_count):
for attr in reaction_count.__slots__:
assert getattr(reaction_count, attr, "err") != "err", f"got extra slot '{attr}'"
assert len(mro_slots(reaction_count)) == len(
set(mro_slots(reaction_count))
), "duplicate slot"
def test_de_json(self, bot):
json_dict = {
"type": self.type.to_dict(),
"total_count": self.total_count,
}
reaction_count = ReactionCount.de_json(json_dict, bot)
assert reaction_count.api_kwargs == {}
assert isinstance(reaction_count, ReactionCount)
assert reaction_count.type == self.type
assert reaction_count.type.type == self.type.type
assert reaction_count.type.emoji == self.type.emoji
assert reaction_count.total_count == self.total_count
assert ReactionCount.de_json(None, bot) is None
def test_to_dict(self, reaction_count):
reaction_count_dict = reaction_count.to_dict()
assert isinstance(reaction_count_dict, dict)
assert reaction_count_dict["type"] == reaction_count.type.to_dict()
assert reaction_count_dict["total_count"] == reaction_count.total_count
def test_equality(self, reaction_count):
a = reaction_count
b = ReactionCount(
type=self.type,
total_count=self.total_count,
)
c = ReactionCount(
type=self.type,
total_count=self.total_count + 1,
)
d = BotCommand("start", "description")
assert a == b
assert hash(a) == hash(b)
assert a != c
assert hash(a) != hash(c)
assert a != d
assert hash(a) != hash(d)