diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/bot/exts/backend/test_logging.py | 5 | ||||
-rw-r--r-- | tests/bot/exts/moderation/test_incidents.py | 8 | ||||
-rw-r--r-- | tests/helpers.py | 10 |
3 files changed, 18 insertions, 5 deletions
diff --git a/tests/bot/exts/backend/test_logging.py b/tests/bot/exts/backend/test_logging.py index 466f207d9..3a41220ad 100644 --- a/tests/bot/exts/backend/test_logging.py +++ b/tests/bot/exts/backend/test_logging.py @@ -3,7 +3,7 @@ from unittest.mock import patch from bot import constants from bot.exts.backend.logging import Logging -from tests.helpers import MockBot, MockTextChannel +from tests.helpers import MockBot, MockTextChannel, no_create_task class LoggingTests(unittest.IsolatedAsyncioTestCase): @@ -11,7 +11,8 @@ class LoggingTests(unittest.IsolatedAsyncioTestCase): def setUp(self): self.bot = MockBot() - self.cog = Logging(self.bot) + with no_create_task(): + self.cog = Logging(self.bot) self.dev_log = MockTextChannel(id=1234, name="dev-log") @patch("bot.exts.backend.logging.DEBUG_MODE", False) diff --git a/tests/bot/exts/moderation/test_incidents.py b/tests/bot/exts/moderation/test_incidents.py index bb337aeba..61f64da44 100644 --- a/tests/bot/exts/moderation/test_incidents.py +++ b/tests/bot/exts/moderation/test_incidents.py @@ -17,7 +17,7 @@ from bot.utils.time import TimestampFormats, discord_timestamp from tests.base import RedisTestCase from tests.helpers import ( MockAsyncWebhook, MockAttachment, MockBot, MockMember, MockMessage, MockReaction, MockRole, MockTextChannel, - MockUser + MockUser, no_create_task ) CURRENT_TIME = datetime.datetime(2022, 1, 1, tzinfo=datetime.UTC) @@ -306,7 +306,8 @@ class TestIncidents(RedisTestCase): Note that this will not schedule `crawl_incidents` in the background, as everything is being mocked. The `crawl_task` attribute will end up being None. """ - self.cog_instance = incidents.Incidents(MockBot()) + with no_create_task(): + self.cog_instance = incidents.Incidents(MockBot()) @patch("asyncio.sleep", AsyncMock()) # Prevent the coro from sleeping to speed up the test @@ -458,7 +459,8 @@ class TestMakeConfirmationTask(TestIncidents): If this function begins to fail, first check that `created_check` is being retrieved correctly. It should be the function that is built locally in the tested method. """ - self.cog_instance.make_confirmation_task(MockMessage(id=123)) + with no_create_task(): + self.cog_instance.make_confirmation_task(MockMessage(id=123)) self.cog_instance.bot.wait_for.assert_called_once() created_check = self.cog_instance.bot.wait_for.call_args.kwargs["check"] diff --git a/tests/helpers.py b/tests/helpers.py index 26ac42697..580848c25 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -6,6 +6,7 @@ import logging import unittest.mock from asyncio import AbstractEventLoop from collections.abc import Iterable +from contextlib import contextmanager from functools import cached_property import discord @@ -664,3 +665,12 @@ class MockAsyncWebhook(CustomMockMixin, unittest.mock.MagicMock): """ spec_set = webhook_instance additional_spec_asyncs = ("send", "edit", "delete", "execute") + +@contextmanager +def no_create_task(): + def side_effect(coro, *_, **__): + coro.close() + + with unittest.mock.patch("pydis_core.utils.scheduling.create_task") as create_task: + create_task.side_effect = side_effect + yield |