Skip to content

Commit 02435a6

Browse files
committed
Fixed redis.
1 parent 1c33e72 commit 02435a6

3 files changed

Lines changed: 11 additions & 19 deletions

File tree

fastapi_template/template/{{cookiecutter.project_name}}/pyproject.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,6 @@ dev = [
161161
"pytest-cov >=7.0.0,<8",
162162
"anyio >=4.11.0,<5",
163163
"pytest-env >=1.2.0,<2",
164-
{%- if cookiecutter.enable_redis == "True" %}
165-
"fakeredis >=2.32.1,<3",
166-
{%- endif %}
167164
{%- if cookiecutter.orm == "tortoise" %}
168165
"asynctest >=0.13.0,<1",
169166
"nest-asyncio >=1.6.0,<2",

fastapi_template/template/{{cookiecutter.project_name}}/tests/conftest.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
from httpx import AsyncClient, ASGITransport
99

1010
{%- if cookiecutter.enable_redis == "True" %}
11-
from fakeredis import FakeServer
12-
from fakeredis.aioredis import FakeConnection
1311
from redis.asyncio import ConnectionPool
1412
from {{cookiecutter.project_name}}.services.redis.dependency import get_redis_pool
1513

@@ -479,15 +477,13 @@ async def test_nats() -> AsyncGenerator[Nats, None]:
479477

480478
{% if cookiecutter.enable_redis == "True" -%}
481479
@pytest.fixture
482-
async def fake_redis_pool() -> AsyncGenerator[ConnectionPool, None]:
480+
async def test_redis_pool() -> AsyncGenerator[ConnectionPool, None]:
483481
"""
484482
Get instance of a fake redis.
485483
486-
:yield: FakeRedis instance.
484+
:yield: ConnectionPool instance.
487485
"""
488-
server = FakeServer()
489-
server.connected = True
490-
pool = ConnectionPool(connection_class=FakeConnection, server=server)
486+
pool = ConnectionPool.from_url(str(settings.redis_url))
491487

492488
yield pool
493489

@@ -503,7 +499,7 @@ def fastapi_app(
503499
dbpool: AsyncConnectionPool[Any],
504500
{%- endif %}
505501
{% if cookiecutter.enable_redis == "True" -%}
506-
fake_redis_pool: ConnectionPool,
502+
test_redis_pool: ConnectionPool,
507503
{%- endif %}
508504
{%- if cookiecutter.enable_rmq == 'True' %}
509505
test_rmq_pool: Pool[Channel],
@@ -527,7 +523,7 @@ def fastapi_app(
527523
application.dependency_overrides[get_db_pool] = lambda: dbpool
528524
{%- endif %}
529525
{%- if cookiecutter.enable_redis == "True" %}
530-
application.dependency_overrides[get_redis_pool] = lambda: fake_redis_pool
526+
application.dependency_overrides[get_redis_pool] = lambda: test_redis_pool
531527
{%- endif %}
532528
{%- if cookiecutter.enable_rmq == 'True' %}
533529
application.dependency_overrides[get_rmq_channel_pool] = lambda: test_rmq_pool

fastapi_template/template/{{cookiecutter.project_name}}/tests/test_redis.py

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

3-
import fakeredis
43
from fastapi import FastAPI
54
from httpx import AsyncClient
65
from redis.asyncio import ConnectionPool, Redis
@@ -9,14 +8,14 @@
98

109
async def test_setting_value(
1110
fastapi_app: FastAPI,
12-
fake_redis_pool: ConnectionPool,
11+
test_redis_pool: ConnectionPool,
1312
client: AsyncClient,
1413
) -> None:
1514
"""
1615
Tests that you can set value in redis.
1716
1817
:param fastapi_app: current application fixture.
19-
:param fake_redis_pool: fake redis pool.
18+
:param test_redis_pool: fake redis pool.
2019
:param client: client fixture.
2120
"""
2221
{%- if cookiecutter.api_type == 'rest' %}
@@ -51,26 +50,26 @@ async def test_setting_value(
5150
{%- endif %}
5251

5352
assert response.status_code == status.HTTP_200_OK
54-
async with Redis(connection_pool=fake_redis_pool) as redis:
53+
async with Redis(connection_pool=test_redis_pool) as redis:
5554
actual_value = await redis.get(test_key)
5655
assert actual_value.decode() == test_val
5756

5857

5958
async def test_getting_value(
6059
fastapi_app: FastAPI,
61-
fake_redis_pool: ConnectionPool,
60+
test_redis_pool: ConnectionPool,
6261
client: AsyncClient,
6362
) -> None:
6463
"""
6564
Tests that you can get value from redis by key.
6665
6766
:param fastapi_app: current application fixture.
68-
:param fake_redis_pool: fake redis pool.
67+
:param test_redis_pool: fake redis pool.
6968
:param client: client fixture.
7069
"""
7170
test_key = uuid.uuid4().hex
7271
test_val = uuid.uuid4().hex
73-
async with Redis(connection_pool=fake_redis_pool) as redis:
72+
async with Redis(connection_pool=test_redis_pool) as redis:
7473
await redis.set(test_key, test_val)
7574

7675
{%- if cookiecutter.api_type == 'rest' %}

0 commit comments

Comments
 (0)