aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/bot/utils/test_redis_cache.py36
-rw-r--r--tests/helpers.py2
2 files changed, 26 insertions, 12 deletions
diff --git a/tests/bot/utils/test_redis_cache.py b/tests/bot/utils/test_redis_cache.py
index 991225481..ad38bfde0 100644
--- a/tests/bot/utils/test_redis_cache.py
+++ b/tests/bot/utils/test_redis_cache.py
@@ -7,27 +7,41 @@ from tests import helpers
class RedisCacheTests(unittest.IsolatedAsyncioTestCase):
- """Tests the RedisDict class from utils.redis_dict.py."""
+ """Tests the RedisCache class from utils.redis_dict.py."""
redis = RedisCache()
- async def asyncSetUp(self): # noqa: N802 - this special method can't be all lowercase
+ async def asyncSetUp(self): # noqa: N802
"""Sets up the objects that only have to be initialized once."""
self.bot = helpers.MockBot()
self.bot.redis_session = await fakeredis.aioredis.create_redis_pool()
- def test_class_attribute_namespace(self):
+ async def test_class_attribute_namespace(self):
"""Test that RedisDict creates a namespace automatically for class attributes."""
self.assertEqual(self.redis._namespace, "RedisCacheTests.redis")
- # Test that errors are raised when this isn't true.
- # def test_set_get_item(self):
- # """Test that users can set and get items from the RedisDict."""
- # self.redis['favorite_fruit'] = 'melon'
- # self.redis['favorite_number'] = 86
- # self.assertEqual(self.redis['favorite_fruit'], 'melon')
- # self.assertEqual(self.redis['favorite_number'], 86)
- #
+ # Test that errors are raised when not assigned as a class attribute
+ bad_cache = RedisCache()
+
+ with self.assertRaises(RuntimeError):
+ await bad_cache.set("test", "me_up_deadman")
+
+ async def test_set_get_item(self):
+ """Test that users can set and get items from the RedisDict."""
+ test_cases = (
+ ('favorite_fruit', 'melon'),
+ ('favorite_number', 86),
+ ('favorite_fraction', 86.54)
+ )
+
+ # Test that we can get and set different types.
+ for test in test_cases:
+ await self.redis.set(*test)
+ self.assertEqual(await self.redis.get(test[0]), test[1])
+
+ # Test that .get allows a default value
+ self.assertEqual(await self.redis.get('favorite_nothing', "bearclaw"), "bearclaw")
+
# def test_set_item_types(self):
# """Test that setitem rejects keys and values that are not strings, ints or floats."""
# fruits = ["lemon", "melon", "apple"]
diff --git a/tests/helpers.py b/tests/helpers.py
index d226be3f0..2b176db79 100644
--- a/tests/helpers.py
+++ b/tests/helpers.py
@@ -299,7 +299,7 @@ class MockBot(CustomMockMixin, unittest.mock.MagicMock):
For more information, see the `MockGuild` docstring.
"""
spec_set = Bot(command_prefix=unittest.mock.MagicMock(), loop=_get_mock_loop())
- additional_spec_asyncs = ("wait_for",)
+ additional_spec_asyncs = ("wait_for", "_redis_ready")
def __init__(self, **kwargs) -> None:
super().__init__(**kwargs)