aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorGravatar Leon Sandøy <[email protected]>2020-05-23 02:06:27 +0200
committerGravatar Leon Sandøy <[email protected]>2020-05-23 02:06:27 +0200
commiteb63fb02a49bf1979afd04a1350304edf00d3a56 (patch)
tree76684be2201e33d86c03f5392e7110fb65f3b800 /tests
parentUse autospecced mocks in MockBot for the stats and aiohttp session (diff)
Finish .set and .get, and add tests.
The .set and .get will accept ints, floats, and strings. These will be converted into "typestrings", which is basically just a simple format that's been invented for this object. For example, an int looks like `b"i|2423"`. Note how it is still stored as a bytestring (like everything in Redis), but because of this prefix we are able to coerce it into the type we want on the way out of the db.
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)