disnake

Форк
0
/
test_events.py 
116 строк · 3.3 Кб
1
# SPDX-License-Identifier: MIT
2
from typing import Any
3

4
import pytest
5

6
import disnake
7
from disnake import Event
8
from disnake.ext import commands
9

10

11
# n.b. the specific choice of events used in this file is irrelevant
12
@pytest.fixture
13
def client():
14
    return disnake.Client()
15

16

17
@pytest.fixture
18
def bot():
19
    return commands.Bot(
20
        command_prefix=commands.when_mentioned,
21
        command_sync_flags=commands.CommandSyncFlags.none(),
22
    )
23

24

25
@pytest.fixture(params=["client", "bot"])
26
def client_or_bot(request):
27
    return request.getfixturevalue(request.param)
28

29

30
# @Client.event
31

32

33
def test_event(client_or_bot: disnake.Client) -> None:
34
    assert not hasattr(client_or_bot, "on_message_edit")
35

36
    @client_or_bot.event
37
    async def on_message_edit(self, *args: Any) -> None:
38
        ...
39

40
    assert client_or_bot.on_message_edit is on_message_edit  # type: ignore
41

42

43
# Client.wait_for
44

45

46
@pytest.mark.parametrize("event", ["thread_create", Event.thread_create])
47
def test_wait_for(client_or_bot: disnake.Client, event) -> None:
48
    coro = client_or_bot.wait_for(event)
49
    assert len(client_or_bot._listeners["thread_create"]) == 1
50
    coro.close()  # close coroutine to avoid warning
51

52

53
# Client.add_listener / Client.remove_listener
54

55

56
@pytest.mark.parametrize("event", ["on_guild_remove", Event.guild_remove])
57
def test_addremove_listener(client_or_bot: disnake.Client, event) -> None:
58
    async def callback(self, *args: Any) -> None:
59
        ...
60

61
    client_or_bot.add_listener(callback, event)
62
    assert len(client_or_bot.extra_events["on_guild_remove"]) == 1
63
    client_or_bot.remove_listener(callback, event)
64
    assert len(client_or_bot.extra_events["on_guild_remove"]) == 0
65

66

67
def test_addremove_listener__implicit(client_or_bot: disnake.Client) -> None:
68
    async def on_guild_remove(self, *args: Any) -> None:
69
        ...
70

71
    client_or_bot.add_listener(on_guild_remove)
72
    assert len(client_or_bot.extra_events["on_guild_remove"]) == 1
73
    client_or_bot.remove_listener(on_guild_remove)
74
    assert len(client_or_bot.extra_events["on_guild_remove"]) == 0
75

76

77
# @Client.listen
78

79

80
@pytest.mark.parametrize("event", ["on_guild_role_create", Event.guild_role_create])
81
def test_listen(client_or_bot: disnake.Client, event) -> None:
82
    @client_or_bot.listen(event)
83
    async def callback(self, *args: Any) -> None:
84
        ...
85

86
    assert len(client_or_bot.extra_events["on_guild_role_create"]) == 1
87

88

89
def test_listen__implicit(client_or_bot: disnake.Client) -> None:
90
    @client_or_bot.listen()
91
    async def on_guild_role_create(self, *args: Any) -> None:
92
        ...
93

94
    assert len(client_or_bot.extra_events["on_guild_role_create"]) == 1
95

96

97
# @commands.Cog.listener
98
@pytest.mark.parametrize("event", ["on_automod_rule_update", Event.automod_rule_update])
99
def test_listener(bot: commands.Bot, event) -> None:
100
    class Cog(commands.Cog):
101
        @commands.Cog.listener(event)
102
        async def callback(self, *args: Any) -> None:
103
            ...
104

105
    bot.add_cog(Cog())
106
    assert len(bot.extra_events["on_automod_rule_update"]) == 1
107

108

109
def test_listener__implicit(bot: commands.Bot) -> None:
110
    class Cog(commands.Cog):
111
        @commands.Cog.listener()
112
        async def on_automod_rule_update(self, *args: Any) -> None:
113
            ...
114

115
    bot.add_cog(Cog())
116
    assert len(bot.extra_events["on_automod_rule_update"]) == 1
117

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.