aboutsummaryrefslogtreecommitdiffstats
path: root/tests
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
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')
-rw-r--r--tests/bot/utils/test_redis_cache.py12
-rw-r--r--tests/helpers.py16
2 files changed, 19 insertions, 9 deletions
diff --git a/tests/bot/utils/test_redis_cache.py b/tests/bot/utils/test_redis_cache.py
index f6344803f..991225481 100644
--- a/tests/bot/utils/test_redis_cache.py
+++ b/tests/bot/utils/test_redis_cache.py
@@ -1,11 +1,9 @@
-import asyncio
import unittest
-from unittest.mock import MagicMock
import fakeredis.aioredis
-from bot.bot import Bot
from bot.utils import RedisCache
+from tests import helpers
class RedisCacheTests(unittest.IsolatedAsyncioTestCase):
@@ -15,12 +13,8 @@ class RedisCacheTests(unittest.IsolatedAsyncioTestCase):
async def asyncSetUp(self): # noqa: N802 - this special method can't be all lowercase
"""Sets up the objects that only have to be initialized once."""
- self.bot = MagicMock(
- spec=Bot,
- redis_session=await fakeredis.aioredis.create_redis_pool(),
- _redis_ready=asyncio.Event(),
- )
- self.bot._redis_ready.set()
+ self.bot = helpers.MockBot()
+ self.bot.redis_session = await fakeredis.aioredis.create_redis_pool()
def test_class_attribute_namespace(self):
"""Test that RedisDict creates a namespace automatically for class attributes."""
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 = {