diff options
| author | 2020-06-21 15:11:30 +0200 | |
|---|---|---|
| committer | 2020-06-21 15:11:30 +0200 | |
| commit | b7addd0d2431112a7e2b02cf85f68924628c73ed (patch) | |
| tree | 006c0543f73f70f9da736c7d478cac9e2b7c7b6c /tests/helpers.py | |
| parent | Merge branch 'master' into truncate-internal-eval (diff) | |
| parent | Merge pull request #1009 from python-discord/bug/mod/bot-2a/webhook-clyde (diff) | |
Merge branch 'master' into truncate-internal-eval
# Conflicts:
# bot/utils/__init__.py
Diffstat (limited to 'tests/helpers.py')
| -rw-r--r-- | tests/helpers.py | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/tests/helpers.py b/tests/helpers.py index 13283339b..facc4e1af 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 typing import Callable, Iterable, Optional import discord from aiohttp import ClientSession @@ -26,6 +26,24 @@ for logger in logging.Logger.manager.loggerDict.values(): logger.setLevel(logging.CRITICAL) +def autospec(target, *attributes: str, **kwargs) -> Callable: + """Patch multiple `attributes` of a `target` with autospecced mocks and `spec_set` as True.""" + # Caller's kwargs should take priority and overwrite the defaults. + kwargs = {'spec_set': True, 'autospec': True, **kwargs} + + # Import the target if it's a string. + # This is to support both object and string targets like patch.multiple. + if type(target) is str: + target = unittest.mock._importer(target) + + def decorator(func): + for attribute in attributes: + patcher = unittest.mock.patch.object(target, attribute, **kwargs) + func = patcher(func) + return func + return decorator + + class HashableMixin(discord.mixins.EqualityComparable): """ Mixin that provides similar hashing and equality functionality as discord.py's `Hashable` mixin. @@ -208,6 +226,10 @@ class MockRole(CustomMockMixin, unittest.mock.Mock, ColourMixin, HashableMixin): """Simplified position-based comparisons similar to those of `discord.Role`.""" return self.position < other.position + def __ge__(self, other): + """Simplified position-based comparisons similar to those of `discord.Role`.""" + return self.position >= other.position + # Create a Member instance to get a realistic Mock of `discord.Member` member_data = {'user': 'lemon', 'roles': [1]} |