-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Expand file tree
/
Copy pathtest_managedbot.py
More file actions
196 lines (173 loc) · 7.31 KB
/
test_managedbot.py
File metadata and controls
196 lines (173 loc) · 7.31 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
#!/usr/bin/env python
#
# A library that provides a Python interface to the Telegram Bot API
# Copyright (C) 2015-2026
# 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 ManagedBotCreated, ManagedBotUpdated, User
from tests.auxil.slots import mro_slots
@pytest.fixture
def managed_bot_created():
return ManagedBotCreated(
bot=User(
id=ManagedBotTestBase.bot_id,
is_bot=ManagedBotTestBase.is_bot,
first_name=ManagedBotTestBase.bot_first_name,
username=ManagedBotTestBase.bot_username,
),
)
@pytest.fixture
def managed_bot_updated():
return ManagedBotUpdated(
user=User(
id=ManagedBotTestBase.bot_creator_id,
is_bot=ManagedBotTestBase.is_bot,
first_name=ManagedBotTestBase.bot_creator_first_name,
username=ManagedBotTestBase.bot_creator_username,
),
bot=User(
id=ManagedBotTestBase.bot_id,
is_bot=ManagedBotTestBase.is_bot,
first_name=ManagedBotTestBase.bot_first_name,
username=ManagedBotTestBase.bot_username,
),
)
class ManagedBotTestBase:
bot_creator_id = 123
bot_creator_first_name = "TestBotManager"
bot_creator_username = "test_bot_manager"
bot_id = 321
bot_first_name = "TestBot"
bot_username = "test_bot"
is_bot = True
class TestManagedBotCreatedWithoutRequest(ManagedBotTestBase):
def test_slot_behaviour(self, managed_bot_created):
inst = managed_bot_created
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, managed_bot_created):
json_dict = {
"bot": managed_bot_created.bot.to_dict(),
}
managed_bot_created = ManagedBotCreated.de_json(json_dict, offline_bot)
assert managed_bot_created.api_kwargs == {}
assert managed_bot_created.bot.id == self.bot_id
assert managed_bot_created.bot.is_bot is True
assert managed_bot_created.bot.first_name == self.bot_first_name
assert managed_bot_created.bot.username == self.bot_username
def test_to_dict(self, managed_bot_created):
managed_bot_created_dict = managed_bot_created.to_dict()
assert managed_bot_created_dict["bot"]["id"] == self.bot_id
assert managed_bot_created_dict["bot"]["is_bot"] is True
assert managed_bot_created_dict["bot"]["first_name"] == self.bot_first_name
assert managed_bot_created_dict["bot"]["username"] == self.bot_username
def test_equality(self, managed_bot_created):
managed_bot_created_2 = ManagedBotCreated(
bot=User(
id=self.bot_id,
is_bot=True,
first_name=self.bot_first_name,
username=self.bot_username,
),
)
managed_bot_created_3 = ManagedBotCreated(
bot=User(
id=4534, is_bot=True, first_name=self.bot_first_name, username=self.bot_username
),
)
not_a_managed_bot_created = ManagedBotUpdated(
user=User(
id=self.bot_creator_id,
is_bot=True,
first_name=self.bot_creator_first_name,
username=self.bot_creator_username,
),
bot=User(
id=self.bot_id,
is_bot=True,
first_name=self.bot_first_name,
username=self.bot_username,
),
)
assert managed_bot_created == managed_bot_created_2
assert hash(managed_bot_created) == hash(managed_bot_created_2)
assert managed_bot_created != managed_bot_created_3
assert hash(managed_bot_created) != hash(managed_bot_created_3)
assert managed_bot_created != not_a_managed_bot_created
assert hash(managed_bot_created) != hash(not_a_managed_bot_created)
class TestManagedBotUpdatedWithoutRequest(ManagedBotTestBase):
def test_slot_behaviour(self, managed_bot_updated):
inst = managed_bot_updated
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, managed_bot_updated):
json_dict = {
"user": managed_bot_updated.user.to_dict(),
"bot": managed_bot_updated.bot.to_dict(),
}
managed_bot_updated_new = ManagedBotUpdated.de_json(json_dict, offline_bot)
assert managed_bot_updated_new.api_kwargs == {}
assert managed_bot_updated_new.user == managed_bot_updated.user
assert managed_bot_updated_new.bot == managed_bot_updated.bot
def test_to_dict(self, managed_bot_updated):
managed_bot_updated_dict = managed_bot_updated.to_dict()
assert managed_bot_updated_dict["user"] == managed_bot_updated.user.to_dict()
assert managed_bot_updated_dict["bot"] == managed_bot_updated.bot.to_dict()
def test_equality(self, managed_bot_updated):
managed_bot_updated_2 = ManagedBotUpdated(
user=User(
id=self.bot_creator_id,
is_bot=True,
first_name=self.bot_creator_first_name,
username=self.bot_creator_username,
),
bot=User(
id=self.bot_id,
is_bot=True,
first_name=self.bot_first_name,
username=self.bot_username,
),
)
managed_bot_updated_3 = ManagedBotUpdated(
user=User(
id=4534,
is_bot=True,
first_name=self.bot_creator_first_name,
username=self.bot_creator_username,
),
bot=User(
id=self.bot_id,
is_bot=True,
first_name=self.bot_first_name,
username=self.bot_username,
),
)
not_a_managed_bot_updated = ManagedBotCreated(
bot=User(
id=self.bot_id,
is_bot=True,
first_name=self.bot_first_name,
username=self.bot_username,
),
)
assert managed_bot_updated == managed_bot_updated_2
assert hash(managed_bot_updated) == hash(managed_bot_updated_2)
assert managed_bot_updated != managed_bot_updated_3
assert hash(managed_bot_updated) != hash(managed_bot_updated_3)
assert managed_bot_updated != not_a_managed_bot_updated
assert hash(managed_bot_updated) != hash(not_a_managed_bot_updated)