]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
✅ Simplify tests for request_model (#13195)
authorAlejandra <90076947+alejsdev@users.noreply.github.com>
Sun, 19 Jan 2025 06:57:50 +0000 (06:57 +0000)
committerGitHub <noreply@github.com>
Sun, 19 Jan 2025 06:57:50 +0000 (06:57 +0000)
tests/test_tutorial/test_response_model/test_tutorial003_01.py
tests/test_tutorial/test_response_model/test_tutorial003_01_py310.py [deleted file]
tests/test_tutorial/test_response_model/test_tutorial003_05.py
tests/test_tutorial/test_response_model/test_tutorial003_05_py310.py [deleted file]
tests/test_tutorial/test_response_model/test_tutorial004.py
tests/test_tutorial/test_response_model/test_tutorial004_py310.py [deleted file]
tests/test_tutorial/test_response_model/test_tutorial004_py39.py [deleted file]
tests/test_tutorial/test_response_model/test_tutorial005.py
tests/test_tutorial/test_response_model/test_tutorial005_py310.py [deleted file]
tests/test_tutorial/test_response_model/test_tutorial006.py
tests/test_tutorial/test_response_model/test_tutorial006_py310.py [deleted file]

index 3a6a0b20d9d20cf0b1d729f0abf9c7eba6816330..3975856b6cf153e767ab364f155ab1ceaa747f69 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_01 import app
+from ...utils import needs_py310
+
+
+@pytest.fixture(
+    name="client",
+    params=[
+        "tutorial003_01",
+        pytest.param("tutorial003_01_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() == {
diff --git a/tests/test_tutorial/test_response_model/test_tutorial003_01_py310.py b/tests/test_tutorial/test_response_model/test_tutorial003_01_py310.py
deleted file mode 100644 (file)
index 6985b9d..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_01_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": {
-                    "summary": "Create User",
-                    "operationId": "create_user_user__post",
-                    "requestBody": {
-                        "content": {
-                            "application/json": {
-                                "schema": {"$ref": "#/components/schemas/UserIn"}
-                            }
-                        },
-                        "required": True,
-                    },
-                    "responses": {
-                        "200": {
-                            "description": "Successful Response",
-                            "content": {
-                                "application/json": {
-                                    "schema": {"$ref": "#/components/schemas/BaseUser"}
-                                }
-                            },
-                        },
-                        "422": {
-                            "description": "Validation Error",
-                            "content": {
-                                "application/json": {
-                                    "schema": {
-                                        "$ref": "#/components/schemas/HTTPValidationError"
-                                    }
-                                }
-                            },
-                        },
-                    },
-                }
-            }
-        },
-        "components": {
-            "schemas": {
-                "BaseUser": {
-                    "title": "BaseUser",
-                    "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"}
-                        ),
-                    },
-                },
-                "HTTPValidationError": {
-                    "title": "HTTPValidationError",
-                    "type": "object",
-                    "properties": {
-                        "detail": {
-                            "title": "Detail",
-                            "type": "array",
-                            "items": {"$ref": "#/components/schemas/ValidationError"},
-                        }
-                    },
-                },
-                "UserIn": {
-                    "title": "UserIn",
-                    "required": ["username", "email", "password"],
-                    "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"}
-                        ),
-                        "password": {"title": "Password", "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"},
-                    },
-                },
-            }
-        },
-    }
index c7a39cc748323924d80a903709e8857ff9aac8eb..9500852e15705243fafa63038ec9aaf4d8032ec1 100644 (file)
@@ -1,23 +1,38 @@
+import importlib
+
+import pytest
 from fastapi.testclient import TestClient
 
