]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
✅ Simplify tests for extra_models (#13178)
authorAlejandra <90076947+alejsdev@users.noreply.github.com>
Sun, 19 Jan 2025 06:29:33 +0000 (06:29 +0000)
committerGitHub <noreply@github.com>
Sun, 19 Jan 2025 06:29:33 +0000 (06:29 +0000)
tests/test_tutorial/test_extra_models/test_tutorial003.py
tests/test_tutorial/test_extra_models/test_tutorial003_py310.py [deleted file]
tests/test_tutorial/test_extra_models/test_tutorial004.py
tests/test_tutorial/test_extra_models/test_tutorial004_py39.py [deleted file]
tests/test_tutorial/test_extra_models/test_tutorial005.py
tests/test_tutorial/test_extra_models/test_tutorial005_py39.py [deleted file]

index 0ccb99948f38170b2091396a406e7ff1585ec204..73aa299039895e44e5e98df47a90321618530667 100644 (file)
@@ -1,12 +1,27 @@
+import importlib
+
+import pytest
 from dirty_equals import IsOneOf
 from fastapi.testclient import TestClient
 
-from docs_src.extra_models.tutorial003 import app
+from ...utils import needs_py310
+
+
+@pytest.fixture(
+    name="client",
+    params=[
+        "tutorial003",
+        pytest.param("tutorial003_py310", marks=needs_py310),
+    ],
+)
+def get_client(request: pytest.FixtureRequest):
+    mod = importlib.import_module(f"docs_src.extra_models.{request.param}")
 
-client = TestClient(app)
+    client = TestClient(mod.app)
+    return client
 
 
-def test_get_car():
+def test_get_car(client: TestClient):
     response = client.get("/items/item1")
     assert response.status_code == 200, response.text
     assert response.json() == {
@@ -15,7 +30,7 @@ def test_get_car():
     }
 
 
-def test_get_plane():
+def test_get_plane(client: TestClient):
     response = client.get("/items/item2")
     assert response.status_code == 200, response.text
     assert response.json() == {
@@ -25,7 +40,7 @@ def test_get_plane():
     }
 
 
-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_extra_models/test_tutorial003_py310.py b/tests/test_tutorial/test_extra_models/test_tutorial003_py310.py
deleted file mode 100644 (file)
index b2fe65f..0000000
+++ /dev/null
@@ -1,144 +0,0 @@
-import pytest
-from dirty_equals import IsOneOf
-from fastapi.testclient import TestClient
-
-from ...utils import needs_py310
-
-
-@pytest.fixture(name="client")
-def get_client():
-    from docs_src.extra_models.tutorial003_py310 import app
-
-    client = TestClient(app)
-    return client
-
-
-@needs_py310
-def test_get_car(client: TestClient):
-    response = client.get("/items/item1")
-    assert response.status_code == 200, response.text
-    assert response.json() == {
-        "description": "All my friends drive a low rider",
-        "type": "car",
-    }
-
-
-@needs_py310
-def test_get_plane(client: TestClient):
-    response = client.get("/items/item2")
-    assert response.status_code == 200, response.text
-    assert response.json() == {
-        "description": "Music is my aeroplane, it's my aeroplane",
-        "type": "plane",
-        "size": 5,
-    }
-
-
-@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": {
-                                        "title": "Response Read Item Items  Item Id  Get",
-                                        "anyOf": [
-                                            {"$ref": "#/components/schemas/PlaneItem"},
-                                            {"$ref": "#/components/schemas/CarItem"},
-                                        ],
-                                    }
-                                }
-                            },
-                        },
-                        "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": {
-                "PlaneItem": {
-                    "title": "PlaneItem",
-                    "required": IsOneOf(
-                        ["description", "type", "size"],
-                        # TODO: remove when deprecating Pydantic v1
-                        ["description", "size"],
-                    ),
-                    "type": "object",
-                    "properties": {
-                        "description": {"title": "Description", "type": "string"},
-                        "type": {"title": "Type", "type": "string", "default": "plane"},
-                        "size": {"title": "Size", "type": "integer"},
-                    },
-                },
-                "CarItem": {
-                    "title": "CarItem",
-                    "required": IsOneOf(
-                        ["description", "type"],
-                        # TODO: remove when deprecating Pydantic v1
-                        ["description"],
-                    ),
-                    "type": "object",
-                    "properties": {
-                        "description": {"title": "Description", "type": "string"},
-                        "type": {"title": "Type", "type": "string", "default": "car"},
-                    },
-                },
-                "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 71f6a8c700b35508aa7bc7cec45c72c05f8b8a56..7628db30c7d9430ea0f4d882d94b7680a177736d 100644 (file)
@@ -1,11 +1,26 @@
+import importlib
+
+import pytest
 from fastapi.testclient import TestClient
 
-from docs_src.extra_models.tutorial004 import app
+from ...utils import needs_py39
+
+
+@pytest.fixture(
+    name="client",
+    params=[
+        "tutorial004",
+        pytest.param("tutorial004_py39", marks=needs_py39),
+    ],
+)
+def get_client(request: pytest.FixtureRequest):
+    mod = importlib.import_module(f"docs_src.extra_models.{request.param}")
 
-client = TestClient(app)
+    client = TestClient(mod.app)
+    return client
 
 
-def test_get_items():
+def test_get_items(client: TestClient):
     response = client.get("/items/")
     assert response.status_code == 200, response.text
     assert response.json() == [
@@ -14,7 +29,7 @@ def test_get_items():
     ]
 
 
-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_extra_models/test_tutorial004_py39.py b/tests/test_tutorial/test_extra_models/test_tutorial004_py39.py
deleted file mode 100644 (file)
index 5475b92..0000000
+++ /dev/null
@@ -1,67 +0,0 @@
-import pytest
-from fastapi.testclient import TestClient
-
-from ...utils import needs_py39
-
-
-@pytest.fixture(name="client")
-def get_client():
-    from docs_src.extra_models.tutorial004_py39 import app
-
-    client = TestClient(app)
-    return client
-
-
-@needs_py39
-def test_get_items(client: TestClient):
-    response = client.get("/items/")
-    assert response.status_code == 200, response.text
-    assert response.json() == [
-        {"name": "Foo", "description": "There comes my hero"},
-        {"name": "Red", "description": "It's my aeroplane"},
-    ]
-
-
-@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/": {
-                "get": {
-                    "responses": {
-                        "200": {
-                            "description": "Successful Response",
-                            "content": {
-                                "application/json": {
-                                    "schema": {
-                                        "title": "Response Read Items Items  Get",
-                                        "type": "array",
-                                        "items": {"$ref": "#/components/schemas/Item"},
-                                    }
-                                }
-                            },
-                        }
-                    },
-                    "summary": "Read Items",
-                    "operationId": "read_items_items__get",
-                }
-            }
-        },
-        "components": {
-            "schemas": {
-                "Item": {
-                    "title": "Item",
-                    "required": ["name", "description"],
-                    "type": "object",
-                    "properties": {
-                        "name": {"title": "Name", "type": "string"},
-                        "description": {"title": "Description", "type": "string"},
-                    },
-                }
-            }
-        },
-    }
index b0861c37f7dff322d6db46504cdaf959209d406f..553e4423852c8608d28644ca296a62601f11b12b 100644 (file)
@@ -1,17 +1,32 @@
+import importlib
+
+import pytest
 from fastapi.testclient import TestClient
 
