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
|
#!/usr/bin/env python
#
# A library that provides a Python interface to the Telegram Bot API
# Copyright (C) 2015-2025
# 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
from telegram import (
BotCommand,
Dice,
ReactionCount,
ReactionType,
ReactionTypeCustomEmoji,
ReactionTypeEmoji,
ReactionTypePaid,
constants,
)
from telegram.constants import ReactionEmoji
from tests.auxil.slots import mro_slots
@pytest.fixture(scope="module")
def reaction_type():
return ReactionType(type=TestReactionTypeWithoutRequest.type)
class ReactionTypeTestBase:
type = "emoji"
emoji = "some_emoji"
custom_emoji_id = "some_custom_emoji_id"
class TestReactionTypeWithoutRequest(ReactionTypeTestBase):
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_type_enum_conversion(self):
assert type(ReactionType("emoji").type) is constants.ReactionType
assert ReactionType("unknown").type == "unknown"
def test_de_json(self, offline_bot):
json_dict = {"type": "unknown"}
reaction_type = ReactionType.de_json(json_dict, offline_bot)
assert reaction_type.api_kwargs == {}
assert reaction_type.type == "unknown"
@pytest.mark.parametrize(
("rt_type", "subclass"),
[
("emoji", ReactionTypeEmoji),
("custom_emoji", ReactionTypeCustomEmoji),
("paid", ReactionTypePaid),
],
)
def test_de_json_subclass(self, offline_bot, rt_type, subclass):
json_dict = {
"type": rt_type,
"emoji": self.emoji,
"custom_emoji_id": self.custom_emoji_id,
}
rt = ReactionType.de_json(json_dict, offline_bot)
assert type(rt) is subclass
assert set(rt.api_kwargs.keys()) == set(json_dict.keys()) - set(subclass.__slots__) - {
"type"
}
assert rt.type == rt_type
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
@pytest.fixture(scope="module")
def reaction_type_emoji():
return ReactionTypeEmoji(emoji=TestReactionTypeEmojiWithoutRequest.emoji)
class TestReactionTypeEmojiWithoutRequest(ReactionTypeTestBase):
type = constants.ReactionType.EMOJI
def test_slot_behaviour(self, reaction_type_emoji):
inst = reaction_type_emoji
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(self, offline_bot):
json_dict = {"emoji": self.emoji}
reaction_type_emoji = ReactionTypeEmoji.de_json(json_dict, offline_bot)
assert reaction_type_emoji.api_kwargs == {}
assert reaction_type_emoji.type == self.type
assert reaction_type_emoji.emoji == self.emoji
def test_to_dict(self, reaction_type_emoji):
reaction_type_emoji_dict = reaction_type_emoji.to_dict()
assert isinstance(reaction_type_emoji_dict, dict)
assert reaction_type_emoji_dict["type"] == reaction_type_emoji.type
assert reaction_type_emoji_dict["emoji"] == reaction_type_emoji.emoji
def test_equality(self, reaction_type_emoji):
a = reaction_type_emoji
b = ReactionTypeEmoji(emoji=self.emoji)
c = ReactionTypeEmoji(emoji="other_emoji")
d = Dice(5, "test")
assert a == b
assert hash(a) == hash(b)
assert a != c
assert hash(a) != hash(c)
assert a != d
assert hash(a) != hash(d)
@pytest.fixture(scope="module")
def reaction_type_custom_emoji():
return ReactionTypeCustomEmoji(
custom_emoji_id=TestReactionTypeCustomEmojiWithoutRequest.custom_emoji_id
)
class TestReactionTypeCustomEmojiWithoutRequest(ReactionTypeTestBase):
type = constants.ReactionType.CUSTOM_EMOJI
def test_slot_behaviour(self, reaction_type_custom_emoji):
inst = reaction_type_custom_emoji
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(self, offline_bot):
json_dict = {"custom_emoji_id": self.custom_emoji_id}
reaction_type_custom_emoji = ReactionTypeCustomEmoji.de_json(json_dict, offline_bot)
assert reaction_type_custom_emoji.api_kwargs == {}
assert reaction_type_custom_emoji.type == self.type
assert reaction_type_custom_emoji.custom_emoji_id == self.custom_emoji_id
def test_to_dict(self, reaction_type_custom_emoji):
reaction_type_custom_emoji_dict = reaction_type_custom_emoji.to_dict()
assert isinstance(reaction_type_custom_emoji_dict, dict)
assert reaction_type_custom_emoji_dict["type"] == reaction_type_custom_emoji.type
assert (
reaction_type_custom_emoji_dict["custom_emoji_id"]
== reaction_type_custom_emoji.custom_emoji_id
)
def test_equality(self, reaction_type_custom_emoji):
a = reaction_type_custom_emoji
b = ReactionTypeCustomEmoji(custom_emoji_id=self.custom_emoji_id)
c = ReactionTypeCustomEmoji(custom_emoji_id="other_custom_emoji_id")
d = Dice(5, "test")
assert a == b
assert hash(a) == hash(b)
assert a != c
assert hash(a) != hash(c)
assert a != d
assert hash(a) != hash(d)
@pytest.fixture(scope="module")
def reaction_type_paid():
return ReactionTypePaid()
class TestReactionTypePaidWithoutRequest(ReactionTypeTestBase):
type = constants.ReactionType.PAID
def test_slot_behaviour(self, reaction_type_paid):
inst = reaction_type_paid
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(self, offline_bot):
json_dict = {}
reaction_type_paid = ReactionTypePaid.de_json(json_dict, offline_bot)
assert reaction_type_paid.api_kwargs == {}
assert reaction_type_paid.type == self.type
def test_to_dict(self, reaction_type_paid):
reaction_type_paid_dict = reaction_type_paid.to_dict()
assert isinstance(reaction_type_paid_dict, dict)
assert reaction_type_paid_dict["type"] == reaction_type_paid.type
@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, offline_bot):
json_dict = {
"type": self.type.to_dict(),
"total_count": self.total_count,
}
reaction_count = ReactionCount.de_json(json_dict, offline_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
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)
|