-from docs_src.response_model.tutorial003_05 import app
+from ...utils import needs_py310
+
+
+@pytest.fixture(
+    name="client",
+    params=[
+        "tutorial003_05",
+        pytest.param("tutorial003_05_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_get_portal():
+def test_get_portal(client: TestClient):
     response = client.get("/portal")
     assert response.status_code == 200, response.text
     assert response.json() == {"message": "Here's your interdimensional portal."}
 
 
-def test_get_redirect():
+def test_get_redirect(client: TestClient):
     response = client.get("/portal", params={"teleport": True}, follow_redirects=False)
     assert response.status_code == 307, response.text
     assert response.headers["location"] == "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
 
 
-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() == {
diff --git a/tests/test_tutorial/test_response_model/test_tutorial003_05_py310.py b/tests/test_tutorial/test_response_model/test_tutorial003_05_py310.py
deleted file mode 100644 (file)
index f80d625..0000000
+++ /dev/null
@@ -1,103 +0,0 @@
-import pytest
-from fastapi.testclient import TestClient
-
-from ...utils import needs_py310
-
-
-@pytest.fixture(name="client")
-def get_client():
-    from docs_src.response_model.tutorial003_05_py310 import app
-
-    client = TestClient(app)
-    return client
-
-
-@needs_py310
-def test_get_portal(client: TestClient):
-    response = client.get("/portal")
-    assert response.status_code == 200, response.text
-    assert response.json() == {"message": "Here's your interdimensional portal."}
-
-
-@needs_py310
-def test_get_redirect(client: TestClient):
-    response = client.get("/portal", params={"teleport": True}, follow_redirects=False)
-    assert response.status_code == 307, response.text
-    assert response.headers["location"] == "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
-
-
-@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": {
-            "/portal": {
-                "get": {
-                    "summary": "Get Portal",
-                    "operationId": "get_portal_portal_get",
-                    "parameters": [
-                        {
-                            "required": False,
-                            "schema": {
-                                "title": "Teleport",
-                                "type": "boolean",
-                                "default": False,
-                            },
-                            "name": "teleport",
-                            "in": "query",
-                        }
-                    ],
-                    "responses": {
-                        "200": {
-                            "description": "Successful Response",
-                            "content": {"application/json": {"schema": {}}},
-                        },
-                        "422": {
-                            "description": "Validation Error",
-                            "content": {
-                                "application/json": {
-                                    "schema": {
-                                        "$ref": "#/components/schemas/HTTPValidationError"
-                                    }
-                                }
-                            },
-                        },
-                    },
-                }
-            }
-        },
-        "components": {
-            "schemas": {
-                "HTTPValidationError": {
-                    "title": "HTTPValidationError",
-                    "type": "object",
-                    "properties": {
-                        "detail": {
-                            "title": "Detail",
-                            "type": "array",
-                            "items": {"$ref": "#/components/schemas/ValidationError"},
-                        }
-                    },
-                },
-                "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"},
-                    },
-                },
-            }
-        },
-    }
index e9bde18dd58c3cb95ec7a225d0d97cf81a636862..449a52b8138a49c31f7c98f0f36dfa2dc1b67329 100644 (file)
@@ -1,10 +1,25 @@
+import importlib
+
 import pytest
 from dirty_equals import IsDict, IsOneOf
 from fastapi.testclient import TestClient
 
