Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion template/pyproject.toml.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ classifiers = [
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
]

[tool.poetry.dependencies]
Expand All @@ -22,7 +23,7 @@ uvicorn = {extras = ["standard"], version = "~0.20.0"}
gunicorn = "~20.1.0"
click = "~8.1.3"
{%- if redis %}
aioredis = "~2.0.1"
redis = "~4.4.0"
{%- endif %}
{%- if aiohttp %}
aiohttp = "~3.8.3"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from unittest import mock

import pytest
from aioredis import Redis
from aioredis.exceptions import RedisError
from redis import asyncio as aioredis
from {{package_name}}.app.utils import RedisClient
from {{package_name}}.config import redis as redis_conf

Expand All @@ -18,14 +17,14 @@ mock.MagicMock.__await__ = lambda x: async_magic().__await__()

def test_open_redis_client():
RedisClient.open_redis_client()
assert isinstance(RedisClient.redis_client, Redis)
assert isinstance(RedisClient.redis_client, aioredis.Redis)
RedisClient.redis_client = None

redis_conf.REDIS_USERNAME = "John"
redis_conf.REDIS_PASSWORD = "Secret"
redis_conf.REDIS_USE_SENTINEL = True
RedisClient.open_redis_client()
assert isinstance(RedisClient.redis_client, Redis)
assert isinstance(RedisClient.redis_client, aioredis.Redis)
RedisClient.redis_client = None


Expand All @@ -39,7 +38,7 @@ async def test_ping():
@pytest.mark.asyncio
async def test_ping_exception():
RedisClient.redis_client = mock.MagicMock()
RedisClient.redis_client.ping.side_effect = RedisError("Mock error")
RedisClient.redis_client.ping.side_effect = aioredis.RedisError("Mock error")
assert await RedisClient.ping() is False


Expand All @@ -53,8 +52,8 @@ async def test_set():
@pytest.mark.asyncio
async def test_set_exception():
RedisClient.redis_client = mock.MagicMock()
RedisClient.redis_client.set.side_effect = RedisError("Mock error")
with pytest.raises(RedisError):
RedisClient.redis_client.set.side_effect = aioredis.RedisError("Mock error")
with pytest.raises(aioredis.RedisError):
await RedisClient.set("key", "value")


Expand All @@ -68,8 +67,8 @@ async def test_rpush():
@pytest.mark.asyncio
async def test_rpush_exception():
RedisClient.redis_client = mock.MagicMock()
RedisClient.redis_client.rpush.side_effect = RedisError("Mock error")
with pytest.raises(RedisError):
RedisClient.redis_client.rpush.side_effect = aioredis.RedisError("Mock error")
with pytest.raises(aioredis.RedisError):
await RedisClient.rpush("key", "value")


Expand All @@ -83,8 +82,8 @@ async def test_exists():
@pytest.mark.asyncio
async def test_exists_exception():
RedisClient.redis_client = mock.MagicMock()
RedisClient.redis_client.exists.side_effect = RedisError("Mock error")
with pytest.raises(RedisError):
RedisClient.redis_client.exists.side_effect = aioredis.RedisError("Mock error")
with pytest.raises(aioredis.RedisError):
await RedisClient.exists("key")


Expand All @@ -98,8 +97,8 @@ async def test_get():
@pytest.mark.asyncio
async def test_get_exception():
RedisClient.redis_client = mock.MagicMock()
RedisClient.redis_client.get.side_effect = RedisError("Mock error")
with pytest.raises(RedisError):
RedisClient.redis_client.get.side_effect = aioredis.RedisError("Mock error")
with pytest.raises(aioredis.RedisError):
await RedisClient.get("key")


Expand All @@ -113,6 +112,6 @@ async def test_lrange():
@pytest.mark.asyncio
async def test_lrange_exception():
RedisClient.redis_client = mock.MagicMock()
RedisClient.redis_client.lrange.side_effect = RedisError("Mock error")
with pytest.raises(RedisError):
RedisClient.redis_client.lrange.side_effect = aioredis.RedisError("Mock error")
with pytest.raises(aioredis.RedisError):
await RedisClient.lrange("key", 1, -1)
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [ '3.8', '3.9', '3.10' ]
python-version: [ '3.8', '3.9', '3.10', '3.11' ]

steps:
- uses: actions/checkout@v3
Expand Down Expand Up @@ -98,7 +98,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: [ '3.8', '3.9', '3.10' ]
python-version: [ '3.8', '3.9', '3.10', '3.11' ]

steps:
- uses: actions/checkout@v3
Expand All @@ -119,7 +119,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: [ '3.8', '3.9', '3.10' ]
python-version: [ '3.8', '3.9', '3.10', '3.11' ]

steps:
- uses: actions/checkout@v3
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
"""Redis client class utility."""
import logging

import aioredis
import aioredis.sentinel
from aioredis.exceptions import RedisError
from redis import asyncio as aioredis
from {{package_name}}.config import redis as redis_conf


Expand Down Expand Up @@ -88,7 +86,7 @@ class RedisClient(object):
cls.log.debug("Execute Redis PING command")
try:
return await redis_client.ping()
except RedisError as ex:
except aioredis.RedisError as ex:
cls.log.exception(
"Redis PING command finished with exception",
exc_info=(type(ex), ex, ex.__traceback__),
Expand Down Expand Up @@ -119,7 +117,7 @@ class RedisClient(object):
cls.log.debug(f"Execute Redis SET command, key: {key}, value: {value}")
try:
await redis_client.set(key, value)
except RedisError as ex:
except aioredis.RedisError as ex:
cls.log.exception(
"Redis SET command finished with exception",
exc_info=(type(ex), ex, ex.__traceback__),
Expand Down Expand Up @@ -153,7 +151,7 @@ class RedisClient(object):
)
try:
await redis_client.rpush(key, value)
except RedisError as ex:
except aioredis.RedisError as ex:
cls.log.exception(
"Redis RPUSH command finished with exception",
exc_info=(type(ex), ex, ex.__traceback__),
Expand Down Expand Up @@ -181,7 +179,7 @@ class RedisClient(object):
cls.log.debug(f"Execute Redis EXISTS command, key: {key}")
try:
return await redis_client.exists(key)
except RedisError as ex:
except aioredis.RedisError as ex:
cls.log.exception(
"Redis EXISTS command finished with exception",
exc_info=(type(ex), ex, ex.__traceback__),
Expand Down Expand Up @@ -211,7 +209,7 @@ class RedisClient(object):
cls.log.debug(f"Execute Redis GET command, key: {key}")
try:
return await redis_client.get(key)
except RedisError as ex:
except aioredis.RedisError as ex:
cls.log.exception(
"Redis GET command finished with exception",
exc_info=(type(ex), ex, ex.__traceback__),
Expand Down Expand Up @@ -248,7 +246,7 @@ class RedisClient(object):
)
try:
return await redis_client.lrange(key, start, end)
except RedisError as ex:
except aioredis.RedisError as ex:
cls.log.exception(
"Redis LRANGE command finished with exception",
exc_info=(type(ex), ex, ex.__traceback__),
Expand Down