Migrate FastAPI and Flask wiring tests

This commit is contained in:
Roman Mogylatov 2021-10-12 22:04:38 -04:00
parent af286548b3
commit ea37ad9dca
3 changed files with 27 additions and 39 deletions

View File

@ -10,7 +10,7 @@ _request_ctx_stack, _app_ctx_stack # noqa
class Service: class Service:
def process(self) -> str: def process(self) -> str:
return "Ok" return "OK"
class Container(containers.DeclarativeContainer): class Container(containers.DeclarativeContainer):

View File

@ -1,13 +1,8 @@
from httpx import AsyncClient 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 # Runtime import to avoid syntax errors in samples on Python < 3.5 and reach top-dir
import os import os
_TOP_DIR = os.path.abspath(
os.path.sep.join((
os.path.dirname(__file__),
"../",
)),
)
_SAMPLES_DIR = os.path.abspath( _SAMPLES_DIR = os.path.abspath(
os.path.sep.join(( os.path.sep.join((
os.path.dirname(__file__), os.path.dirname(__file__),
@ -15,38 +10,34 @@ _SAMPLES_DIR = os.path.abspath(
)), )),
) )
import sys import sys
sys.path.append(_TOP_DIR)
sys.path.append(_SAMPLES_DIR) sys.path.append(_SAMPLES_DIR)
from asyncutils import AsyncTestCase
from wiringfastapi import web 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: @mark.asyncio
super().setUp() async def test_depends_marker_injection(async_client: AsyncClient):
self.client = AsyncClient(app=web.app, base_url="http://test") class ServiceMock:
async def process(self):
return "Foo"
def tearDown(self) -> None: with web.container.service.override(ServiceMock()):
self._run(self.client.aclose()) response = await async_client.get("/")
super().tearDown()
def test_depends_marker_injection(self): assert response.status_code == 200
class ServiceMock: assert response.json() == {"result": "Foo"}
async def process(self):
return "Foo"
with web.container.service.override(ServiceMock()):
response = self._run(self.client.get("/"))
self.assertEqual(response.status_code, 200) @mark.asyncio
self.assertEqual(response.json(), {"result": "Foo"}) async def test_depends_injection(async_client: AsyncClient):
response = await async_client.get("/auth", auth=("john_smith", "secret"))
def test_depends_injection(self): assert response.status_code == 200
response = self._run(self.client.get("/auth", auth=("john_smith", "secret"))) assert response.json() == {"username": "john_smith", "password": "secret"}
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json(), {"username": "john_smith", "password": "secret"})

View File

@ -1,4 +1,3 @@
import unittest
import json import json
# Runtime import to avoid syntax errors in samples on Python < 3.5 and reach top-dir # 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 from wiringflask import web
class WiringFlaskTest(unittest.TestCase): def test_wiring_with_flask():
client = web.app.test_client()
def test(self): with web.app.app_context():
client = web.app.test_client() response = client.get("/")
with web.app.app_context(): assert response.status_code == 200
response = client.get("/") assert json.loads(response.data) == {"result": "OK"}
self.assertEqual(response.status_code, 200)
self.assertEqual(json.loads(response.data), {"result": "Ok"})