3
3
4
4
import pytest
5
5
6
- from cashews .backends .interface import NOT_EXIST , UNLIMITED , Backend
6
+ from cashews import Cache
7
+ from cashews .backends .interface import NOT_EXIST , UNLIMITED
7
8
from cashews .backends .memory import Memory
8
9
9
10
pytestmark = pytest .mark .asyncio
10
11
VALUE = Decimal ("100.2" )
11
12
12
13
13
- async def test_set_get (cache ):
14
+ async def test_set_get (cache : Cache ):
14
15
await cache .set ("key" , VALUE )
15
16
assert await cache .get ("key" ) == VALUE
16
17
17
18
18
- async def test_set_get_bytes (cache ):
19
+ async def test_set_get_bytes (cache : Cache ):
19
20
await cache .set ("key" , b"10" )
20
21
assert await cache .get ("key" ) == b"10"
21
22
22
23
23
- async def test_incr_get (cache ):
24
+ async def test_incr_get (cache : Cache ):
24
25
await cache .incr ("key" )
25
26
assert await cache .get ("key" ) == 1
26
27
27
28
28
- async def test_incr_value_get (cache ):
29
+ async def test_incr_value_get (cache : Cache ):
29
30
await cache .incr ("key" )
30
31
await cache .incr ("key" , 2 )
31
32
assert await cache .get ("key" ) == 3
32
33
33
34
34
- async def test_incr_expire (cache ):
35
+ async def test_incr_expire (cache : Cache ):
35
36
await cache .incr ("key" , expire = 10 )
36
37
assert await cache .get_expire ("key" ) == 10
37
38
38
39
39
- async def test_set_get_many (cache ):
40
+ async def test_set_get_many (cache : Cache ):
40
41
await cache .set ("key" , VALUE )
41
42
assert await cache .get_many ("key" , "no_exists" ) == (VALUE , None )
42
43
43
44
44
- async def test_diff_types_get_many (cache ):
45
+ async def test_diff_types_get_many (cache : Cache ):
45
46
await cache .incr ("key" )
46
47
await cache .incr_bits ("key2" , 0 )
47
48
assert await cache .get_many ("key" , "key2" ) == (1 , None )
48
49
49
50
50
- async def test_set_exist (cache ):
51
+ async def test_set_exist (cache : Cache ):
51
52
assert await cache .set ("key" , "value" )
52
53
assert await cache .set ("key" , VALUE , exist = True )
53
54
assert await cache .get ("key" ) == VALUE
@@ -62,49 +63,49 @@ async def test_set_exist(cache):
62
63
assert await cache .get ("key2" ) == "value"
63
64
64
65
65
- async def test_set_many (cache ):
66
+ async def test_set_many (cache : Cache ):
66
67
await cache .set_many ({"key1" : VALUE , "key2" : "value2" }, expire = 1 )
67
68
assert await cache .get ("key1" , VALUE )
68
69
assert await cache .get ("key2" , "value2" )
69
70
70
71
71
- async def test_get_no_value (cache ):
72
+ async def test_get_no_value (cache : Cache ):
72
73
assert await cache .get ("key2" ) is None
73
74
assert await cache .get ("key2" , default = VALUE ) is VALUE
74
75
75
76
76
- async def test_incr (cache ):
77
+ async def test_incr (cache : Cache ):
77
78
assert await cache .incr ("incr" ) == 1
78
79
assert await cache .incr ("incr" ) == 2
79
80
assert await cache .incr ("incr" , 5 ) == 7
80
81
assert await cache .get ("incr" ) == 7
81
82
82
83
83
- async def test_incr_set (cache ):
84
+ async def test_incr_set (cache : Cache ):
84
85
await cache .set ("incr" , "test" )
85
86
with pytest .raises (Exception ):
86
87
assert await cache .incr ("incr" ) == 1
87
88
assert await cache .get ("incr" ) == "test"
88
89
89
90
90
- async def test_ping (cache ):
91
+ async def test_ping (cache : Cache ):
91
92
assert await cache .ping () == b"PONG"
92
93
93
94
94
- async def test_exists (cache ):
95
+ async def test_exists (cache : Cache ):
95
96
assert not await cache .exists ("not" )
96
97
await cache .set ("yes" , VALUE )
97
98
assert await cache .exists ("yes" )
98
99
99
100
100
- async def test_expire (cache ):
101
+ async def test_expire (cache : Cache ):
101
102
await cache .set ("key" , VALUE , expire = 0.01 )
102
103
assert await cache .get ("key" ) == VALUE
103
104
await asyncio .sleep (0.01 )
104
105
assert await cache .get ("key" ) is None
105
106
106
107
107
- async def test_get_set_expire (cache ):
108
+ async def test_get_set_expire (cache : Cache ):
108
109
assert await cache .get_expire ("key" ) == NOT_EXIST
109
110
await cache .set ("key" , VALUE )
110
111
assert await cache .get ("key" ) == VALUE
@@ -113,7 +114,7 @@ async def test_get_set_expire(cache):
113
114
assert await cache .get_expire ("key" ) == 1
114
115
115
116
116
- async def test_delete_many (cache : Backend ):
117
+ async def test_delete_many (cache : Cache ):
117
118
await cache .set ("key1" , VALUE )
118
119
await cache .set ("key2" , VALUE )
119
120
await cache .set ("key3" , VALUE )
@@ -125,7 +126,7 @@ async def test_delete_many(cache: Backend):
125
126
assert await cache .get ("key3" ) == VALUE
126
127
127
128
128
- async def test_delete_match (cache : Backend ):
129
+ async def test_delete_match (cache : Cache ):
129
130
await cache .set ("pref:test:test" , VALUE )
130
131
await cache .set ("pref:value:test" , b"value2" )
131
132
await cache .set ("pref:-:test" , b"-" )
@@ -145,7 +146,7 @@ async def test_delete_match(cache: Backend):
145
146
assert await cache .get ("pref:test:tests" ) is not None
146
147
147
148
148
- async def test_scan (cache : Backend ):
149
+ async def test_scan (cache : Cache ):
149
150
await cache .set ("pref:test:test" , VALUE )
150
151
await cache .set ("pref:value:test" , b"value2" )
151
152
await cache .set ("pref:-:test" , b"-" )
@@ -160,7 +161,7 @@ async def test_scan(cache: Backend):
160
161
assert set (keys ) == {"pref:test:test" , "pref:value:test" , "pref:-:test" , "pref:*:test" }
161
162
162
163
163
- async def test_get_match (cache : Backend ):
164
+ async def test_get_match (cache : Cache ):
164
165
await cache .set ("pref:test:test" , VALUE )
165
166
await cache .set ("pref:value:test" , b"value2" )
166
167
await cache .set ("pref:-:test" , b"-" )
@@ -182,39 +183,39 @@ async def test_get_match(cache: Backend):
182
183
assert len (match ) == 0
183
184
184
185
185
- async def test_set_lock_unlock (cache : Backend ):
186
+ async def test_set_lock_unlock (cache : Cache ):
186
187
await cache .set_lock ("lock" , "lock" , 10 )
187
188
assert await cache .is_locked ("lock" )
188
189
await cache .unlock ("lock" , "lock" )
189
190
assert not await cache .is_locked ("lock" )
190
191
191
192
192
- async def test_lock (cache : Backend ):
193
+ async def test_lock (cache : Cache ):
193
194
async with cache .lock ("lock" , 10 ):
194
195
assert await cache .is_locked ("lock" )
195
196
196
197
assert not await cache .is_locked ("lock" )
197
198
198
199
199
- async def test_diff_types_get_match (cache ):
200
+ async def test_diff_types_get_match (cache : Cache ):
200
201
await cache .incr ("key" )
201
202
await cache .incr_bits ("key2" , 0 )
202
203
match = [(key , value ) async for key , value in cache .get_match ("*" )]
203
204
assert len (match ) == 1 , match
204
205
assert dict (match ) == {"key" : 1 }
205
206
206
207
207
- async def test_get_size (cache : Backend ):
208
+ async def test_get_size (cache : Cache ):
208
209
await cache .set ("test" , b"1" )
209
210
assert isinstance (await cache .get_size ("test" ), int )
210
211
211
212
212
- async def test_get_keys_count (cache : Backend ):
213
+ async def test_get_keys_count (cache : Cache ):
213
214
await cache .set ("test" , b"1" )
214
215
assert await cache .get_keys_count () == 1
215
216
216
217
217
- async def test_get_bits (cache : Backend ):
218
+ async def test_get_bits (cache : Cache ):
218
219
assert await cache .get_bits ("test" , 0 , 2 , 10 , 50000 , size = 1 ) == (0 , 0 , 0 , 0 )
219
220
assert await cache .get_bits ("test" , 0 , 1 , 3 , size = 15 ) == (
220
221
0 ,
@@ -223,17 +224,17 @@ async def test_get_bits(cache: Backend):
223
224
)
224
225
225
226
226
- async def test_incr_bits (cache : Backend ):
227
+ async def test_incr_bits (cache : Cache ):
227
228
await cache .incr_bits ("test" , 0 , 1 , 4 )
228
229
assert await cache .get_bits ("test" , 0 , 1 , 2 , 3 , 4 ) == (1 , 1 , 0 , 0 , 1 )
229
230
230
231
231
- async def test_bits_size (cache : Backend ):
232
+ async def test_bits_size (cache : Cache ):
232
233
await cache .incr_bits ("test" , 0 , 1 , 4 , size = 5 , by = 3 )
233
234
assert await cache .get_bits ("test" , 0 , 1 , 2 , 3 , 4 , size = 5 ) == (3 , 3 , 0 , 0 , 3 )
234
235
235
236
236
- async def test_slice_incr (cache : Backend ):
237
+ async def test_slice_incr (cache : Cache ):
237
238
assert await cache .slice_incr ("test" , 0 , 5 , maxvalue = 10 ) == 1
238
239
assert await cache .slice_incr ("test" , 1 , 6 , maxvalue = 10 ) == 2
239
240
assert await cache .slice_incr ("test" , 2 , 7 , maxvalue = 10 ) == 3
0 commit comments