]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
✅ Simplify tests for extra_data_types (#13177)
authorAlejandra <90076947+alejsdev@users.noreply.github.com>
Sun, 19 Jan 2025 06:28:09 +0000 (06:28 +0000)
committerGitHub <noreply@github.com>
Sun, 19 Jan 2025 06:28:09 +0000 (06:28 +0000)
tests/test_tutorial/test_extra_data_types/test_tutorial001.py
tests/test_tutorial/test_extra_data_types/test_tutorial001_an.py [deleted file]
tests/test_tutorial/test_extra_data_types/test_tutorial001_an_py310.py [deleted file]
tests/test_tutorial/test_extra_data_types/test_tutorial001_an_py39.py [deleted file]
tests/test_tutorial/test_extra_data_types/test_tutorial001_py310.py [deleted file]

index 5558671b919b54b861ddf7e3bd7a014f4da21587..b816c9cab50fb48b4eef9b13c1d0c45737ec1982 100644 (file)
@@ -1,12 +1,30 @@
+import importlib
+
+import pytest
 from dirty_equals import IsDict
 from fastapi.testclient import TestClient
 
-from docs_src.extra_data_types.tutorial001 import app
+from ...utils import needs_py39, needs_py310
+
+
+@pytest.fixture(
+    name="client",
+    params=[
+        "tutorial001",
+        pytest.param("tutorial001_py310", marks=needs_py310),
+        "tutorial001_an",
+        pytest.param("tutorial001_an_py39", marks=needs_py39),
+        pytest.param("tutorial001_an_py310", marks=needs_py310),
+    ],
+)
+def get_client(request: pytest.FixtureRequest):
+    mod = importlib.import_module(f"docs_src.extra_data_types.{request.param}")
 
-client = TestClient(app)
+    client = TestClient(mod.app)
+    return client
 
 
-def test_extra_types():
+def test_extra_types(client: TestClient):
     item_id = "ff97dd87-a4a5-4a12-b412-cde99f33e00e"
     data = {
         "start_datetime": "2018-12-22T14:00:00+00:00",
@@ -27,7 +45,7 @@ def test_extra_types():
     assert response.json() == expected_response
 
 
-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_data_types/test_tutorial001_an.py b/tests/test_tutorial/test_extra_data_types/test_tutorial001_an.py
deleted file mode 100644 (file)
index e309f8b..0000000
+++ /dev/null
@@ -1,175 +0,0 @@
-from dirty_equals import IsDict
-from fastapi.testclient import TestClient
-
-from docs_src.extra_data_types.tutorial001_an import app
-
-client = TestClient(app)
-
-
-def test_extra_types():
-    item_id = "ff97dd87-a4a5-4a12-b412-cde99f33e00e"
-    data = {
-        "start_datetime": "2018-12-22T14:00:00+00:00",
-        "end_datetime": "2018-12-24T15:00:00+00:00",
-        "repeat_at": "15:30:00",
-        "process_after": 300,
-    }
-    expected_response = data.copy()
-    expected_response.update(
-        {
-            "start_process": "2018-12-22T14:05:00+00:00",
-            "duration": 176_100,
-            "item_id": item_id,
-        }
-    )
-    response = client.put(f"/items/{item_id}", json=data)
-    assert response.status_code == 200, response.text
-    assert response.json() == expected_response
-
-
-def test_openapi_schema():
-    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}": {
-                "put": {
-                    "responses": {
-                        "200": {
-                            "description": "Successful Response",
-                            "content": {"application/json": {"schema": {}}},
-                        },
-                        "422": {
-                            "description": "Validation Error",
-                            "content": {
-                                "application/json": {
-                                    "schema": {
-                                        "$ref": "#/components/schemas/HTTPValidationError"
-                                    }
-                                }
-                            },
-                        },
-                    },
-                    "summary": "Read Items",
-                    "operationId": "read_items_items__item_id__put",
-                    "parameters": [
-                        {
-                            "required": True,
-                            "schema": {
-                                "title": "Item Id",
-                                "type": "string",
-                                "format": "uuid",
-                            },
-                            "name": "item_id",
-                            "in": "path",
-                        }
-                    ],
-                    "requestBody": {
-                        "required": True,
-                        "content": {
-                            "application/json": {
-                                "schema": IsDict(
-                                    {
-                                        "allOf": [
-                                            {
-                                                "$ref": "#/components/schemas/Body_read_items_items__item_id__put"
-                                            }
-                                        ],
-                                        "title": "Body",
-                                    }
-                                )
-                                | IsDict(
-                                    # TODO: remove when deprecating Pydantic v1
-                                    {
-                                        "$ref": "#/components/schemas/Body_read_items_items__item_id__put"
-                                    }
-                                )
-                            }
-                        },
-                    },
-                }
-            }
-        },
-        "components": {
-            "schemas": {
-                "Body_read_items_items__item_id__put": {
-                    "title": "Body_read_items_items__item_id__put",
-                    "type": "object",
-                    "properties": {
-                        "start_datetime": {
-                            "title": "Start Datetime",
-                            "type": "string",
-                            "format": "date-time",
-                        },
-                        "end_datetime": {
-                            "title": "End Datetime",
-                            "type": "string",
-                            "format": "date-time",
-                        },
-                        "repeat_at": IsDict(
-                            {
-                                "title": "Repeat At",
-                                "anyOf": [
-                                    {"type": "string", "format": "time"},
-                                    {"type": "null"},
-                                ],
-                            }
-                        )
-                        | IsDict(
-                            # TODO: remove when deprecating Pydantic v1
-                            {
-                                "title": "Repeat At",
-                                "type": "string",
-                                "format": "time",
-                            }
-                        ),
-                        "process_after": IsDict(
-                            {
-                                "title": "Process After",
-                                "type": "string",
-                                "format": "duration",
-                            }
-                        )
-                        | IsDict(
-                            # TODO: remove when deprecating Pydantic v1
-                            {
-                                "title": "Process After",
-                                "type": "number",
-                                "format": "time-delta",
-                            }
-                        ),
-                    },
-                    "required": ["start_datetime", "end_datetime", "process_after"],
-                },
-                "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_extra_data_types/test_tutorial001_an_py310.py b/tests/test_tutorial/test_extra_data_types/test_tutorial001_an_py310.py
deleted file mode 100644 (file)
index ca110dc..0000000
+++ /dev/null
@@ -1,184 +0,0 @@
-import pytest
-from dirty_equals import IsDict
-from fastapi.testclient import TestClient
-
-from ...utils import needs_py310
-
-
-@pytest.fixture(name="client")
-def get_client():
-    from docs_src.extra_data_types.tutorial001_an_py310 import app
-
-    client = TestClient(app)
-    return client
-
-
-@needs_py310
-def test_extra_types(client: TestClient):
-    item_id = "ff97dd87-a4a5-4a12-b412-cde99f33e00e"
-    data = {
-        "start_datetime": "2018-12-22T14:00:00+00:00",
-        "end_datetime": "2018-12-24T15:00:00+00:00",
-        "repeat_at": "15:30:00",
-        "process_after": 300,
-    }
-    expected_response = data.copy()
-    expected_response.update(
-        {
-            "start_process": "2018-12-22T14:05:00+00:00",
-            "duration": 176_100,
-            "item_id": item_id,
-        }
-    )
-    response = client.put(f"/items/{item_id}", json=data)
-    assert response.status_code == 200, response.text
-    assert response.json() == expected_response
-
-
-@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}": {
-                "put": {
-                    "responses": {
-                        "200": {
-                            "description": "Successful Response",
-                            "content": {"application/json": {"schema": {}}},
-                        },
-                        "422": {
-                            "description": "Validation Error",
-                            "content": {
-                                "application/json": {
-                                    "schema": {
-                                        "$ref": "#/components/schemas/HTTPValidationError"
-                                    }
-                                }
-                            },
-                        },
-                    },
-                    "summary": "Read Items",
-                    "operationId": "read_items_items__item_id__put",
-                    "parameters": [
-                        {
-                            "required": True,
-                            "schema": {
-                                "title": "Item Id",
-                                "type": "string",
-                                "format": "uuid",
-                            },
-                            "name": "item_id",
-                            "in": "path",
-                        }
-                    ],
-                    "requestBody": {
-                        "required": True,
-                        "content": {
-                            "application/json": {
-                                "schema": IsDict(
-                                    {
-                                        "allOf": [
-                                            {
-                                                "$ref": "#/components/schemas/Body_read_items_items__item_id__put"
-                                            }
-                                        ],
-                                        "title": "Body",
-                                    }
-                                )
-                                | IsDict(
-                                    # TODO: remove when deprecating Pydantic v1
-                                    {
-                                        "$ref": "#/components/schemas/Body_read_items_items__item_id__put"
-                                    }
-                                )
-                            }
-                        },
-                    },
-                }
-            }
-        },
-        "components": {
-            "schemas": {
-                "Body_read_items_items__item_id__put": {
-                    "title": "Body_read_items_items__item_id__put",
-                    "type": "object",
-                    "properties": {
-                        "start_datetime": {
-                            "title": "Start Datetime",
-                            "type": "string",
-                            "format": "date-time",
-                        },
-                        "end_datetime": {
-                            "title": "End Datetime",
-                            "type": "string",
-                            "format": "date-time",
-                        },
-                        "repeat_at": IsDict(
-                            {
-                                "title": "Repeat At",
-                                "anyOf": [
-                                    {"type": "string", "format": "time"},
-                                    {"type": "null"},
-                                ],
-                            }
-                        )
-                        | IsDict(
-                            # TODO: remove when deprecating Pydantic v1
-                            {
-                                "title": "Repeat At",
-                                "type": "string",
-                                "format": "time",
-                            }
-                        ),
-                        "process_after": IsDict(
-                            {
-                                "title": "Process After",
-                                "type": "string",
-                                "format": "duration",
-                            }
-                        )
-                        | IsDict(
-                            # TODO: remove when deprecating Pydantic v1
-                            {
-                                "title": "Process After",
-                                "type": "number",
-                                "format": "time-delta",
-                            }
-                        ),
-                    },
-                    "required": ["start_datetime", "end_datetime", "process_after"],
-                },
-                "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_extra_data_types/test_tutorial001_an_py39.py b/tests/test_tutorial/test_extra_data_types/test_tutorial001_an_py39.py
deleted file mode 100644 (file)
index 3386fb1..0000000
+++ /dev/null
@@ -1,184 +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.extra_data_types.tutorial001_an_py39 import app
-
-    client = TestClient(app)
-    return client
-
-
-@needs_py39
-def test_extra_types(client: TestClient):
-    item_id = "ff97dd87-a4a5-4a12-b412-cde99f33e00e"
-    data = {
-        "start_datetime": "2018-12-22T14:00:00+00:00",
-        "end_datetime": "2018-12-24T15:00:00+00:00",
-        "repeat_at": "15:30:00",
-        "process_after": 300,
-    }
-    expected_response = data.copy()
-    expected_response.update(
-        {
-            "start_process": "2018-12-22T14:05:00+00:00",
-            "duration": 176_100,
-            "item_id": item_id,
-        }
-    )
-    response = client.put(f"/items/{item_id}", json=data)
-    assert response.status_code == 200, response.text
-    assert response.json() == expected_response
-
-
-@needs_py39
-def test_openapi_schema(client: TestClient):
-    response = client.get("/openapi.json")
-    assert response.status_code == 200, response.text
-    assert response.json() == {
-        "openapi": "3.1.0",
-        "info": {"title": "FastAPI", "version": "0.1.0"},
-        "paths": {
-            "/items/{item_id}": {
-                "put": {
-                    "responses": {
-                        "200": {
-                            "description": "Successful Response",
-                            "content": {"application/json": {"schema": {}}},
-                        },
-                        "422": {
-                            "description": "Validation Error",
-                            "content": {
-                                "application/json": {
-                                    "schema": {
-                                        "$ref": "#/components/schemas/HTTPValidationError"
-                                    }
-                                }
-                            },
-                        },
-                    },
-                    "summary": "Read Items",
-                    "operationId": "read_items_items__item_id__put",
-                    "parameters": [
-                        {
-                            "required": True,
-                            "schema": {
-                                "title": "Item Id",
-                                "type": "string",
-                                "format": "uuid",
-                            },
-                            "name": "item_id",
-                            "in": "path",
-                        }
-                    ],
-                    "requestBody": {
-                        "required": True,
-                        "content": {
-                            "application/json": {
-                                "schema": IsDict(
-                                    {
-                                        "allOf": [
-                                            {
-                                                "$ref": "#/components/schemas/Body_read_items_items__item_id__put"
-                                            }
-                                        ],
-                                        "title": "Body",
-                                    }
-                                )
-                                | IsDict(
-                                    # TODO: remove when deprecating Pydantic v1
-                                    {
-                                        "$ref": "#/components/schemas/Body_read_items_items__item_id__put"
-                                    }
-                                )
-                            }
-                        },
-                    },
-                }
-            }
-        },
-        "components": {
-            "schemas": {
-                "Body_read_items_items__item_id__put": {
-                    "title": "Body_read_items_items__item_id__put",
-                    "type": "object",
-                    "properties": {
-                        "start_datetime": {
-                            "title": "Start Datetime",
-                            "type": "string",
-                            "format": "date-time",
-                        },
-                        "end_datetime": {
-                            "title": "End Datetime",
-                            "type": "string",
-                            "format": "date-time",
-                        },
-                        "repeat_at": IsDict(
-                            {
-                                "title": "Repeat At",
-                                "anyOf": [
-                                    {"type": "string", "format": "time"},
-                                    {"type": "null"},
-                                ],
-                            }
-                        )
-                        | IsDict(
-                            # TODO: remove when deprecating Pydantic v1
-                            {
-                                "title": "Repeat At",
-                                "type": "string",
-                                "format": "time",
-                            }
-                        ),
-                        "process_after": IsDict(
-                            {
-                                "title": "Process After",
-                                "type": "string",
-                                "format": "duration",
-                            }
-                        )
-                        | IsDict(
-                            # TODO: remove when deprecating Pydantic v1
-                            {
-                                "title": "Process After",
-                                "type": "number",
-                                "format": "time-delta",
-                            }
-                        ),
-                    },
-                    "required": ["start_datetime", "end_datetime", "process_after"],
-                },
-                "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_extra_data_types/test_tutorial001_py310.py b/tests/test_tutorial/test_extra_data_types/test_tutorial001_py310.py
deleted file mode 100644 (file)
index 50c9aef..0000000
+++ /dev/null
@@ -1,184 +0,0 @@
-import pytest
-from dirty_equals import IsDict
-from fastapi.testclient import TestClient
-
-from ...utils import needs_py310
-
-
-@pytest.fixture(name="client")
-def get_client():
-    from docs_src.extra_data_types.tutorial001_py310 import app
-
-    client = TestClient(app)
-    return client
-
-
-@needs_py310
-def test_extra_types(client: TestClient):
-    item_id = "ff97dd87-a4a5-4a12-b412-cde99f33e00e"
-    data = {
-        "start_datetime": "2018-12-22T14:00:00+00:00",
-        "end_datetime": "2018-12-24T15:00:00+00:00",
-        "repeat_at": "15:30:00",
-        "process_after": 300,
-    }
-    expected_response = data.copy()
-    expected_response.update(
-        {
-            "start_process": "2018-12-22T14:05:00+00:00",
-            "duration": 176_100,
-            "item_id": item_id,
-        }
-    )
-    response = client.put(f"/items/{item_id}", json=data)
-    assert response.status_code == 200, response.text
-    assert response.json() == expected_response
-
-
-@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}": {
-                "put": {
-                    "responses": {
-                        "200": {
-                            "description": "Successful Response",
-                            "content": {"application/json": {"schema": {}}},
-                        },
-                        "422": {
-                            "description": "Validation Error",
-                            "content": {
-                                "application/json": {
-                                    "schema": {
-                                        "$ref": "#/components/schemas/HTTPValidationError"
-                                    }
-                                }
-                            },
-                        },
-                    },
-                    "summary": "Read Items",
-                    "operationId": "read_items_items__item_id__put",
-                    "parameters": [
-                        {
-                            "required": True,
-                            "schema": {
-                                "title": "Item Id",
-                                "type": "string",
-                                "format": "uuid",
-                            },
-                            "name": "item_id",
-                            "in": "path",
-                        }
-                    ],
-                    "requestBody": {
-                        "required": True,
-                        "content": {
-                            "application/json": {
-                                "schema": IsDict(
-                                    {
-                                        "allOf": [
-                                            {
-                                                "$ref": "#/components/schemas/Body_read_items_items__item_id__put"
-                                            }
-                                        ],
-                                        "title": "Body",
-                                    }
-                                )
-                                | IsDict(
-                                    # TODO: remove when deprecating Pydantic v1
-                                    {
-                                        "$ref": "#/components/schemas/Body_read_items_items__item_id__put"
-                                    }
-                                )
-                            }
-                        },
-                    },
-                }
-            }
-        },
-        "components": {
-            "schemas": {
-                "Body_read_items_items__item_id__put": {
-                    "title": "Body_read_items_items__item_id__put",
-                    "type": "object",
-                    "properties": {
-                        "start_datetime": {
-                            "title": "Start Datetime",
-                            "type": "string",
-                            "format": "date-time",
-                        },
-                        "end_datetime": {
-                            "title": "End Datetime",
-                            "type": "string",
-                            "format": "date-time",
-                        },
-                        "repeat_at": IsDict(
-                            {
-                                "title": "Repeat At",
-                                "anyOf": [
-                                    {"type": "string", "format": "time"},
-                                    {"type": "null"},
-                                ],
-                            }
-                        )
-                        | IsDict(
-                            # TODO: remove when deprecating Pydantic v1
-                            {
-                                "title": "Repeat At",
-                                "type": "string",
-                                "format": "time",
-                            }
-                        ),
-                        "process_after": IsDict(
-                            {
-                                "title": "Process After",
-                                "type": "string",
-                                "format": "duration",
-                            }
-                        )
-                        | IsDict(
-                            # TODO: remove when deprecating Pydantic v1
-                            {
-                                "title": "Process After",
-                                "type": "number",
-                                "format": "time-delta",
-                            }
-                        ),
-                    },
-                    "required": ["start_datetime", "end_datetime", "process_after"],
-                },
-                "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"},
-                        }
-                    },
-                },
-            }
-        },
-    }