File: test_suggestedpost.py

package info (click to toggle)
python-telegram-bot 22.4-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 11,796 kB
  • sloc: python: 92,379; makefile: 179; sh: 4
file content (600 lines) | stat: -rw-r--r-- 21,869 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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
#!/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 Dice
from telegram._chat import Chat
from telegram._message import Message
from telegram._payment.stars.staramount import StarAmount
from telegram._suggestedpost import (
    SuggestedPostApprovalFailed,
    SuggestedPostApproved,
    SuggestedPostDeclined,
    SuggestedPostInfo,
    SuggestedPostPaid,
    SuggestedPostParameters,
    SuggestedPostPrice,
    SuggestedPostRefunded,
)
from telegram._utils.datetime import UTC, to_timestamp
from telegram.constants import SuggestedPostInfoState
from tests.auxil.slots import mro_slots


@pytest.fixture(scope="module")
def suggested_post_parameters():
    return SuggestedPostParameters(
        price=SuggestedPostParametersTestBase.price,
        send_date=SuggestedPostParametersTestBase.send_date,
    )


class SuggestedPostParametersTestBase:
    price = SuggestedPostPrice(currency="XTR", amount=100)
    send_date = dtm.datetime.now(tz=UTC).replace(microsecond=0)


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

    def test_de_json(self, offline_bot):
        json_dict = {
            "price": self.price.to_dict(),
            "send_date": to_timestamp(self.send_date),
        }
        spp = SuggestedPostParameters.de_json(json_dict, offline_bot)
        assert spp.price == self.price
        assert spp.send_date == self.send_date
        assert spp.api_kwargs == {}

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

        spp_bot = SuggestedPostParameters.de_json(json_dict, offline_bot)
        spp_bot_raw = SuggestedPostParameters.de_json(json_dict, raw_bot)
        spp_bot_tz = SuggestedPostParameters.de_json(json_dict, tz_bot)

        # comparing utcoffsets because comparing tzinfo objects is not reliable
        send_date_offset = spp_bot_tz.send_date.utcoffset()
        send_date_offset_tz = tz_bot.defaults.tzinfo.utcoffset(
            spp_bot_tz.send_date.replace(tzinfo=None)
        )

        assert spp_bot.send_date.tzinfo == UTC
        assert spp_bot_raw.send_date.tzinfo == UTC
        assert send_date_offset_tz == send_date_offset

    def test_to_dict(self, suggested_post_parameters):
        spp_dict = suggested_post_parameters.to_dict()

        assert isinstance(spp_dict, dict)
        assert spp_dict["price"] == self.price.to_dict()
        assert spp_dict["send_date"] == to_timestamp(self.send_date)

    def test_equality(self, suggested_post_parameters):
        a = suggested_post_parameters
        b = SuggestedPostParameters(price=self.price, send_date=self.send_date)
        c = SuggestedPostParameters(
            price=self.price, send_date=self.send_date + dtm.timedelta(seconds=1)
        )
        e = 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)


@pytest.fixture(scope="module")
def suggested_post_info():
    return SuggestedPostInfo(
        state=SuggestedPostInfoTestBase.state,
        price=SuggestedPostInfoTestBase.price,
        send_date=SuggestedPostInfoTestBase.send_date,
    )


