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

29 lines
678 B
Python
Raw Normal View History

2020-11-18 19:13:31 +03:00
from unittest import mock
import pytest
2024-12-08 19:53:29 +03:00
import pytest_asyncio
from httpx import ASGITransport, AsyncClient
2020-11-18 19:13:31 +03:00
from fastapi_di_example import app, container, Service
2024-12-08 19:53:29 +03:00
@pytest_asyncio.fixture
async def client():
async with AsyncClient(
transport=ASGITransport(app=app),
base_url="http://test",
) as client:
yield client
2020-11-18 19:13:31 +03:00
@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"}