aboutsummaryrefslogtreecommitdiffstats
path: root/tests/pydis_core/utils/test_cooldown.py
blob: eed16da33175a1c5e64acdea4f6b56d23d46ca9c (plain) (blame)
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
import unittest
from unittest.mock import patch

from pydis_core.utils.cooldown import _CommandCooldownManager, _create_argument_tuple


class CommandCooldownManagerTests(unittest.IsolatedAsyncioTestCase):
    test_call_args = (
        _create_argument_tuple(0),
        _create_argument_tuple(a=0),
        _create_argument_tuple([]),
        _create_argument_tuple(a=[]),
        _create_argument_tuple(1, 2, 3, a=4, b=5, c=6),
        _create_argument_tuple([1], [2], [3], a=[4], b=[5], c=[6]),
        _create_argument_tuple([1], 2, [3], a=4, b=[5], c=6),
    )

    async def asyncSetUp(self):
        self.cooldown_manager = _CommandCooldownManager(cooldown_duration=5)

    def test_no_cooldown_on_unset(self):
        for call_args in self.test_call_args:
            with self.subTest(arguments_tuple=call_args, channel=0):
                self.assertFalse(self.cooldown_manager.is_on_cooldown(0, call_args))

        for call_args in self.test_call_args:
            with self.subTest(arguments_tuple=call_args, channel=1):
                self.assertFalse(self.cooldown_manager.is_on_cooldown(1, call_args))

    @patch("time.monotonic")
    def test_cooldown_is_set(self, monotonic):
        monotonic.side_effect = lambda: 0
        for call_args in self.test_call_args:
            with self.subTest(arguments_tuple=call_args):
                self.cooldown_manager.set_cooldown(0, call_args)
                self.assertTrue(self.cooldown_manager.is_on_cooldown(0, call_args))

    @patch("time.monotonic")
    def test_cooldown_expires(self, monotonic):
        for call_args in self.test_call_args:
            monotonic.side_effect = (0, 1000)
            with self.subTest(arguments_tuple=call_args):
                self.cooldown_manager.set_cooldown(0, call_args)
                self.assertFalse(self.cooldown_manager.is_on_cooldown(0, call_args))

    def test_keywords_and_tuples_differentiated(self):
        self.cooldown_manager.set_cooldown(0, _create_argument_tuple(("a", 0)))
        self.assertFalse(self.cooldown_manager.is_on_cooldown(0, _create_argument_tuple(a=0)))
        self.assertTrue(self.cooldown_manager.is_on_cooldown(0, _create_argument_tuple(("a", 0))))