]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
✅ Simplify tests for body_nested_models (#13171)
authorAlejandra <90076947+alejsdev@users.noreply.github.com>
Sun, 19 Jan 2025 06:20:41 +0000 (06:20 +0000)
committerGitHub <noreply@github.com>
Sun, 19 Jan 2025 06:20:41 +0000 (06:20 +0000)
tests/test_tutorial/test_body_nested_models/test_tutorial009.py
tests/test_tutorial/test_body_nested_models/test_tutorial009_py39.py [deleted file]

index 762073aea6a70d674a17200b95ff811436133c05..38ba3c88751ec73825e0fc70a68459cb14d8df32 100644 (file)
@@ -1,13 +1,23 @@
+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.body_nested_models.tutorial009 import app
+@pytest.fixture(
+    name="client",
+    params=[
+        "tutorial009",
+        pytest.param("tutorial009_py39", marks=needs_py39),
+    ],
+)
+def get_client(request: pytest.FixtureRequest):
+    mod = importlib.import_module(f"docs_src.body_nested_models.{request.param}")
 
-    client = TestClient(app)
+    client = TestClient(mod.app)
     return client
 
 
diff --git a/tests/test_tutorial/test_body_nested_models/test_tutorial009_py39.py b/tests/test_tutorial/test_body_nested_models/test_tutorial009_py39.py
deleted file mode 100644 (file)
index 24623ce..0000000
+++ /dev/null
@@ -1,128 +0,0 @@
-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.body_nested_models.tutorial009_py39 import app
-
-    client = TestClient(app)
-    return client
-
-
-@needs_py39
-def test_post_body(client: TestClient):
-    data = {"2": 2.2, "3": 3.3}
-    response = client.post("/index-weights/", json=data)
-    assert response.status_code == 200, response.text
-    assert response.json() == data
-
-
-@needs_py39
-def test_post_invalid_body(client: TestClient):
-    data = {"foo": 2.2, "3": 3.3}
-    response = client.post("/index-weights/", json=data)
-    assert response.status_code == 422, response.text
-    assert response.json() == IsDict(
-        {
-            "detail": [
-                {
-                    "type": "int_parsing",
-                    "loc": ["body", "foo", "[key]"],
-                    "msg": "Input should be a valid integer, unable to parse string as an integer",
-                    "input": "foo",
-                }
-            ]
-        }
-    ) | IsDict(
-        # TODO: remove when deprecating Pydantic v1
-        {
-            "detail": [
-                {
-                    "loc": ["body", "__key__"],
-                    "msg": "value is not a valid integer",
-                    "type": "type_error.integer",
-                }
-            ]
-        }
-    )
-
-
-@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": {
-            "/index-weights/": {
-                "post": {
-                    "responses": {
-                        "200": {
-                            "description": "Successful Response",
-                            "content": {"application/json": {"schema": {}}},
-                        },
-                        "422": {
-                            "description": "Validation Error",
-                            "content": {
-                                "application/json": {
-                                    "schema": {
-                                        "$ref": "#/components/schemas/HTTPValidationError"
-                                    }
-                                }
-                            },
-                        },
-                    },
-                    "summary": "Create Index Weights",
-                    "operationId": "create_index_weights_index_weights__post",
-                    "requestBody": {
-                        "content": {
-                            "application/json": {
-                                "schema": {
-                                    "title": "Weights",
-                                    "type": "object",
-                                    "additionalProperties": {"type": "number"},
-                                }
-                            }
-                        },
-                        "required": True,
-                    },
-                }
-            }
-        },
-        "components": {
-            "schemas": {
-                "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"},
-                        }
-                    },
-                },
-            }
-        },
-    }