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
|
#!/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 OrderInfo, SuccessfulPayment
from telegram._utils.datetime import UTC, to_timestamp
from tests.auxil.slots import mro_slots
@pytest.fixture(scope="module")
def successful_payment():
return SuccessfulPayment(
SuccessfulPaymentTestBase.currency,
SuccessfulPaymentTestBase.total_amount,
SuccessfulPaymentTestBase.invoice_payload,
SuccessfulPaymentTestBase.telegram_payment_charge_id,
SuccessfulPaymentTestBase.provider_payment_charge_id,
shipping_option_id=SuccessfulPaymentTestBase.shipping_option_id,
order_info=SuccessfulPaymentTestBase.order_info,
subscription_expiration_date=SuccessfulPaymentTestBase.subscription_expiration_date,
is_recurring=SuccessfulPaymentTestBase.is_recurring,
is_first_recurring=SuccessfulPaymentTestBase.is_first_recurring,
)
class SuccessfulPaymentTestBase:
invoice_payload = "invoice_payload"
shipping_option_id = "shipping_option_id"
currency = "EUR"
total_amount = 100
order_info = OrderInfo()
telegram_payment_charge_id = "telegram_payment_charge_id"
provider_payment_charge_id = "provider_payment_charge_id"
subscription_expiration_date = dtm.datetime.now(tz=UTC).replace(microsecond=0)
is_recurring = True
is_first_recurring = True
class TestSuccessfulPaymentWithoutRequest(SuccessfulPaymentTestBase):
def test_slot_behaviour(self, successful_payment):
inst = successful_payment
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 = {
"invoice_payload": self.invoice_payload,
"shipping_option_id": self.shipping_option_id,
"currency": self.currency,
"total_amount": self.total_amount,
"order_info": self.order_info.to_dict(),
"telegram_payment_charge_id": self.telegram_payment_charge_id,
"provider_payment_charge_id": self.provider_payment_charge_id,
"subscription_expiration_date": to_timestamp(self.subscription_expiration_date),
"is_recurring": self.is_recurring,
"is_first_recurring": self.is_first_recurring,
}
successful_payment = SuccessfulPayment.de_json(json_dict, offline_bot)
assert successful_payment.api_kwargs == {}
assert successful_payment.invoice_payload == self.invoice_payload
assert successful_payment.shipping_option_id == self.shipping_option_id
assert successful_payment.currency == self.currency
assert successful_payment.total_amount == self.total_amount
assert successful_payment.order_info == self.order_info
assert successful_payment.telegram_payment_charge_id == self.telegram_payment_charge_id
assert successful_payment.provider_payment_charge_id == self.provider_payment_charge_id
assert successful_payment.subscription_expiration_date == self.subscription_expiration_date
assert successful_payment.is_recurring == self.is_recurring
assert successful_payment.is_first_recurring == self.is_first_recurring
def test_de_json_localization(self, offline_bot, raw_bot, tz_bot):
json_dict = {
"invoice_payload": self.invoice_payload,
"currency": self.currency,
"total_amount": self.total_amount,
"telegram_payment_charge_id": self.telegram_payment_charge_id,
"provider_payment_charge_id": self.provider_payment_charge_id,
"subscription_expiration_date": to_timestamp(self.subscription_expiration_date),
}
successful_payment = SuccessfulPayment.de_json(json_dict, offline_bot)
successful_payment_raw = SuccessfulPayment.de_json(json_dict, raw_bot)
successful_payment_tz = SuccessfulPayment.de_json(json_dict, tz_bot)
# comparing utcoffsets because comparing timezones is unpredicatable
date_offset = successful_payment_tz.subscription_expiration_date.utcoffset()
tz_bot_offset = tz_bot.defaults.tzinfo.utcoffset(
successful_payment_tz.subscription_expiration_date.replace(tzinfo=None)
)
assert successful_payment_raw.subscription_expiration_date.tzinfo == UTC
assert successful_payment.subscription_expiration_date.tzinfo == UTC
assert date_offset == tz_bot_offset
def test_to_dict(self, successful_payment):
successful_payment_dict = successful_payment.to_dict()
assert isinstance(successful_payment_dict, dict)
assert successful_payment_dict["invoice_payload"] == successful_payment.invoice_payload
assert (
successful_payment_dict["shipping_option_id"] == successful_payment.shipping_option_id
)
assert successful_payment_dict["currency"] == successful_payment.currency
assert successful_payment_dict["total_amount"] == successful_payment.total_amount
assert successful_payment_dict["order_info"] == successful_payment.order_info.to_dict()
assert (
successful_payment_dict["telegram_payment_charge_id"]
== successful_payment.telegram_payment_charge_id
)
assert (
successful_payment_dict["provider_payment_charge_id"]
== successful_payment.provider_payment_charge_id
)
assert successful_payment_dict["subscription_expiration_date"] == to_timestamp(
successful_payment.subscription_expiration_date
)
assert successful_payment_dict["is_recurring"] == successful_payment.is_recurring
assert (
successful_payment_dict["is_first_recurring"] == successful_payment.is_first_recurring
)
def test_equality(self):
a = SuccessfulPayment(
self.currency,
self.total_amount,
self.invoice_payload,
self.telegram_payment_charge_id,
self.provider_payment_charge_id,
)
b = SuccessfulPayment(
self.currency,
self.total_amount,
self.invoice_payload,
self.telegram_payment_charge_id,
self.provider_payment_charge_id,
)
c = SuccessfulPayment(
"", 0, "", self.telegram_payment_charge_id, self.provider_payment_charge_id
)
d = SuccessfulPayment(
self.currency,
self.total_amount,
self.invoice_payload,
self.telegram_payment_charge_id,
"",
)
assert a == b
assert hash(a) == hash(b)
assert a is not b
assert a == c
assert hash(a) == hash(c)
assert a != d
assert hash(a) != hash(d)
|