aboutsummaryrefslogtreecommitdiffstats
path: root/tests/helpers.py
diff options
context:
space:
mode:
authorGravatar MarkKoz <[email protected]>2020-05-22 16:23:12 -0700
committerGravatar MarkKoz <[email protected]>2020-05-22 16:23:12 -0700
commit1ad7833d800918efca06e5d6b2fbafdb0d757009 (patch)
treee93bd6d96b076bc38a15ee94656df9770895337a /tests/helpers.py
parentFix unawaited coro warning when instantiating Bot for MockBot's spec (diff)
Properly mock the redis pool in MockBot
Because some of the redis pool/connection methods return futures rather than being coroutines, the redis pool had to be mocked using the CustomMockMixin so it could take advantage of `additional_spec_asyncs` to use AsyncMocks for these future-returning methods.
Diffstat (limited to 'tests/helpers.py')
-rw-r--r--tests/helpers.py16
1 files changed, 16 insertions, 0 deletions
diff --git a/tests/helpers.py b/tests/helpers.py
index 2efeff7db..33d4f787c 100644
--- a/tests/helpers.py
+++ b/tests/helpers.py
@@ -7,6 +7,7 @@ import unittest.mock
from asyncio import AbstractEventLoop
from typing import Iterable, Optional
+import aioredis.abc
import discord
from discord.ext.commands import Context
@@ -265,6 +266,17 @@ class MockAPIClient(CustomMockMixin, unittest.mock.MagicMock):
spec_set = APIClient
+class MockRedisPool(CustomMockMixin, unittest.mock.MagicMock):
+ """
+ A MagicMock subclass to mock an aioredis connection pool.
+
+ Instances of this class will follow the specifications of `aioredis.abc.AbcPool` instances.
+ For more information, see the `MockGuild` docstring.
+ """
+ spec_set = aioredis.abc.AbcPool
+ additional_spec_asyncs = ("execute", "execute_pubsub")
+
+
def _get_mock_loop() -> unittest.mock.Mock:
"""Return a mocked asyncio.AbstractEventLoop."""
loop = unittest.mock.create_autospec(spec=AbstractEventLoop, spec_set=True)
@@ -293,6 +305,10 @@ class MockBot(CustomMockMixin, unittest.mock.MagicMock):
self.loop = _get_mock_loop()
self.api_client = MockAPIClient(loop=self.loop)
+ # fakeredis can't be used cause it'd require awaiting a coroutine to create the pool,
+ # which cannot be done here in __init__.
+ self.redis_session = MockRedisPool()
+
# Create a TextChannel instance to get a realistic MagicMock of `discord.TextChannel`
channel_data = {