2020-10-16 14:16:55 +00:00
|
|
|
import asyncio
|
|
|
|
|
|
|
|
from django.core.cache import CacheKeyWarning, cache
|
|
|
|
from django.test import SimpleTestCase, override_settings
|
|
|
|
|
|
|
|
from .tests import KEY_ERRORS_WITH_MEMCACHED_MSG
|
|
|
|
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
@override_settings(
|
|
|
|
CACHES={
|
|
|
|
"default": {
|
|
|
|
"BACKEND": "django.core.cache.backends.dummy.DummyCache",
|
|
|
|
}
|
2020-10-16 14:16:55 +00:00
|
|
|
}
|
2022-02-03 19:24:19 +00:00
|
|
|
)
|
2020-10-16 14:16:55 +00:00
|
|
|
class AsyncDummyCacheTests(SimpleTestCase):
|
|
|
|
async def test_simple(self):
|
|
|
|
"""Dummy cache backend ignores cache set calls."""
|
2022-02-03 19:24:19 +00:00
|
|
|
await cache.aset("key", "value")
|
|
|
|
self.assertIsNone(await cache.aget("key"))
|
2020-10-16 14:16:55 +00:00
|
|
|
|
|
|
|
async def test_aadd(self):
|
|
|
|
"""Add doesn't do anything in dummy cache backend."""
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertIs(await cache.aadd("key", "value"), True)
|
|
|
|
self.assertIs(await cache.aadd("key", "new_value"), True)
|
|
|
|
self.assertIsNone(await cache.aget("key"))
|
2020-10-16 14:16:55 +00:00
|
|
|
|
|
|
|
async def test_non_existent(self):
|
|
|
|
"""Nonexistent keys aren't found in the dummy cache backend."""
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertIsNone(await cache.aget("does_not_exist"))
|
|
|
|
self.assertEqual(await cache.aget("does_not_exist", "default"), "default")
|
2020-10-16 14:16:55 +00:00
|
|
|
|
|
|
|
async def test_aget_many(self):
|
|
|
|
"""aget_many() returns nothing for the dummy cache backend."""
|
2022-02-03 19:24:19 +00:00
|
|
|
await cache.aset_many({"a": "a", "b": "b", "c": "c", "d": "d"})
|
|
|
|
self.assertEqual(await cache.aget_many(["a", "c", "d"]), {})
|
|
|
|
self.assertEqual(await cache.aget_many(["a", "b", "e"]), {})
|
2020-10-16 14:16:55 +00:00
|
|
|
|
|
|
|
async def test_aget_many_invalid_key(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
msg = KEY_ERRORS_WITH_MEMCACHED_MSG % ":1:key with spaces"
|
2020-10-16 14:16:55 +00:00
|
|
|
with self.assertWarnsMessage(CacheKeyWarning, msg):
|
2022-02-03 19:24:19 +00:00
|
|
|
await cache.aget_many(["key with spaces"])
|
2020-10-16 14:16:55 +00:00
|
|
|
|
|
|
|
async def test_adelete(self):
|
|
|
|
"""
|
|
|
|
Cache deletion is transparently ignored on the dummy cache backend.
|
|
|
|
"""
|
2022-02-03 19:24:19 +00:00
|
|
|
await cache.aset_many({"key1": "spam", "key2": "eggs"})
|
|
|
|
self.assertIsNone(await cache.aget("key1"))
|
|
|
|
self.assertIs(await cache.adelete("key1"), False)
|
|
|
|
self.assertIsNone(await cache.aget("key1"))
|
|
|
|
self.assertIsNone(await cache.aget("key2"))
|
2020-10-16 14:16:55 +00:00
|
|
|
|
|
|
|
async def test_ahas_key(self):
|
|
|
|
"""ahas_key() doesn't ever return True for the dummy cache backend."""
|
2022-02-03 19:24:19 +00:00
|
|
|
await cache.aset("hello1", "goodbye1")
|
|
|
|
self.assertIs(await cache.ahas_key("hello1"), False)
|
|
|
|
self.assertIs(await cache.ahas_key("goodbye1"), False)
|
2020-10-16 14:16:55 +00:00
|
|
|
|
|
|
|
async def test_aincr(self):
|
|
|
|
"""Dummy cache values can't be incremented."""
|
2022-02-03 19:24:19 +00:00
|
|
|
await cache.aset("answer", 42)
|
2020-10-16 14:16:55 +00:00
|
|
|
with self.assertRaises(ValueError):
|
2022-02-03 19:24:19 +00:00
|
|
|
await cache.aincr("answer")
|
2020-10-16 14:16:55 +00:00
|
|
|
with self.assertRaises(ValueError):
|
2022-02-03 19:24:19 +00:00
|
|
|
await cache.aincr("does_not_exist")
|
2020-10-16 14:16:55 +00:00
|
|
|
with self.assertRaises(ValueError):
|
2022-02-03 19:24:19 +00:00
|
|
|
await cache.aincr("does_not_exist", -1)
|
2020-10-16 14:16:55 +00:00
|
|
|
|
|
|
|
async def test_adecr(self):
|
|
|
|
"""Dummy cache values can't be decremented."""
|
2022-02-03 19:24:19 +00:00
|
|
|
await cache.aset("answer", 42)
|
2020-10-16 14:16:55 +00:00
|
|
|
with self.assertRaises(ValueError):
|
2022-02-03 19:24:19 +00:00
|
|
|
await cache.adecr("answer")
|
2020-10-16 14:16:55 +00:00
|
|
|
with self.assertRaises(ValueError):
|
2022-02-03 19:24:19 +00:00
|
|
|
await cache.adecr("does_not_exist")
|
2020-10-16 14:16:55 +00:00
|
|
|
with self.assertRaises(ValueError):
|
2022-02-03 19:24:19 +00:00
|
|
|
await cache.adecr("does_not_exist", -1)
|
2020-10-16 14:16:55 +00:00
|
|
|
|
|
|
|
async def test_atouch(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertIs(await cache.atouch("key"), False)
|
2020-10-16 14:16:55 +00:00
|
|
|
|
|
|
|
async def test_data_types(self):
|
|
|
|
"""All data types are ignored equally by the dummy cache."""
|
2022-02-03 19:24:19 +00:00
|
|
|
|
2020-10-16 14:16:55 +00:00
|
|
|
def f():
|
|
|
|
return 42
|
|
|
|
|
|
|
|
class C:
|
|
|
|
def m(n):
|
|
|
|
return 24
|
|
|
|
|
|
|
|
data = {
|
2022-02-03 19:24:19 +00:00
|
|
|
"string": "this is a string",
|
|
|
|
"int": 42,
|
|
|
|
"list": [1, 2, 3, 4],
|
|
|
|
"tuple": (1, 2, 3, 4),
|
|
|
|
"dict": {"A": 1, "B": 2},
|
|
|
|
"function": f,
|
|
|
|
"class": C,
|
2020-10-16 14:16:55 +00:00
|
|
|
}
|
2022-02-03 19:24:19 +00:00
|
|
|
await cache.aset("data", data)
|
|
|
|
self.assertIsNone(await cache.aget("data"))
|
2020-10-16 14:16:55 +00:00
|
|
|
|
|
|
|
async def test_expiration(self):
|
|
|
|
"""Expiration has no effect on the dummy cache."""
|
2022-02-03 19:24:19 +00:00
|
|
|
await cache.aset("expire1", "very quickly", 1)
|
|
|
|
await cache.aset("expire2", "very quickly", 1)
|
|
|
|
await cache.aset("expire3", "very quickly", 1)
|
2020-10-16 14:16:55 +00:00
|
|
|
|
|
|
|
await asyncio.sleep(2)
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertIsNone(await cache.aget("expire1"))
|
2020-10-16 14:16:55 +00:00
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertIs(await cache.aadd("expire2", "new_value"), True)
|
|
|
|
self.assertIsNone(await cache.aget("expire2"))
|
|
|
|
self.assertIs(await cache.ahas_key("expire3"), False)
|
2020-10-16 14:16:55 +00:00
|
|
|
|
|
|
|
async def test_unicode(self):
|
|
|
|
"""Unicode values are ignored by the dummy cache."""
|
|
|
|
tests = {
|
2022-02-03 19:24:19 +00:00
|
|
|
"ascii": "ascii_value",
|
|
|
|
"unicode_ascii": "Iñtërnâtiônàlizætiøn1",
|
|
|
|
"Iñtërnâtiônàlizætiøn": "Iñtërnâtiônàlizætiøn2",
|
|
|
|
"ascii2": {"x": 1},
|
2020-10-16 14:16:55 +00:00
|
|
|
}
|
|
|
|
for key, value in tests.items():
|
|
|
|
with self.subTest(key=key):
|
|
|
|
await cache.aset(key, value)
|
|
|
|
self.assertIsNone(await cache.aget(key))
|
|
|
|
|
|
|
|
async def test_aset_many(self):
|
|
|
|
"""aset_many() does nothing for the dummy cache backend."""
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(await cache.aset_many({"a": 1, "b": 2}), [])
|
2020-10-16 14:16:55 +00:00
|
|
|
self.assertEqual(
|
2022-02-03 19:24:19 +00:00
|
|
|
await cache.aset_many({"a": 1, "b": 2}, timeout=2, version="1"),
|
2020-10-16 14:16:55 +00:00
|
|
|
[],
|
|
|
|
)
|
|
|
|
|
|
|
|
async def test_aset_many_invalid_key(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
msg = KEY_ERRORS_WITH_MEMCACHED_MSG % ":1:key with spaces"
|
2020-10-16 14:16:55 +00:00
|
|
|
with self.assertWarnsMessage(CacheKeyWarning, msg):
|
2022-02-03 19:24:19 +00:00
|
|
|
await cache.aset_many({"key with spaces": "foo"})
|
2020-10-16 14:16:55 +00:00
|
|
|
|
|
|
|
async def test_adelete_many(self):
|
|
|
|
"""adelete_many() does nothing for the dummy cache backend."""
|
2022-02-03 19:24:19 +00:00
|
|
|
await cache.adelete_many(["a", "b"])
|
2020-10-16 14:16:55 +00:00
|
|
|
|
|
|
|
async def test_adelete_many_invalid_key(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
msg = KEY_ERRORS_WITH_MEMCACHED_MSG % ":1:key with spaces"
|
2020-10-16 14:16:55 +00:00
|
|
|
with self.assertWarnsMessage(CacheKeyWarning, msg):
|
2022-02-03 19:24:19 +00:00
|
|
|
await cache.adelete_many({"key with spaces": "foo"})
|
2020-10-16 14:16:55 +00:00
|
|
|
|
|
|
|
async def test_aclear(self):
|
|
|
|
"""aclear() does nothing for the dummy cache backend."""
|
|
|
|
await cache.aclear()
|
|
|
|
|
|
|
|
async def test_aclose(self):
|
|
|
|
"""aclose() does nothing for the dummy cache backend."""
|
|
|
|
await cache.aclose()
|
|
|
|
|
|
|
|
async def test_aincr_version(self):
|
|
|
|
"""Dummy cache versions can't be incremented."""
|
2022-02-03 19:24:19 +00:00
|
|
|
await cache.aset("answer", 42)
|
2020-10-16 14:16:55 +00:00
|
|
|
with self.assertRaises(ValueError):
|
2022-02-03 19:24:19 +00:00
|
|
|
await cache.aincr_version("answer")
|
2020-10-16 14:16:55 +00:00
|
|
|
with self.assertRaises(ValueError):
|
2022-02-03 19:24:19 +00:00
|
|
|
await cache.aincr_version("answer", version=2)
|
2020-10-16 14:16:55 +00:00
|
|
|
with self.assertRaises(ValueError):
|
2022-02-03 19:24:19 +00:00
|
|
|
await cache.aincr_version("does_not_exist")
|
2020-10-16 14:16:55 +00:00
|
|
|
|
|
|
|
async def test_adecr_version(self):
|
|
|
|
"""Dummy cache versions can't be decremented."""
|
2022-02-03 19:24:19 +00:00
|
|
|
await cache.aset("answer", 42)
|
2020-10-16 14:16:55 +00:00
|
|
|
with self.assertRaises(ValueError):
|
2022-02-03 19:24:19 +00:00
|
|
|
await cache.adecr_version("answer")
|
2020-10-16 14:16:55 +00:00
|
|
|
with self.assertRaises(ValueError):
|
2022-02-03 19:24:19 +00:00
|
|
|
await cache.adecr_version("answer", version=2)
|
2020-10-16 14:16:55 +00:00
|
|
|
with self.assertRaises(ValueError):
|
2022-02-03 19:24:19 +00:00
|
|
|
await cache.adecr_version("does_not_exist")
|
2020-10-16 14:16:55 +00:00
|
|
|
|
|
|
|
async def test_aget_or_set(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(await cache.aget_or_set("key", "default"), "default")
|
|
|
|
self.assertIsNone(await cache.aget_or_set("key", None))
|
2020-10-16 14:16:55 +00:00
|
|
|
|
|
|
|
async def test_aget_or_set_callable(self):
|
|
|
|
def my_callable():
|
2022-02-03 19:24:19 +00:00
|
|
|
return "default"
|
2020-10-16 14:16:55 +00:00
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(await cache.aget_or_set("key", my_callable), "default")
|
|
|
|
self.assertEqual(await cache.aget_or_set("key", my_callable()), "default")
|