File: test_videochat.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 (233 lines) | stat: -rw-r--r-- 8,573 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#!/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 datetime as dtm

import pytest

from telegram import (
    User,
    VideoChatEnded,
    VideoChatParticipantsInvited,
    VideoChatScheduled,
    VideoChatStarted,
)
from telegram._utils.datetime import UTC, to_timestamp
from telegram.warnings import PTBDeprecationWarning
from tests.auxil.slots import mro_slots


@pytest.fixture(scope="module")
def user1():
    return User(first_name="Misses Test", id=123, is_bot=False)


@pytest.fixture(scope="module")
def user2():
    return User(first_name="Mister Test", id=124, is_bot=False)


class TestVideoChatStartedWithoutRequest:
    def test_slot_behaviour(self):
        action = VideoChatStarted()
        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"

    def test_de_json(self):
        video_chat_started = VideoChatStarted.de_json({}, None)
        assert video_chat_started.api_kwargs == {}
        assert isinstance(video_chat_started, VideoChatStarted)

    def test_to_dict(self):
        video_chat_started = VideoChatStarted()
        video_chat_dict = video_chat_started.to_dict()
        assert video_chat_dict == {}


class TestVideoChatEndedWithoutRequest:
    duration = dtm.timedelta(seconds=100)

    def test_slot_behaviour(self):
        action = VideoChatEnded(8)
        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"

    def test_de_json(self):
        json_dict = {"duration": int(self.duration.total_seconds())}
        video_chat_ended = VideoChatEnded.de_json(json_dict, None)
        assert video_chat_ended.api_kwargs == {}

        assert video_chat_ended._duration == self.duration

    def test_to_dict(self):
        video_chat_ended = VideoChatEnded(self.duration)
        video_chat_dict = video_chat_ended.to_dict()

        assert isinstance(video_chat_dict, dict)
        assert video_chat_dict["duration"] == int(self.duration.total_seconds())

    def test_time_period_properties(self, PTB_TIMEDELTA):
        duration = VideoChatEnded(duration=self.duration).duration

        if PTB_TIMEDELTA:
            assert duration == self.duration
            assert isinstance(duration, dtm.timedelta)
        else:
            assert duration == int(self.duration.total_seconds())
            assert isinstance(duration, int)

    def test_time_period_int_deprecated(self, recwarn, PTB_TIMEDELTA):
        VideoChatEnded(self.duration).duration

        if PTB_TIMEDELTA:
            assert len(recwarn) == 0
        else:
            assert len(recwarn) == 1
            assert "`duration` will be of type `datetime.timedelta`" in str(recwarn[0].message)
            assert recwarn[0].category is PTBDeprecationWarning

    def test_equality(self):
        a = VideoChatEnded(100)
        b = VideoChatEnded(100)
        x = VideoChatEnded(dtm.timedelta(seconds=100))
        c = VideoChatEnded(50)
        d = VideoChatStarted()

        assert a == b
        assert hash(a) == hash(b)
        assert b == x
        assert hash(b) == hash(x)

        assert a != c
        assert hash(a) != hash(c)

        assert a != d
        assert hash(a) != hash(d)


class TestVideoChatParticipantsInvitedWithoutRequest:
    def test_slot_behaviour(self, user1):
        action = VideoChatParticipantsInvited([user1])
        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"

    def test_de_json(self, user1, user2, offline_bot):
        json_data = {"users": [user1.to_dict(), user2.to_dict()]}
        video_chat_participants = VideoChatParticipantsInvited.de_json(json_data, offline_bot)
        assert video_chat_participants.api_kwargs == {}

        assert isinstance(video_chat_participants.users, tuple)
        assert video_chat_participants.users[0] == user1
        assert video_chat_participants.users[1] == user2
        assert video_chat_participants.users[0].id == user1.id
        assert video_chat_participants.users[1].id == user2.id

    @pytest.mark.parametrize("use_users", [True, False])
    def test_to_dict(self, user1, user2, use_users):
        video_chat_participants = VideoChatParticipantsInvited([user1, user2] if use_users else ())
        video_chat_dict = video_chat_participants.to_dict()

        assert isinstance(video_chat_dict, dict)
        if use_users:
            assert video_chat_dict["users"] == [user1.to_dict(), user2.to_dict()]
            assert video_chat_dict["users"][0]["id"] == user1.id
            assert video_chat_dict["users"][1]["id"] == user2.id
        else:
            assert video_chat_dict == {}

    def test_equality(self, user1, user2):
        a = VideoChatParticipantsInvited([user1])
        b = VideoChatParticipantsInvited([user1])
        c = VideoChatParticipantsInvited([user1, user2])
        d = VideoChatParticipantsInvited([])
        e = VideoChatStarted()

        assert a == b
        assert hash(a) == hash(b)

        assert a != c
        assert hash(a) != hash(c)

        assert a != d
        assert hash(a) != hash(d)

        assert a != e
        assert hash(a) != hash(e)


class TestVideoChatScheduledWithoutRequest:
    start_date = dtm.datetime.now(dtm.timezone.utc)

    def test_slot_behaviour(self):
        inst = VideoChatScheduled(self.start_date)
        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_expected_values(self):
        assert VideoChatScheduled(self.start_date).start_date == self.start_date

    def test_de_json(self, offline_bot):

        json_dict = {"start_date": to_timestamp(self.start_date)}
        video_chat_scheduled = VideoChatScheduled.de_json(json_dict, offline_bot)
        assert video_chat_scheduled.api_kwargs == {}

        assert abs(video_chat_scheduled.start_date - self.start_date) < dtm.timedelta(seconds=1)

    def test_de_json_localization(self, tz_bot, offline_bot, raw_bot):
        json_dict = {"start_date": to_timestamp(self.start_date)}

        videochat_raw = VideoChatScheduled.de_json(json_dict, raw_bot)
        videochat_bot = VideoChatScheduled.de_json(json_dict, offline_bot)
        videochat_tz = VideoChatScheduled.de_json(json_dict, tz_bot)

        # comparing utcoffsets because comparing timezones is unpredicatable
        videochat_offset = videochat_tz.start_date.utcoffset()
        tz_bot_offset = tz_bot.defaults.tzinfo.utcoffset(
            videochat_tz.start_date.replace(tzinfo=None)
        )

        assert videochat_raw.start_date.tzinfo == UTC
        assert videochat_bot.start_date.tzinfo == UTC
        assert videochat_offset == tz_bot_offset

    def test_to_dict(self):
        video_chat_scheduled = VideoChatScheduled(self.start_date)
        video_chat_scheduled_dict = video_chat_scheduled.to_dict()

        assert isinstance(video_chat_scheduled_dict, dict)
        assert video_chat_scheduled_dict["start_date"] == to_timestamp(self.start_date)

    def test_equality(self):
        a = VideoChatScheduled(self.start_date)
        b = VideoChatScheduled(self.start_date)
        c = VideoChatScheduled(dtm.datetime.utcnow() + dtm.timedelta(seconds=5))
        d = VideoChatStarted()

        assert a == b
        assert hash(a) == hash(b)

        assert a != c
        assert hash(a) != hash(c)

        assert a != d
        assert hash(a) != hash(d)