-from docs_src.response_model.tutorial004 import app
+from ...utils import needs_py39, needs_py310
+
+
+@pytest.fixture(
+    name="client",
+    params=[
+        "tutorial004",
+        pytest.param("tutorial004_py39", marks=needs_py39),
+        pytest.param("tutorial004_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
 
 
 @pytest.mark.parametrize(
@@ -27,13 +42,13 @@ client = TestClient(app)
         ),
     ],
 )
-def test_get(url, data):
+def test_get(url, data, client: TestClient):
     response = client.get(url)
     assert response.status_code == 200, response.text
     assert response.json() == data
 
 
-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() == {
diff --git a/tests/test_tutorial/test_response_model/test_tutorial004_py310.py b/tests/test_tutorial/test_response_model/test_tutorial004_py310.py
deleted file mode 100644 (file)
index 6f8a3cb..0000000
+++ /dev/null
@@ -1,147 +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.tutorial004_py310 import app
-
-    client = TestClient(app)
-    return client
-
-
-@needs_py310
-@pytest.mark.parametrize(
-    "url,data",
-    [
-        ("/items/foo", {"name": "Foo", "price": 50.2}),
-        (
-            "/items/bar",
-            {"name": "Bar", "description": "The bartenders", "price": 62, "tax": 20.2},
-        ),
-        (
-            "/items/baz",
-            {
-                "name": "Baz",
-                "description": None,
-                "price": 50.2,
-                "tax": 10.5,
-                "tags": [],
-            },
-        ),
-    ],
-)
-def test_get(url, data, client: TestClient):
-    response = client.get(url)
-    assert response.status_code == 200, response.text
-    assert response.json() == data
-
-
-@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": {
-            "/items/{item_id}": {
-                "get": {
-                    "responses": {
-                        "200": {
-                            "description": "Successful Response",
-                            "content": {
-                                "application/json": {
-                                    "schema": {"$ref": "#/components/schemas/Item"}
-                                }
-                            },
-                        },
-                        "422": {
-                            "description": "Validation Error",
-                            "content": {
-                                "application/json": {
-                                    "schema": {
-                                        "$ref": "#/components/schemas/HTTPValidationError"
-                                    }
-                                }
-                            },
-                        },
-                    },
-                    "summary": "Read Item",
-                    "operationId": "read_item_items__item_id__get",
-                    "parameters": [
-                        {
-                            "required": True,
-                            "schema": {"title": "Item Id", "type": "string"},
-                            "name": "item_id",
-                            "in": "path",
-                        }
-                    ],
-                }
-            }
-        },
-        "components": {
-            "schemas": {
-                "Item": {
-                    "title": "Item",
-                    "required": IsOneOf(
-                        ["name", "description", "price", "tax", "tags"],
-                        # TODO: remove when deprecating Pydantic v1
-                        ["name", "price"],
-                    ),
-                    "type": "object",
-                    "properties": {
-                        "name": {"title": "Name", "type": "string"},
-                        "price": {"title": "Price", "type": "number"},
-                        "description": IsDict(
-                            {
-                                "title": "Description",
-                                "anyOf": [{"type": "string"}, {"type": "null"}],
-                            }
-                        )
-                        | IsDict(
-                            # TODO: remove when deprecating Pydantic v1
-                            {"title": "Description", "type": "string"}
-                        ),
-                        "tax": {"title": "Tax", "type": "number", "default": 10.5},
-                        "tags": {
-                            "title": "Tags",
-                            "type": "array",
-                            "items": {"type": "string"},
-                            "default": [],
-                        },
-                    },
-                },
-                "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"},
-                        }
-                    },
-                },
-            }
-        },
-    }
diff --git a/tests/test_tutorial/test_response_model/test_tutorial004_py39.py b/tests/test_tutorial/test_response_model/test_tutorial004_py39.py
deleted file mode 100644 (file)
index cfaa1eb..0000000
+++ /dev/null
@@ -1,147 +0,0 @@
-import pytest
-from dirty_equals import IsDict, IsOneOf
-from fastapi.testclient import TestClient
-
-from ...utils import needs_py39
-
-
-@pytest.fixture(name="client")
-def get_client():
-    from docs_src.response_model.tutorial004_py39 import app
-
-    client = TestClient(app)
-    return client
-
-
-@needs_py39
-@pytest.mark.parametrize(
-    "url,data",
-    [
-        ("/items/foo", {"name": "Foo", "price": 50.2}),
-        (
-            "/items/bar",
-            {"name": "Bar", "description": "The bartenders", "price": 62, "tax": 20.2},
-        ),
-        (
-            "/items/baz",
-            {
-                "name": "Baz",
-                "description": None,
-                "price": 50.2,
-                "tax": 10.5,
-                "tags": [],
-            },
-        ),
-    ],
-)
-def test_get(url, data, client: TestClient):
-    response = client.get(url)
-    assert response.status_code == 200, response.text
-    assert response.json() == data
-
-
-@needs_py39
-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": {
-            "/items/{item_id}": {
-                "get": {
-                    "responses": {
-                        "200": {
-                            "description": "Successful Response",
-                            "content": {
-                                "application/json": {
-                                    "schema": {"$ref": "#/components/schemas/Item"}
-                                }
-                            },
-                        },
-                        "422": {
-                            "description": "Validation Error",
-                            "content": {
-                                "application/json": {
-                                    "schema": {
-                                        "$ref": "#/components/schemas/HTTPValidationError"
-                                    }
-                                }
-                            },
-                        },
-                    },
-                    "summary": "Read Item",
-                    "operationId": "read_item_items__item_id__get",
-                    "parameters": [
-                        {
-                            "required": True,
-                            "schema": {"title": "Item Id", "type": "string"},
-                            "name": "item_id",
-                            "in": "path",
-                        }
-                    ],
-                }
-            }
-        },
-        "components": {
-            "schemas": {
-                "Item": {
-                    "title": "Item",
-                    "required": IsOneOf(
-                        ["name", "description", "price", "tax", "tags"],
-                        # TODO: remove when deprecating Pydantic v1
-                        ["name", "price"],
-                    ),
-                    "type": "object",
-                    "properties": {
-                        "name": {"title": "Name", "type": "string"},
-                        "price": {"title": "Price", "type": "number"},
-                        "description": IsDict(
-                            {
-                                "title": "Description",
-                                "anyOf": [{"type": "string"}, {"type": "null"}],
-                            }
-                        )
-                        | IsDict(
-                            # TODO: remove when deprecating Pydantic v1
-                            {"title": "Description", "type": "string"}
-                        ),
-                        "tax": {"title": "Tax", "type": "number", "default": 10.5},
-                        "tags": {
-                            "title": "Tags",
-                            "type": "array",
-                            "items": {"type": "string"},
-                            "default": [],
-                        },
-                    },
-                },
-                "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"},
-                        }
-                    },
-                },
-            }
-        },
-    }
index b20864c07ab448cab3761f5cf09748f418d23f88..a633a3fddd6f352b76862c03aaddc56c57dee94d 100644 (file)
@@ -1,18 +1,33 @@
+import importlib
+
+import pytest
 from dirty_equals import IsDict, IsOneOf
 from fastapi.testclient import TestClient
 
