mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-07-04 20:33:13 +03:00
Migrate FastAPI and Flask wiring tests
This commit is contained in:
parent
af286548b3
commit
ea37ad9dca
|
@ -10,7 +10,7 @@ _request_ctx_stack, _app_ctx_stack # noqa
|
|||
|
||||
class Service:
|
||||
def process(self) -> str:
|
||||
return "Ok"
|
||||
return "OK"
|
||||
|
||||
|
||||
class Container(containers.DeclarativeContainer):
|
||||
|
|
|
@ -1,13 +1,8 @@
|
|||
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
|
||||
_TOP_DIR = os.path.abspath(
|
||||
os.path.sep.join((
|
||||
os.path.dirname(__file__),
|
||||
"../",
|
||||
)),
|
||||
)
|
||||
_SAMPLES_DIR = os.path.abspath(
|
||||
os.path.sep.join((
|
||||
os.path.dirname(__file__),
|
||||
|
@ -15,38 +10,34 @@ _SAMPLES_DIR = os.path.abspath(
|
|||
)),
|
||||
)
|
||||
import sys
|
||||
sys.path.append(_TOP_DIR)
|
||||
sys.path.append(_SAMPLES_DIR)
|
||||
|
||||
from asyncutils import AsyncTestCase
|
||||
|
||||
from wiringfastapi import web
|
||||
|
||||
|
||||
class WiringFastAPITest(AsyncTestCase):
|
||||
@fixture
|
||||
async def async_client():
|
||||
client = AsyncClient(app=web.app, base_url="http://test")
|
||||
yield client
|
||||
await client.aclose()
|
||||
|
||||
client: AsyncClient
|
||||
|
||||
def setUp(self) -> None:
|
||||
super().setUp()
|
||||
self.client = AsyncClient(app=web.app, base_url="http://test")
|
||||
@mark.asyncio
|
||||
async def test_depends_marker_injection(async_client: AsyncClient):
|
||||
class ServiceMock:
|
||||
async def process(self):
|
||||
return "Foo"
|
||||
|
||||
def tearDown(self) -> None:
|
||||
self._run(self.client.aclose())
|
||||
super().tearDown()
|
||||
with web.container.service.override(ServiceMock()):
|
||||
response = await async_client.get("/")
|
||||
|
||||
def test_depends_marker_injection(self):
|
||||
class ServiceMock:
|
||||
async def process(self):
|
||||
return "Foo"
|
||||
assert response.status_code == 200
|
||||
assert response.json() == {"result": "Foo"}
|
||||
|
||||
with web.container.service.override(ServiceMock()):
|
||||
response = self._run(self.client.get("/"))
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.json(), {"result": "Foo"})
|
||||
|
||||
def test_depends_injection(self):
|
||||
response = self._run(self.client.get("/auth", auth=("john_smith", "secret")))
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.json(), {"username": "john_smith", "password": "secret"})
|
||||
@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"}
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
import unittest
|
||||
import json
|
||||
|
||||
# Runtime import to avoid syntax errors in samples on Python < 3.5 and reach top-dir
|
||||
|
@ -22,13 +21,11 @@ sys.path.append(_SAMPLES_DIR)
|
|||
from wiringflask import web
|
||||
|
||||
|
||||
class WiringFlaskTest(unittest.TestCase):
|
||||
def test_wiring_with_flask():
|
||||
client = web.app.test_client()
|
||||
|
||||
def test(self):
|
||||
client = web.app.test_client()
|
||||
with web.app.app_context():
|
||||
response = client.get("/")
|
||||
|
||||
with web.app.app_context():
|
||||
response = client.get("/")
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(json.loads(response.data), {"result": "Ok"})
|
||||
assert response.status_code == 200
|
||||
assert json.loads(response.data) == {"result": "OK"}
|
||||
|
|
Loading…
Reference in New Issue
Block a user