2
2
import os
3
3
from pathlib import Path
4
4
5
- import anysqlite
6
5
import pytest
7
6
from httpcore import Request , Response
8
7
9
8
from hishel import AsyncFileStorage , AsyncInMemoryStorage , AsyncRedisStorage , AsyncSQLiteStorage
10
9
from hishel ._serializers import Metadata
11
10
from hishel ._utils import asleep , generate_key
12
11
12
+ try :
13
+ import anysqlite
14
+ except ImportError :
15
+ anysqlite = None
16
+
17
+ try :
18
+ import redis
19
+ except ImportError :
20
+ redis = None
21
+
13
22
dummy_metadata = Metadata (cache_key = "test" , number_of_uses = 0 , created_at = datetime .datetime .now (datetime .timezone .utc ))
14
23
15
24
@@ -48,6 +57,8 @@ async def test_filestorage(use_temp_dir):
48
57
49
58
@pytest .mark .parametrize ("anyio_backend" , ["asyncio" ])
50
59
async def test_redisstorage (anyio_backend ):
60
+ if redis is None :
61
+ pytest .skip ("redis is not installed" )
51
62
if await is_redis_down (): # pragma: no cover
52
63
pytest .fail ("Redis server was not found" )
53
64
storage = AsyncRedisStorage ()
@@ -73,6 +84,8 @@ async def test_redisstorage(anyio_backend):
73
84
74
85
@pytest .mark .anyio
75
86
async def test_sqlitestorage ():
87
+ if anysqlite is None :
88
+ pytest .skip ("anysqlite not installed" )
76
89
storage = AsyncSQLiteStorage (connection = await anysqlite .connect (":memory:" ))
77
90
78
91
request = Request (b"GET" , "https://example.com" )
@@ -196,6 +209,8 @@ async def test_filestorage_ttl_after_hits(use_temp_dir, anyio_backend):
196
209
197
210
@pytest .mark .parametrize ("anyio_backend" , ["asyncio" ])
198
211
async def test_redisstorage_expired (anyio_backend ):
212
+ if redis is None :
213
+ pytest .skip ("redis is not installed" )
199
214
if await is_redis_down (): # pragma: no cover
200
215
pytest .fail ("Redis server was not found" )
201
216
storage = AsyncRedisStorage (ttl = 0.1 )
@@ -219,6 +234,10 @@ async def test_redisstorage_expired(anyio_backend):
219
234
220
235
@pytest .mark .parametrize ("anyio_backend" , ["asyncio" ])
221
236
async def test_redis_ttl_after_hits (use_temp_dir , anyio_backend ):
237
+ if redis is None :
238
+ pytest .skip ("redis not installed" )
239
+ if await is_redis_down (): # pragma: no cover
240
+ pytest .fail ("Redis server was not found" )
222
241
storage = AsyncRedisStorage (ttl = 0.2 )
223
242
224
243
request = Request (b"GET" , "https://example.com" )
@@ -249,6 +268,8 @@ async def test_redis_ttl_after_hits(use_temp_dir, anyio_backend):
249
268
250
269
@pytest .mark .parametrize ("anyio_backend" , ["asyncio" ])
251
270
async def test_sqlite_expired (anyio_backend ):
271
+ if anysqlite is None :
272
+ pytest .skip ("anysqlite not installed" )
252
273
storage = AsyncSQLiteStorage (ttl = 0.1 , connection = await anysqlite .connect (":memory:" ))
253
274
first_request = Request (b"GET" , "https://example.com" )
254
275
second_request = Request (b"GET" , "https://anotherexample.com" )
@@ -271,6 +292,8 @@ async def test_sqlite_expired(anyio_backend):
271
292
@pytest .mark .xfail
272
293
@pytest .mark .parametrize ("anyio_backend" , ["asyncio" ])
273
294
async def test_sqlite_ttl_after_hits (use_temp_dir , anyio_backend ):
295
+ if anysqlite is None :
296
+ pytest .skip ("anysqlite not installed" )
274
297
storage = AsyncSQLiteStorage (ttl = 0.2 )
275
298
276
299
request = Request (b"GET" , "https://example.com" )
@@ -390,6 +413,8 @@ async def test_filestorage_remove(use_temp_dir):
390
413
391
414
@pytest .mark .parametrize ("anyio_backend" , ["asyncio" ])
392
415
async def test_redisstorage_remove (anyio_backend ):
416
+ if redis is None :
417
+ pytest .skip ("redis not installed" )
393
418
if await is_redis_down (): # pragma: no cover
394
419
pytest .fail ("Redis server was not found" )
395
420
@@ -408,6 +433,8 @@ async def test_redisstorage_remove(anyio_backend):
408
433
409
434
@pytest .mark .anyio
410
435
async def test_sqlitestorage_remove ():
436
+ if anysqlite is None :
437
+ pytest .skip ("anysqlite not installed" )
411
438
storage = AsyncSQLiteStorage (connection = await anysqlite .connect (":memory:" ))
412
439
request = Request (b"GET" , "https://example.com" )
413
440
0 commit comments