Skip to content

Commit

Permalink
Merge pull request #62 from alex-oleshkevich/improve-coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-oleshkevich authored Apr 27, 2023
2 parents aa8d432 + 9d0c201 commit 11bd0bc
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 7 deletions.
7 changes: 4 additions & 3 deletions starsessions/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ def __getattribute__(self, item: str) -> typing.Any:

self._raise()

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

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

def _raise(self) -> typing.NoReturn:
Expand Down Expand Up @@ -76,7 +76,8 @@ async def send_wrapper(message: Message) -> None:
await send(message)
return

if not handler.is_loaded: # session was not loaded, do nothing
# session was not loaded, do nothing
if not handler.is_loaded: # pragma: nocover
await send(message)
return

Expand Down
3 changes: 2 additions & 1 deletion starsessions/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ def __init__(
self.metadata: typing.Optional[SessionMetadata] = None

async def load(self) -> None:
if self.is_loaded: # don't refresh existing session, it may contain user data
# don't refresh existing session, it may contain user data
if self.is_loaded: # pragma: nocover, IDK how to test it :(
return

self.is_loaded = True
Expand Down
2 changes: 1 addition & 1 deletion starsessions/stores/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def prefix_factory(prefix: str, key: str) -> str:
return prefix + key


if typing.TYPE_CHECKING:
if typing.TYPE_CHECKING: # pragma: nocover
BaseRedis = Redis[bytes]
else:
BaseRedis = Redis
Expand Down
6 changes: 6 additions & 0 deletions tests/backends/test_cookie.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,9 @@ async def test_cookie_remove(cookie_store: SessionStore) -> None:
@pytest.mark.asyncio
async def test_cookie_exists(cookie_store: SessionStore) -> None:
assert await cookie_store.exists("session_id") is False


@pytest.mark.asyncio
async def test_returns_empty_string_for_bad_signature(cookie_store: SessionStore) -> None:
# the session_id value is a signed session cookie value
assert await cookie_store.read("session_id", lifetime=10) == b""
3 changes: 3 additions & 0 deletions tests/backends/test_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ async def test_in_memory_remove(in_memory_store: SessionStore) -> None:
await in_memory_store.remove("session_id")
assert await in_memory_store.exists("session_id") is False

# should not fail on missing key
await in_memory_store.remove("missing")


@pytest.mark.asyncio
async def test_in_memory_exists(in_memory_store: SessionStore) -> None:
Expand Down
21 changes: 21 additions & 0 deletions tests/backends/test_redis.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import os
import pytest
import redis.asyncio
from pytest_asyncio.plugin import SubRequest

from starsessions import ImproperlyConfigured
from starsessions.stores.base import SessionStore
from starsessions.stores.redis import RedisStore

Expand Down Expand Up @@ -46,3 +48,22 @@ async def test_redis_exists(redis_store: SessionStore) -> None:
@pytest.mark.asyncio
async def test_redis_empty_session(redis_store: SessionStore) -> None:
assert await redis_store.read("unknown_session_id", lifetime=60) == b""


@pytest.mark.asyncio
async def test_redis_requires_url_or_connection() -> None:
with pytest.raises(ImproperlyConfigured):
RedisStore()


@pytest.mark.asyncio
async def test_redis_uses_url() -> None:
store = RedisStore(url="redis://")
assert isinstance(store._connection, redis.asyncio.Redis)


@pytest.mark.asyncio
async def test_redis_uses_connection() -> None:
connection = redis.asyncio.Redis.from_url("redis://")
store = RedisStore(connection=connection)
assert store._connection == connection
2 changes: 1 addition & 1 deletion tests/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


def test_requires_loaded_session(store: SessionStore) -> None:
async def app(scope: Scope, receive: Receive, send: Send) -> None:
async def app(scope: Scope, receive: Receive, send: Send) -> None: # pragma: nocover
connection = HTTPConnection(scope, receive)
response = JSONResponse(get_session_metadata(connection))
await response(scope, receive, send)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ async def app(scope: Scope, receive: Receive, send: Send) -> None:
def test_session_timedelta_lifetime(store: SessionStore) -> None:
"""It should accept datetime.timedelta as lifetime value."""

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

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

0 comments on commit 11bd0bc

Please sign in to comment.