mirror of
				https://github.com/ets-labs/python-dependency-injector.git
				synced 2025-10-31 07:57:43 +03:00 
			
		
		
		
	Update fastapi-simple example
This commit is contained in:
		
							parent
							
								
									3c52756d3f
								
							
						
					
					
						commit
						6b4c7e50b5
					
				|  | @ -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__]) | ||||
|  |  | |||
|  | @ -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"} | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue
	
	Block a user