-from docs_src.response_model.tutorial005 import app
+from ...utils import needs_py310
+
+
+@pytest.fixture(
+    name="client",
+    params=[
+        "tutorial005",
+        pytest.param("tutorial005_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_read_item_name():
+def test_read_item_name(client: TestClient):
     response = client.get("/items/bar/name")
     assert response.status_code == 200, response.text
     assert response.json() == {"name": "Bar", "description": "The Bar fighters"}
 
 
-def test_read_item_public_data():
+def test_read_item_public_data(client: TestClient):
     response = client.get("/items/bar/public")
     assert response.status_code == 200, response.text
     assert response.json() == {
@@ -22,7 +37,7 @@ def test_read_item_public_data():
     }
 
 
-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() == {
diff --git a/tests/test_tutorial/test_response_model/test_tutorial005_py310.py b/tests/test_tutorial/test_response_model/test_tutorial005_py310.py
deleted file mode 100644 (file)
index de552c8..0000000
+++ /dev/null
@@ -1,166 +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.tutorial005_py310 import app
-
-    client = TestClient(app)
-    return client
-
-
-@needs_py310
-def test_read_item_name(client: TestClient):
-    response = client.get("/items/bar/name")
-    assert response.status_code == 200, response.text
-    assert response.json() == {"name": "Bar", "description": "The Bar fighters"}
-
-
-@needs_py310
-def test_read_item_public_data(client: TestClient):
-    response = client.get("/items/bar/public")
-    assert response.status_code == 200, response.text
-    assert response.json() == {
-        "name": "Bar",
-        "description": "The Bar fighters",
-        "price": 62,
-    }
-
-
-@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": {
-            "/items/{item_id}/name": {
-                "get": {
-                    "responses": {
-                        "200": {
-                            "description": "Successful Response",
-                            "content": {
-                                "application/json": {
-                                    "schema": {"$ref": "#/components/schemas/Item"}
-                                }
-                            },
-                        },
-                        "422": {
-                            "description": "Validation Error",
-                            "content": {
-                                "application/json": {
-                                    "schema": {
-                                        "$ref": "#/components/schemas/HTTPValidationError"
-                                    }
-                                }
-                            },
-                        },
-                    },
-                    "summary": "Read Item Name",
-                    "operationId": "read_item_name_items__item_id__name_get",
-                    "parameters": [
-                        {
-                            "required": True,
-                            "schema": {"title": "Item Id", "type": "string"},
-                            "name": "item_id",
-                            "in": "path",
-                        }
-                    ],
-                }
-            },
-            "/items/{item_id}/public": {
-                "get": {
-                    "responses": {
-                        "200": {
-                            "description": "Successful Response",
-                            "content": {
-                                "application/json": {
-                                    "schema": {"$ref": "#/components/schemas/Item"}
-                                }
-                            },
-                        },
-                        "422": {
-                            "description": "Validation Error",
-                            "content": {
-                                "application/json": {
-                                    "schema": {
-                                        "$ref": "#/components/schemas/HTTPValidationError"
-                                    }
-                                }
-                            },
-                        },
-                    },
-                    "summary": "Read Item Public Data",
-                    "operationId": "read_item_public_data_items__item_id__public_get",
-                    "parameters": [
-                        {
-                            "required": True,
-                            "schema": {"title": "Item Id", "type": "string"},
-                            "name": "item_id",
-                            "in": "path",
-                        }
-                    ],
-                }
-            },
-        },
-        "components": {
-            "schemas": {
-                "Item": {
-                    "title": "Item",
-                    "required": IsOneOf(
-                        ["name", "description", "price", "tax"],
-                        # TODO: remove when deprecating Pydantic v1
-                        ["name", "price"],
-                    ),
-                    "type": "object",
-                    "properties": {
-                        "name": {"title": "Name", "type": "string"},
-                        "price": {"title": "Price", "type": "number"},
-                        "description": IsDict(
-                            {
-                                "title": "Description",
-                                "anyOf": [{"type": "string"}, {"type": "null"}],
-                            }
-                        )
-                        | IsDict(
-                            # TODO: remove when deprecating Pydantic v1
-                            {"title": "Description", "type": "string"}
-                        ),
-                        "tax": {"title": "Tax", "type": "number", "default": 10.5},
-                    },
-                },
-                "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"},
-                        }
-                    },
-                },
-            }
-        },
-    }
index 1e47e2eadb05ee1709f4bbc93dc50243ca350249..863522d1b4ceb0cf19307a8c4ea5088bc30e7b55 100644 (file)
@@ -1,18 +1,33 @@
+import importlib
+
+import pytest
 from dirty_equals import IsDict, IsOneOf
 from fastapi.testclient import TestClient
 
