aboutsummaryrefslogtreecommitdiffstats
path: root/tests/helpers.py
diff options
context:
space:
mode:
authorGravatar Mark <[email protected]>2020-02-23 15:42:20 -0800
committerGravatar GitHub <[email protected]>2020-02-23 15:42:20 -0800
commitc81a4d401ea434e98b0a1ece51d3d10f1a3ad226 (patch)
tree1fe8caf0ae751cfe94a71fcf02ffbbfda5bd8812 /tests/helpers.py
parentMerge pull request #749 from python-discord/reminder-enhancements (diff)
parentMerge remote-tracking branch 'origin/master' into bug/backend/b704/ready-miss... (diff)
Merge pull request #711 from python-discord/bug/backend/b704/ready-missing-cache
Prevent the role syncer from wiping the database table during API latency
Diffstat (limited to 'tests/helpers.py')
-rw-r--r--tests/helpers.py29
1 files changed, 28 insertions, 1 deletions
diff --git a/tests/helpers.py b/tests/helpers.py
index 5df796c23..9d9dd5da6 100644
--- a/tests/helpers.py
+++ b/tests/helpers.py
@@ -12,6 +12,7 @@ from typing import Any, Iterable, Optional
import discord
from discord.ext.commands import Context
+from bot.api import APIClient
from bot.bot import Bot
@@ -269,9 +270,21 @@ class MockRole(CustomMockMixin, unittest.mock.Mock, ColourMixin, HashableMixin):
information, see the `MockGuild` docstring.
"""
def __init__(self, **kwargs) -> None:
- default_kwargs = {'id': next(self.discord_id), 'name': 'role', 'position': 1}
+ default_kwargs = {
+ 'id': next(self.discord_id),
+ 'name': 'role',
+ 'position': 1,
+ 'colour': discord.Colour(0xdeadbf),
+ 'permissions': discord.Permissions(),
+ }
super().__init__(spec_set=role_instance, **collections.ChainMap(kwargs, default_kwargs))
+ if isinstance(self.colour, int):
+ self.colour = discord.Colour(self.colour)
+
+ if isinstance(self.permissions, int):
+ self.permissions = discord.Permissions(self.permissions)
+
if 'mention' not in kwargs:
self.mention = f'&{self.name}'
@@ -324,6 +337,18 @@ class MockUser(CustomMockMixin, unittest.mock.Mock, ColourMixin, HashableMixin):
self.mention = f"@{self.name}"
+class MockAPIClient(CustomMockMixin, unittest.mock.MagicMock):
+ """
+ A MagicMock subclass to mock APIClient objects.
+
+ Instances of this class will follow the specifications of `bot.api.APIClient` instances.
+ For more information, see the `MockGuild` docstring.
+ """
+
+ def __init__(self, **kwargs) -> None:
+ super().__init__(spec_set=APIClient, **kwargs)
+
+
# Create a Bot instance to get a realistic MagicMock of `discord.ext.commands.Bot`
bot_instance = Bot(command_prefix=unittest.mock.MagicMock())
bot_instance.http_session = None
@@ -340,6 +365,7 @@ class MockBot(CustomMockMixin, unittest.mock.MagicMock):
def __init__(self, **kwargs) -> None:
super().__init__(spec_set=bot_instance, **kwargs)
+ self.api_client = MockAPIClient()
# self.wait_for is *not* a coroutine function, but returns a coroutine nonetheless and
# and should therefore be awaited. (The documentation calls it a coroutine as well, which
@@ -503,6 +529,7 @@ class MockReaction(CustomMockMixin, unittest.mock.MagicMock):
self.emoji = kwargs.get('emoji', MockEmoji())
self.message = kwargs.get('message', MockMessage())
self.users = AsyncIteratorMock(kwargs.get('users', []))
+ self.__str__.return_value = str(self.emoji)
webhook_instance = discord.Webhook(data=unittest.mock.MagicMock(), adapter=unittest.mock.MagicMock())