Skip to content

Commit 9cc7310

Browse files
improve test coverage
1 parent aa8d432 commit 9cc7310

File tree

7 files changed

+35
-5
lines changed

7 files changed

+35
-5
lines changed

starsessions/middleware.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ def __getattribute__(self, item: str) -> typing.Any:
2020

2121
self._raise()
2222

23-
def __setitem__(self, key: str, value: typing.Any) -> typing.NoReturn:
23+
def __setitem__(self, key: str, value: typing.Any) -> typing.NoReturn: # pragma: nocover
2424
self._raise()
2525

26-
def __getitem__(self, key: str) -> typing.NoReturn:
26+
def __getitem__(self, key: str) -> typing.NoReturn: # pragma: nocover
2727
self._raise()
2828

2929
def _raise(self) -> typing.NoReturn:

starsessions/stores/redis.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def prefix_factory(prefix: str, key: str) -> str:
1111
return prefix + key
1212

1313

14-
if typing.TYPE_CHECKING:
14+
if typing.TYPE_CHECKING: # pragma: nocover
1515
BaseRedis = Redis[bytes]
1616
else:
1717
BaseRedis = Redis

tests/backends/test_cookie.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,9 @@ async def test_cookie_remove(cookie_store: SessionStore) -> None:
2828
@pytest.mark.asyncio
2929
async def test_cookie_exists(cookie_store: SessionStore) -> None:
3030
assert await cookie_store.exists("session_id") is False
31+
32+
33+
@pytest.mark.asyncio
34+
async def test_returns_empty_string_for_bad_signature(cookie_store: SessionStore) -> None:
35+
# the session_id value is a signed session cookie value
36+
assert await cookie_store.read("session_id", lifetime=10) == b""

tests/backends/test_memory.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ async def test_in_memory_remove(in_memory_store: SessionStore) -> None:
2121
await in_memory_store.remove("session_id")
2222
assert await in_memory_store.exists("session_id") is False
2323

24+
# should not fail on missing key
25+
await in_memory_store.remove("missing")
26+
2427

2528
@pytest.mark.asyncio
2629
async def test_in_memory_exists(in_memory_store: SessionStore) -> None:

tests/backends/test_redis.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import os
22
import pytest
3+
import redis.asyncio
34
from pytest_asyncio.plugin import SubRequest
45

6+
from starsessions import ImproperlyConfigured
57
from starsessions.stores.base import SessionStore
68
from starsessions.stores.redis import RedisStore
79

@@ -46,3 +48,22 @@ async def test_redis_exists(redis_store: SessionStore) -> None:
4648
@pytest.mark.asyncio
4749
async def test_redis_empty_session(redis_store: SessionStore) -> None:
4850
assert await redis_store.read("unknown_session_id", lifetime=60) == b""
51+
52+
53+
@pytest.mark.asyncio
54+
async def test_redis_requires_url_or_connection() -> None:
55+
with pytest.raises(ImproperlyConfigured):
56+
RedisStore()
57+
58+
59+
@pytest.mark.asyncio
60+
async def test_redis_uses_url() -> None:
61+
store = RedisStore(url="redis://")
62+
assert isinstance(store._connection, redis.asyncio.Redis)
63+
64+
65+
@pytest.mark.asyncio
66+
async def test_redis_uses_connection() -> None:
67+
connection = redis.asyncio.Redis.from_url("redis://")
68+
store = RedisStore(connection=connection)
69+
assert store._connection == connection

tests/test_metadata.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212

1313
def test_requires_loaded_session(store: SessionStore) -> None:
14-
async def app(scope: Scope, receive: Receive, send: Send) -> None:
14+
async def app(scope: Scope, receive: Receive, send: Send) -> None: # pragma: nocover
1515
connection = HTTPConnection(scope, receive)
1616
response = JSONResponse(get_session_metadata(connection))
1717
await response(scope, receive, send)

tests/test_middleware.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ async def app(scope: Scope, receive: Receive, send: Send) -> None:
240240
def test_session_timedelta_lifetime(store: SessionStore) -> None:
241241
"""It should accept datetime.timedelta as lifetime value."""
242242

243-
async def app(scope: Scope, receive: Receive, send: Send) -> None:
243+
async def app(scope: Scope, receive: Receive, send: Send) -> None: # pragma: nocover
244244
pass
245245

246246
app = SessionMiddleware(app, store=store, lifetime=datetime.timedelta(seconds=60))

0 commit comments

Comments
 (0)