From a3b0ffb72a1e72fc3be0e96c7407ddff2ade67c9 Mon Sep 17 00:00:00 2001 From: Numerlor Date: Tue, 14 Jun 2022 00:37:07 +0200 Subject: Add decorator to block duplicate command invocations in a channel --- tests/botcore/utils/test_cooldown.py | 48 ++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 tests/botcore/utils/test_cooldown.py (limited to 'tests') diff --git a/tests/botcore/utils/test_cooldown.py b/tests/botcore/utils/test_cooldown.py new file mode 100644 index 00000000..e7fe0f59 --- /dev/null +++ b/tests/botcore/utils/test_cooldown.py @@ -0,0 +1,48 @@ +import unittest +from unittest.mock import patch + +from botcore.utils.cooldown import _ArgsTuple, _CommandCooldownManager + + +def create_argument_tuple(*args, **kwargs) -> _ArgsTuple: + return (*args, *kwargs.items()) + + +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)) -- cgit v1.2.3