2021-09-22 16:49:10 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# A library that provides a Python interface to the Telegram Bot API
|
2023-01-01 21:31:29 +01:00
|
|
|
# Copyright (C) 2015-2023
|
2021-09-22 16:49:10 +02:00
|
|
|
# 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/].
|
|
|
|
from collections import defaultdict
|
2022-05-05 09:27:54 +02:00
|
|
|
from pathlib import Path
|
2021-09-22 16:49:10 +02:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2021-10-10 15:10:21 +02:00
|
|
|
from telegram._utils.warnings import warn
|
2022-05-05 09:27:54 +02:00
|
|
|
from telegram.warnings import PTBDeprecationWarning, PTBRuntimeWarning, PTBUserWarning
|
2023-02-22 20:19:46 +01:00
|
|
|
from tests.auxil.files import PROJECT_ROOT_PATH
|
|
|
|
from tests.auxil.slots import mro_slots
|
2021-09-22 16:49:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TestWarnings:
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"inst",
|
|
|
|
[
|
|
|
|
(PTBUserWarning("test message")),
|
|
|
|
(PTBRuntimeWarning("test message")),
|
|
|
|
(PTBDeprecationWarning()),
|
|
|
|
],
|
|
|
|
)
|
2023-02-22 20:19:46 +01:00
|
|
|
def test_slots_behavior(self, inst):
|
2021-09-22 16:49:10 +02:00
|
|
|
for attr in inst.__slots__:
|
2022-05-05 17:40:22 +02:00
|
|
|
assert getattr(inst, attr, "err") != "err", f"got extra slot '{attr}'"
|
2021-09-22 16:49:10 +02:00
|
|
|
assert len(mro_slots(inst)) == len(set(mro_slots(inst))), "duplicate slot"
|
|
|
|
|
|
|
|
def test_test_coverage(self):
|
|
|
|
"""This test is only here to make sure that new warning classes will set __slots__
|
|
|
|
properly.
|
|
|
|
Add the new warning class to the below covered_subclasses dict, if it's covered in the
|
|
|
|
above test_slots_behavior tests.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def make_assertion(cls):
|
|
|
|
assert set(cls.__subclasses__()) == covered_subclasses[cls]
|
|
|
|
for subcls in cls.__subclasses__():
|
|
|
|
make_assertion(subcls)
|
|
|
|
|
|
|
|
covered_subclasses = defaultdict(set)
|
|
|
|
covered_subclasses.update(
|
|
|
|
{
|
|
|
|
PTBUserWarning: {
|
|
|
|
PTBRuntimeWarning,
|
|
|
|
PTBDeprecationWarning,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
make_assertion(PTBUserWarning)
|
|
|
|
|
|
|
|
def test_warn(self, recwarn):
|
2022-05-05 17:40:22 +02:00
|
|
|
expected_file = PROJECT_ROOT_PATH / "telegram" / "_utils" / "warnings.py"
|
2021-09-22 16:49:10 +02:00
|
|
|
|
2022-05-05 17:40:22 +02:00
|
|
|
warn("test message")
|
2021-09-22 16:49:10 +02:00
|
|
|
assert len(recwarn) == 1
|
|
|
|
assert recwarn[0].category is PTBUserWarning
|
2022-05-05 17:40:22 +02:00
|
|
|
assert str(recwarn[0].message) == "test message"
|
2021-10-13 08:12:48 +02:00
|
|
|
assert Path(recwarn[0].filename) == expected_file, "incorrect stacklevel!"
|
2021-09-22 16:49:10 +02:00
|
|
|
|
2022-05-05 17:40:22 +02:00
|
|
|
warn("test message 2", category=PTBRuntimeWarning)
|
2021-09-22 16:49:10 +02:00
|
|
|
assert len(recwarn) == 2
|
|
|
|
assert recwarn[1].category is PTBRuntimeWarning
|
2022-05-05 17:40:22 +02:00
|
|
|
assert str(recwarn[1].message) == "test message 2"
|
2021-10-13 08:12:48 +02:00
|
|
|
assert Path(recwarn[1].filename) == expected_file, "incorrect stacklevel!"
|
2021-09-22 16:49:10 +02:00
|
|
|
|
2022-05-05 17:40:22 +02:00
|
|
|
warn("test message 3", stacklevel=1, category=PTBDeprecationWarning)
|
2021-10-13 08:12:48 +02:00
|
|
|
expected_file = Path(__file__)
|
2021-09-22 16:49:10 +02:00
|
|
|
assert len(recwarn) == 3
|
|
|
|
assert recwarn[2].category is PTBDeprecationWarning
|
2022-05-05 17:40:22 +02:00
|
|
|
assert str(recwarn[2].message) == "test message 3"
|
2021-10-13 08:12:48 +02:00
|
|
|
assert Path(recwarn[2].filename) == expected_file, "incorrect stacklevel!"
|