Skip to content

Commit bb1a4a1

Browse files
committed
skip anysqlite, yaml and redis tests if optional deps are missing
1 parent f9b4cee commit bb1a4a1

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

tests/_async/test_storages.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,23 @@
22
import os
33
from pathlib import Path
44

5-
import anysqlite
65
import pytest
76
from httpcore import Request, Response
87

98
from hishel import AsyncFileStorage, AsyncInMemoryStorage, AsyncRedisStorage, AsyncSQLiteStorage
109
from hishel._serializers import Metadata
1110
from hishel._utils import asleep, generate_key
1211

12+
try:
13+
import anysqlite
14+
except ImportError:
15+
anysqlite = None
16+
17+
try:
18+
import redis
19+
except ImportError:
20+
redis = None
21+
1322
dummy_metadata = Metadata(cache_key="test", number_of_uses=0, created_at=datetime.datetime.now(datetime.timezone.utc))
1423

1524

@@ -48,6 +57,8 @@ async def test_filestorage(use_temp_dir):
4857

4958
@pytest.mark.parametrize("anyio_backend", ["asyncio"])
5059
async def test_redisstorage(anyio_backend):
60+
if redis is None:
61+
pytest.skip("redis is not installed")
5162
if await is_redis_down(): # pragma: no cover
5263
pytest.fail("Redis server was not found")
5364
storage = AsyncRedisStorage()
@@ -73,6 +84,8 @@ async def test_redisstorage(anyio_backend):
7384

7485
@pytest.mark.anyio
7586
async def test_sqlitestorage():
87+
if anysqlite is None:
88+
pytest.skip("anysqlite not installed")
7689
storage = AsyncSQLiteStorage(connection=await anysqlite.connect(":memory:"))
7790

7891
request = Request(b"GET", "https://example.com")
@@ -196,6 +209,8 @@ async def test_filestorage_ttl_after_hits(use_temp_dir, anyio_backend):
196209

197210
@pytest.mark.parametrize("anyio_backend", ["asyncio"])
198211
async def test_redisstorage_expired(anyio_backend):
212+
if redis is None:
213+
pytest.skip("redis is not installed")
199214
if await is_redis_down(): # pragma: no cover
200215
pytest.fail("Redis server was not found")
201216
storage = AsyncRedisStorage(ttl=0.1)
@@ -219,6 +234,10 @@ async def test_redisstorage_expired(anyio_backend):
219234

220235
@pytest.mark.parametrize("anyio_backend", ["asyncio"])
221236
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")
222241
storage = AsyncRedisStorage(ttl=0.2)
223242

224243
request = Request(b"GET", "https://example.com")
@@ -249,6 +268,8 @@ async def test_redis_ttl_after_hits(use_temp_dir, anyio_backend):
249268

250269
@pytest.mark.parametrize("anyio_backend", ["asyncio"])
251270
async def test_sqlite_expired(anyio_backend):
271+
if anysqlite is None:
272+
pytest.skip("anysqlite not installed")
252273
storage = AsyncSQLiteStorage(ttl=0.1, connection=await anysqlite.connect(":memory:"))
253274
first_request = Request(b"GET", "https://example.com")
254275
second_request = Request(b"GET", "https://anotherexample.com")
@@ -271,6 +292,8 @@ async def test_sqlite_expired(anyio_backend):
271292
@pytest.mark.xfail
272293
@pytest.mark.parametrize("anyio_backend", ["asyncio"])
273294
async def test_sqlite_ttl_after_hits(use_temp_dir, anyio_backend):
295+
if anysqlite is None:
296+
pytest.skip("anysqlite not installed")
274297
storage = AsyncSQLiteStorage(ttl=0.2)
275298

276299
request = Request(b"GET", "https://example.com")
@@ -390,6 +413,8 @@ async def test_filestorage_remove(use_temp_dir):
390413

391414
@pytest.mark.parametrize("anyio_backend", ["asyncio"])
392415
async def test_redisstorage_remove(anyio_backend):
416+
if redis is None:
417+
pytest.skip("redis not installed")
393418
if await is_redis_down(): # pragma: no cover
394419
pytest.fail("Redis server was not found")
395420

@@ -408,6 +433,8 @@ async def test_redisstorage_remove(anyio_backend):
408433

409434
@pytest.mark.anyio
410435
async def test_sqlitestorage_remove():
436+
if anysqlite is None:
437+
pytest.skip("anysqlite not installed")
411438
storage = AsyncSQLiteStorage(connection=await anysqlite.connect(":memory:"))
412439
request = Request(b"GET", "https://example.com")
413440

tests/_sync/test_storages.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
from hishel._serializers import Metadata
1111
from hishel._utils import sleep, generate_key
1212

13+
try:
14+
import redis
15+
except ImportError:
16+
redis = None
17+
1318
dummy_metadata = Metadata(cache_key="test", number_of_uses=0, created_at=datetime.datetime.now(datetime.timezone.utc))
1419

1520

@@ -48,6 +53,8 @@ def test_filestorage(use_temp_dir):
4853

4954

5055
def test_redisstorage(anyio_backend):
56+
if redis is None:
57+
pytest.skip("redis not installed")
5158
if is_redis_down(): # pragma: no cover
5259
pytest.fail("Redis server was not found")
5360
storage = RedisStorage()
@@ -196,6 +203,8 @@ def test_filestorage_ttl_after_hits(use_temp_dir, anyio_backend):
196203

197204

198205
def test_redisstorage_expired(anyio_backend):
206+
if redis is None:
207+
pytest.skip("redis not installed")
199208
if is_redis_down(): # pragma: no cover
200209
pytest.fail("Redis server was not found")
201210
storage = RedisStorage(ttl=0.1)
@@ -219,6 +228,10 @@ def test_redisstorage_expired(anyio_backend):
219228

220229

221230
def test_redis_ttl_after_hits(use_temp_dir, anyio_backend):
231+
if redis is None:
232+
pytest.skip("redis not installed")
233+
if is_redis_down(): # pragma: no cover
234+
pytest.fail("Redis server was not found")
222235
storage = RedisStorage(ttl=0.2)
223236

224237
request = Request(b"GET", "https://example.com")
@@ -390,6 +403,8 @@ def test_filestorage_remove(use_temp_dir):
390403

391404

392405
def test_redisstorage_remove(anyio_backend):
406+
if redis is None:
407+
pytest.skip("redis not installed")
393408
if is_redis_down(): # pragma: no cover
394409
pytest.fail("Redis server was not found")
395410

tests/test_serializers.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import datetime
22

3+
import pytest
34
from httpcore import Request, Response
45

56
from hishel._serializers import (
@@ -10,6 +11,11 @@
1011
)
1112
from hishel._utils import normalized_url
1213

14+
try:
15+
import yaml
16+
except ImportError:
17+
yaml = None
18+
1319

1420
def test_pickle_serializer_dumps_and_loads():
1521
request = Request(
@@ -192,6 +198,8 @@ def test_dict_serializer_loads():
192198

193199

194200
def test_yaml_serializer_dumps():
201+
if yaml is None:
202+
pytest.skip("yaml not installed")
195203
request = Request(
196204
method="GET",
197205
url="https://example.com",
@@ -248,6 +256,8 @@ def test_yaml_serializer_dumps():
248256

249257

250258
def test_yaml_serializer_loads():
259+
if yaml is None:
260+
pytest.skip("yaml not installed")
251261
raw_response = "\n".join(
252262
[
253263
"response:",

0 commit comments

Comments
 (0)