python-dependency-injector/tests/unit/wiring/test_fastapi_py36.py
2023-07-09 18:19:14 +09:00

58 lines
1.4 KiB
Python

from httpx import AsyncClient
from pytest import fixture, mark
# Runtime import to avoid syntax errors in samples on Python < 3.5 and reach top-dir
import os
_SAMPLES_DIR = os.path.abspath(
os.path.sep.join((
os.path.dirname(__file__),
"../samples/",
)),
)
import sys
sys.path.append(_SAMPLES_DIR)
from wiringfastapi import web
@fixture
async def async_client():
client = AsyncClient(app=web.app, base_url="http://test")
yield client
await client.aclose()
@mark.asyncio
async def test_depends_marker_injection(async_client: AsyncClient):
class ServiceMock:
async def process(self):
return "Foo"
with web.container.service.override(ServiceMock()):
response = await async_client.get("/")
assert response.status_code == 200
assert response.json() == {"result": "Foo"}
@mark.asyncio
async def test_depends_with_annotated(async_client: AsyncClient):
class ServiceMock:
async def process(self):
return "Foo"
with web.container.service.override(ServiceMock()):
response = await async_client.get("/")
assert response.status_code == 200
assert response.json() == {"result": "Foo"}
@mark.asyncio
async def test_depends_injection(async_client: AsyncClient):
response = await async_client.get("/auth", auth=("john_smith", "secret"))
assert response.status_code == 200
assert response.json() == {"username": "john_smith", "password": "secret"}