aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorGravatar Leon Sandøy <[email protected]>2020-05-24 13:04:41 +0200
committerGravatar Leon Sandøy <[email protected]>2020-05-24 13:04:41 +0200
commit01bedcadf762262eef0a2b406faf66cdc16a5c85 (patch)
treeb03fa0a94c620b33c2e350699936d68b79bdc765 /tests
parentMake .items return ItemsView instead of AsyncIter (diff)
Add .increment and .decrement methods.
Sometimes, we just want to store a counter in the cache. In this case, it is convenient to have a single method that will allow us to increment or decrement this counter. These methods allow you to decrement or increment floats and integers by an specified amount. By default, it'll increment or decrement by 1. Since this involves several API requests, we create an asyncio.Lock so that we don't end up with race conditions.
Diffstat (limited to 'tests')
-rw-r--r--tests/bot/utils/test_redis_cache.py34
1 files changed, 34 insertions, 0 deletions
diff --git a/tests/bot/utils/test_redis_cache.py b/tests/bot/utils/test_redis_cache.py
index 6e12002ed..dbbaef018 100644
--- a/tests/bot/utils/test_redis_cache.py
+++ b/tests/bot/utils/test_redis_cache.py
@@ -173,3 +173,37 @@ class RedisCacheTests(unittest.IsolatedAsyncioTestCase):
with self.assertRaises(TypeError):
self.redis._to_typestring(["internet"])
self.redis._from_typestring("o|firedog")
+
+ async def test_increment_decrement(self):
+ """Test .increment and .decrement methods."""
+ await self.redis.set("entropic", 5)
+ await self.redis.set("disentropic", 12.5)
+
+ # Test default increment
+ await self.redis.increment("entropic")
+ self.assertEqual(await self.redis.get("entropic"), 6)
+
+ # Test default decrement
+ await self.redis.decrement("entropic")
+ self.assertEqual(await self.redis.get("entropic"), 5)
+
+ # Test float increment with float
+ await self.redis.increment("disentropic", 2.0)
+ self.assertEqual(await self.redis.get("disentropic"), 14.5)
+
+ # Test float increment with int
+ await self.redis.increment("disentropic", 2)
+ self.assertEqual(await self.redis.get("disentropic"), 16.5)
+
+ # Test negative increments, because why not.
+ await self.redis.increment("entropic", -5)
+ self.assertEqual(await self.redis.get("entropic"), 0)
+
+ # Negative decrements? Sure.
+ await self.redis.decrement("entropic", -5)
+ self.assertEqual(await self.redis.get("entropic"), 5)
+
+ # What about if we use a negative float to decrement an int?
+ # This should convert the type into a float.
+ await self.redis.decrement("entropic", -2.5)
+ self.assertEqual(await self.redis.get("entropic"), 7.5)