diff options
author | 2020-02-24 01:09:58 +0100 | |
---|---|---|
committer | 2020-02-24 01:09:58 +0100 | |
commit | 0826d89f59d3f9be120f3a67412574271ef18b4c (patch) | |
tree | bd2b3de216cd0568ac14716443d5771802d2c299 /tests/helpers.py | |
parent | Update Dockerfile to use python:3.8-slim (diff) | |
parent | Merge pull request #711 from python-discord/bug/backend/b704/ready-missing-cache (diff) |
Merge branch 'master' into python38-migration
Diffstat (limited to 'tests/helpers.py')
-rw-r--r-- | tests/helpers.py | 35 |
1 files changed, 29 insertions, 6 deletions
diff --git a/tests/helpers.py b/tests/helpers.py index 506fe9894..7ae7ed621 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -9,6 +9,7 @@ from typing import Iterable, Optional import discord from discord.ext.commands import Context +from bot.api import APIClient from bot.bot import Bot @@ -182,9 +183,21 @@ class MockRole(CustomMockMixin, unittest.mock.Mock, ColourMixin, HashableMixin): spec_set = role_instance 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__(**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}' @@ -241,6 +254,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 @@ -259,11 +284,7 @@ class MockBot(CustomMockMixin, unittest.mock.MagicMock): def __init__(self, **kwargs) -> None: super().__init__(**kwargs) - - # 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 - # is technically incorrect, since it's a regular def.) - # self.wait_for = unittest.mock.AsyncMock() + self.api_client = MockAPIClient() # Since calling `create_task` on our MockBot does not actually schedule the coroutine object # as a task in the asyncio loop, this `side_effect` calls `close()` on the coroutine object @@ -429,6 +450,8 @@ class MockReaction(CustomMockMixin, unittest.mock.MagicMock): user_iterator.__aiter__.return_value = _users self.users.return_value = user_iterator + self.__str__.return_value = str(self.emoji) + webhook_instance = discord.Webhook(data=unittest.mock.MagicMock(), adapter=unittest.mock.MagicMock()) |