-from docs_src.response_model.tutorial006 import app
+from ...utils import needs_py310
+
+
+@pytest.fixture(
+    name="client",
+    params=[
+        "tutorial006",
+        pytest.param("tutorial006_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_read_item_name():
+def test_read_item_name(client: TestClient):
     response = client.get("/items/bar/name")
     assert response.status_code == 200, response.text
     assert response.json() == {"name": "Bar", "description": "The Bar fighters"}
 
 
-def test_read_item_public_data():
+def test_read_item_public_data(client: TestClient):
     response = client.get("/items/bar/public")
     assert response.status_code == 200, response.text
     assert response.json() == {
@@ -22,7 +37,7 @@ def test_read_item_public_data():
     }
 
 
-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() == {
diff --git a/tests/test_tutorial/test_response_model/test_tutorial006_py310.py b/tests/test_tutorial/test_response_model/test_tutorial006_py310.py
deleted file mode 100644 (file)
index 40058b1..0000000
+++ /dev/null
@@ -1,166 +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.tutorial006_py310 import app
-
-    client = TestClient(app)
-    return client
-
-
-@needs_py310
-def test_read_item_name(client: TestClient):
-    response = client.get("/items/bar/name")
-    assert response.status_code == 200, response.text
-    assert response.json() == {"name": "Bar", "description": "The Bar fighters"}
-
-
-@needs_py310
-def test_read_item_public_data(client: TestClient):
-    response = client.get("/items/bar/public")
-    assert response.status_code == 200, response.text
-    assert response.json() == {
-        "name": "Bar",
-        "description": "The Bar fighters",
-        "price": 62,
-    }
-
-
-@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": {
-            "/items/{item_id}/name": {
-                "get": {
-                    "responses": {
-                        "200": {
-                            "description": "Successful Response",
-                            "content": {
-                                "application/json": {
-                                    "schema": {"$ref": "#/components/schemas/Item"}
-                                }
-                            },
-                        },
-                        "422": {
-                            "description": "Validation Error",
-                            "content": {
-                                "application/json": {
-                                    "schema": {
-                                        "$ref": "#/components/schemas/HTTPValidationError"
-                                    }
-                                }
-                            },
-                        },
-                    },
-                    "summary": "Read Item Name",
-                    "operationId": "read_item_name_items__item_id__name_get",
-                    "parameters": [
-                        {
-                            "required": True,
-                            "schema": {"title": "Item Id", "type": "string"},
-                            "name": "item_id",
-                            "in": "path",
-                        }
-                    ],
-                }
-            },
-            "/items/{item_id}/public": {
-                "get": {
-                    "responses": {
-                        "200": {
-                            "description": "Successful Response",
-                            "content": {
-                                "application/json": {
-                                    "schema": {"$ref": "#/components/schemas/Item"}
-                                }
-                            },
-                        },
-                        "422": {
-                            "description": "Validation Error",
-                            "content": {
-                                "application/json": {
-                                    "schema": {
-                                        "$ref": "#/components/schemas/HTTPValidationError"
-                                    }
-                                }
-                            },
-                        },
-                    },
-                    "summary": "Read Item Public Data",
-                    "operationId": "read_item_public_data_items__item_id__public_get",
-                    "parameters": [
-                        {
-                            "required": True,
-                            "schema": {"title": "Item Id", "type": "string"},
-                            "name": "item_id",
-                            "in": "path",
-                        }
-                    ],
-                }
-            },
-        },
-        "components": {
-            "schemas": {
-                "Item": {
-                    "title": "Item",
-                    "required": IsOneOf(
-                        ["name", "description", "price", "tax"],
-                        # TODO: remove when deprecating Pydantic v1
-                        ["name", "price"],
-                    ),
-                    "type": "object",
-                    "properties": {
-                        "name": {"title": "Name", "type": "string"},
-                        "price": {"title": "Price", "type": "number"},
-                        "description": IsDict(
-                            {
-                                "title": "Description",
-                                "anyOf": [{"type": "string"}, {"type": "null"}],
-                            }
-                        )
-                        | IsDict(
-                            # TODO: remove when deprecating Pydantic v1
-                            {"title": "Description", "type": "string"}
-                        ),
-                        "tax": {"title": "Tax", "type": "number", "default": 10.5},
-                    },
-                },
-                "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"},
-                        }
-                    },
-                },
-            }
-        },
-    }