mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-07-01 19:03:19 +03:00
Update fastapi+redis example
This commit is contained in:
parent
98e3c68563
commit
9a43ee7e88
|
@ -1,9 +1,7 @@
|
|||
"""Application module."""
|
||||
|
||||
import sys
|
||||
|
||||
from fastapi import FastAPI, Depends
|
||||
from dependency_injector.wiring import inject, Provide
|
||||
from fastapi import FastAPI, Depends
|
||||
|
||||
from .containers import Container
|
||||
from .services import Service
|
||||
|
@ -12,14 +10,14 @@ from .services import Service
|
|||
app = FastAPI()
|
||||
|
||||
|
||||
@app.api_route('/')
|
||||
@app.api_route("/")
|
||||
@inject
|
||||
async def index(service: Service = Depends(Provide[Container.service])):
|
||||
value = await service.process()
|
||||
return {'result': value}
|
||||
return {"result": value}
|
||||
|
||||
|
||||
container = Container()
|
||||
container.config.redis_host.from_env('REDIS_HOST', 'localhost')
|
||||
container.config.redis_password.from_env('REDIS_PASSWORD', 'password')
|
||||
container.wire(modules=[sys.modules[__name__]])
|
||||
container.config.redis_host.from_env("REDIS_HOST", "localhost")
|
||||
container.config.redis_password.from_env("REDIS_PASSWORD", "password")
|
||||
container.wire(modules=[__name__])
|
||||
|
|
|
@ -6,7 +6,7 @@ from aioredis import create_redis_pool, Redis
|
|||
|
||||
|
||||
async def init_redis_pool(host: str, password: str) -> AsyncIterator[Redis]:
|
||||
pool = await create_redis_pool(f'redis://{host}', password=password)
|
||||
pool = await create_redis_pool(f"redis://{host}", password=password)
|
||||
yield pool
|
||||
pool.close()
|
||||
await pool.wait_closed()
|
||||
|
|
|
@ -8,5 +8,5 @@ class Service:
|
|||
self._redis = redis
|
||||
|
||||
async def process(self) -> str:
|
||||
await self._redis.set('my-key', 'value')
|
||||
return await self._redis.get('my-key', encoding='utf-8')
|
||||
await self._redis.set("my-key", "value")
|
||||
return await self._redis.get("my-key", encoding="utf-8")
|
||||
|
|
|
@ -11,7 +11,7 @@ from .services import Service
|
|||
|
||||
@pytest.fixture
|
||||
def client(event_loop):
|
||||
client = AsyncClient(app=app, base_url='http://test')
|
||||
client = AsyncClient(app=app, base_url="http://test")
|
||||
yield client
|
||||
event_loop.run_until_complete(client.aclose())
|
||||
|
||||
|
@ -19,10 +19,10 @@ def client(event_loop):
|
|||
@pytest.mark.asyncio
|
||||
async def test_index(client):
|
||||
service_mock = mock.AsyncMock(spec=Service)
|
||||
service_mock.process.return_value = 'Foo'
|
||||
service_mock.process.return_value = "Foo"
|
||||
|
||||
with container.service.override(service_mock):
|
||||
response = await client.get('/')
|
||||
response = await client.get("/")
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.json() == {'result': 'Foo'}
|
||||
assert response.json() == {"result": "Foo"}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
dependency-injector
|
||||
fastapi
|
||||
uvicorn
|
||||
aioredis
|
||||
aioredis<2 # TODO: Update example to work with aioredis >= 2.0
|
||||
|
||||
# For testing:
|
||||
pytest
|
||||
|
|
Loading…
Reference in New Issue
Block a user