Я использую fakeredis и pytest в приложении
Моя функция get_redis в файле app/helpers/providers.py:
Код: Выделить всё
from redis import ConnectionPool, Redis
redis_pool = None
def get_redis() -> Redis:
global redis_pool
if redis_pool is None:
redis_pool = ConnectionPool()
return Redis.from_pool(redis_pool)
Код: Выделить всё
from app.helpers.providers import get_redis
@router.get("/secrets/{secret_id}")
async def get_secret(
request: Request,
secret_id: UUID,
credentials: Annotated[HTTPBasicCredentials, Depends(security)],
redis: Redis = Depends(get_redis),
):
with redis.pipeline() as pipe: # I use redis from get_redis here
pipe.get(key)
pipe.delete(key)
results = pipe.execute()
return results # just to simplify
Код: Выделить всё
import sys
from unittest.mock import patch
import fakeredis
import pytest
from fastapi import HTTPException, status
from fastapi.exceptions import RequestValidationError
from fastapi.testclient import TestClient
@pytest.fixture
def fake_redis():
return fakeredis.FakeStrictRedis()
@pytest.fixture(autouse=True)
def mock_redis_dependencies(fake_redis):
with patch('app.endpoints.secrets.get_redis', return_value=fake_redis):
yield
sys.path.append(".")
from app.endpoints.secrets import router
def test_secret_not_found(fake_redis):
client = TestClient(router)
with pytest.raises(HTTPException) as exc:
client.get(
"/api/secrets/aaaaaaaa-bbbb-4ccc-aaaa-eeeeeeeeeef1",
auth=("admin", "admin"),
)
assert exc.value.status_code == status.HTTP_404_NOT_FOUND
What I am doing wrong? I have another test that works fine.
Источник: https://stackoverflow.com/questions/781 ... ith-pytest