class SuggestedPostInfoTestBase:
    state = SuggestedPostInfoState.PENDING
    price = SuggestedPostPrice(currency="XTR", amount=100)
    send_date = dtm.datetime.now(tz=UTC).replace(microsecond=0)


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

    def test_type_enum_conversion(self):
        assert type(SuggestedPostInfo("pending").state) is SuggestedPostInfoState
        assert SuggestedPostInfo("unknown").state == "unknown"

    def test_de_json(self, offline_bot):
        json_dict = {
            "state": self.state,
            "price": self.price.to_dict(),
            "send_date": to_timestamp(self.send_date),
        }
        spi = SuggestedPostInfo.de_json(json_dict, offline_bot)
        assert spi.state == self.state
        assert spi.price == self.price
        assert spi.send_date == self.send_date
        assert spi.api_kwargs == {}

    def test_de_json_localization(self, offline_bot, raw_bot, tz_bot):
        json_dict = {
            "state": self.state,
            "price": self.price.to_dict(),
            "send_date": to_timestamp(self.send_date),
        }

        spi_bot = SuggestedPostInfo.de_json(json_dict, offline_bot)
        spi_bot_raw = SuggestedPostInfo.de_json(json_dict, raw_bot)
        spi_bot_tz = SuggestedPostInfo.de_json(json_dict, tz_bot)

        # comparing utcoffsets because comparing tzinfo objects is not reliable
        send_date_offset = spi_bot_tz.send_date.utcoffset()
        send_date_offset_tz = tz_bot.defaults.tzinfo.utcoffset(
            spi_bot_tz.send_date.replace(tzinfo=None)
        )

        assert spi_bot.send_date.tzinfo == UTC
        assert spi_bot_raw.send_date.tzinfo == UTC
        assert send_date_offset_tz == send_date_offset

    def test_to_dict(self, suggested_post_info):
        spi_dict = suggested_post_info.to_dict()

        assert isinstance(spi_dict, dict)
        assert spi_dict["state"] == self.state
        assert spi_dict["price"] == self.price.to_dict()
        assert spi_dict["send_date"] == to_timestamp(self.send_date)

    def test_equality(self, suggested_post_info):
        a = suggested_post_info
        b = SuggestedPostInfo(state=self.state, price=self.price)
        c = SuggestedPostInfo(state=SuggestedPostInfoState.DECLINED, price=self.price)
        e = 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)


@pytest.fixture(scope="module")
def suggested_post_price():
    return SuggestedPostPrice(
        currency=SuggestedPostPriceTestBase.currency,
        amount=SuggestedPostPriceTestBase.amount,
    )


class SuggestedPostPriceTestBase:
    currency = "XTR"
    amount = 100


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

    def test_de_json(self, offline_bot):
        json_dict = {
            "currency": self.currency,
            "amount": self.amount,
        }
        spp = SuggestedPostPrice.de_json(json_dict, offline_bot)
        assert spp.currency == self.currency
        assert spp.amount == self.amount
        assert spp.api_kwargs == {}

    def test_to_dict(self, suggested_post_price):
        spp_dict = suggested_post_price.to_dict()

        assert isinstance(spp_dict, dict)
        assert spp_dict["currency"] == self.currency
        assert spp_dict["amount"] == self.amount

    def test_equality(self, suggested_post_price):
        a = suggested_post_price
        b = SuggestedPostPrice(currency=self.currency, amount=self.amount)
        c = SuggestedPostPrice(currency="TON", amount=self.amount)
        e = 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)


@pytest.fixture(scope="module")
def suggested_post_declined():
    return SuggestedPostDeclined(
        suggested_post_message=SuggestedPostDeclinedTestBase.suggested_post_message,
        comment=SuggestedPostDeclinedTestBase.comment,
    )


class SuggestedPostDeclinedTestBase:
    suggested_post_message = Message(1, dtm.datetime.now(), Chat(1, ""), text="post this pls.")
    comment = "another time"


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

    def test_de_json(self, offline_bot):
        json_dict = {
            "suggested_post_message": self.suggested_post_message.to_dict(),
            "comment": self.comment,
        }
        spd = SuggestedPostDeclined.de_json(json_dict, offline_bot)
        assert spd.suggested_post_message == self.suggested_post_message
        assert spd.comment == self.comment
        assert spd.api_kwargs == {}

    def test_to_dict(self, suggested_post_declined):
        spd_dict = suggested_post_declined.to_dict()

        assert isinstance(spd_dict, dict)
        assert spd_dict["suggested_post_message"] == self.suggested_post_message.to_dict()
        assert spd_dict["comment"] == self.comment

    def test_equality(self, suggested_post_declined):
        a = suggested_post_declined
        b = SuggestedPostDeclined(
            suggested_post_message=self.suggested_post_message, comment=self.comment
        )
        c = SuggestedPostDeclined(suggested_post_message=self.suggested_post_message, comment="no")
        e = 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)


