From 9204af00318f98bc32c6535b5b873e749a0d2165 Mon Sep 17 00:00:00 2001 From: Waket Zheng Date: Tue, 13 Sep 2022 02:23:05 +0800 Subject: [PATCH] fix: fastapi example test not working. (#1029) --- examples/fastapi/_tests.py | 32 ++++++--------- poetry.lock | 83 +++++++++++++++++++++++++++++++++----- pyproject.toml | 2 + 3 files changed, 86 insertions(+), 31 deletions(-) diff --git a/examples/fastapi/_tests.py b/examples/fastapi/_tests.py index 1e6f3d58a..720f56791 100644 --- a/examples/fastapi/_tests.py +++ b/examples/fastapi/_tests.py @@ -1,40 +1,32 @@ # mypy: no-disallow-untyped-decorators # pylint: disable=E0611,E0401 -import asyncio -from typing import Generator - import pytest -from fastapi.testclient import TestClient +from asgi_lifespan import LifespanManager +from httpx import AsyncClient from main import app from models import Users -from tortoise.contrib.test import finalizer, initializer - @pytest.fixture(scope="module") -def client() -> Generator: - initializer(["models"]) - with TestClient(app) as c: - yield c - finalizer() +def anyio_backend(): + return "asyncio" @pytest.fixture(scope="module") -def event_loop(client: TestClient) -> Generator: - yield client.task.get_loop() # type: ignore +async def client(): + async with LifespanManager(app): + async with AsyncClient(app=app, base_url="http://test") as c: + yield c -def test_create_user(client: TestClient, event_loop: asyncio.AbstractEventLoop): # nosec - response = client.post("/users", json={"username": "admin"}) +@pytest.mark.anyio +async def test_create_user(client: AsyncClient): # nosec + response = await client.post("/users", json={"username": "admin"}) assert response.status_code == 200, response.text data = response.json() assert data["username"] == "admin" assert "id" in data user_id = data["id"] - async def get_user_by_db(): - user = await Users.get(id=user_id) - return user - - user_obj = event_loop.run_until_complete(get_user_by_db()) + user_obj = await Users.get(id=user_id) assert user_obj.id == user_id diff --git a/poetry.lock b/poetry.lock index 7d4a11807..7600d9ae0 100644 --- a/poetry.lock +++ b/poetry.lock @@ -99,6 +99,17 @@ category = "dev" optional = false python-versions = "*" +[[package]] +name = "asgi-lifespan" +version = "1.0.1" +description = "Programmatic startup/shutdown of ASGI apps." +category = "dev" +optional = false +python-versions = ">=3.6" + +[package.dependencies] +sniffio = "*" + [[package]] name = "astroid" version = "2.11.7" @@ -585,15 +596,12 @@ jwt = ["PyJWT (>=2.3.0,<2.4.0)", "cryptography (>=35.0.0,<35.1.0)"] [[package]] name = "h11" -version = "0.13.0" +version = "0.12.0" description = "A pure-Python, bring-your-own-I/O implementation of HTTP/1.1" category = "dev" optional = false python-versions = ">=3.6" -[package.dependencies] -typing-extensions = {version = "*", markers = "python_version < \"3.8\""} - [[package]] name = "h2" version = "4.1.0" @@ -614,6 +622,24 @@ category = "dev" optional = false python-versions = ">=3.6.1" +[[package]] +name = "httpcore" +version = "0.15.0" +description = "A minimal low-level HTTP client." +category = "dev" +optional = false +python-versions = ">=3.7" + +[package.dependencies] +anyio = ">=3.0.0,<4.0.0" +certifi = "*" +h11 = ">=0.11,<0.13" +sniffio = ">=1.0.0,<2.0.0" + +[package.extras] +http2 = ["h2 (>=3,<5)"] +socks = ["socksio (>=1.0.0,<2.0.0)"] + [[package]] name = "httptools" version = "0.4.0" @@ -625,6 +651,26 @@ python-versions = ">=3.5.0" [package.extras] test = ["Cython (>=0.29.24,<0.30.0)"] +[[package]] +name = "httpx" +version = "0.23.0" +description = "The next generation HTTP client." +category = "dev" +optional = false +python-versions = ">=3.7" + +[package.dependencies] +certifi = "*" +httpcore = ">=0.15.0,<0.16.0" +rfc3986 = {version = ">=1.3,<2", extras = ["idna2008"]} +sniffio = "*" + +[package.extras] +brotli = ["brotlicffi", "brotli"] +cli = ["click (>=8.0.0,<9.0.0)", "rich (>=10,<13)", "pygments (>=2.0.0,<3.0.0)"] +http2 = ["h2 (>=3,<5)"] +socks = ["socksio (>=1.0.0,<2.0.0)"] + [[package]] name = "hypercorn" version = "0.14.3" @@ -1278,11 +1324,14 @@ requests = ">=2.0.1,<3.0.0" [[package]] name = "rfc3986" -version = "2.0.0" +version = "1.5.0" description = "Validating URI References per RFC 3986" category = "dev" optional = false -python-versions = ">=3.7" +python-versions = "*" + +[package.dependencies] +idna = {version = "*", optional = true, markers = "extra == \"idna2008\""} [package.extras] idna2008 = ["idna"] @@ -1757,7 +1806,7 @@ psycopg = ["psycopg"] [metadata] lock-version = "1.1" python-versions = "^3.7" -content-hash = "fa4c2a0013dae6fcb68c91964e5094da542bac6d82cec36825f75c5c372e612f" +content-hash = "7aeef13c6e92683242b81ab2dcf1441dcf0336f728b75ab1d93d9d01d32f2f7d" [metadata.files] aiofiles = [] @@ -1856,6 +1905,10 @@ anyio = [ {file = "anyio-3.6.1.tar.gz", hash = "sha256:413adf95f93886e442aea925f3ee43baa5a765a64a0f52c6081894f9992fdd0b"}, ] appdirs = [] +asgi-lifespan = [ + {file = "asgi-lifespan-1.0.1.tar.gz", hash = "sha256:9a33e7da2073c4764bc79bd6136501d6c42f60e3d2168ba71235e84122eadb7f"}, + {file = "asgi_lifespan-1.0.1-py3-none-any.whl", hash = "sha256:9ea969dc5eb5cf08e52c08dce6f61afcadd28112e72d81c972b1d8eb8691ab53"}, +] astroid = [] async-timeout = [ {file = "async-timeout-4.0.2.tar.gz", hash = "sha256:2163e1640ddb52b7a8c80d0a67a08587e5d245cc9c553a74a847056bc2976b15"}, @@ -2030,8 +2083,8 @@ guardpost = [ {file = "guardpost-0.0.9.tar.gz", hash = "sha256:4c309d6bb2975e55edb95f7ac5019b9cefeba43736ce71f6275156978aa75fc9"}, ] h11 = [ - {file = "h11-0.13.0-py3-none-any.whl", hash = "sha256:8ddd78563b633ca55346c8cd41ec0af27d3c79931828beffb46ce70a379e7442"}, - {file = "h11-0.13.0.tar.gz", hash = "sha256:70813c1135087a248a4d38cc0e1a0181ffab2188141a93eaf567940c3957ff06"}, + {file = "h11-0.12.0-py3-none-any.whl", hash = "sha256:36a3cb8c0a032f56e2da7084577878a035d3b61d104230d4bd49c0c6b555a9c6"}, + {file = "h11-0.12.0.tar.gz", hash = "sha256:47222cb6067e4a307d535814917cd98fd0a57b6788ce715755fa2b6c28b56042"}, ] h2 = [ {file = "h2-4.1.0-py3-none-any.whl", hash = "sha256:03a46bcf682256c95b5fd9e9a99c1323584c3eec6440d379b9903d709476bc6d"}, @@ -2041,6 +2094,10 @@ hpack = [ {file = "hpack-4.0.0-py3-none-any.whl", hash = "sha256:84a076fad3dc9a9f8063ccb8041ef100867b1878b25ef0ee63847a5d53818a6c"}, {file = "hpack-4.0.0.tar.gz", hash = "sha256:fc41de0c63e687ebffde81187a948221294896f6bdc0ae2312708df339430095"}, ] +httpcore = [ + {file = "httpcore-0.15.0-py3-none-any.whl", hash = "sha256:1105b8b73c025f23ff7c36468e4432226cbb959176eab66864b8e31c4ee27fa6"}, + {file = "httpcore-0.15.0.tar.gz", hash = "sha256:18b68ab86a3ccf3e7dc0f43598eaddcf472b602aba29f9aa6ab85fe2ada3980b"}, +] httptools = [ {file = "httptools-0.4.0-cp310-cp310-macosx_10_9_universal2.whl", hash = "sha256:fcddfe70553be717d9745990dfdb194e22ee0f60eb8f48c0794e7bfeda30d2d5"}, {file = "httptools-0.4.0-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:1ee0b459257e222b878a6c09ccf233957d3a4dcb883b0847640af98d2d9aac23"}, @@ -2077,6 +2134,10 @@ httptools = [ {file = "httptools-0.4.0-cp39-cp39-win_amd64.whl", hash = "sha256:34d2903dd2a3dd85d33705b6fde40bf91fc44411661283763fd0746723963c83"}, {file = "httptools-0.4.0.tar.gz", hash = "sha256:2c9a930c378b3d15d6b695fb95ebcff81a7395b4f9775c4f10a076beb0b2c1ff"}, ] +httpx = [ + {file = "httpx-0.23.0-py3-none-any.whl", hash = "sha256:42974f577483e1e932c3cdc3cd2303e883cbfba17fe228b0f63589764d7b9c4b"}, + {file = "httpx-0.23.0.tar.gz", hash = "sha256:f28eac771ec9eb4866d3fb4ab65abd42d38c424739e80c08d8d20570de60b0ef"}, +] hypercorn = [] hyperframe = [ {file = "hyperframe-6.0.1-py3-none-any.whl", hash = "sha256:0ec6bafd80d8ad2195c4f03aacba3a8265e57bc4cff261e802bf39970ed02a15"}, @@ -2386,8 +2447,8 @@ requests-toolbelt = [ {file = "requests_toolbelt-0.9.1-py2.py3-none-any.whl", hash = "sha256:380606e1d10dc85c3bd47bf5a6095f815ec007be7a8b69c878507068df059e6f"}, ] rfc3986 = [ - {file = "rfc3986-2.0.0-py2.py3-none-any.whl", hash = "sha256:50b1502b60e289cb37883f3dfd34532b8873c7de9f49bb546641ce9cbd256ebd"}, - {file = "rfc3986-2.0.0.tar.gz", hash = "sha256:97aacf9dbd4bfd829baad6e6309fa6573aaf1be3f6fa735c8ab05e46cecb261c"}, + {file = "rfc3986-1.5.0-py2.py3-none-any.whl", hash = "sha256:a86d6e1f5b1dc238b218b012df0aa79409667bb209e58da56d0b94704e712a97"}, + {file = "rfc3986-1.5.0.tar.gz", hash = "sha256:270aaf10d87d0d4e095063c65bf3ddbc6ee3d0b226328ce21e036f946e421835"}, ] rich = [] rodi = [ diff --git a/pyproject.toml b/pyproject.toml index a99747f3d..065aa4921 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -77,6 +77,8 @@ starlette = "*" pydantic = "*" # FastAPI support fastapi = "*" +asgi_lifespan = "*" +httpx = "*" # Aiohttp support aiohttp = "*" # BlackSheep support