Update fastapi-simple example

This commit is contained in:
Roman Mogylatov 2021-09-30 19:08:49 -04:00
parent 3c52756d3f
commit 6b4c7e50b5
2 changed files with 9 additions and 11 deletions

View File

@ -1,13 +1,11 @@
import sys
from fastapi import FastAPI, Depends
from dependency_injector import containers, providers
from dependency_injector.wiring import inject, Provide
from dependency_injector.wiring import Provide, inject
class Service:
async def process(self) -> str:
return 'Ok'
return "OK"
class Container(containers.DeclarativeContainer):
@ -18,12 +16,12 @@ class Container(containers.DeclarativeContainer):
app = FastAPI()
@app.api_route('/')
@app.api_route("/")
@inject
async def index(service: Service = Depends(Provide[Container.service])):
result = await service.process()
return {'result': result}
return {"result": result}
container = Container()
container.wire(modules=[sys.modules[__name__]])
container.wire(modules=[__name__])

View File

@ -8,7 +8,7 @@ from fastapi_di_example import app, container, 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())
@ -16,10 +16,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"}