@pytest.fixture(scope="module")
def suggested_post_paid():
    return SuggestedPostPaid(
        currency=SuggestedPostPaidTestBase.currency,
        suggested_post_message=SuggestedPostPaidTestBase.suggested_post_message,
        amount=SuggestedPostPaidTestBase.amount,
        star_amount=SuggestedPostPaidTestBase.star_amount,
    )


class SuggestedPostPaidTestBase:
    suggested_post_message = Message(1, dtm.datetime.now(), Chat(1, ""), text="post this pls.")
    currency = "XTR"
    amount = 100
    star_amount = StarAmount(100)


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

    def test_de_json(self, offline_bot):
        json_dict = {
            "suggested_post_message": self.suggested_post_message.to_dict(),
            "currency": self.currency,
            "amount": self.amount,
            "star_amount": self.star_amount.to_dict(),
        }
        spp = SuggestedPostPaid.de_json(json_dict, offline_bot)
        assert spp.suggested_post_message == self.suggested_post_message
        assert spp.currency == self.currency
        assert spp.amount == self.amount
        assert spp.star_amount == self.star_amount
        assert spp.api_kwargs == {}

    def test_to_dict(self, suggested_post_paid):
        spp_dict = suggested_post_paid.to_dict()

        assert isinstance(spp_dict, dict)
        assert spp_dict["suggested_post_message"] == self.suggested_post_message.to_dict()
        assert spp_dict["currency"] == self.currency
        assert spp_dict["amount"] == self.amount
        assert spp_dict["star_amount"] == self.star_amount.to_dict()

    def test_equality(self, suggested_post_paid):
        a = suggested_post_paid
        b = SuggestedPostPaid(
            suggested_post_message=self.suggested_post_message,
            currency=self.currency,
            amount=self.amount,
            star_amount=self.star_amount,
        )
        c = SuggestedPostPaid(
            suggested_post_message=self.suggested_post_message,
            currency=self.currency,
            amount=self.amount - 1,
            star_amount=StarAmount(self.amount - 1),
        )
        e = 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)


@pytest.fixture(scope="module")
def suggested_post_refunded():
    return SuggestedPostRefunded(
        reason=SuggestedPostRefundedTestBase.reason,
        suggested_post_message=SuggestedPostRefundedTestBase.suggested_post_message,
    )


class SuggestedPostRefundedTestBase:
    reason = "post_deleted"
    suggested_post_message = Message(1, dtm.datetime.now(), Chat(1, ""), text="post this pls.")


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

    def test_de_json(self, offline_bot):
        json_dict = {
            "suggested_post_message": self.suggested_post_message.to_dict(),
            "reason": self.reason,
        }
        spr = SuggestedPostRefunded.de_json(json_dict, offline_bot)
        assert spr.suggested_post_message == self.suggested_post_message
        assert spr.reason == self.reason
        assert spr.api_kwargs == {}

    def test_to_dict(self, suggested_post_refunded):
        spr_dict = suggested_post_refunded.to_dict()

        assert isinstance(spr_dict, dict)
        assert spr_dict["suggested_post_message"] == self.suggested_post_message.to_dict()
        assert spr_dict["reason"] == self.reason

    def test_equality(self, suggested_post_refunded):
        a = suggested_post_refunded
        b = SuggestedPostRefunded(
            suggested_post_message=self.suggested_post_message, reason=self.reason
        )
        c = SuggestedPostRefunded(
            suggested_post_message=self.suggested_post_message, reason="payment_refunded"
        )
        e = 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)


@pytest.fixture(scope="module")
def suggested_post_approved():
    return SuggestedPostApproved(
        send_date=SuggestedPostApprovedTestBase.send_date,
        suggested_post_message=SuggestedPostApprovedTestBase.suggested_post_message,
        price=SuggestedPostApprovedTestBase.price,
    )


