FastAPI: add redis integration

This includes the addition of the python-fakeredis package,
used for stubbing python-redis when a user does not have a
configured cache.

Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
Kevin Morris 2021-06-28 08:49:02 -07:00
parent 96d1af9363
commit 91e769f603
No known key found for this signature in database
GPG key ID: F7E46DED420788F3
7 changed files with 123 additions and 3 deletions

View file

@ -9,6 +9,24 @@ from fastapi import HTTPException
import aurweb.asgi
import aurweb.config
import aurweb.redis
@pytest.mark.asyncio
async def test_asgi_startup_session_secret_exception(monkeypatch):
""" Test that we get an IOError on app_startup when we cannot
connect to options.redis_address. """
redis_addr = aurweb.config.get("options", "redis_address")
def mock_get(section: str, key: str):
if section == "fastapi" and key == "session_secret":
return None
return redis_addr
with mock.patch("aurweb.config.get", side_effect=mock_get):
with pytest.raises(Exception):
await aurweb.asgi.app_startup()
@pytest.mark.asyncio

40
test/test_redis.py Normal file
View file

@ -0,0 +1,40 @@
from unittest import mock
import pytest
import aurweb.config
from aurweb.redis import redis_connection
@pytest.fixture
def rediss():
""" Create a RedisStub. """
def mock_get(section, key):
return "none"
with mock.patch("aurweb.config.get", side_effect=mock_get):
aurweb.config.rehash()
redis = redis_connection()
aurweb.config.rehash()
yield redis
def test_redis_stub(rediss):
# We don't yet have a test key set.
assert rediss.get("test") is None
# Set the test key to abc.
rediss.set("test", "abc")
assert rediss.get("test").decode() == "abc"
# Test expire.
rediss.expire("test", 0)
assert rediss.get("test") is None
# Now, set the test key again and use delete() on it.
rediss.set("test", "abc")
assert rediss.get("test").decode() == "abc"
rediss.delete("test")
assert rediss.get("test") is None