]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
✅ Simplify tests for separate_openapi_schemas (#13201)
authorAlejandra <90076947+alejsdev@users.noreply.github.com>
Sun, 19 Jan 2025 22:36:49 +0000 (22:36 +0000)
committerGitHub <noreply@github.com>
Sun, 19 Jan 2025 22:36:49 +0000 (22:36 +0000)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
tests/test_tutorial/test_separate_openapi_schemas/test_tutorial001.py
tests/test_tutorial/test_separate_openapi_schemas/test_tutorial001_py310.py [deleted file]
tests/test_tutorial/test_separate_openapi_schemas/test_tutorial001_py39.py [deleted file]
tests/test_tutorial/test_separate_openapi_schemas/test_tutorial002.py
tests/test_tutorial/test_separate_openapi_schemas/test_tutorial002_py310.py [deleted file]
tests/test_tutorial/test_separate_openapi_schemas/test_tutorial002_py39.py [deleted file]

index cdfae9f8c1b679e0925bcf419c2fc4adf654763d..059fb889b3fea1fafdeb6cce1ff7f242326685a2 100644 (file)
@@ -1,14 +1,23 @@
+import importlib
+
 import pytest
 from fastapi.testclient import TestClient
 
-from ...utils import needs_pydanticv2
+from ...utils import needs_py39, needs_py310, needs_pydanticv2
 
 
-@pytest.fixture(name="client")
-def get_client() -> TestClient:
-    from docs_src.separate_openapi_schemas.tutorial001 import app
+@pytest.fixture(
+    name="client",
+    params=[
+        "tutorial001",
+        pytest.param("tutorial001_py310", marks=needs_py310),
+        pytest.param("tutorial001_py39", marks=needs_py39),
+    ],
+)
+def get_client(request: pytest.FixtureRequest) -> TestClient:
+    mod = importlib.import_module(f"docs_src.separate_openapi_schemas.{request.param}")
 
-    client = TestClient(app)
+    client = TestClient(mod.app)
     return client
 
 
diff --git a/tests/test_tutorial/test_separate_openapi_schemas/test_tutorial001_py310.py b/tests/test_tutorial/test_separate_openapi_schemas/test_tutorial001_py310.py
deleted file mode 100644 (file)
index 3b22146..0000000
+++ /dev/null
@@ -1,136 +0,0 @@
-import pytest
-from fastapi.testclient import TestClient
-
-from ...utils import needs_py310, needs_pydanticv2
-
-
-@pytest.fixture(name="client")
-def get_client() -> TestClient:
-    from docs_src.separate_openapi_schemas.tutorial001_py310 import app
-
-    client = TestClient(app)
-    return client
-
-
-@needs_py310
-def test_create_item(client: TestClient) -> None:
-    response = client.post("/items/", json={"name": "Foo"})
-    assert response.status_code == 200, response.text
-    assert response.json() == {"name": "Foo", "description": None}
-
-
-@needs_py310
-def test_read_items(client: TestClient) -> None:
-    response = client.get("/items/")
-    assert response.status_code == 200, response.text
-    assert response.json() == [
-        {
-            "name": "Portal Gun",
-            "description": "Device to travel through the multi-rick-verse",
-        },
-        {"name": "Plumbus", "description": None},
-    ]
-
-
-@needs_py310
-@needs_pydanticv2
-def test_openapi_schema(client: TestClient) -> None:
-    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/": {
-                "get": {
-                    "summary": "Read Items",
-                    "operationId": "read_items_items__get",
-                    "responses": {
-                        "200": {
-                            "description": "Successful Response",
-                            "content": {
-                                "application/json": {
-                                    "schema": {
-                                        "items": {"$ref": "#/components/schemas/Item"},
-                                        "type": "array",
-                                        "title": "Response Read Items Items  Get",
-                                    }
-                                }
-                            },
-                        }
-                    },
-                },
-                "post": {
-                    "summary": "Create Item",
-                    "operationId": "create_item_items__post",
-                    "requestBody": {
-                        "content": {
-                            "application/json": {
-                                "schema": {"$ref": "#/components/schemas/Item"}
-                            }
-                        },
-                        "required": True,
-                    },
-                    "responses": {
-                        "200": {
-                            "description": "Successful Response",
-                            "content": {"application/json": {"schema": {}}},
-                        },
-                        "422": {
-                            "description": "Validation Error",
-                            "content": {
-                                "application/json": {
-                                    "schema": {
-                                        "$ref": "#/components/schemas/HTTPValidationError"
-                                    }
-                                }
-                            },
-                        },
-                    },
-                },
-            }
-        },
-        "components": {
-            "schemas": {
-                "HTTPValidationError": {
-                    "properties": {
-                        "detail": {
-                            "items": {"$ref": "#/components/schemas/ValidationError"},
-                            "type": "array",
-                            "title": "Detail",
-                        }
-                    },
-                    "type": "object",
-                    "title": "HTTPValidationError",
-                },
-                "Item": {
-                    "properties": {
-                        "name": {"type": "string", "title": "Name"},
-                        "description": {
-                            "anyOf": [{"type": "string"}, {"type": "null"}],
-                            "title": "Description",
-                        },
-                    },
-                    "type": "object",
-                    "required": ["name"],
-                    "title": "Item",
-                },
-                "ValidationError": {
-                    "properties": {
-                        "loc": {
-                            "items": {
-                                "anyOf": [{"type": "string"}, {"type": "integer"}]
-                            },
-                            "type": "array",
-                            "title": "Location",
-                        },
-                        "msg": {"type": "string", "title": "Message"},
-                        "type": {"type": "string", "title": "Error Type"},
-                    },
-                    "type": "object",
-                    "required": ["loc", "msg", "type"],
-                    "title": "ValidationError",
-                },
-            }
-        },
-    }
diff --git a/tests/test_tutorial/test_separate_openapi_schemas/test_tutorial001_py39.py b/tests/test_tutorial/test_separate_openapi_schemas/test_tutorial001_py39.py
deleted file mode 100644 (file)
index 991abe8..0000000
+++ /dev/null
@@ -1,136 +0,0 @@
-import pytest
-from fastapi.testclient import TestClient
-
-from ...utils import needs_py39, needs_pydanticv2
-
-
-@pytest.fixture(name="client")
-def get_client() -> TestClient:
-    from docs_src.separate_openapi_schemas.tutorial001_py39 import app
-
-    client = TestClient(app)
-    return client
-
-
-@needs_py39
-def test_create_item(client: TestClient) -> None:
-    response = client.post("/items/", json={"name": "Foo"})
-    assert response.status_code == 200, response.text
-    assert response.json() == {"name": "Foo", "description": None}
-
-
-@needs_py39
-def test_read_items(client: TestClient) -> None:
-    response = client.get("/items/")
-    assert response.status_code == 200, response.text
-    assert response.json() == [
-        {
-            "name": "Portal Gun",
-            "description": "Device to travel through the multi-rick-verse",
-        },
-        {"name": "Plumbus", "description": None},
-    ]
-
-
-@needs_py39
-@needs_pydanticv2
-def test_openapi_schema(client: TestClient) -> None:
-    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/": {
-                "get": {
-                    "summary": "Read Items",
-                    "operationId": "read_items_items__get",
-                    "responses": {
-                        "200": {
-                            "description": "Successful Response",
-                            "content": {
-                                "application/json": {
-                                    "schema": {
-                                        "items": {"$ref": "#/components/schemas/Item"},
-                                        "type": "array",
-                                        "title": "Response Read Items Items  Get",
-                                    }
-                                }
-                            },
-                        }
-                    },
-                },
-                "post": {
-                    "summary": "Create Item",
-                    "operationId": "create_item_items__post",
-                    "requestBody": {
-                        "content": {
-                            "application/json": {
-                                "schema": {"$ref": "#/components/schemas/Item"}
-                            }
-                        },
-                        "required": True,
-                    },
-                    "responses": {
-                        "200": {
-                            "description": "Successful Response",
-                            "content": {"application/json": {"schema": {}}},
-                        },
-                        "422": {
-                            "description": "Validation Error",
-                            "content": {
-                                "application/json": {
-                                    "schema": {
-                                        "$ref": "#/components/schemas/HTTPValidationError"
-                                    }
-                                }
-                            },
-                        },
-                    },
-                },
-            }
-        },
-        "components": {
-            "schemas": {
-                "HTTPValidationError": {
-                    "properties": {
-                        "detail": {
-                            "items": {"$ref": "#/components/schemas/ValidationError"},
-                            "type": "array",
-                            "title": "Detail",
-                        }
-                    },
-                    "type": "object",
-                    "title": "HTTPValidationError",
-                },
-                "Item": {
-                    "properties": {
-                        "name": {"type": "string", "title": "Name"},
-                        "description": {
-                            "anyOf": [{"type": "string"}, {"type": "null"}],
-                            "title": "Description",
-                        },
-                    },
-                    "type": "object",
-                    "required": ["name"],
-                    "title": "Item",
-                },
-                "ValidationError": {
-                    "properties": {
-                        "loc": {
-                            "items": {
-                                "anyOf": [{"type": "string"}, {"type": "integer"}]
-                            },
-                            "type": "array",
-                            "title": "Location",
-                        },
-                        "msg": {"type": "string", "title": "Message"},
-                        "type": {"type": "string", "title": "Error Type"},
-                    },
-                    "type": "object",
-                    "required": ["loc", "msg", "type"],
-                    "title": "ValidationError",
-                },
-            }
-        },
-    }
index d2cf7945b71d77b8aac825e80815114d285d8276..cc9afeab75cb20fd523e1f850c7c1c153a83d125 100644 (file)
@@ -1,14 +1,23 @@
+import importlib
+
 import pytest
 from fastapi.testclient import TestClient
 
