From 62a4239f608249e64d7a941929778085b87b767f Mon Sep 17 00:00:00 2001 From: Waket Zheng Date: Thu, 20 Jun 2024 14:51:59 +0800 Subject: [PATCH] tests: use TestClient to refactor test files --- .../test_private_cdn_with_default_asset_path.py | 13 ++++--------- tests/custom_static_root/test_static_root.py | 12 +++--------- tests/defined_root_path/test_custom_root_path.py | 12 +++--------- tests/explicit_cdn_host/test_explicit_cdn.py | 12 +++--------- tests/extend_choices/test_extend.py | 12 +++--------- tests/favicon_online_cdn/test_race_favicon.py | 10 +++------- tests/http_race/test_http_race.py | 5 ----- tests/lock_docs/test_lock.py | 5 ----- tests/missing_js/test_missing_js.py | 12 +++--------- tests/no_need_to_patch/test_no_openapi_url.py | 12 +++--------- tests/online_cdn/test_online_race.py | 12 +++--------- tests/private_cdn/test_private_cdn.py | 13 ++++--------- tests/root_path_without_static/test_root_path.py | 12 +++--------- tests/simple_asset_path/test_simple_asset.py | 13 ++++--------- tests/static_auto/test_auto_mount.py | 12 +++--------- .../test_favicon.py | 12 +++--------- tests/static_mounted/test_mounted.py | 12 +++--------- tests/static_with_favicon/test_auto_find_favicon.py | 12 +++--------- 18 files changed, 51 insertions(+), 152 deletions(-) diff --git a/tests/cdn_with_default_asset_path/test_private_cdn_with_default_asset_path.py b/tests/cdn_with_default_asset_path/test_private_cdn_with_default_asset_path.py index 34ffa48..c159219 100644 --- a/tests/cdn_with_default_asset_path/test_private_cdn_with_default_asset_path.py +++ b/tests/cdn_with_default_asset_path/test_private_cdn_with_default_asset_path.py @@ -4,9 +4,11 @@ import pytest from config import MY_CDN, PORT -from httpx import ASGITransport, AsyncClient +from httpx import AsyncClient from main import app +from fastapi_cdn_host.utils import TestClient + try: from tests.http_race.utils import UvicornServer except ImportError: @@ -15,16 +17,9 @@ from utils import UvicornServer # type: ignore[no-redef] -@pytest.fixture(scope="module") -def anyio_backend(): - return "asyncio" - - @pytest.fixture(scope="module") async def client(): - async with AsyncClient( - transport=ASGITransport(app=app), base_url="http://test" - ) as c: + async with TestClient(app) as c: yield c diff --git a/tests/custom_static_root/test_static_root.py b/tests/custom_static_root/test_static_root.py index 9c5f909..818c61a 100644 --- a/tests/custom_static_root/test_static_root.py +++ b/tests/custom_static_root/test_static_root.py @@ -1,23 +1,17 @@ # mypy: no-disallow-untyped-decorators import pytest -from httpx import ASGITransport, AsyncClient +from httpx import AsyncClient from main import app from fastapi_cdn_host.client import CdnHostBuilder +from fastapi_cdn_host.utils import TestClient default_favicon_url = "https://fastapi.tiangolo.com/img/favicon.png" -@pytest.fixture(scope="module") -def anyio_backend(): - return "asyncio" - - @pytest.fixture(scope="module") async def client(): - async with AsyncClient( - transport=ASGITransport(app=app), base_url="http://test" - ) as c: + async with TestClient(app) as c: yield c diff --git a/tests/defined_root_path/test_custom_root_path.py b/tests/defined_root_path/test_custom_root_path.py index 4cd21b5..df43477 100644 --- a/tests/defined_root_path/test_custom_root_path.py +++ b/tests/defined_root_path/test_custom_root_path.py @@ -2,24 +2,18 @@ import re import pytest -from httpx import ASGITransport, AsyncClient +from httpx import AsyncClient from main import app from fastapi_cdn_host.client import CdnHostBuilder +from fastapi_cdn_host.utils import TestClient default_favicon_url = "https://fastapi.tiangolo.com/img/favicon.png" -@pytest.fixture(scope="module") -def anyio_backend(): - return "asyncio" - - @pytest.fixture(scope="module") async def client(): - async with AsyncClient( - transport=ASGITransport(app=app), base_url="http://test" - ) as c: + async with TestClient(app) as c: yield c diff --git a/tests/explicit_cdn_host/test_explicit_cdn.py b/tests/explicit_cdn_host/test_explicit_cdn.py index c0864ea..af67132 100644 --- a/tests/explicit_cdn_host/test_explicit_cdn.py +++ b/tests/explicit_cdn_host/test_explicit_cdn.py @@ -1,21 +1,15 @@ # mypy: no-disallow-untyped-decorators import pytest -from httpx import ASGITransport, AsyncClient +from httpx import AsyncClient from main import app from fastapi_cdn_host import CdnHostEnum - - -@pytest.fixture(scope="module") -def anyio_backend(): - return "asyncio" +from fastapi_cdn_host.utils import TestClient @pytest.fixture(scope="module") async def client(): - async with AsyncClient( - transport=ASGITransport(app=app), base_url="http://test" - ) as c: + async with TestClient(app) as c: yield c diff --git a/tests/extend_choices/test_extend.py b/tests/extend_choices/test_extend.py index 7eb47e3..86f66b6 100644 --- a/tests/extend_choices/test_extend.py +++ b/tests/extend_choices/test_extend.py @@ -1,6 +1,6 @@ # mypy: no-disallow-untyped-decorators import pytest -from httpx import ASGITransport, AsyncClient +from httpx import AsyncClient from main import app from fastapi_cdn_host.client import ( @@ -11,18 +11,12 @@ CdnHostItem, HttpSniff, ) - - -@pytest.fixture(scope="module") -def anyio_backend(): - return "asyncio" +from fastapi_cdn_host.utils import TestClient @pytest.fixture(scope="module") async def client(): - async with AsyncClient( - transport=ASGITransport(app=app), base_url="http://test" - ) as c: + async with TestClient(app) as c: yield c diff --git a/tests/favicon_online_cdn/test_race_favicon.py b/tests/favicon_online_cdn/test_race_favicon.py index d5986b0..5a9cd6d 100644 --- a/tests/favicon_online_cdn/test_race_favicon.py +++ b/tests/favicon_online_cdn/test_race_favicon.py @@ -1,6 +1,6 @@ # mypy: no-disallow-untyped-decorators import pytest -from httpx import ASGITransport, AsyncClient +from httpx import AsyncClient from main import app from fastapi_cdn_host.client import CdnHostBuilder, CdnHostEnum, HttpSniff @@ -13,16 +13,12 @@ def timeit(f): # type:ignore return f -@pytest.fixture(scope="module") -def anyio_backend(): - return "asyncio" +from fastapi_cdn_host.utils import TestClient @pytest.fixture(scope="module") async def client(): - async with AsyncClient( - transport=ASGITransport(app=app), base_url="http://test" - ) as c: + async with TestClient(app) as c: yield c diff --git a/tests/http_race/test_http_race.py b/tests/http_race/test_http_race.py index f24c0db..e08960a 100644 --- a/tests/http_race/test_http_race.py +++ b/tests/http_race/test_http_race.py @@ -17,11 +17,6 @@ def timeit(f): # type:ignore return f -@pytest.fixture(scope="module") -def anyio_backend(): - return "asyncio" - - @pytest.fixture(scope="module") async def client(): async with TestClient(app) as c: diff --git a/tests/lock_docs/test_lock.py b/tests/lock_docs/test_lock.py index a6dafa4..41accb8 100644 --- a/tests/lock_docs/test_lock.py +++ b/tests/lock_docs/test_lock.py @@ -9,11 +9,6 @@ from fastapi_cdn_host.utils import TestClient -@pytest.fixture(scope="module") -def anyio_backend(): - return "asyncio" - - @pytest.fixture(scope="module") async def client(): async with TestClient(app) as c: diff --git a/tests/missing_js/test_missing_js.py b/tests/missing_js/test_missing_js.py index a99b72c..6f601a5 100644 --- a/tests/missing_js/test_missing_js.py +++ b/tests/missing_js/test_missing_js.py @@ -1,23 +1,17 @@ # mypy: no-disallow-untyped-decorators import pytest -from httpx import ASGITransport, AsyncClient +from httpx import AsyncClient from main import app from fastapi_cdn_host.client import CdnHostBuilder +from fastapi_cdn_host.utils import TestClient default_favicon_url = "https://fastapi.tiangolo.com/img/favicon.png" -@pytest.fixture(scope="module") -def anyio_backend(): - return "asyncio" - - @pytest.fixture(scope="module") async def client(): - async with AsyncClient( - transport=ASGITransport(app=app), base_url="http://test" - ) as c: + async with TestClient(app) as c: yield c diff --git a/tests/no_need_to_patch/test_no_openapi_url.py b/tests/no_need_to_patch/test_no_openapi_url.py index ba0e384..ac00019 100644 --- a/tests/no_need_to_patch/test_no_openapi_url.py +++ b/tests/no_need_to_patch/test_no_openapi_url.py @@ -3,24 +3,18 @@ import pytest from fastapi import FastAPI -from httpx import ASGITransport, AsyncClient +from httpx import AsyncClient from main import app from fastapi_cdn_host.client import StaticBuilder, monkey_patch_for_docs_ui +from fastapi_cdn_host.utils import TestClient default_favicon_url = "https://fastapi.tiangolo.com/img/favicon.png" -@pytest.fixture(scope="module") -def anyio_backend(): - return "asyncio" - - @pytest.fixture(scope="module") async def client(): - async with AsyncClient( - transport=ASGITransport(app=app), base_url="http://test" - ) as c: + async with TestClient(app) as c: yield c diff --git a/tests/online_cdn/test_online_race.py b/tests/online_cdn/test_online_race.py index 791b16b..66bd1d4 100644 --- a/tests/online_cdn/test_online_race.py +++ b/tests/online_cdn/test_online_race.py @@ -2,24 +2,18 @@ import re import pytest -from httpx import ASGITransport, AsyncClient +from httpx import AsyncClient from main import app from fastapi_cdn_host.client import CdnHostBuilder, CdnHostEnum, HttpSniff +from fastapi_cdn_host.utils import TestClient default_favicon_url = "https://fastapi.tiangolo.com/img/favicon.png" -@pytest.fixture(scope="module") -def anyio_backend(): - return "asyncio" - - @pytest.fixture(scope="module") async def client(): - async with AsyncClient( - transport=ASGITransport(app=app), base_url="http://test" - ) as c: + async with TestClient(app) as c: yield c diff --git a/tests/private_cdn/test_private_cdn.py b/tests/private_cdn/test_private_cdn.py index 8ad5252..5707e67 100644 --- a/tests/private_cdn/test_private_cdn.py +++ b/tests/private_cdn/test_private_cdn.py @@ -4,9 +4,11 @@ import pytest from config import MY_CDN, PORT -from httpx import ASGITransport, AsyncClient +from httpx import AsyncClient from main import app +from fastapi_cdn_host.utils import TestClient + try: from tests.http_race.utils import UvicornServer except ImportError: @@ -15,16 +17,9 @@ from utils import UvicornServer # type: ignore[no-redef] -@pytest.fixture(scope="module") -def anyio_backend(): - return "asyncio" - - @pytest.fixture(scope="module") async def client(): - async with AsyncClient( - transport=ASGITransport(app=app), base_url="http://test" - ) as c: + async with TestClient(app) as c: yield c diff --git a/tests/root_path_without_static/test_root_path.py b/tests/root_path_without_static/test_root_path.py index ea29dcc..4260299 100644 --- a/tests/root_path_without_static/test_root_path.py +++ b/tests/root_path_without_static/test_root_path.py @@ -1,23 +1,17 @@ # mypy: no-disallow-untyped-decorators import pytest -from httpx import ASGITransport, AsyncClient +from httpx import AsyncClient from main import app from fastapi_cdn_host.client import CdnHostBuilder, CdnHostEnum, HttpSniff +from fastapi_cdn_host.utils import TestClient default_favicon_url = "https://fastapi.tiangolo.com/img/favicon.png" -@pytest.fixture(scope="module") -def anyio_backend(): - return "asyncio" - - @pytest.fixture(scope="module") async def client(): - async with AsyncClient( - transport=ASGITransport(app=app), base_url="http://test" - ) as c: + async with TestClient(app) as c: yield c diff --git a/tests/simple_asset_path/test_simple_asset.py b/tests/simple_asset_path/test_simple_asset.py index 4a12143..b6328d7 100644 --- a/tests/simple_asset_path/test_simple_asset.py +++ b/tests/simple_asset_path/test_simple_asset.py @@ -4,9 +4,11 @@ import pytest from config import MY_CDN, PORT -from httpx import ASGITransport, AsyncClient +from httpx import AsyncClient from main import app +from fastapi_cdn_host.utils import TestClient + try: from tests.http_race.utils import UvicornServer except ImportError: @@ -15,16 +17,9 @@ from utils import UvicornServer # type: ignore[no-redef] -@pytest.fixture(scope="module") -def anyio_backend(): - return "asyncio" - - @pytest.fixture(scope="module") async def client(): - async with AsyncClient( - transport=ASGITransport(app=app), base_url="http://test" - ) as c: + async with TestClient(app) as c: yield c diff --git a/tests/static_auto/test_auto_mount.py b/tests/static_auto/test_auto_mount.py index 9c5f909..818c61a 100644 --- a/tests/static_auto/test_auto_mount.py +++ b/tests/static_auto/test_auto_mount.py @@ -1,23 +1,17 @@ # mypy: no-disallow-untyped-decorators import pytest -from httpx import ASGITransport, AsyncClient +from httpx import AsyncClient from main import app from fastapi_cdn_host.client import CdnHostBuilder +from fastapi_cdn_host.utils import TestClient default_favicon_url = "https://fastapi.tiangolo.com/img/favicon.png" -@pytest.fixture(scope="module") -def anyio_backend(): - return "asyncio" - - @pytest.fixture(scope="module") async def client(): - async with AsyncClient( - transport=ASGITransport(app=app), base_url="http://test" - ) as c: + async with TestClient(app) as c: yield c diff --git a/tests/static_favicon_without_swagger_ui/test_favicon.py b/tests/static_favicon_without_swagger_ui/test_favicon.py index 9805664..b0d2957 100644 --- a/tests/static_favicon_without_swagger_ui/test_favicon.py +++ b/tests/static_favicon_without_swagger_ui/test_favicon.py @@ -1,23 +1,17 @@ # mypy: no-disallow-untyped-decorators import pytest -from httpx import ASGITransport, AsyncClient +from httpx import AsyncClient from main import app from fastapi_cdn_host.client import CdnHostBuilder, CdnHostEnum, HttpSniff +from fastapi_cdn_host.utils import TestClient default_favicon_url = "https://fastapi.tiangolo.com/img/favicon.png" -@pytest.fixture(scope="module") -def anyio_backend(): - return "asyncio" - - @pytest.fixture(scope="module") async def client(): - async with AsyncClient( - transport=ASGITransport(app=app), base_url="http://test" - ) as c: + async with TestClient(app) as c: yield c diff --git a/tests/static_mounted/test_mounted.py b/tests/static_mounted/test_mounted.py index 9c5f909..818c61a 100644 --- a/tests/static_mounted/test_mounted.py +++ b/tests/static_mounted/test_mounted.py @@ -1,23 +1,17 @@ # mypy: no-disallow-untyped-decorators import pytest -from httpx import ASGITransport, AsyncClient +from httpx import AsyncClient from main import app from fastapi_cdn_host.client import CdnHostBuilder +from fastapi_cdn_host.utils import TestClient default_favicon_url = "https://fastapi.tiangolo.com/img/favicon.png" -@pytest.fixture(scope="module") -def anyio_backend(): - return "asyncio" - - @pytest.fixture(scope="module") async def client(): - async with AsyncClient( - transport=ASGITransport(app=app), base_url="http://test" - ) as c: + async with TestClient(app) as c: yield c diff --git a/tests/static_with_favicon/test_auto_find_favicon.py b/tests/static_with_favicon/test_auto_find_favicon.py index fb0a6a2..f97c29f 100644 --- a/tests/static_with_favicon/test_auto_find_favicon.py +++ b/tests/static_with_favicon/test_auto_find_favicon.py @@ -1,23 +1,17 @@ # mypy: no-disallow-untyped-decorators import pytest -from httpx import ASGITransport, AsyncClient +from httpx import AsyncClient from main import app from fastapi_cdn_host.client import CdnHostBuilder +from fastapi_cdn_host.utils import TestClient default_favicon_url = "https://fastapi.tiangolo.com/img/favicon.png" -@pytest.fixture(scope="module") -def anyio_backend(): - return "asyncio" - - @pytest.fixture(scope="module") async def client(): - async with AsyncClient( - transport=ASGITransport(app=app), base_url="http://test" - ) as c: + async with TestClient(app) as c: yield c