diff options
author | 2023-04-15 17:57:12 +0100 | |
---|---|---|
committer | 2023-04-15 17:57:12 +0100 | |
commit | 835ee25e9f4ab4b05be840c09134385759c34697 (patch) | |
tree | 4a95263d1395344fcae7e93be31c0631cd8d4c01 | |
parent | Add the required ports to snekbox urls in docker compose file (#2538) (diff) | |
parent | Improve test speed using some cached_property decorators on mocks (diff) |
Merge pull request #2541 from python-discord/faster-tests
Improve test speed with some cached_property decorators
-rw-r--r-- | tests/helpers.py | 36 |
1 files changed, 30 insertions, 6 deletions
diff --git a/tests/helpers.py b/tests/helpers.py index bb12c4977..b5a3f2463 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 functools import cached_property import discord from aiohttp import ClientSession @@ -174,9 +175,16 @@ class MockGuild(CustomMockMixin, unittest.mock.Mock, HashableMixin): default_kwargs = {"id": next(self.discord_id), "members": [], "chunked": True} super().__init__(**collections.ChainMap(kwargs, default_kwargs)) - self.roles = [MockRole(name="@everyone", position=1, id=0)] if roles: - self.roles.extend(roles) + self.roles = [ + MockRole(name="@everyone", position=1, id=0), + *roles + ] + + @cached_property + def roles(self) -> list[MockRole]: + """Cached roles property.""" + return [MockRole(name="@everyone", position=1, id=0)] # Create a Role instance to get a realistic Mock of `discord.Role` @@ -322,12 +330,28 @@ class MockBot(CustomMockMixin, unittest.mock.MagicMock): def __init__(self, **kwargs) -> None: super().__init__(**kwargs) - self.loop = _get_mock_loop() - self.api_client = MockAPIClient(loop=self.loop) - self.http_session = unittest.mock.create_autospec(spec=ClientSession, spec_set=True) - self.stats = unittest.mock.create_autospec(spec=AsyncStatsClient, spec_set=True) self.add_cog = unittest.mock.AsyncMock() + @cached_property + def loop(self) -> unittest.mock.Mock: + """Cached loop property.""" + return _get_mock_loop() + + @cached_property + def api_client(self) -> MockAPIClient: + """Cached api_client property.""" + return MockAPIClient() + + @cached_property + def http_session(self) -> unittest.mock.Mock: + """Cached http_session property.""" + return unittest.mock.create_autospec(spec=ClientSession, spec_set=True) + + @cached_property + def stats(self) -> unittest.mock.Mock: + """Cached stats property.""" + return unittest.mock.create_autospec(spec=AsyncStatsClient, spec_set=True) + # Create a TextChannel instance to get a realistic MagicMock of `discord.TextChannel` channel_data = { |