diff --git a/tests/unit/samples/wiringflask/web.py b/tests/unit/samples/wiringflask/web.py index fe6e39dc..37fbd5e0 100644 --- a/tests/unit/samples/wiringflask/web.py +++ b/tests/unit/samples/wiringflask/web.py @@ -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): diff --git a/tests/unit/wiring/test_wiringfastapi_py36.py b/tests/unit/wiring/test_wiringfastapi_py36.py index baf93e3f..d93cc9c2 100644 --- a/tests/unit/wiring/test_wiringfastapi_py36.py +++ b/tests/unit/wiring/test_wiringfastapi_py36.py @@ -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"} diff --git a/tests/unit/wiring/test_wiringflask_py36.py b/tests/unit/wiring/test_wiringflask_py36.py index 586ade16..751f04d8 100644 --- a/tests/unit/wiring/test_wiringflask_py36.py +++ b/tests/unit/wiring/test_wiringflask_py36.py @@ -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"}