File tree Expand file tree Collapse file tree 2 files changed +9
-11
lines changed
examples/miniapps/fastapi-simple Expand file tree Collapse file tree 2 files changed +9
-11
lines changed Original file line number Diff line number Diff line change 1
- import sys
2
-
3
1
from fastapi import FastAPI , Depends
4
2
from dependency_injector import containers , providers
5
- from dependency_injector .wiring import inject , Provide
3
+ from dependency_injector .wiring import Provide , inject
6
4
7
5
8
6
class Service :
9
7
async def process (self ) -> str :
10
- return 'Ok'
8
+ return "OK"
11
9
12
10
13
11
class Container (containers .DeclarativeContainer ):
@@ -18,12 +16,12 @@ class Container(containers.DeclarativeContainer):
18
16
app = FastAPI ()
19
17
20
18
21
- @app .api_route ('/' )
19
+ @app .api_route ("/" )
22
20
@inject
23
21
async def index (service : Service = Depends (Provide [Container .service ])):
24
22
result = await service .process ()
25
- return {' result' : result }
23
+ return {" result" : result }
26
24
27
25
28
26
container = Container ()
29
- container .wire (modules = [sys . modules [ __name__ ] ])
27
+ container .wire (modules = [__name__ ])
Original file line number Diff line number Diff line change 8
8
9
9
@pytest .fixture
10
10
def client (event_loop ):
11
- client = AsyncClient (app = app , base_url = ' http://test' )
11
+ client = AsyncClient (app = app , base_url = " http://test" )
12
12
yield client
13
13
event_loop .run_until_complete (client .aclose ())
14
14
15
15
16
16
@pytest .mark .asyncio
17
17
async def test_index (client ):
18
18
service_mock = mock .AsyncMock (spec = Service )
19
- service_mock .process .return_value = ' Foo'
19
+ service_mock .process .return_value = " Foo"
20
20
21
21
with container .service .override (service_mock ):
22
- response = await client .get ('/' )
22
+ response = await client .get ("/" )
23
23
24
24
assert response .status_code == 200
25
- assert response .json () == {' result' : ' Foo' }
25
+ assert response .json () == {" result" : " Foo" }
You can’t perform that action at this time.
0 commit comments