diff options
author | 2023-04-09 21:15:18 +0100 | |
---|---|---|
committer | 2023-04-11 16:48:14 +0100 | |
commit | 8dca42846d2956122d45795763095559a6a51b64 (patch) | |
tree | 480cd7d3c1a6d6bc87710e2d3c19f223a92f7c5d /tests/helpers.py | |
parent | Replace CI flake8 config with ruff (diff) |
Migrate code style to ruff
Co-authored-by: Boris Muratov <[email protected]>
Co-authored-by: wookie184 <[email protected]>
Diffstat (limited to 'tests/helpers.py')
-rw-r--r-- | tests/helpers.py | 187 |
1 files changed, 93 insertions, 94 deletions
diff --git a/tests/helpers.py b/tests/helpers.py index 020f1aee5..bb12c4977 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -5,7 +5,7 @@ import itertools import logging import unittest.mock from asyncio import AbstractEventLoop -from typing import Iterable, Optional +from collections.abc import Iterable import discord from aiohttp import ClientSession @@ -79,7 +79,7 @@ class CustomMockMixin: additional_spec_asyncs = None def __init__(self, **kwargs): - name = kwargs.pop('name', None) # `name` has special meaning for Mock classes, so we need to set it manually. + name = kwargs.pop("name", None) # `name` has special meaning for Mock classes, so we need to set it manually. super().__init__(spec_set=self.spec_set, **kwargs) if self.additional_spec_asyncs: @@ -101,7 +101,7 @@ class CustomMockMixin: This override will look for an attribute called `child_mock_type` and use that as the type of the child mock. """ _new_name = kw.get("_new_name") - if _new_name in self.__dict__['_spec_asyncs']: + if _new_name in self.__dict__["_spec_asyncs"]: return unittest.mock.AsyncMock(**kw) _type = type(self) @@ -121,23 +121,23 @@ class CustomMockMixin: # Create a guild instance to get a realistic Mock of `discord.Guild` guild_data = { - 'id': 1, - 'name': 'guild', - 'region': 'Europe', - 'verification_level': 2, - 'default_notications': 1, - 'afk_timeout': 100, - 'icon': "icon.png", - 'banner': 'banner.png', - 'mfa_level': 1, - 'splash': 'splash.png', - 'system_channel_id': 464033278631084042, - 'description': 'mocking is fun', - 'max_presences': 10_000, - 'max_members': 100_000, - 'preferred_locale': 'UTC', - 'owner_id': 1, - 'afk_channel_id': 464033278631084042, + "id": 1, + "name": "guild", + "region": "Europe", + "verification_level": 2, + "default_notications": 1, + "afk_timeout": 100, + "icon": "icon.png", + "banner": "banner.png", + "mfa_level": 1, + "splash": "splash.png", + "system_channel_id": 464033278631084042, + "description": "mocking is fun", + "max_presences": 10_000, + "max_members": 100_000, + "preferred_locale": "UTC", + "owner_id": 1, + "afk_channel_id": 464033278631084042, } guild_instance = discord.Guild(data=guild_data, state=unittest.mock.MagicMock()) @@ -170,8 +170,8 @@ class MockGuild(CustomMockMixin, unittest.mock.Mock, HashableMixin): """ spec_set = guild_instance - def __init__(self, roles: Optional[Iterable[MockRole]] = None, **kwargs) -> None: - default_kwargs = {'id': next(self.discord_id), 'members': [], "chunked": True} + def __init__(self, roles: Iterable[MockRole] | None = None, **kwargs) -> None: + 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)] @@ -180,7 +180,7 @@ class MockGuild(CustomMockMixin, unittest.mock.Mock, HashableMixin): # Create a Role instance to get a realistic Mock of `discord.Role` -role_data = {'name': 'role', 'id': 1} +role_data = {"name": "role", "id": 1} role_instance = discord.Role(guild=guild_instance, state=unittest.mock.MagicMock(), data=role_data) @@ -195,11 +195,11 @@ class MockRole(CustomMockMixin, unittest.mock.Mock, ColourMixin, HashableMixin): def __init__(self, **kwargs) -> None: default_kwargs = { - 'id': next(self.discord_id), - 'name': 'role', - 'position': 1, - 'colour': discord.Colour(0xdeadbf), - 'permissions': discord.Permissions(), + "id": next(self.discord_id), + "name": "role", + "position": 1, + "colour": discord.Colour(0xdeadbf), + "permissions": discord.Permissions(), } super().__init__(**collections.ChainMap(kwargs, default_kwargs)) @@ -209,8 +209,8 @@ class MockRole(CustomMockMixin, unittest.mock.Mock, ColourMixin, HashableMixin): if isinstance(self.permissions, int): self.permissions = discord.Permissions(self.permissions) - if 'mention' not in kwargs: - self.mention = f'&{self.name}' + if "mention" not in kwargs: + self.mention = f"&{self.name}" def __lt__(self, other): """Simplified position-based comparisons similar to those of `discord.Role`.""" @@ -222,7 +222,7 @@ class MockRole(CustomMockMixin, unittest.mock.Mock, ColourMixin, HashableMixin): # Create a Member instance to get a realistic Mock of `discord.Member` -member_data = {'user': 'lemon', 'roles': [1], 'flags': 2} +member_data = {"user": "lemon", "roles": [1], "flags": 2} state_mock = unittest.mock.MagicMock() member_instance = discord.Member(data=member_data, guild=guild_instance, state=state_mock) @@ -236,8 +236,8 @@ class MockMember(CustomMockMixin, unittest.mock.Mock, ColourMixin, HashableMixin """ spec_set = member_instance - def __init__(self, roles: Optional[Iterable[MockRole]] = None, **kwargs) -> None: - default_kwargs = {'name': 'member', 'id': next(self.discord_id), 'bot': False, "pending": False} + def __init__(self, roles: Iterable[MockRole] | None = None, **kwargs) -> None: + default_kwargs = {"name": "member", "id": next(self.discord_id), "bot": False, "pending": False} super().__init__(**collections.ChainMap(kwargs, default_kwargs)) self.roles = [MockRole(name="@everyone", position=1, id=0)] @@ -245,7 +245,7 @@ class MockMember(CustomMockMixin, unittest.mock.Mock, ColourMixin, HashableMixin self.roles.extend(roles) self.top_role = max(self.roles) - if 'mention' not in kwargs: + if "mention" not in kwargs: self.mention = f"@{self.name}" @@ -269,10 +269,10 @@ class MockUser(CustomMockMixin, unittest.mock.Mock, ColourMixin, HashableMixin): spec_set = user_instance def __init__(self, **kwargs) -> None: - default_kwargs = {'name': 'user', 'id': next(self.discord_id), 'bot': False} + default_kwargs = {"name": "user", "id": next(self.discord_id), "bot": False} super().__init__(**collections.ChainMap(kwargs, default_kwargs)) - if 'mention' not in kwargs: + if "mention" not in kwargs: self.mention = f"@{self.name}" @@ -331,16 +331,16 @@ class MockBot(CustomMockMixin, unittest.mock.MagicMock): # Create a TextChannel instance to get a realistic MagicMock of `discord.TextChannel` channel_data = { - 'id': 1, - 'type': 'TextChannel', - 'name': 'channel', - 'parent_id': 1234567890, - 'topic': 'topic', - 'position': 1, - 'nsfw': False, - 'last_message_id': 1, - 'bitrate': 1337, - 'user_limit': 25, + "id": 1, + "type": "TextChannel", + "name": "channel", + "parent_id": 1234567890, + "topic": "topic", + "position": 1, + "nsfw": False, + "last_message_id": 1, + "bitrate": 1337, + "user_limit": 25, } state = unittest.mock.MagicMock() guild = unittest.mock.MagicMock() @@ -360,10 +360,10 @@ class MockTextChannel(CustomMockMixin, unittest.mock.Mock, HashableMixin): spec_set = text_channel_instance def __init__(self, **kwargs) -> None: - default_kwargs = {'id': next(self.discord_id), 'name': 'channel', 'guild': MockGuild()} + default_kwargs = {"id": next(self.discord_id), "name": "channel", "guild": MockGuild()} super().__init__(**collections.ChainMap(kwargs, default_kwargs)) - if 'mention' not in kwargs: + if "mention" not in kwargs: self.mention = f"#{self.name}" @@ -377,10 +377,10 @@ class MockVoiceChannel(CustomMockMixin, unittest.mock.Mock, HashableMixin): spec_set = voice_channel_instance def __init__(self, **kwargs) -> None: - default_kwargs = {'id': next(self.discord_id), 'name': 'channel', 'guild': MockGuild()} + default_kwargs = {"id": next(self.discord_id), "name": "channel", "guild": MockGuild()} super().__init__(**collections.ChainMap(kwargs, default_kwargs)) - if 'mention' not in kwargs: + if "mention" not in kwargs: self.mention = f"#{self.name}" @@ -401,16 +401,16 @@ class MockDMChannel(CustomMockMixin, unittest.mock.Mock, HashableMixin): spec_set = dm_channel_instance def __init__(self, **kwargs) -> None: - default_kwargs = {'id': next(self.discord_id), 'recipient': MockUser(), "me": MockUser(), 'guild': None} + default_kwargs = {"id": next(self.discord_id), "recipient": MockUser(), "me": MockUser(), "guild": None} super().__init__(**collections.ChainMap(kwargs, default_kwargs)) # Create CategoryChannel instance to get a realistic MagicMock of `discord.CategoryChannel` category_channel_data = { - 'id': 1, - 'type': discord.ChannelType.category, - 'name': 'category', - 'position': 1, + "id": 1, + "type": discord.ChannelType.category, + "name": "category", + "position": 1, } state = unittest.mock.MagicMock() @@ -422,26 +422,26 @@ category_channel_instance = discord.CategoryChannel( class MockCategoryChannel(CustomMockMixin, unittest.mock.Mock, HashableMixin): def __init__(self, **kwargs) -> None: - default_kwargs = {'id': next(self.discord_id)} + default_kwargs = {"id": next(self.discord_id)} super().__init__(**collections.ChainMap(kwargs, default_kwargs)) # Create a Message instance to get a realistic MagicMock of `discord.Message` message_data = { - 'id': 1, - 'webhook_id': 431341013479718912, - 'attachments': [], - 'embeds': [], - 'application': {"id": 4, "description": "A Python Bot", "name": "Python Discord", "icon": None}, - 'activity': 'mocking', - 'channel': unittest.mock.MagicMock(), - 'edited_timestamp': '2019-10-14T15:33:48+00:00', - 'type': 'message', - 'pinned': False, - 'mention_everyone': False, - 'tts': None, - 'content': 'content', - 'nonce': None, + "id": 1, + "webhook_id": 431341013479718912, + "attachments": [], + "embeds": [], + "application": {"id": 4, "description": "A Python Bot", "name": "Python Discord", "icon": None}, + "activity": "mocking", + "channel": unittest.mock.MagicMock(), + "edited_timestamp": "2019-10-14T15:33:48+00:00", + "type": "message", + "pinned": False, + "mention_everyone": False, + "tts": None, + "content": "content", + "nonce": None, } state = unittest.mock.MagicMock() channel = unittest.mock.MagicMock() @@ -470,13 +470,13 @@ class MockContext(CustomMockMixin, unittest.mock.MagicMock): def __init__(self, **kwargs) -> None: super().__init__(**kwargs) - self.me = kwargs.get('me', MockMember()) - self.bot = kwargs.get('bot', MockBot()) - self.guild = kwargs.get('guild', MockGuild()) - self.author = kwargs.get('author', MockMember()) - self.channel = kwargs.get('channel', MockTextChannel()) - self.message = kwargs.get('message', MockMessage()) - self.invoked_from_error_handler = kwargs.get('invoked_from_error_handler', False) + self.me = kwargs.get("me", MockMember()) + self.bot = kwargs.get("bot", MockBot()) + self.guild = kwargs.get("guild", MockGuild()) + self.author = kwargs.get("author", MockMember()) + self.channel = kwargs.get("channel", MockTextChannel()) + self.message = kwargs.get("message", MockMessage()) + self.invoked_from_error_handler = kwargs.get("invoked_from_error_handler", False) class MockInteraction(CustomMockMixin, unittest.mock.MagicMock): @@ -489,13 +489,13 @@ class MockInteraction(CustomMockMixin, unittest.mock.MagicMock): def __init__(self, **kwargs) -> None: super().__init__(**kwargs) - self.me = kwargs.get('me', MockMember()) - self.client = kwargs.get('client', MockBot()) - self.guild = kwargs.get('guild', MockGuild()) - self.user = kwargs.get('user', MockMember()) - self.channel = kwargs.get('channel', MockTextChannel()) - self.message = kwargs.get('message', MockMessage()) - self.invoked_from_error_handler = kwargs.get('invoked_from_error_handler', False) + self.me = kwargs.get("me", MockMember()) + self.client = kwargs.get("client", MockBot()) + self.guild = kwargs.get("guild", MockGuild()) + self.user = kwargs.get("user", MockMember()) + self.channel = kwargs.get("channel", MockTextChannel()) + self.message = kwargs.get("message", MockMessage()) + self.invoked_from_error_handler = kwargs.get("invoked_from_error_handler", False) attachment_instance = discord.Attachment(data=unittest.mock.MagicMock(id=1), state=unittest.mock.MagicMock()) @@ -543,10 +543,10 @@ class MockMessage(CustomMockMixin, unittest.mock.MagicMock): spec_set = message_instance def __init__(self, **kwargs) -> None: - default_kwargs = {'attachments': []} + default_kwargs = {"attachments": []} super().__init__(**collections.ChainMap(kwargs, default_kwargs)) - self.author = kwargs.get('author', MockMember()) - self.channel = kwargs.get('channel', MockTextChannel()) + self.author = kwargs.get("author", MockMember()) + self.channel = kwargs.get("channel", MockTextChannel()) class MockInteractionMessage(MockMessage): @@ -556,10 +556,9 @@ class MockInteractionMessage(MockMessage): Instances of this class will follow the specifications of `discord.InteractionMessage` instances. For more information, see the `MockGuild` docstring. """ - pass -emoji_data = {'require_colons': True, 'managed': True, 'id': 1, 'name': 'hyperlemon'} +emoji_data = {"require_colons": True, "managed": True, "id": 1, "name": "hyperlemon"} emoji_instance = discord.Emoji(guild=MockGuild(), state=unittest.mock.MagicMock(), data=emoji_data) @@ -574,10 +573,10 @@ class MockEmoji(CustomMockMixin, unittest.mock.MagicMock): def __init__(self, **kwargs) -> None: super().__init__(**kwargs) - self.guild = kwargs.get('guild', MockGuild()) + self.guild = kwargs.get("guild", MockGuild()) -partial_emoji_instance = discord.PartialEmoji(animated=False, name='guido') +partial_emoji_instance = discord.PartialEmoji(animated=False, name="guido") class MockPartialEmoji(CustomMockMixin, unittest.mock.MagicMock): @@ -590,7 +589,7 @@ class MockPartialEmoji(CustomMockMixin, unittest.mock.MagicMock): spec_set = partial_emoji_instance -reaction_instance = discord.Reaction(message=MockMessage(), data={'me': True}, emoji=MockEmoji()) +reaction_instance = discord.Reaction(message=MockMessage(), data={"me": True}, emoji=MockEmoji()) class MockReaction(CustomMockMixin, unittest.mock.MagicMock): @@ -605,8 +604,8 @@ class MockReaction(CustomMockMixin, unittest.mock.MagicMock): def __init__(self, **kwargs) -> None: _users = kwargs.pop("users", []) super().__init__(**kwargs) - self.emoji = kwargs.get('emoji', MockEmoji()) - self.message = kwargs.get('message', MockMessage()) + self.emoji = kwargs.get("emoji", MockEmoji()) + self.message = kwargs.get("message", MockMessage()) user_iterator = unittest.mock.AsyncMock() user_iterator.__aiter__.return_value = _users |