python-dependency-injector/examples/miniapps/fastapi-simple/tests.py

26 lines
623 B
Python
Raw Normal View History

2020-11-18 19:13:31 +03:00
from unittest import mock
import pytest
from httpx import AsyncClient
from fastapi_di_example import app, container, Service
@pytest.fixture
def client(event_loop):
2021-10-01 02:08:49 +03:00
client = AsyncClient(app=app, base_url="http://test")
2020-11-18 19:13:31 +03:00
yield client
event_loop.run_until_complete(client.aclose())
@pytest.mark.asyncio
async def test_index(client):
service_mock = mock.AsyncMock(spec=Service)
2021-10-01 02:08:49 +03:00
service_mock.process.return_value = "Foo"
2020-11-18 19:13:31 +03:00
with container.service.override(service_mock):
2021-10-01 02:08:49 +03:00
response = await client.get("/")
2020-11-18 19:13:31 +03:00
assert response.status_code == 200
2021-10-01 02:08:49 +03:00
assert response.json() == {"result": "Foo"}