class SuggestedPostApprovedTestBase:
    send_date = dtm.datetime.now(tz=UTC).replace(microsecond=0)
    suggested_post_message = Message(1, dtm.datetime.now(), Chat(1, ""), text="post this pls.")
    price = SuggestedPostPrice(currency="XTR", amount=100)


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

    def test_de_json(self, offline_bot):
        json_dict = {
            "send_date": to_timestamp(self.send_date),
            "suggested_post_message": self.suggested_post_message.to_dict(),
            "price": self.price.to_dict(),
        }
        spa = SuggestedPostApproved.de_json(json_dict, offline_bot)
        assert spa.send_date == self.send_date
        assert spa.suggested_post_message == self.suggested_post_message
        assert spa.price == self.price
        assert spa.api_kwargs == {}

    def test_de_json_localization(self, offline_bot, raw_bot, tz_bot):
        json_dict = {
            "send_date": to_timestamp(self.send_date),
            "suggested_post_message": self.suggested_post_message.to_dict(),
            "price": self.price.to_dict(),
        }

        spa_bot = SuggestedPostApproved.de_json(json_dict, offline_bot)
        spa_bot_raw = SuggestedPostApproved.de_json(json_dict, raw_bot)
        spi_bot_tz = SuggestedPostApproved.de_json(json_dict, tz_bot)

        # comparing utcoffsets because comparing tzinfo objects is not reliable
        send_date_offset = spi_bot_tz.send_date.utcoffset()
        send_date_offset_tz = tz_bot.defaults.tzinfo.utcoffset(
            spi_bot_tz.send_date.replace(tzinfo=None)
        )

        assert spa_bot.send_date.tzinfo == UTC
        assert spa_bot_raw.send_date.tzinfo == UTC
        assert send_date_offset_tz == send_date_offset

    def test_to_dict(self, suggested_post_approved):
        spa_dict = suggested_post_approved.to_dict()

        assert isinstance(spa_dict, dict)
        assert spa_dict["send_date"] == to_timestamp(self.send_date)
        assert spa_dict["suggested_post_message"] == self.suggested_post_message.to_dict()
        assert spa_dict["price"] == self.price.to_dict()

    def test_equality(self, suggested_post_approved):
        a = suggested_post_approved
        b = SuggestedPostApproved(
            send_date=self.send_date,
            suggested_post_message=self.suggested_post_message,
            price=self.price,
        )
        c = SuggestedPostApproved(
            send_date=self.send_date,
            suggested_post_message=self.suggested_post_message,
            price=SuggestedPostPrice(currency="XTR", amount=self.price.amount - 1),
        )
        e = 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)


@pytest.fixture(scope="module")
def suggested_post_approval_failed():
    return SuggestedPostApprovalFailed(
        price=SuggestedPostApprovalFailedTestBase.price,
        suggested_post_message=SuggestedPostApprovalFailedTestBase.suggested_post_message,
    )


class SuggestedPostApprovalFailedTestBase:
    price = SuggestedPostPrice(currency="XTR", amount=100)
    suggested_post_message = Message(1, dtm.datetime.now(), Chat(1, ""), text="post this pls.")


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

    def test_de_json(self, offline_bot):
        json_dict = {
            "price": self.price.to_dict(),
            "suggested_post_message": self.suggested_post_message.to_dict(),
        }
        spaf = SuggestedPostApprovalFailed.de_json(json_dict, offline_bot)
        assert spaf.price == self.price
        assert spaf.suggested_post_message == self.suggested_post_message
        assert spaf.api_kwargs == {}

    def test_to_dict(self, suggested_post_approval_failed):
        spaf_dict = suggested_post_approval_failed.to_dict()

        assert isinstance(spaf_dict, dict)
        assert spaf_dict["price"] == self.price.to_dict()
        assert spaf_dict["suggested_post_message"] == self.suggested_post_message.to_dict()

    def test_equality(self, suggested_post_approval_failed):
        a = suggested_post_approval_failed
        b = SuggestedPostApprovalFailed(
            price=self.price,
            suggested_post_message=self.suggested_post_message,
        )
        c = SuggestedPostApprovalFailed(
            price=SuggestedPostPrice(currency="XTR", amount=self.price.amount - 1),
            suggested_post_message=self.suggested_post_message,
        )
        e = 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)