File: test_chatboosthandler.py

package info (click to toggle)
python-telegram-bot 22.3-1
  • links: PTS
  • area: main
  • in suites: sid
  • size: 11,060 kB
  • sloc: python: 90,298; makefile: 176; sh: 4
file content (217 lines) | stat: -rw-r--r-- 7,831 bytes parent folder | download
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
#!/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 time

import pytest

from telegram import (
    Chat,
    ChatBoost,
    ChatBoostRemoved,
    ChatBoostSourcePremium,
    ChatBoostUpdated,
    Update,
    User,
)
from telegram._utils.datetime import from_timestamp
from telegram.ext import CallbackContext, ChatBoostHandler
from tests.auxil.slots import mro_slots
from tests.test_update import all_types as really_all_types
from tests.test_update import params as all_params

# Remove "chat_boost" from params
params = [param for param in all_params for key in param if "chat_boost" not in key]
all_types = [param for param in really_all_types if "chat_boost" not in param]
ids = (*all_types, "callback_query_without_message")


@pytest.fixture(scope="class", params=params, ids=ids)
def false_update(request):
    return Update(update_id=2, **request.param)


def chat_boost():
    return ChatBoost(
        "1",
        from_timestamp(int(time.time())),
        from_timestamp(int(time.time())),
        ChatBoostSourcePremium(
            User(1, "first_name", False),
        ),
    )


@pytest.fixture(scope="module")
def removed_chat_boost():
    return ChatBoostRemoved(
        Chat(1, "group", username="chat"),
        "1",
        from_timestamp(int(time.time())),
        ChatBoostSourcePremium(
            User(1, "first_name", False),
        ),
    )


def removed_chat_boost_update():
    return Update(
        update_id=2,
        removed_chat_boost=ChatBoostRemoved(
            Chat(1, "group", username="chat"),
            "1",
            from_timestamp(int(time.time())),
            ChatBoostSourcePremium(
                User(1, "first_name", False),
            ),
        ),
    )


@pytest.fixture(scope="module")
def chat_boost_updated():
    return ChatBoostUpdated(Chat(1, "group", username="chat"), chat_boost())


def chat_boost_updated_update():
    return Update(
        update_id=2,
        chat_boost=ChatBoostUpdated(
            Chat(1, "group", username="chat"),
            chat_boost(),
        ),
    )


class TestChatBoostHandler:
    test_flag = False

    def test_slot_behaviour(self):
        action = ChatBoostHandler(self.cb_chat_boost_removed)
        for attr in action.__slots__:
            assert getattr(action, attr, "err") != "err", f"got extra slot '{attr}'"
        assert len(mro_slots(action)) == len(set(mro_slots(action))), "duplicate slot"

    @pytest.fixture(autouse=True)
    def _reset(self):
        self.test_flag = False

    async def cb_chat_boost_updated(self, update, context):
        self.test_flag = (
            isinstance(context, CallbackContext)
            and isinstance(update.chat_boost, ChatBoostUpdated)
            and not isinstance(update.removed_chat_boost, ChatBoostRemoved)
        )

    async def cb_chat_boost_removed(self, update, context):
        self.test_flag = (
            isinstance(context, CallbackContext)
            and isinstance(update.removed_chat_boost, ChatBoostRemoved)
            and not isinstance(update.chat_boost, ChatBoostUpdated)
        )

    async def cb_chat_boost_any(self, update, context):
        self.test_flag = isinstance(context, CallbackContext) and (
            isinstance(update.removed_chat_boost, ChatBoostRemoved)
            or isinstance(update.chat_boost, ChatBoostUpdated)
        )

    @pytest.mark.parametrize(
        argnames=("allowed_types", "cb", "expected"),
        argvalues=[
            (ChatBoostHandler.CHAT_BOOST, "cb_chat_boost_updated", (True, False)),
            (ChatBoostHandler.REMOVED_CHAT_BOOST, "cb_chat_boost_removed", (False, True)),
            (ChatBoostHandler.ANY_CHAT_BOOST, "cb_chat_boost_any", (True, True)),
        ],
        ids=["CHAT_BOOST", "REMOVED_CHAT_BOOST", "ANY_CHAT_MEMBER"],
    )
    async def test_chat_boost_types(self, app, cb, expected, allowed_types):
        result_1, result_2 = expected

        update_type, other = chat_boost_updated_update(), removed_chat_boost_update()

        handler = ChatBoostHandler(getattr(self, cb), chat_boost_types=allowed_types)
        app.add_handler(handler)

        async with app:
            assert handler.check_update(update_type) == result_1
            await app.process_update(update_type)
            assert self.test_flag == result_1

            self.test_flag = False

            assert handler.check_update(other) == result_2
            await app.process_update(other)
            assert self.test_flag == result_2

    def test_other_update_types(self, false_update):
        handler = ChatBoostHandler(self.cb_chat_boost_removed)
        assert not handler.check_update(false_update)
        assert not handler.check_update(True)

    async def test_context(self, app):
        handler = ChatBoostHandler(self.cb_chat_boost_updated)
        app.add_handler(handler)

        async with app:
            await app.process_update(chat_boost_updated_update())
            assert self.test_flag

    def test_with_chat_id(self):
        update = chat_boost_updated_update()
        cb = self.cb_chat_boost_updated
        handler = ChatBoostHandler(cb, chat_id=1)
        assert handler.check_update(update)
        handler = ChatBoostHandler(cb, chat_id=[1])
        assert handler.check_update(update)
        handler = ChatBoostHandler(cb, chat_id=2, chat_username="@chat")
        assert handler.check_update(update)

        handler = ChatBoostHandler(cb, chat_id=2)
        assert not handler.check_update(update)
        handler = ChatBoostHandler(cb, chat_id=[2])
        assert not handler.check_update(update)

    def test_with_username(self):
        update = removed_chat_boost_update()
        cb = self.cb_chat_boost_removed
        handler = ChatBoostHandler(cb, chat_boost_types=0, chat_username="chat")
        assert handler.check_update(update)
        handler = ChatBoostHandler(cb, chat_boost_types=0, chat_username="@chat")
        assert handler.check_update(update)
        handler = ChatBoostHandler(cb, chat_boost_types=0, chat_username=["chat"])
        assert handler.check_update(update)
        handler = ChatBoostHandler(cb, chat_boost_types=0, chat_username=["@chat"])
        assert handler.check_update(update)
        handler = ChatBoostHandler(
            cb, chat_boost_types=0, chat_id=1, chat_username="@chat_something"
        )
        assert handler.check_update(update)

        handler = ChatBoostHandler(cb, chat_boost_types=0, chat_username="chat_b")
        assert not handler.check_update(update)
        handler = ChatBoostHandler(cb, chat_boost_types=0, chat_username="@chat_b")
        assert not handler.check_update(update)
        handler = ChatBoostHandler(cb, chat_boost_types=0, chat_username=["chat_b"])
        assert not handler.check_update(update)
        handler = ChatBoostHandler(cb, chat_boost_types=0, chat_username=["@chat_b"])
        assert not handler.check_update(update)

        update.removed_chat_boost.chat._unfreeze()
        update.removed_chat_boost.chat.username = None
        assert not handler.check_update(update)