]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
✅ Simplify tests for request_form_models (#13183)
authorAlejandra <90076947+alejsdev@users.noreply.github.com>
Sun, 19 Jan 2025 22:39:18 +0000 (22:39 +0000)
committerGitHub <noreply@github.com>
Sun, 19 Jan 2025 22:39:18 +0000 (22:39 +0000)
Co-authored-by: Sofie Van Landeghem <svlandeg@users.noreply.github.com>
tests/test_tutorial/test_request_form_models/test_tutorial001.py
tests/test_tutorial/test_request_form_models/test_tutorial001_an.py [deleted file]
tests/test_tutorial/test_request_form_models/test_tutorial001_an_py39.py [deleted file]
tests/test_tutorial/test_request_form_models/test_tutorial002.py
tests/test_tutorial/test_request_form_models/test_tutorial002_an.py [deleted file]
tests/test_tutorial/test_request_form_models/test_tutorial002_an_py39.py [deleted file]
tests/test_tutorial/test_request_form_models/test_tutorial002_pv1.py
tests/test_tutorial/test_request_form_models/test_tutorial002_pv1_an.py [deleted file]
tests/test_tutorial/test_request_form_models/test_tutorial002_pv1_an_p39.py [deleted file]

index 46c130ee8c1670283bdae93edad28fb5e4e8ebc5..1ca3c96d331aad557381c1d759d0a69dcc38a29c 100644 (file)
@@ -1,13 +1,24 @@
+import importlib
+
 import pytest
 from dirty_equals import IsDict
 from fastapi.testclient import TestClient
 
+from ...utils import needs_py39
+
 
-@pytest.fixture(name="client")
-def get_client():
-    from docs_src.request_form_models.tutorial001 import app
+@pytest.fixture(
+    name="client",
+    params=[
+        "tutorial001",
+        "tutorial001_an",
+        pytest.param("tutorial001_an_py39", marks=needs_py39),
+    ],
+)
+def get_client(request: pytest.FixtureRequest):
+    mod = importlib.import_module(f"docs_src.request_form_models.{request.param}")
 
-    client = TestClient(app)
+    client = TestClient(mod.app)
     return client
 
 
diff --git a/tests/test_tutorial/test_request_form_models/test_tutorial001_an.py b/tests/test_tutorial/test_request_form_models/test_tutorial001_an.py
deleted file mode 100644 (file)
index 4e14d89..0000000
+++ /dev/null
@@ -1,232 +0,0 @@
-import pytest
-from dirty_equals import IsDict
-from fastapi.testclient import TestClient
-
-
-@pytest.fixture(name="client")
-def get_client():
-    from docs_src.request_form_models.tutorial001_an import app
-
-    client = TestClient(app)
-    return client
-
-
-def test_post_body_form(client: TestClient):
-    response = client.post("/login/", data={"username": "Foo", "password": "secret"})
-    assert response.status_code == 200
-    assert response.json() == {"username": "Foo", "password": "secret"}
-
-
-def test_post_body_form_no_password(client: TestClient):
-    response = client.post("/login/", data={"username": "Foo"})
-    assert response.status_code == 422
-    assert response.json() == IsDict(
-        {
-            "detail": [
-                {
-                    "type": "missing",
-                    "loc": ["body", "password"],
-                    "msg": "Field required",
-                    "input": {"username": "Foo"},
-                }
-            ]
-        }
-    ) | IsDict(
-        # TODO: remove when deprecating Pydantic v1
-        {
-            "detail": [
-                {
-                    "loc": ["body", "password"],
-                    "msg": "field required",
-                    "type": "value_error.missing",
-                }
-            ]
-        }
-    )
-
-
-def test_post_body_form_no_username(client: TestClient):
-    response = client.post("/login/", data={"password": "secret"})
-    assert response.status_code == 422
-    assert response.json() == IsDict(
-        {
-            "detail": [
-                {
-                    "type": "missing",
-                    "loc": ["body", "username"],
-                    "msg": "Field required",
-                    "input": {"password": "secret"},
-                }
-            ]
-        }
-    ) | IsDict(
-        # TODO: remove when deprecating Pydantic v1
-        {
-            "detail": [
-                {
-                    "loc": ["body", "username"],
-                    "msg": "field required",
-                    "type": "value_error.missing",
-                }
-            ]
-        }
-    )
-
-
-def test_post_body_form_no_data(client: TestClient):
-    response = client.post("/login/")
-    assert response.status_code == 422
-    assert response.json() == IsDict(
-        {
-            "detail": [
-                {
-                    "type": "missing",
-                    "loc": ["body", "username"],
-                    "msg": "Field required",
-                    "input": {},
-                },
-                {
-                    "type": "missing",
-                    "loc": ["body", "password"],
-                    "msg": "Field required",
-                    "input": {},
-                },
-            ]
-        }
-    ) | IsDict(
-        # TODO: remove when deprecating Pydantic v1
-        {
-            "detail": [
-                {
-                    "loc": ["body", "username"],
-                    "msg": "field required",
-                    "type": "value_error.missing",
-                },
-                {
-                    "loc": ["body", "password"],
-                    "msg": "field required",
-                    "type": "value_error.missing",
-                },
-            ]
-        }
-    )
-
-
-def test_post_body_json(client: TestClient):
-    response = client.post("/login/", json={"username": "Foo", "password": "secret"})
-    assert response.status_code == 422, response.text
-    assert response.json() == IsDict(
-        {
-            "detail": [
-                {
-                    "type": "missing",
-                    "loc": ["body", "username"],
-                    "msg": "Field required",
-                    "input": {},
-                },
-                {
-                    "type": "missing",
-                    "loc": ["body", "password"],
-                    "msg": "Field required",
-                    "input": {},
-                },
-            ]
-        }
-    ) | IsDict(
-        # TODO: remove when deprecating Pydantic v1
-        {
-            "detail": [
-                {
-                    "loc": ["body", "username"],
-                    "msg": "field required",
-                    "type": "value_error.missing",
-                },
-                {
-                    "loc": ["body", "password"],
-                    "msg": "field required",
-                    "type": "value_error.missing",
-                },
-            ]
-        }
-    )
-
-
-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": {
-            "/login/": {
-                "post": {
-                    "responses": {
-                        "200": {
-                            "description": "Successful Response",
-                            "content": {"application/json": {"schema": {}}},
-                        },
-                        "422": {
-                            "description": "Validation Error",
-                            "content": {
-                                "application/json": {
-                                    "schema": {
-                                        "$ref": "#/components/schemas/HTTPValidationError"
-                                    }
-                                }
-                            },
-                        },
-                    },
-                    "summary": "Login",
-                    "operationId": "login_login__post",
-                    "requestBody": {
-                        "content": {
-                            "application/x-www-form-urlencoded": {
-                                "schema": {"$ref": "#/components/schemas/FormData"}
-                            }
-                        },
-                        "required": True,
-                    },
-                }
-            }
-        },
-        "components": {
-            "schemas": {
-                "FormData": {
-                    "properties": {
-                        "username": {"type": "string", "title": "Username"},
-                        "password": {"type": "string", "title": "Password"},
-                    },
-                    "type": "object",
-                    "required": ["username", "password"],
-                    "title": "FormData",
-                },
-                "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_request_form_models/test_tutorial001_an_py39.py b/tests/test_tutorial/test_request_form_models/test_tutorial001_an_py39.py
deleted file mode 100644 (file)
index 2e6426a..0000000
+++ /dev/null
@@ -1,240 +0,0 @@
-import pytest
-from dirty_equals import IsDict
-from fastapi.testclient import TestClient
-
-from tests.utils import needs_py39
-
-
-@pytest.fixture(name="client")
-def get_client():
-    from docs_src.request_form_models.tutorial001_an_py39 import app
-
-    client = TestClient(app)
-    return client
-
-
-@needs_py39
-def test_post_body_form(client: TestClient):
-    response = client.post("/login/", data={"username": "Foo", "password": "secret"})
-    assert response.status_code == 200
-    assert response.json() == {"username": "Foo", "password": "secret"}
-
-
-@needs_py39
-def test_post_body_form_no_password(client: TestClient):
-    response = client.post("/login/", data={"username": "Foo"})
-    assert response.status_code == 422
-    assert response.json() == IsDict(
-        {
-            "detail": [
-                {
-                    "type": "missing",
-                    "loc": ["body", "password"],
-                    "msg": "Field required",
-                    "input": {"username": "Foo"},
-                }
-            ]
-        }
-    ) | IsDict(
-        # TODO: remove when deprecating Pydantic v1
-        {
-            "detail": [
-                {
-                    "loc": ["body", "password"],
-                    "msg": "field required",
-                    "type": "value_error.missing",
-                }
-            ]
-        }
-    )
-
-
-@needs_py39
-def test_post_body_form_no_username(client: TestClient):
-    response = client.post("/login/", data={"password": "secret"})
-    assert response.status_code == 422
-    assert response.json() == IsDict(
-        {
-            "detail": [
-                {
-                    "type": "missing",
-                    "loc": ["body", "username"],
-                    "msg": "Field required",
-                    "input": {"password": "secret"},
-                }
-            ]
-        }
-    ) | IsDict(
-        # TODO: remove when deprecating Pydantic v1
-        {
-            "detail": [
-                {
-                    "loc": ["body", "username"],
-                    "msg": "field required",
-                    "type": "value_error.missing",
-                }
-            ]
-        }
-    )
-
-
-@needs_py39
-def test_post_body_form_no_data(client: TestClient):
-    response = client.post("/login/")
-    assert response.status_code == 422
-    assert response.json() == IsDict(
-        {
-            "detail": [
-                {
-                    "type": "missing",
-                    "loc": ["body", "username"],
-                    "msg": "Field required",
-                    "input": {},
-                },
-                {
-                    "type": "missing",
-                    "loc": ["body", "password"],
-                    "msg": "Field required",
-                    "input": {},
-                },
-            ]
-        }
-    ) | IsDict(
-        # TODO: remove when deprecating Pydantic v1
-        {
-            "detail": [
-                {
-                    "loc": ["body", "username"],
-                    "msg": "field required",
-                    "type": "value_error.missing",
-                },
-                {
-                    "loc": ["body", "password"],
-                    "msg": "field required",
-                    "type": "value_error.missing",
-                },
-            ]
-        }
-    )
-
-
-@needs_py39
-def test_post_body_json(client: TestClient):
-    response = client.post("/login/", json={"username": "Foo", "password": "secret"})
-    assert response.status_code == 422, response.text
-    assert response.json() == IsDict(
-        {
-            "detail": [
-                {
-                    "type": "missing",
-                    "loc": ["body", "username"],
-                    "msg": "Field required",
-                    "input": {},
-                },
-                {
-                    "type": "missing",
-                    "loc": ["body", "password"],
-                    "msg": "Field required",
-                    "input": {},
-                },
-            ]
-        }
-    ) | IsDict(
-        # TODO: remove when deprecating Pydantic v1
-        {
-            "detail": [
-                {
-                    "loc": ["body", "username"],
-                    "msg": "field required",
-                    "type": "value_error.missing",
-                },
-                {
-                    "loc": ["body", "password"],
-                    "msg": "field required",
-                    "type": "value_error.missing",
-                },
-            ]
-        }
-    )
-
-
-@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": {
-            "/login/": {
-                "post": {
-                    "responses": {
-                        "200": {
-                            "description": "Successful Response",
-                            "content": {"application/json": {"schema": {}}},
-                        },
-                        "422": {
-                            "description": "Validation Error",
-                            "content": {
-                                "application/json": {
-                                    "schema": {
-                                        "$ref": "#/components/schemas/HTTPValidationError"
-                                    }
-                                }
-                            },
-                        },
-                    },
-                    "summary": "Login",
-                    "operationId": "login_login__post",
-                    "requestBody": {
-                        "content": {
-                            "application/x-www-form-urlencoded": {
-                                "schema": {"$ref": "#/components/schemas/FormData"}
-                            }
-                        },
-                        "required": True,
-                    },
-                }
-            }
-        },
-        "components": {
-            "schemas": {
-                "FormData": {
-                    "properties": {
-                        "username": {"type": "string", "title": "Username"},
-                        "password": {"type": "string", "title": "Password"},
-                    },
-                    "type": "object",
-                    "required": ["username", "password"],
-                    "title": "FormData",
-                },
-                "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 76f4800016fb89d134033a3be63b64bcd991e0e4..b3f6be63ab96094af9db6bb225f46dd19b81cc49 100644 (file)
@@ -1,14 +1,23 @@
+import importlib
+
 import pytest
 from fastapi.testclient import TestClient
 
-from tests.utils import needs_pydanticv2
+from ...utils import needs_py39, needs_pydanticv2
 
 
-@pytest.fixture(name="client")
-def get_client():
-    from docs_src.request_form_models.tutorial002 import app
+@pytest.fixture(
+    name="client",
+    params=[
+        "tutorial002",
+        "tutorial002_an",
+        pytest.param("tutorial002_an_py39", marks=needs_py39),
+    ],
+)
+def get_client(request: pytest.FixtureRequest):
+    mod = importlib.import_module(f"docs_src.request_form_models.{request.param}")
 
-    client = TestClient(app)
+    client = TestClient(mod.app)
     return client
 
 
diff --git a/tests/test_tutorial/test_request_form_models/test_tutorial002_an.py b/tests/test_tutorial/test_request_form_models/test_tutorial002_an.py
deleted file mode 100644 (file)
index 179b297..0000000
+++ /dev/null
@@ -1,196 +0,0 @@
-import pytest
-from fastapi.testclient import TestClient
-
-from tests.utils import needs_pydanticv2
-
-
-@pytest.fixture(name="client")
-def get_client():
-    from docs_src.request_form_models.tutorial002_an import app
-
-    client = TestClient(app)
-    return client
-
-
-@needs_pydanticv2
-def test_post_body_form(client: TestClient):
-    response = client.post("/login/", data={"username": "Foo", "password": "secret"})
-    assert response.status_code == 200
-    assert response.json() == {"username": "Foo", "password": "secret"}
-
-
-@needs_pydanticv2
-def test_post_body_extra_form(client: TestClient):
-    response = client.post(
-        "/login/", data={"username": "Foo", "password": "secret", "extra": "extra"}
-    )
-    assert response.status_code == 422
-    assert response.json() == {
-        "detail": [
-            {
-                "type": "extra_forbidden",
-                "loc": ["body", "extra"],
-                "msg": "Extra inputs are not permitted",
-                "input": "extra",
-            }
-        ]
-    }
-
-
-@needs_pydanticv2
-def test_post_body_form_no_password(client: TestClient):
-    response = client.post("/login/", data={"username": "Foo"})
-    assert response.status_code == 422
-    assert response.json() == {
-        "detail": [
-            {
-                "type": "missing",
-                "loc": ["body", "password"],
-                "msg": "Field required",
-                "input": {"username": "Foo"},
-            }
-        ]
-    }
-
-
-@needs_pydanticv2
-def test_post_body_form_no_username(client: TestClient):
-    response = client.post("/login/", data={"password": "secret"})
-    assert response.status_code == 422
-    assert response.json() == {
-        "detail": [
-            {
-                "type": "missing",
-                "loc": ["body", "username"],
-                "msg": "Field required",
-                "input": {"password": "secret"},
-            }
-        ]
-    }
-
-
-@needs_pydanticv2
-def test_post_body_form_no_data(client: TestClient):
-    response = client.post("/login/")
-    assert response.status_code == 422
-    assert response.json() == {
-        "detail": [
-            {
-                "type": "missing",
-                "loc": ["body", "username"],
-                "msg": "Field required",
-                "input": {},
-            },
-            {
-                "type": "missing",
-                "loc": ["body", "password"],
-                "msg": "Field required",
-                "input": {},
-            },
-        ]
-    }
-
-
-@needs_pydanticv2
-def test_post_body_json(client: TestClient):
-    response = client.post("/login/", json={"username": "Foo", "password": "secret"})
-    assert response.status_code == 422, response.text
-    assert response.json() == {
-        "detail": [
-            {
-                "type": "missing",
-                "loc": ["body", "username"],
-                "msg": "Field required",
-                "input": {},
-            },
-            {
-                "type": "missing",
-                "loc": ["body", "password"],
-                "msg": "Field required",
-                "input": {},
-            },
-        ]
-    }
-
-
-@needs_pydanticv2
-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": {
-            "/login/": {
-                "post": {
-                    "responses": {
-                        "200": {
-                            "description": "Successful Response",
-                            "content": {"application/json": {"schema": {}}},
-                        },
-                        "422": {
-                            "description": "Validation Error",
-                            "content": {
-                                "application/json": {
-                                    "schema": {
-                                        "$ref": "#/components/schemas/HTTPValidationError"
-                                    }
-                                }
-                            },
-                        },
-                    },
-                    "summary": "Login",
-                    "operationId": "login_login__post",
-                    "requestBody": {
-                        "content": {
-                            "application/x-www-form-urlencoded": {
-                                "schema": {"$ref": "#/components/schemas/FormData"}
-                            }
-                        },
-                        "required": True,
-                    },
-                }
-            }
-        },
-        "components": {
-            "schemas": {
-                "FormData": {
-                    "properties": {
-                        "username": {"type": "string", "title": "Username"},
-                        "password": {"type": "string", "title": "Password"},
-                    },
-                    "additionalProperties": False,
-                    "type": "object",
-                    "required": ["username", "password"],
-                    "title": "FormData",
-                },
-                "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_request_form_models/test_tutorial002_an_py39.py b/tests/test_tutorial/test_request_form_models/test_tutorial002_an_py39.py
deleted file mode 100644 (file)
index 510ad9d..0000000
+++ /dev/null
@@ -1,203 +0,0 @@
-import pytest
-from fastapi.testclient import TestClient
-
-from tests.utils import needs_py39, needs_pydanticv2
-
-
-@pytest.fixture(name="client")
-def get_client():
-    from docs_src.request_form_models.tutorial002_an_py39 import app
-
-    client = TestClient(app)
-    return client
-
-
-@needs_pydanticv2
-@needs_py39
-def test_post_body_form(client: TestClient):
-    response = client.post("/login/", data={"username": "Foo", "password": "secret"})
-    assert response.status_code == 200
-    assert response.json() == {"username": "Foo", "password": "secret"}
-
-
-@needs_pydanticv2
-@needs_py39
-def test_post_body_extra_form(client: TestClient):
-    response = client.post(
-        "/login/", data={"username": "Foo", "password": "secret", "extra": "extra"}
-    )
-    assert response.status_code == 422
-    assert response.json() == {
-        "detail": [
-            {
-                "type": "extra_forbidden",
-                "loc": ["body", "extra"],
-                "msg": "Extra inputs are not permitted",
-                "input": "extra",
-            }
-        ]
-    }
-
-
-@needs_pydanticv2
-@needs_py39
-def test_post_body_form_no_password(client: TestClient):
-    response = client.post("/login/", data={"username": "Foo"})
-    assert response.status_code == 422
-    assert response.json() == {
-        "detail": [
-            {
-                "type": "missing",
-                "loc": ["body", "password"],
-                "msg": "Field required",
-                "input": {"username": "Foo"},
-            }
-        ]
-    }
-
-
-@needs_pydanticv2
-@needs_py39
-def test_post_body_form_no_username(client: TestClient):
-    response = client.post("/login/", data={"password": "secret"})
-    assert response.status_code == 422
-    assert response.json() == {
-        "detail": [
-            {
-                "type": "missing",
-                "loc": ["body", "username"],
-                "msg": "Field required",
-                "input": {"password": "secret"},
-            }
-        ]
-    }
-
-
-@needs_pydanticv2
-@needs_py39
-def test_post_body_form_no_data(client: TestClient):
-    response = client.post("/login/")
-    assert response.status_code == 422
-    assert response.json() == {
-        "detail": [
-            {
-                "type": "missing",
-                "loc": ["body", "username"],
-                "msg": "Field required",
-                "input": {},
-            },
-            {
-                "type": "missing",
-                "loc": ["body", "password"],
-                "msg": "Field required",
-                "input": {},
-            },
-        ]
-    }
-
-
-@needs_pydanticv2
-@needs_py39
-def test_post_body_json(client: TestClient):
-    response = client.post("/login/", json={"username": "Foo", "password": "secret"})
-    assert response.status_code == 422, response.text
-    assert response.json() == {
-        "detail": [
-            {
-                "type": "missing",
-                "loc": ["body", "username"],
-                "msg": "Field required",
-                "input": {},
-            },
-            {
-                "type": "missing",
-                "loc": ["body", "password"],
-                "msg": "Field required",
-                "input": {},
-            },
-        ]
-    }
-
-
-@needs_pydanticv2
-@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": {
-            "/login/": {
-                "post": {
-                    "responses": {
-                        "200": {
-                            "description": "Successful Response",
-                            "content": {"application/json": {"schema": {}}},
-                        },
-                        "422": {
-                            "description": "Validation Error",
-                            "content": {
-                                "application/json": {
-                                    "schema": {
-                                        "$ref": "#/components/schemas/HTTPValidationError"
-                                    }
-                                }
-                            },
-                        },
-                    },
-                    "summary": "Login",
-                    "operationId": "login_login__post",
-                    "requestBody": {
-                        "content": {
-                            "application/x-www-form-urlencoded": {
-                                "schema": {"$ref": "#/components/schemas/FormData"}
-                            }
-                        },
-                        "required": True,
-                    },
-                }
-            }
-        },
-        "components": {
-            "schemas": {
-                "FormData": {
-                    "properties": {
-                        "username": {"type": "string", "title": "Username"},
-                        "password": {"type": "string", "title": "Password"},
-                    },
-                    "additionalProperties": False,
-                    "type": "object",
-                    "required": ["username", "password"],
-                    "title": "FormData",
-                },
-                "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 249b9379df7261479513728a6de69dda1cf66868..b503f23a535da55c4d3b6e235aadbed8fdcf64cd 100644 (file)
@@ -1,17 +1,27 @@
+import importlib
+
 import pytest
 from fastapi.testclient import TestClient
 
-from tests.utils import needs_pydanticv1
+from ...utils import needs_py39, needs_pydanticv1
 
 
-@pytest.fixture(name="client")
-def get_client():
-    from docs_src.request_form_models.tutorial002_pv1 import app
+@pytest.fixture(
+    name="client",
+    params=[
+        "tutorial002_pv1",
+        "tutorial002_pv1_an",
+        pytest.param("tutorial002_pv1_an_py39", marks=needs_py39),
+    ],
+)
+def get_client(request: pytest.FixtureRequest):
+    mod = importlib.import_module(f"docs_src.request_form_models.{request.param}")
 
-    client = TestClient(app)
+    client = TestClient(mod.app)
     return client
 
 
+# TODO: remove when deprecating Pydantic v1
 @needs_pydanticv1
 def test_post_body_form(client: TestClient):
     response = client.post("/login/", data={"username": "Foo", "password": "secret"})
@@ -19,6 +29,7 @@ def test_post_body_form(client: TestClient):
     assert response.json() == {"username": "Foo", "password": "secret"}
 
 
+# TODO: remove when deprecating Pydantic v1
 @needs_pydanticv1
 def test_post_body_extra_form(client: TestClient):
     response = client.post(
@@ -36,6 +47,7 @@ def test_post_body_extra_form(client: TestClient):
     }
 
 
+# TODO: remove when deprecating Pydantic v1
 @needs_pydanticv1
 def test_post_body_form_no_password(client: TestClient):
     response = client.post("/login/", data={"username": "Foo"})
@@ -51,6 +63,7 @@ def test_post_body_form_no_password(client: TestClient):
     }
 
 
+# TODO: remove when deprecating Pydantic v1
 @needs_pydanticv1
 def test_post_body_form_no_username(client: TestClient):
     response = client.post("/login/", data={"password": "secret"})
@@ -66,6 +79,7 @@ def test_post_body_form_no_username(client: TestClient):
     }
 
 
+# TODO: remove when deprecating Pydantic v1
 @needs_pydanticv1
 def test_post_body_form_no_data(client: TestClient):
     response = client.post("/login/")
@@ -86,6 +100,7 @@ def test_post_body_form_no_data(client: TestClient):
     }
 
 
+# TODO: remove when deprecating Pydantic v1
 @needs_pydanticv1
 def test_post_body_json(client: TestClient):
     response = client.post("/login/", json={"username": "Foo", "password": "secret"})
@@ -106,6 +121,7 @@ def test_post_body_json(client: TestClient):
     }
 
 
+# TODO: remove when deprecating Pydantic v1
 @needs_pydanticv1
 def test_openapi_schema(client: TestClient):
     response = client.get("/openapi.json")
diff --git a/tests/test_tutorial/test_request_form_models/test_tutorial002_pv1_an.py b/tests/test_tutorial/test_request_form_models/test_tutorial002_pv1_an.py
deleted file mode 100644 (file)
index 44cb3c3..0000000
+++ /dev/null
@@ -1,196 +0,0 @@
-import pytest
-from fastapi.testclient import TestClient
-
-from tests.utils import needs_pydanticv1
-
-
-@pytest.fixture(name="client")
-def get_client():
-    from docs_src.request_form_models.tutorial002_pv1_an import app
-
-    client = TestClient(app)
-    return client
-
-
-# TODO: remove when deprecating Pydantic v1
-@needs_pydanticv1
-def test_post_body_form(client: TestClient):
-    response = client.post("/login/", data={"username": "Foo", "password": "secret"})
-    assert response.status_code == 200
-    assert response.json() == {"username": "Foo", "password": "secret"}
-
-
-# TODO: remove when deprecating Pydantic v1
-@needs_pydanticv1
-def test_post_body_extra_form(client: TestClient):
-    response = client.post(
-        "/login/", data={"username": "Foo", "password": "secret", "extra": "extra"}
-    )
-    assert response.status_code == 422
-    assert response.json() == {
-        "detail": [
-            {
-                "type": "value_error.extra",
-                "loc": ["body", "extra"],
-                "msg": "extra fields not permitted",
-            }
-        ]
-    }
-
-
-# TODO: remove when deprecating Pydantic v1
-@needs_pydanticv1
-def test_post_body_form_no_password(client: TestClient):
-    response = client.post("/login/", data={"username": "Foo"})
-    assert response.status_code == 422
-    assert response.json() == {
-        "detail": [
-            {
-                "type": "value_error.missing",
-                "loc": ["body", "password"],
-                "msg": "field required",
-            }
-        ]
-    }
-
-
-# TODO: remove when deprecating Pydantic v1
-@needs_pydanticv1
-def test_post_body_form_no_username(client: TestClient):
-    response = client.post("/login/", data={"password": "secret"})
-    assert response.status_code == 422
-    assert response.json() == {
-        "detail": [
-            {
-                "type": "value_error.missing",
-                "loc": ["body", "username"],
-                "msg": "field required",
-            }
-        ]
-    }
-
-
-# TODO: remove when deprecating Pydantic v1
-@needs_pydanticv1
-def test_post_body_form_no_data(client: TestClient):
-    response = client.post("/login/")
-    assert response.status_code == 422
-    assert response.json() == {
-        "detail": [
-            {
-                "type": "value_error.missing",
-                "loc": ["body", "username"],
-                "msg": "field required",
-            },
-            {
-                "type": "value_error.missing",
-                "loc": ["body", "password"],
-                "msg": "field required",
-            },
-        ]
-    }
-
-
-# TODO: remove when deprecating Pydantic v1
-@needs_pydanticv1
-def test_post_body_json(client: TestClient):
-    response = client.post("/login/", json={"username": "Foo", "password": "secret"})
-    assert response.status_code == 422, response.text
-    assert response.json() == {
-        "detail": [
-            {
-                "type": "value_error.missing",
-                "loc": ["body", "username"],
-                "msg": "field required",
-            },
-            {
-                "type": "value_error.missing",
-                "loc": ["body", "password"],
-                "msg": "field required",
-            },
-        ]
-    }
-
-
-# TODO: remove when deprecating Pydantic v1
-@needs_pydanticv1
-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": {
-            "/login/": {
-                "post": {
-                    "responses": {
-                        "200": {
-                            "description": "Successful Response",
-                            "content": {"application/json": {"schema": {}}},
-                        },
-                        "422": {
-                            "description": "Validation Error",
-                            "content": {
-                                "application/json": {
-                                    "schema": {
-                                        "$ref": "#/components/schemas/HTTPValidationError"
-                                    }
-                                }
-                            },
-                        },
-                    },
-                    "summary": "Login",
-                    "operationId": "login_login__post",
-                    "requestBody": {
-                        "content": {
-                            "application/x-www-form-urlencoded": {
-                                "schema": {"$ref": "#/components/schemas/FormData"}
-                            }
-                        },
-                        "required": True,
-                    },
-                }
-            }
-        },
-        "components": {
-            "schemas": {
-                "FormData": {
-                    "properties": {
-                        "username": {"type": "string", "title": "Username"},
-                        "password": {"type": "string", "title": "Password"},
-                    },
-                    "additionalProperties": False,
-                    "type": "object",
-                    "required": ["username", "password"],
-                    "title": "FormData",
-                },
-                "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_request_form_models/test_tutorial002_pv1_an_p39.py b/tests/test_tutorial/test_request_form_models/test_tutorial002_pv1_an_p39.py
deleted file mode 100644 (file)
index 899549e..0000000
+++ /dev/null
@@ -1,203 +0,0 @@
-import pytest
-from fastapi.testclient import TestClient
-
-from tests.utils import needs_py39, needs_pydanticv1
-
-
-@pytest.fixture(name="client")
-def get_client():
-    from docs_src.request_form_models.tutorial002_pv1_an_py39 import app
-
-    client = TestClient(app)
-    return client
-
-
-# TODO: remove when deprecating Pydantic v1
-@needs_pydanticv1
-@needs_py39
-def test_post_body_form(client: TestClient):
-    response = client.post("/login/", data={"username": "Foo", "password": "secret"})
-    assert response.status_code == 200
-    assert response.json() == {"username": "Foo", "password": "secret"}
-
-
-# TODO: remove when deprecating Pydantic v1
-@needs_pydanticv1
-@needs_py39
-def test_post_body_extra_form(client: TestClient):
-    response = client.post(
-        "/login/", data={"username": "Foo", "password": "secret", "extra": "extra"}
-    )
-    assert response.status_code == 422
-    assert response.json() == {
-        "detail": [
-            {
-                "type": "value_error.extra",
-                "loc": ["body", "extra"],
-                "msg": "extra fields not permitted",
-            }
-        ]
-    }
-
-
-# TODO: remove when deprecating Pydantic v1
-@needs_pydanticv1
-@needs_py39
-def test_post_body_form_no_password(client: TestClient):
-    response = client.post("/login/", data={"username": "Foo"})
-    assert response.status_code == 422
-    assert response.json() == {
-        "detail": [
-            {
-                "type": "value_error.missing",
-                "loc": ["body", "password"],
-                "msg": "field required",
-            }
-        ]
-    }
-
-
-# TODO: remove when deprecating Pydantic v1
-@needs_pydanticv1
-@needs_py39
-def test_post_body_form_no_username(client: TestClient):
-    response = client.post("/login/", data={"password": "secret"})
-    assert response.status_code == 422
-    assert response.json() == {
-        "detail": [
-            {
-                "type": "value_error.missing",
-                "loc": ["body", "username"],
-                "msg": "field required",
-            }
-        ]
-    }
-
-
-# TODO: remove when deprecating Pydantic v1
-@needs_pydanticv1
-@needs_py39
-def test_post_body_form_no_data(client: TestClient):
-    response = client.post("/login/")
-    assert response.status_code == 422
-    assert response.json() == {
-        "detail": [
-            {
-                "type": "value_error.missing",
-                "loc": ["body", "username"],
-                "msg": "field required",
-            },
-            {
-                "type": "value_error.missing",
-                "loc": ["body", "password"],
-                "msg": "field required",
-            },
-        ]
-    }
-
-
-# TODO: remove when deprecating Pydantic v1
-@needs_pydanticv1
-@needs_py39
-def test_post_body_json(client: TestClient):
-    response = client.post("/login/", json={"username": "Foo", "password": "secret"})
-    assert response.status_code == 422, response.text
-    assert response.json() == {
-        "detail": [
-            {
-                "type": "value_error.missing",
-                "loc": ["body", "username"],
-                "msg": "field required",
-            },
-            {
-                "type": "value_error.missing",
-                "loc": ["body", "password"],
-                "msg": "field required",
-            },
-        ]
-    }
-
-
-# TODO: remove when deprecating Pydantic v1
-@needs_pydanticv1
-@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": {
-            "/login/": {
-                "post": {
-                    "responses": {
-                        "200": {
-                            "description": "Successful Response",
-                            "content": {"application/json": {"schema": {}}},
-                        },
-                        "422": {
-                            "description": "Validation Error",
-                            "content": {
-                                "application/json": {
-                                    "schema": {
-                                        "$ref": "#/components/schemas/HTTPValidationError"
-                                    }
-                                }
-                            },
-                        },
-                    },
-                    "summary": "Login",
-                    "operationId": "login_login__post",
-                    "requestBody": {
-                        "content": {
-                            "application/x-www-form-urlencoded": {
-                                "schema": {"$ref": "#/components/schemas/FormData"}
-                            }
-                        },
-                        "required": True,
-                    },
-                }
-            }
-        },
-        "components": {
-            "schemas": {
-                "FormData": {
-                    "properties": {
-                        "username": {"type": "string", "title": "Username"},
-                        "password": {"type": "string", "title": "Password"},
-                    },
-                    "additionalProperties": False,
-                    "type": "object",
-                    "required": ["username", "password"],
-                    "title": "FormData",
-                },
-                "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"},
-                        }
-                    },
-                },
-            }
-        },
-    }