Skip to content

Commit

Permalink
tests: use TestClient to refactor test files
Browse files Browse the repository at this point in the history
  • Loading branch information
waketzheng committed Jun 20, 2024
1 parent 64b8fe5 commit 62a4239
Show file tree
Hide file tree
Showing 18 changed files with 51 additions and 152 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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


Expand Down
12 changes: 3 additions & 9 deletions tests/custom_static_root/test_static_root.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down
12 changes: 3 additions & 9 deletions tests/defined_root_path/test_custom_root_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
12 changes: 3 additions & 9 deletions tests/explicit_cdn_host/test_explicit_cdn.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down
12 changes: 3 additions & 9 deletions tests/extend_choices/test_extend.py
Original file line number Diff line number Diff line change
@@ -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 (
Expand All @@ -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


Expand Down
10 changes: 3 additions & 7 deletions tests/favicon_online_cdn/test_race_favicon.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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


Expand Down
5 changes: 0 additions & 5 deletions tests/http_race/test_http_race.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 0 additions & 5 deletions tests/lock_docs/test_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
12 changes: 3 additions & 9 deletions tests/missing_js/test_missing_js.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down
12 changes: 3 additions & 9 deletions tests/no_need_to_patch/test_no_openapi_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
12 changes: 3 additions & 9 deletions tests/online_cdn/test_online_race.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
13 changes: 4 additions & 9 deletions tests/private_cdn/test_private_cdn.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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


Expand Down
12 changes: 3 additions & 9 deletions tests/root_path_without_static/test_root_path.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down
13 changes: 4 additions & 9 deletions tests/simple_asset_path/test_simple_asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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


Expand Down
12 changes: 3 additions & 9 deletions tests/static_auto/test_auto_mount.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down
Loading

0 comments on commit 62a4239

Please sign in to comment.