-from docs_src.extra_models.tutorial005 import app
+from ...utils import needs_py39
+
+
+@pytest.fixture(
+    name="client",
+    params=[
+        "tutorial005",
+        pytest.param("tutorial005_py39", marks=needs_py39),
+    ],
+)
+def get_client(request: pytest.FixtureRequest):
+    mod = importlib.import_module(f"docs_src.extra_models.{request.param}")
 
-client = TestClient(app)
+    client = TestClient(mod.app)
+    return client
 
 
-def test_get_items():
+def test_get_items(client: TestClient):
     response = client.get("/keyword-weights/")
     assert response.status_code == 200, response.text
     assert response.json() == {"foo": 2.3, "bar": 3.4}
 
 
-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_extra_models/test_tutorial005_py39.py b/tests/test_tutorial/test_extra_models/test_tutorial005_py39.py
deleted file mode 100644 (file)
index 7278e93..0000000
+++ /dev/null
@@ -1,51 +0,0 @@
-import pytest
-from fastapi.testclient import TestClient
-
-from ...utils import needs_py39
-
-
-@pytest.fixture(name="client")
-def get_client():
-    from docs_src.extra_models.tutorial005_py39 import app
-
-    client = TestClient(app)
-    return client
-
-
-@needs_py39
-def test_get_items(client: TestClient):
-    response = client.get("/keyword-weights/")
-    assert response.status_code == 200, response.text
-    assert response.json() == {"foo": 2.3, "bar": 3.4}
-
-
-@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": {
-            "/keyword-weights/": {
-                "get": {
-                    "responses": {
-                        "200": {
-                            "description": "Successful Response",
-                            "content": {
-                                "application/json": {
-                                    "schema": {
-                                        "title": "Response Read Keyword Weights Keyword Weights  Get",
-                                        "type": "object",
-                                        "additionalProperties": {"type": "number"},
-                                    }
-                                }
-                            },
-                        }
-                    },
-                    "summary": "Read Keyword Weights",
-                    "operationId": "read_keyword_weights_keyword_weights__get",
-                }
-            }
-        },
-    }