Debian Codegraph @

tests/cache/tests_async.py

raw
1 import asyncio
2
3 from django.core.cache import CacheKeyWarning, cache
4 from django.test import SimpleTestCase, override_settings
5
6 from .tests import KEY_ERRORS_WITH_MEMCACHED_MSG
7
8
9 @override_settings(
10 CACHES={
11 "default": {
12 "BACKEND": "django.core.cache.backends.dummy.DummyCache",
13 }
14 }
15 )
16 class AsyncDummyCacheTests(SimpleTestCase):
17 async def test_simple(self):
18 """Dummy cache backend ignores cache set calls."""
19 await cache.aset("key", "value")
20 self.assertIsNone(await cache.aget("key"))
21
22 async def test_aadd(self):
23 """Add doesn't do anything in dummy cache backend."""
24 self.assertIs(await cache.aadd("key", "value"), True)
25 self.assertIs(await cache.aadd("key", "new_value"), True)
26 self.assertIsNone(await cache.aget("key"))
27
28 async def test_non_existent(self):
29 """Nonexistent keys aren't found in the dummy cache backend."""
30 self.assertIsNone(await cache.aget("does_not_exist"))
31 self.assertEqual(await cache.aget("does_not_exist", "default"), "default")
32
33 async def test_aget_many(self):
34 """aget_many() returns nothing for the dummy cache backend."""
35 await cache.aset_many({"a": "a", "b": "b", "c": "c", "d": "d"})
36 self.assertEqual(await cache.aget_many(["a", "c", "d"]), {})
37 self.assertEqual(await cache.aget_many(["a", "b", "e"]), {})
38
39 async def test_aget_many_invalid_key(self):
40 msg = KEY_ERRORS_WITH_MEMCACHED_MSG % ":1:key with spaces"
41 with self.assertWarnsMessage(CacheKeyWarning, msg):
42 await cache.aget_many(["key with spaces"])
43
44 async def test_adelete(self):
45 """
46 Cache deletion is transparently ignored on the dummy cache backend.
47 """
48 await cache.aset_many({"key1": "spam", "key2": "eggs"})
49 self.assertIsNone(await cache.aget("key1"))
50 self.assertIs(await cache.adelete("key1"), False)
51 self.assertIsNone(await cache.aget("key1"))
52 self.assertIsNone(await cache.aget("key2"))
53
54 async def test_ahas_key(self):
55 """ahas_key() doesn't ever return True for the dummy cache backend."""
56 await cache.aset("hello1", "goodbye1")
57 self.assertIs(await cache.ahas_key("hello1"), False)
58 self.assertIs(await cache.ahas_key("goodbye1"), False)
59
60 async def test_aincr(self):
61 """Dummy cache values can't be incremented."""
62 await cache.aset("answer", 42)
63 with self.assertRaises(ValueError):
64 await cache.aincr("answer")
65 with self.assertRaises(ValueError):
66 await cache.aincr("does_not_exist")
67 with self.assertRaises(ValueError):
68 await cache.aincr("does_not_exist", -1)
69
70 async def test_adecr(self):
71 """Dummy cache values can't be decremented."""
72 await cache.aset("answer", 42)
73 with self.assertRaises(ValueError):
74 await cache.adecr("answer")
75 with self.assertRaises(ValueError):
76 await cache.adecr("does_not_exist")
77 with self.assertRaises(ValueError):
78 await cache.adecr("does_not_exist", -1)
79
80 async def test_atouch(self):
81 self.assertIs(await cache.atouch("key"), False)
82
83 async def test_data_types(self):
84 """All data types are ignored equally by the dummy cache."""
85
86 def f():
87 return 42
88
89 class C:
90 def m(n):
91 return 24
92
93 data = {
94 "string": "this is a string",
95 "int": 42,
96 "list": [1, 2, 3, 4],
97 "tuple": (1, 2, 3, 4),
98 "dict": {"A": 1, "B": 2},
99 "function": f,
100 "class": C,
101 }
102 await cache.aset("data", data)
103 self.assertIsNone(await cache.aget("data"))
104
105 async def test_expiration(self):
106 """Expiration has no effect on the dummy cache."""
107 await cache.aset("expire1", "very quickly", 1)
108 await cache.aset("expire2", "very quickly", 1)
109 await cache.aset("expire3", "very quickly", 1)
110
111 await asyncio.sleep(2)
112 self.assertIsNone(await cache.aget("expire1"))
113
114 self.assertIs(await cache.aadd("expire2", "new_value"), True)
115 self.assertIsNone(await cache.aget("expire2"))
116 self.assertIs(await cache.ahas_key("expire3"), False)
117
118 async def test_unicode(self):
119 """Unicode values are ignored by the dummy cache."""
120 tests = {
121 "ascii": "ascii_value",
122 "unicode_ascii": "Iñtërnâtiônàlizætiøn1",
123 "Iñtërnâtiônàlizætiøn": "Iñtërnâtiônàlizætiøn2",
124 "ascii2": {"x": 1},
125 }
126 for key, value in tests.items():
127 with self.subTest(key=key):
128 await cache.aset(key, value)
129 self.assertIsNone(await cache.aget(key))
130
131 async def test_aset_many(self):
132 """aset_many() does nothing for the dummy cache backend."""
133 self.assertEqual(await cache.aset_many({"a": 1, "b": 2}), [])
134 self.assertEqual(
135 await cache.aset_many({"a": 1, "b": 2}, timeout=2, version="1"),
136 [],
137 )
138
139 async def test_aset_many_invalid_key(self):
140 msg = KEY_ERRORS_WITH_MEMCACHED_MSG % ":1:key with spaces"
141 with self.assertWarnsMessage(CacheKeyWarning, msg):
142 await cache.aset_many({"key with spaces": "foo"})
143
144 async def test_adelete_many(self):
145 """adelete_many() does nothing for the dummy cache backend."""
146 await cache.adelete_many(["a", "b"])
147
148 async def test_adelete_many_invalid_key(self):
149 msg = KEY_ERRORS_WITH_MEMCACHED_MSG % ":1:key with spaces"
150 with self.assertWarnsMessage(CacheKeyWarning, msg):
151 await cache.adelete_many({"key with spaces": "foo"})
152
153 async def test_aclear(self):
154 """aclear() does nothing for the dummy cache backend."""
155 await cache.aclear()
156
157 async def test_aclose(self):
158 """aclose() does nothing for the dummy cache backend."""
159 await cache.aclose()
160
161 async def test_aincr_version(self):
162 """Dummy cache versions can't be incremented."""
163 await cache.aset("answer", 42)
164 with self.assertRaises(ValueError):
165 await cache.aincr_version("answer")
166 with self.assertRaises(ValueError):
167 await cache.aincr_version("answer", version=2)
168 with self.assertRaises(ValueError):
169 await cache.aincr_version("does_not_exist")
170
171 async def test_adecr_version(self):
172 """Dummy cache versions can't be decremented."""
173 await cache.aset("answer", 42)
174 with self.assertRaises(ValueError):
175 await cache.adecr_version("answer")
176 with self.assertRaises(ValueError):
177 await cache.adecr_version("answer", version=2)
178 with self.assertRaises(ValueError):
179 await cache.adecr_version("does_not_exist")
180
181 async def test_aget_or_set(self):
182 self.assertEqual(await cache.aget_or_set("key", "default"), "default")
183 self.assertIsNone(await cache.aget_or_set("key", None))
184
185 async def test_aget_or_set_callable(self):
186 def my_callable():
187 return "default"
188
189 self.assertEqual(await cache.aget_or_set("key", my_callable), "default")
190 self.assertEqual(await cache.aget_or_set("key", my_callable()), "default")