aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Leon Sandøy <[email protected]>2020-05-24 19:09:45 +0200
committerGravatar Leon Sandøy <[email protected]>2020-05-24 19:09:45 +0200
commitad8b1fa455e141074daec5047682e82ed96db1f5 (patch)
treeb00f5eb6096e96267886635077a23ee9e49bcc9e
parentAdd an option to use fakeredis in Bot. (diff)
Improve error and error testing for increment
Changed a RuntimeError to a KeyError (thanks @MarkKoz), and also added some tests to ensure that the right errors are raised whenever this method is used incorrectly.
-rw-r--r--bot/utils/redis_cache.py2
-rw-r--r--tests/bot/utils/test_redis_cache.py8
2 files changed, 9 insertions, 1 deletions
diff --git a/bot/utils/redis_cache.py b/bot/utils/redis_cache.py
index 5fc34d464..b91d663f3 100644
--- a/bot/utils/redis_cache.py
+++ b/bot/utils/redis_cache.py
@@ -341,7 +341,7 @@ class RedisCache:
# Can't increment a non-existing value
if value is None:
log.exception("Attempt to increment/decrement value for non-existent key.")
- raise RuntimeError("The provided key does not exist!")
+ raise KeyError("The provided key does not exist!")
# If it does exist, and it's an int or a float, increment and set it.
if isinstance(value, int) or isinstance(value, float):
diff --git a/tests/bot/utils/test_redis_cache.py b/tests/bot/utils/test_redis_cache.py
index dbbaef018..7405487ed 100644
--- a/tests/bot/utils/test_redis_cache.py
+++ b/tests/bot/utils/test_redis_cache.py
@@ -207,3 +207,11 @@ class RedisCacheTests(unittest.IsolatedAsyncioTestCase):
# This should convert the type into a float.
await self.redis.decrement("entropic", -2.5)
self.assertEqual(await self.redis.get("entropic"), 7.5)
+
+ # Let's test that they raise the right errors
+ with self.assertRaises(KeyError):
+ await self.redis.increment("doesn't_exist!")
+
+ await self.redis.set("stringthing", "stringthing")
+ with self.assertRaises(TypeError):
+ await self.redis.increment("stringthing")