Skip to content

Commit 760ccab

Browse files
citramonStanislav Bushuev
andauthored
Test keys refactoring (#572)
Co-authored-by: Stanislav Bushuev <[email protected]>
1 parent 02782c2 commit 760ccab

File tree

13 files changed

+457
-462
lines changed

13 files changed

+457
-462
lines changed

tests/acceptance/conftest.py

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
11
import pytest
2+
from tests.utils import Keys
23

34
from aiocache import Cache, caches
45

56

6-
def pytest_configure():
7-
"""
8-
Before pytest_namespace was being used to set the keys for
9-
testing but the feature was removed
10-
https://docs.pytest.org/en/latest/deprecations.html#pytest-namespace
11-
"""
12-
pytest.KEY = "key"
13-
pytest.KEY_1 = "random"
14-
15-
167
@pytest.fixture(autouse=True)
178
def reset_caches():
189
caches._caches = {}
@@ -31,9 +22,8 @@ def redis_cache(event_loop):
3122
cache = Cache(Cache.REDIS, namespace="test")
3223
yield cache
3324

34-
event_loop.run_until_complete(cache.delete(pytest.KEY))
35-
event_loop.run_until_complete(cache.delete(pytest.KEY_1))
36-
event_loop.run_until_complete(cache.delete(pytest.KEY + "-lock"))
25+
for key in Keys:
26+
event_loop.run_until_complete(cache.delete(key))
3727
event_loop.run_until_complete(cache.close())
3828

3929

@@ -42,9 +32,8 @@ def memory_cache(event_loop):
4232
cache = Cache(namespace="test")
4333
yield cache
4434

45-
event_loop.run_until_complete(cache.delete(pytest.KEY))
46-
event_loop.run_until_complete(cache.delete(pytest.KEY_1))
47-
event_loop.run_until_complete(cache.delete(pytest.KEY + "-lock"))
35+
for key in Keys:
36+
event_loop.run_until_complete(cache.delete(key))
4837
event_loop.run_until_complete(cache.close())
4938

5039

@@ -53,9 +42,8 @@ def memcached_cache(event_loop):
5342
cache = Cache(Cache.MEMCACHED, namespace="test")
5443
yield cache
5544

56-
event_loop.run_until_complete(cache.delete(pytest.KEY))
57-
event_loop.run_until_complete(cache.delete(pytest.KEY_1))
58-
event_loop.run_until_complete(cache.delete(pytest.KEY + "-lock"))
45+
for key in Keys:
46+
event_loop.run_until_complete(cache.delete(key))
5947
event_loop.run_until_complete(cache.close())
6048

6149

tests/acceptance/test_base.py

Lines changed: 68 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import asyncio
22

33
import pytest
4+
from tests.utils import Keys
45

56
from aiocache.backends.memcached import MemcachedCache
67
from aiocache.backends.memory import SimpleMemoryCache
@@ -21,142 +22,142 @@ async def test_setup(self, cache):
2122

2223
@pytest.mark.asyncio
2324
async def test_get_missing(self, cache):
24-
assert await cache.get(pytest.KEY) is None
25-
assert await cache.get(pytest.KEY, default=1) == 1
25+
assert await cache.get(Keys.KEY) is None
26+
assert await cache.get(Keys.KEY, default=1) == 1
2627

2728
@pytest.mark.asyncio
2829
async def test_get_existing(self, cache):
29-
await cache.set(pytest.KEY, "value")
30-
assert await cache.get(pytest.KEY) == "value"
30+
await cache.set(Keys.KEY, "value")
31+
assert await cache.get(Keys.KEY) == "value"
3132

3233
@pytest.mark.asyncio
3334
async def test_multi_get(self, cache):
34-
await cache.set(pytest.KEY, "value")
35-
assert await cache.multi_get([pytest.KEY, pytest.KEY_1]) == ["value", None]
35+
await cache.set(Keys.KEY, "value")
36+
assert await cache.multi_get([Keys.KEY, Keys.KEY_1]) == ["value", None]
3637

3738
@pytest.mark.asyncio
3839
async def test_delete_missing(self, cache):
39-
assert await cache.delete(pytest.KEY) == 0
40+
assert await cache.delete(Keys.KEY) == 0
4041

4142
@pytest.mark.asyncio
4243
async def test_delete_existing(self, cache):
43-
await cache.set(pytest.KEY, "value")
44-
assert await cache.delete(pytest.KEY) == 1
44+
await cache.set(Keys.KEY, "value")
45+
assert await cache.delete(Keys.KEY) == 1
4546

46-
assert await cache.get(pytest.KEY) is None
47+
assert await cache.get(Keys.KEY) is None
4748

4849
@pytest.mark.asyncio
4950
async def test_set(self, cache):
50-
assert await cache.set(pytest.KEY, "value") is True
51+
assert await cache.set(Keys.KEY, "value") is True
5152

5253
@pytest.mark.asyncio
5354
async def test_set_cancel_previous_ttl_handle(self, cache):
54-
await cache.set(pytest.KEY, "value", ttl=2)
55+
await cache.set(Keys.KEY, "value", ttl=2)
5556

5657
await asyncio.sleep(1)
57-
assert await cache.get(pytest.KEY) == "value"
58-
await cache.set(pytest.KEY, "new_value", ttl=2)
58+
assert await cache.get(Keys.KEY) == "value"
59+
await cache.set(Keys.KEY, "new_value", ttl=2)
5960

6061
await asyncio.sleep(1)
61-
assert await cache.get(pytest.KEY) == "new_value"
62+
assert await cache.get(Keys.KEY) == "new_value"
6263

6364
@pytest.mark.asyncio
6465
async def test_multi_set(self, cache):
65-
pairs = [(pytest.KEY, "value"), [pytest.KEY_1, "random_value"]]
66+
pairs = [(Keys.KEY, "value"), [Keys.KEY_1, "random_value"]]
6667
assert await cache.multi_set(pairs) is True
67-
assert await cache.multi_get([pytest.KEY, pytest.KEY_1]) == ["value", "random_value"]
68+
assert await cache.multi_get([Keys.KEY, Keys.KEY_1]) == ["value", "random_value"]
6869

6970
@pytest.mark.asyncio
7071
async def test_multi_set_with_ttl(self, cache):
71-
pairs = [(pytest.KEY, "value"), [pytest.KEY_1, "random_value"]]
72+
pairs = [(Keys.KEY, "value"), [Keys.KEY_1, "random_value"]]
7273
assert await cache.multi_set(pairs, ttl=1) is True
7374
await asyncio.sleep(1.1)
7475

75-
assert await cache.multi_get([pytest.KEY, pytest.KEY_1]) == [None, None]
76+
assert await cache.multi_get([Keys.KEY, Keys.KEY_1]) == [None, None]
7677

7778
@pytest.mark.asyncio
7879
async def test_set_with_ttl(self, cache):
79-
await cache.set(pytest.KEY, "value", ttl=1)
80+
await cache.set(Keys.KEY, "value", ttl=1)
8081
await asyncio.sleep(1.1)
8182

82-
assert await cache.get(pytest.KEY) is None
83+
assert await cache.get(Keys.KEY) is None
8384

8485
@pytest.mark.asyncio
8586
async def test_add_missing(self, cache):
86-
assert await cache.add(pytest.KEY, "value", ttl=1) is True
87+
assert await cache.add(Keys.KEY, "value", ttl=1) is True
8788

8889
@pytest.mark.asyncio
8990
async def test_add_existing(self, cache):
90-
assert await cache.set(pytest.KEY, "value") is True
91+
assert await cache.set(Keys.KEY, "value") is True
9192
with pytest.raises(ValueError):
92-
await cache.add(pytest.KEY, "value")
93+
await cache.add(Keys.KEY, "value")
9394

9495
@pytest.mark.asyncio
9596
async def test_exists_missing(self, cache):
96-
assert await cache.exists(pytest.KEY) is False
97+
assert await cache.exists(Keys.KEY) is False
9798

9899
@pytest.mark.asyncio
99100
async def test_exists_existing(self, cache):
100-
await cache.set(pytest.KEY, "value")
101-
assert await cache.exists(pytest.KEY) is True
101+
await cache.set(Keys.KEY, "value")
102+
assert await cache.exists(Keys.KEY) is True
102103

103104
@pytest.mark.asyncio
104105
async def test_increment_missing(self, cache):
105-
assert await cache.increment(pytest.KEY, delta=2) == 2
106-
assert await cache.increment(pytest.KEY_1, delta=-2) == -2
106+
assert await cache.increment(Keys.KEY, delta=2) == 2
107+
assert await cache.increment(Keys.KEY_1, delta=-2) == -2
107108

108109
@pytest.mark.asyncio
109110
async def test_increment_existing(self, cache):
110-
await cache.set(pytest.KEY, 2)
111-
assert await cache.increment(pytest.KEY, delta=2) == 4
112-
assert await cache.increment(pytest.KEY, delta=1) == 5
113-
assert await cache.increment(pytest.KEY, delta=-3) == 2
111+
await cache.set(Keys.KEY, 2)
112+
assert await cache.increment(Keys.KEY, delta=2) == 4
113+
assert await cache.increment(Keys.KEY, delta=1) == 5
114+
assert await cache.increment(Keys.KEY, delta=-3) == 2
114115

115116
@pytest.mark.asyncio
116117
async def test_increment_typeerror(self, cache):
117-
await cache.set(pytest.KEY, "value")
118+
await cache.set(Keys.KEY, "value")
118119
with pytest.raises(TypeError):
119-
assert await cache.increment(pytest.KEY)
120+
assert await cache.increment(Keys.KEY)
120121

121122
@pytest.mark.asyncio
122123
async def test_expire_existing(self, cache):
123-
await cache.set(pytest.KEY, "value")
124-
assert await cache.expire(pytest.KEY, 1) is True
124+
await cache.set(Keys.KEY, "value")
125+
assert await cache.expire(Keys.KEY, 1) is True
125126
await asyncio.sleep(1.1)
126-
assert await cache.exists(pytest.KEY) is False
127+
assert await cache.exists(Keys.KEY) is False
127128

128129
@pytest.mark.asyncio
129130
async def test_expire_with_0(self, cache):
130-
await cache.set(pytest.KEY, "value", 1)
131-
assert await cache.expire(pytest.KEY, 0) is True
131+
await cache.set(Keys.KEY, "value", 1)
132+
assert await cache.expire(Keys.KEY, 0) is True
132133
await asyncio.sleep(1.1)
133-
assert await cache.exists(pytest.KEY) is True
134+
assert await cache.exists(Keys.KEY) is True
134135

135136
@pytest.mark.asyncio
136137
async def test_expire_missing(self, cache):
137-
assert await cache.expire(pytest.KEY, 1) is False
138+
assert await cache.expire(Keys.KEY, 1) is False
138139

139140
@pytest.mark.asyncio
140141
async def test_clear(self, cache):
141-
await cache.set(pytest.KEY, "value")
142+
await cache.set(Keys.KEY, "value")
142143
await cache.clear()
143144

144-
assert await cache.exists(pytest.KEY) is False
145+
assert await cache.exists(Keys.KEY) is False
145146

146147
@pytest.mark.asyncio
147148
async def test_close_pool_only_clears_resources(self, cache):
148-
await cache.set(pytest.KEY, "value")
149+
await cache.set(Keys.KEY, "value")
149150
await cache.close()
150-
assert await cache.set(pytest.KEY, "value") is True
151-
assert await cache.get(pytest.KEY) == "value"
151+
assert await cache.set(Keys.KEY, "value") is True
152+
assert await cache.get(Keys.KEY) == "value"
152153

153154
@pytest.mark.asyncio
154155
async def test_single_connection(self, cache):
155156
pytest.skip("aioredis code is broken")
156157
async with cache.get_connection() as conn:
157158
assert isinstance(conn, _Conn)
158-
assert await conn.set(pytest.KEY, "value") is True
159-
assert await conn.get(pytest.KEY) == "value"
159+
assert await conn.set(Keys.KEY, "value") is True
160+
assert await conn.get(Keys.KEY) == "value"
160161

161162

162163
class TestMemoryCache:
@@ -167,18 +168,18 @@ async def test_accept_explicit_args(self):
167168

168169
@pytest.mark.asyncio
169170
async def test_set_float_ttl(self, memory_cache):
170-
await memory_cache.set(pytest.KEY, "value", ttl=0.1)
171+
await memory_cache.set(Keys.KEY, "value", ttl=0.1)
171172
await asyncio.sleep(0.15)
172173

173-
assert await memory_cache.get(pytest.KEY) is None
174+
assert await memory_cache.get(Keys.KEY) is None
174175

175176
@pytest.mark.asyncio
176177
async def test_multi_set_float_ttl(self, memory_cache):
177-
pairs = [(pytest.KEY, "value"), [pytest.KEY_1, "random_value"]]
178+
pairs = [(Keys.KEY, "value"), [Keys.KEY_1, "random_value"]]
178179
assert await memory_cache.multi_set(pairs, ttl=0.1) is True
179180
await asyncio.sleep(0.15)
180181

181-
assert await memory_cache.multi_get([pytest.KEY, pytest.KEY_1]) == [None, None]
182+
assert await memory_cache.multi_get([Keys.KEY, Keys.KEY_1]) == [None, None]
182183

183184
@pytest.mark.asyncio
184185
async def test_raw(self, memory_cache):
@@ -188,10 +189,10 @@ async def test_raw(self, memory_cache):
188189

189190
@pytest.mark.asyncio
190191
async def test_clear_with_namespace_memory(self, memory_cache):
191-
await memory_cache.set(pytest.KEY, "value", namespace="test")
192+
await memory_cache.set(Keys.KEY, "value", namespace="test")
192193
await memory_cache.clear(namespace="test")
193194

194-
assert await memory_cache.exists(pytest.KEY, namespace="test") is False
195+
assert await memory_cache.exists(Keys.KEY, namespace="test") is False
195196

196197

197198
class TestMemcachedCache:
@@ -209,13 +210,13 @@ async def test_set_too_long_key(self, memcached_cache):
209210
@pytest.mark.asyncio
210211
async def test_set_float_ttl_fails(self, memcached_cache):
211212
with pytest.raises(TypeError) as exc_info:
212-
await memcached_cache.set(pytest.KEY, "value", ttl=0.1)
213+
await memcached_cache.set(Keys.KEY, "value", ttl=0.1)
213214
assert str(exc_info.value) == "aiomcache error: exptime not int: 0.1"
214215

215216
@pytest.mark.asyncio
216217
async def test_multi_set_float_ttl(self, memcached_cache):
217218
with pytest.raises(TypeError) as exc_info:
218-
pairs = [(pytest.KEY, "value"), [pytest.KEY_1, "random_value"]]
219+
pairs = [(Keys.KEY, "value"), [Keys.KEY_1, "random_value"]]
219220
assert await memcached_cache.multi_set(pairs, ttl=0.1) is True
220221
assert str(exc_info.value) == "aiomcache error: exptime not int: 0.1"
221222

@@ -228,16 +229,16 @@ async def test_raw(self, memcached_cache):
228229

229230
@pytest.mark.asyncio
230231
async def test_clear_with_namespace_memcached(self, memcached_cache):
231-
await memcached_cache.set(pytest.KEY, "value", namespace="test")
232+
await memcached_cache.set(Keys.KEY, "value", namespace="test")
232233

233234
with pytest.raises(ValueError):
234235
await memcached_cache.clear(namespace="test")
235236

236-
assert await memcached_cache.exists(pytest.KEY, namespace="test") is True
237+
assert await memcached_cache.exists(Keys.KEY, namespace="test") is True
237238

238239
@pytest.mark.asyncio
239240
async def test_close(self, memcached_cache):
240-
await memcached_cache.set(pytest.KEY, "value")
241+
await memcached_cache.set(Keys.KEY, "value")
241242
await memcached_cache._close()
242243
assert memcached_cache.client._pool._pool.qsize() == 0
243244

@@ -250,18 +251,18 @@ async def test_accept_explicit_args(self):
250251

251252
@pytest.mark.asyncio
252253
async def test_float_ttl(self, redis_cache):
253-
await redis_cache.set(pytest.KEY, "value", ttl=0.1)
254+
await redis_cache.set(Keys.KEY, "value", ttl=0.1)
254255
await asyncio.sleep(0.15)
255256

256-
assert await redis_cache.get(pytest.KEY) is None
257+
assert await redis_cache.get(Keys.KEY) is None
257258

258259
@pytest.mark.asyncio
259260
async def test_multi_set_float_ttl(self, redis_cache):
260-
pairs = [(pytest.KEY, "value"), [pytest.KEY_1, "random_value"]]
261+
pairs = [(Keys.KEY, "value"), [Keys.KEY_1, "random_value"]]
261262
assert await redis_cache.multi_set(pairs, ttl=0.1) is True
262263
await asyncio.sleep(0.15)
263264

264-
assert await redis_cache.multi_get([pytest.KEY, pytest.KEY_1]) == [None, None]
265+
assert await redis_cache.multi_get([Keys.KEY, Keys.KEY_1]) == [None, None]
265266

266267
@pytest.mark.asyncio
267268
async def test_raw(self, redis_cache):
@@ -271,13 +272,13 @@ async def test_raw(self, redis_cache):
271272

272273
@pytest.mark.asyncio
273274
async def test_clear_with_namespace_redis(self, redis_cache):
274-
await redis_cache.set(pytest.KEY, "value", namespace="test")
275+
await redis_cache.set(Keys.KEY, "value", namespace="test")
275276
await redis_cache.clear(namespace="test")
276277

277-
assert await redis_cache.exists(pytest.KEY, namespace="test") is False
278+
assert await redis_cache.exists(Keys.KEY, namespace="test") is False
278279

279280
@pytest.mark.asyncio
280281
async def test_close(self, redis_cache):
281-
await redis_cache.set(pytest.KEY, "value")
282+
await redis_cache.set(Keys.KEY, "value")
282283
await redis_cache._close()
283284
assert redis_cache._pool.size == 0

0 commit comments

Comments
 (0)