-from ...utils import needs_pydanticv2
+from ...utils import needs_py39, needs_py310, needs_pydanticv2
 
 
-@pytest.fixture(name="client")
-def get_client() -> TestClient:
-    from docs_src.separate_openapi_schemas.tutorial002 import app
+@pytest.fixture(
+    name="client",
+    params=[
+        "tutorial002",
+        pytest.param("tutorial002_py310", marks=needs_py310),
+        pytest.param("tutorial002_py39", marks=needs_py39),
+    ],
+)
+def get_client(request: pytest.FixtureRequest) -> TestClient:
+    mod = importlib.import_module(f"docs_src.separate_openapi_schemas.{request.param}")
 
-    client = TestClient(app)
+    client = TestClient(mod.app)
     return client
 
 
diff --git a/tests/test_tutorial/test_separate_openapi_schemas/test_tutorial002_py310.py b/tests/test_tutorial/test_separate_openapi_schemas/test_tutorial002_py310.py
deleted file mode 100644 (file)
index 89c9ce9..0000000
+++ /dev/null
@@ -1,136 +0,0 @@
-import pytest
-from fastapi.testclient import TestClient
-
-from ...utils import needs_py310, needs_pydanticv2
-
-
-@pytest.fixture(name="client")
-def get_client() -> TestClient:
-    from docs_src.separate_openapi_schemas.tutorial002_py310 import app
-
-    client = TestClient(app)
-    return client
-
-
-@needs_py310
-def test_create_item(client: TestClient) -> None:
-    response = client.post("/items/", json={"name": "Foo"})
-    assert response.status_code == 200, response.text
-    assert response.json() == {"name": "Foo", "description": None}
-
-
-@needs_py310
-def test_read_items(client: TestClient) -> None:
-    response = client.get("/items/")
-    assert response.status_code == 200, response.text
-    assert response.json() == [
-        {
-            "name": "Portal Gun",
-            "description": "Device to travel through the multi-rick-verse",
-        },
-        {"name": "Plumbus", "description": None},
-    ]
-
-
-@needs_py310
-@needs_pydanticv2
-def test_openapi_schema(client: TestClient) -> None:
-    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/": {
-                "get": {
-                    "summary": "Read Items",
-                    "operationId": "read_items_items__get",
-                    "responses": {
-                        "200": {
-                            "description": "Successful Response",
-                            "content": {
-                                "application/json": {
-                                    "schema": {
-                                        "items": {"$ref": "#/components/schemas/Item"},
-                                        "type": "array",
-                                        "title": "Response Read Items Items  Get",
-                                    }
-                                }
-                            },
-                        }
-                    },
-                },
-                "post": {
-                    "summary": "Create Item",
-                    "operationId": "create_item_items__post",
-                    "requestBody": {
-                        "content": {
-                            "application/json": {
-                                "schema": {"$ref": "#/components/schemas/Item"}
-                            }
-                        },
-                        "required": True,
-                    },
-                    "responses": {
-                        "200": {
-                            "description": "Successful Response",
-                            "content": {"application/json": {"schema": {}}},
-                        },
-                        "422": {
-                            "description": "Validation Error",
-                            "content": {
-                                "application/json": {
-                                    "schema": {
-                                        "$ref": "#/components/schemas/HTTPValidationError"
-                                    }
-                                }
-                            },
-                        },
-                    },
-                },
-            }
-        },
-        "components": {
-            "schemas": {
-                "HTTPValidationError": {
-                    "properties": {
-                        "detail": {
-                            "items": {"$ref": "#/components/schemas/ValidationError"},
-                            "type": "array",
-                            "title": "Detail",
-                        }
-                    },
-                    "type": "object",
-                    "title": "HTTPValidationError",
-                },
-                "Item": {
-                    "properties": {
-                        "name": {"type": "string", "title": "Name"},
-                        "description": {
-                            "anyOf": [{"type": "string"}, {"type": "null"}],
-                            "title": "Description",
-                        },
-                    },
-                    "type": "object",
-                    "required": ["name"],
-                    "title": "Item",
-                },
-                "ValidationError": {
-                    "properties": {
-                        "loc": {
-                            "items": {
-                                "anyOf": [{"type": "string"}, {"type": "integer"}]
-                            },
-                            "type": "array",
-                            "title": "Location",
-                        },
-                        "msg": {"type": "string", "title": "Message"},
-                        "type": {"type": "string", "title": "Error Type"},
-                    },
-                    "type": "object",
-                    "required": ["loc", "msg", "type"],
-                    "title": "ValidationError",
-                },
-            }
-        },
-    }
diff --git a/tests/test_tutorial/test_separate_openapi_schemas/test_tutorial002_py39.py b/tests/test_tutorial/test_separate_openapi_schemas/test_tutorial002_py39.py
deleted file mode 100644 (file)
index 6ac3d8f..0000000
+++ /dev/null
@@ -1,136 +0,0 @@
-import pytest
-from fastapi.testclient import TestClient
-
-from ...utils import needs_py39, needs_pydanticv2
-
-
-@pytest.fixture(name="client")
-def get_client() -> TestClient:
-    from docs_src.separate_openapi_schemas.tutorial002_py39 import app
-
-    client = TestClient(app)
-    return client
-
-
-@needs_py39
-def test_create_item(client: TestClient) -> None:
-    response = client.post("/items/", json={"name": "Foo"})
-    assert response.status_code == 200, response.text
-    assert response.json() == {"name": "Foo", "description": None}
-
-
-@needs_py39
-def test_read_items(client: TestClient) -> None:
-    response = client.get("/items/")
-    assert response.status_code == 200, response.text
-    assert response.json() == [
-        {
-            "name": "Portal Gun",
-            "description": "Device to travel through the multi-rick-verse",
-        },
-        {"name": "Plumbus", "description": None},
-    ]
-
-
-@needs_py39
-@needs_pydanticv2
-def test_openapi_schema(client: TestClient) -> None:
-    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/": {
-                "get": {
-                    "summary": "Read Items",
-                    "operationId": "read_items_items__get",
-                    "responses": {
-                        "200": {
-                            "description": "Successful Response",
-                            "content": {
-                                "application/json": {
-                                    "schema": {
-                                        "items": {"$ref": "#/components/schemas/Item"},
-                                        "type": "array",
-                                        "title": "Response Read Items Items  Get",
-                                    }
-                                }
-                            },
-                        }
-                    },
-                },
-                "post": {
-                    "summary": "Create Item",
-                    "operationId": "create_item_items__post",
-                    "requestBody": {
-                        "content": {
-                            "application/json": {
-                                "schema": {"$ref": "#/components/schemas/Item"}
-                            }
-                        },
-                        "required": True,
-                    },
-                    "responses": {
-                        "200": {
-                            "description": "Successful Response",
-                            "content": {"application/json": {"schema": {}}},
-                        },
-                        "422": {
-                            "description": "Validation Error",
-                            "content": {
-                                "application/json": {
-                                    "schema": {
-                                        "$ref": "#/components/schemas/HTTPValidationError"
-                                    }
-                                }
-                            },
-                        },
-                    },
-                },
-            }
-        },
-        "components": {
-            "schemas": {
-                "HTTPValidationError": {
-                    "properties": {
-                        "detail": {
-                            "items": {"$ref": "#/components/schemas/ValidationError"},
-                            "type": "array",
-                            "title": "Detail",
-                        }
-                    },
-                    "type": "object",
-                    "title": "HTTPValidationError",
-                },
-                "Item": {
-                    "properties": {
-                        "name": {"type": "string", "title": "Name"},
-                        "description": {
-                            "anyOf": [{"type": "string"}, {"type": "null"}],
-                            "title": "Description",
-                        },
-                    },
-                    "type": "object",
-                    "required": ["name"],
-                    "title": "Item",
-                },
-                "ValidationError": {
-                    "properties": {
-                        "loc": {
-                            "items": {
-                                "anyOf": [{"type": "string"}, {"type": "integer"}]
-                            },
-                            "type": "array",
-                            "title": "Location",
-                        },
-                        "msg": {"type": "string", "title": "Message"},
-                        "type": {"type": "string", "title": "Error Type"},
-                    },
-                    "type": "object",
-                    "required": ["loc", "msg", "type"],
-                    "title": "ValidationError",
-                },
-            }
-        },
-    }