]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
✅ Simplify tests for response_model (#14062)
author山崎ヒカル <fermat.little.theorem@gmail.com>
Sat, 20 Sep 2025 16:26:21 +0000 (00:26 +0800)
committerGitHub <noreply@github.com>
Sat, 20 Sep 2025 16:26:21 +0000 (18:26 +0200)
tests/test_tutorial/test_response_model/test_tutorial003.py
tests/test_tutorial/test_response_model/test_tutorial003_04.py
tests/test_tutorial/test_response_model/test_tutorial003_04_py310.py [deleted file]
tests/test_tutorial/test_response_model/test_tutorial003_py310.py [deleted file]

index 384c8e0f146d0ca04ef66f18dc0cfda8b25ebed1..70cfd6e4cc2a30276c8e6e74d28bac0664bc6a00 100644 (file)
@@ -1,12 +1,27 @@
+import importlib
+
+import pytest
 from dirty_equals import IsDict, IsOneOf
 from fastapi.testclient import TestClient
 
-from docs_src.response_model.tutorial003 import app
+from ...utils import needs_py310
+
+
+@pytest.fixture(
+    name="client",
+    params=[
+        "tutorial003",
+        pytest.param("tutorial003_py310", marks=needs_py310),
+    ],
+)
+def get_client(request: pytest.FixtureRequest):
+    mod = importlib.import_module(f"docs_src.response_model.{request.param}")
 
-client = TestClient(app)
+    client = TestClient(mod.app)
+    return client
 
 
-def test_post_user():
+def test_post_user(client: TestClient):
     response = client.post(
         "/user/",
         json={
@@ -24,7 +39,7 @@ def test_post_user():
     }
 
 
-def test_openapi_schema():
+def test_openapi_schema(client: TestClient):
     response = client.get("/openapi.json")
     assert response.status_code == 200, response.text
     assert response.json() == {
index 4aa80145a5d5ebb81147183aa48be35b09f6cb93..f32e93ddcb8cc85c5e726af17092954bf67c8575 100644 (file)
@@ -1,9 +1,18 @@
+import importlib
+
 import pytest
 from fastapi.exceptions import FastAPIError
 
+from ...utils import needs_py310
 
-def test_invalid_response_model():
-    with pytest.raises(FastAPIError):
-        from docs_src.response_model.tutorial003_04 import app
 
-        assert app  # pragma: no cover
+@pytest.mark.parametrize(
+    "module_name",
+    [
+        "tutorial003_04",
+        pytest.param("tutorial003_04_py310", marks=needs_py310),
+    ],
+)
+def test_invalid_response_model(module_name: str) -> None:
+    with pytest.raises(FastAPIError):
+        importlib.import_module(f"docs_src.response_model.{module_name}")
diff --git a/tests/test_tutorial/test_response_model/test_tutorial003_04_py310.py b/tests/test_tutorial/test_response_model/test_tutorial003_04_py310.py
deleted file mode 100644 (file)
index b876fac..0000000
+++ /dev/null
@@ -1,12 +0,0 @@
-import pytest
-from fastapi.exceptions import FastAPIError
-
-from ...utils import needs_py310
-
-
-@needs_py310
-def test_invalid_response_model():
-    with pytest.raises(FastAPIError):
-        from docs_src.response_model.tutorial003_04_py310 import app
-
-        assert app  # pragma: no cover
diff --git a/tests/test_tutorial/test_response_model/test_tutorial003_py310.py b/tests/test_tutorial/test_response_model/test_tutorial003_py310.py
deleted file mode 100644 (file)
index 3a3aee3..0000000
+++ /dev/null
@@ -1,160 +0,0 @@
-import pytest
-from dirty_equals import IsDict, IsOneOf
-from fastapi.testclient import TestClient
-
-from ...utils import needs_py310
-
-
-@pytest.fixture(name="client")
-def get_client():
-    from docs_src.response_model.tutorial003_py310 import app
-
-    client = TestClient(app)
-    return client
-
-
-@needs_py310
-def test_post_user(client: TestClient):
-    response = client.post(
-        "/user/",
-        json={
-            "username": "foo",
-            "password": "fighter",
-            "email": "foo@example.com",
-            "full_name": "Grave Dohl",
-        },
-    )
-    assert response.status_code == 200, response.text
-    assert response.json() == {
-        "username": "foo",
-        "email": "foo@example.com",
-        "full_name": "Grave Dohl",
-    }
-
-
-@needs_py310
-def test_openapi_schema(client: TestClient):
-    response = client.get("/openapi.json")
-    assert response.status_code == 200, response.text
-    assert response.json() == {
-        "openapi": "3.1.0",
-        "info": {"title": "FastAPI", "version": "0.1.0"},
-        "paths": {
-            "/user/": {
-                "post": {
-                    "responses": {
-                        "200": {
-                            "description": "Successful Response",
-                            "content": {
-                                "application/json": {
-                                    "schema": {"$ref": "#/components/schemas/UserOut"}
-                                }
-                            },
-                        },
-                        "422": {
-                            "description": "Validation Error",
-                            "content": {
-                                "application/json": {
-                                    "schema": {
-                                        "$ref": "#/components/schemas/HTTPValidationError"
-                                    }
-                                }
-                            },
-                        },
-                    },
-                    "summary": "Create User",
-                    "operationId": "create_user_user__post",
-                    "requestBody": {
-                        "content": {
-                            "application/json": {
-                                "schema": {"$ref": "#/components/schemas/UserIn"}
-                            }
-                        },
-                        "required": True,
-                    },
-                }
-            }
-        },
-        "components": {
-            "schemas": {
-                "UserOut": {
-                    "title": "UserOut",
-                    "required": IsOneOf(
-                        ["username", "email", "full_name"],
-                        # TODO: remove when deprecating Pydantic v1
-                        ["username", "email"],
-                    ),
-                    "type": "object",
-                    "properties": {
-                        "username": {"title": "Username", "type": "string"},
-                        "email": {
-                            "title": "Email",
-                            "type": "string",
-                            "format": "email",
-                        },
-                        "full_name": IsDict(
-                            {
-                                "title": "Full Name",
-                                "anyOf": [{"type": "string"}, {"type": "null"}],
-                            }
-                        )
-                        | IsDict(
-                            # TODO: remove when deprecating Pydantic v1
-                            {"title": "Full Name", "type": "string"}
-                        ),
-                    },
-                },
-                "UserIn": {
-                    "title": "UserIn",
-                    "required": ["username", "password", "email"],
-                    "type": "object",
-                    "properties": {
-                        "username": {"title": "Username", "type": "string"},
-                        "password": {"title": "Password", "type": "string"},
-                        "email": {
-                            "title": "Email",
-                            "type": "string",
-                            "format": "email",
-                        },
-                        "full_name": IsDict(
-                            {
-                                "title": "Full Name",
-                                "anyOf": [{"type": "string"}, {"type": "null"}],
-                            }
-                        )
-                        | IsDict(
-                            # TODO: remove when deprecating Pydantic v1
-                            {"title": "Full Name", "type": "string"}
-                        ),
-                    },
-                },
-                "ValidationError": {
-                    "title": "ValidationError",
-                    "required": ["loc", "msg", "type"],
-                    "type": "object",
-                    "properties": {
-                        "loc": {
-                            "title": "Location",
-                            "type": "array",
-                            "items": {
-                                "anyOf": [{"type": "string"}, {"type": "integer"}]
-                            },
-                        },
-                        "msg": {"title": "Message", "type": "string"},
-                        "type": {"title": "Error Type", "type": "string"},
-                    },
-                },
-                "HTTPValidationError": {
-                    "title": "HTTPValidationError",
-                    "type": "object",
-                    "properties": {
-                        "detail": {
-                            "title": "Detail",
-                            "type": "array",
-                            "items": {"$ref": "#/components/schemas/ValidationError"},
-                        }
-                    },
-                },
-            }
-        },
-    }