From 438195abebdf30fca2675cee298d78d35e031cb9 Mon Sep 17 00:00:00 2001 From: Philipp Eisen Date: Mon, 26 Feb 2024 14:08:12 +0100 Subject: [PATCH] Tests should check that there are different models in the schema --- .../test_tutorial001.py | 22 ++++++++++++++++--- .../test_tutorial001_py310.py | 22 ++++++++++++++++--- .../test_tutorial001_py39.py | 22 ++++++++++++++++--- .../test_tutorial002.py | 2 ++ .../test_tutorial002_py310.py | 2 ++ .../test_tutorial002_py39.py | 2 ++ 6 files changed, 63 insertions(+), 9 deletions(-) diff --git a/tests/test_tutorial/test_separate_openapi_schemas/test_tutorial001.py b/tests/test_tutorial/test_separate_openapi_schemas/test_tutorial001.py index cdfae9f8c..44dc3bf0c 100644 --- a/tests/test_tutorial/test_separate_openapi_schemas/test_tutorial001.py +++ b/tests/test_tutorial/test_separate_openapi_schemas/test_tutorial001.py @@ -12,12 +12,14 @@ def get_client() -> TestClient: return client +@needs_pydanticv2 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_pydanticv2 def test_read_items(client: TestClient) -> None: response = client.get("/items/") assert response.status_code == 200, response.text @@ -48,7 +50,9 @@ def test_openapi_schema(client: TestClient) -> None: "content": { "application/json": { "schema": { - "items": {"$ref": "#/components/schemas/Item"}, + "items": { + "$ref": "#/components/schemas/Item-Output" + }, "type": "array", "title": "Response Read Items Items Get", } @@ -63,7 +67,7 @@ def test_openapi_schema(client: TestClient) -> None: "requestBody": { "content": { "application/json": { - "schema": {"$ref": "#/components/schemas/Item"} + "schema": {"$ref": "#/components/schemas/Item-Input"} } }, "required": True, @@ -100,7 +104,7 @@ def test_openapi_schema(client: TestClient) -> None: "type": "object", "title": "HTTPValidationError", }, - "Item": { + "Item-Input": { "properties": { "name": {"type": "string", "title": "Name"}, "description": { @@ -112,6 +116,18 @@ def test_openapi_schema(client: TestClient) -> None: "required": ["name"], "title": "Item", }, + "Item-Output": { + "properties": { + "name": {"type": "string", "title": "Name"}, + "description": { + "anyOf": [{"type": "string"}, {"type": "null"}], + "title": "Description", + }, + }, + "type": "object", + "required": ["name", "description"], + "title": "Item", + }, "ValidationError": { "properties": { "loc": { 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 index 3b22146f6..72d6ad64a 100644 --- a/tests/test_tutorial/test_separate_openapi_schemas/test_tutorial001_py310.py +++ b/tests/test_tutorial/test_separate_openapi_schemas/test_tutorial001_py310.py @@ -13,6 +13,7 @@ def get_client() -> TestClient: @needs_py310 +@needs_pydanticv2 def test_create_item(client: TestClient) -> None: response = client.post("/items/", json={"name": "Foo"}) assert response.status_code == 200, response.text @@ -20,6 +21,7 @@ def test_create_item(client: TestClient) -> None: @needs_py310 +@needs_pydanticv2 def test_read_items(client: TestClient) -> None: response = client.get("/items/") assert response.status_code == 200, response.text @@ -51,7 +53,9 @@ def test_openapi_schema(client: TestClient) -> None: "content": { "application/json": { "schema": { - "items": {"$ref": "#/components/schemas/Item"}, + "items": { + "$ref": "#/components/schemas/Item-Output" + }, "type": "array", "title": "Response Read Items Items Get", } @@ -66,7 +70,7 @@ def test_openapi_schema(client: TestClient) -> None: "requestBody": { "content": { "application/json": { - "schema": {"$ref": "#/components/schemas/Item"} + "schema": {"$ref": "#/components/schemas/Item-Input"} } }, "required": True, @@ -103,7 +107,7 @@ def test_openapi_schema(client: TestClient) -> None: "type": "object", "title": "HTTPValidationError", }, - "Item": { + "Item-Input": { "properties": { "name": {"type": "string", "title": "Name"}, "description": { @@ -115,6 +119,18 @@ def test_openapi_schema(client: TestClient) -> None: "required": ["name"], "title": "Item", }, + "Item-Output": { + "properties": { + "name": {"type": "string", "title": "Name"}, + "description": { + "anyOf": [{"type": "string"}, {"type": "null"}], + "title": "Description", + }, + }, + "type": "object", + "required": ["name", "description"], + "title": "Item", + }, "ValidationError": { "properties": { "loc": { 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 index 991abe811..7f07b4768 100644 --- a/tests/test_tutorial/test_separate_openapi_schemas/test_tutorial001_py39.py +++ b/tests/test_tutorial/test_separate_openapi_schemas/test_tutorial001_py39.py @@ -13,6 +13,7 @@ def get_client() -> TestClient: @needs_py39 +@needs_pydanticv2 def test_create_item(client: TestClient) -> None: response = client.post("/items/", json={"name": "Foo"}) assert response.status_code == 200, response.text @@ -20,6 +21,7 @@ def test_create_item(client: TestClient) -> None: @needs_py39 +@needs_pydanticv2 def test_read_items(client: TestClient) -> None: response = client.get("/items/") assert response.status_code == 200, response.text @@ -51,7 +53,9 @@ def test_openapi_schema(client: TestClient) -> None: "content": { "application/json": { "schema": { - "items": {"$ref": "#/components/schemas/Item"}, + "items": { + "$ref": "#/components/schemas/Item-Output" + }, "type": "array", "title": "Response Read Items Items Get", } @@ -66,7 +70,7 @@ def test_openapi_schema(client: TestClient) -> None: "requestBody": { "content": { "application/json": { - "schema": {"$ref": "#/components/schemas/Item"} + "schema": {"$ref": "#/components/schemas/Item-Input"} } }, "required": True, @@ -103,7 +107,7 @@ def test_openapi_schema(client: TestClient) -> None: "type": "object", "title": "HTTPValidationError", }, - "Item": { + "Item-Input": { "properties": { "name": {"type": "string", "title": "Name"}, "description": { @@ -115,6 +119,18 @@ def test_openapi_schema(client: TestClient) -> None: "required": ["name"], "title": "Item", }, + "Item-Output": { + "properties": { + "name": {"type": "string", "title": "Name"}, + "description": { + "anyOf": [{"type": "string"}, {"type": "null"}], + "title": "Description", + }, + }, + "type": "object", + "required": ["name", "description"], + "title": "Item", + }, "ValidationError": { "properties": { "loc": { diff --git a/tests/test_tutorial/test_separate_openapi_schemas/test_tutorial002.py b/tests/test_tutorial/test_separate_openapi_schemas/test_tutorial002.py index d2cf7945b..3d686fdfd 100644 --- a/tests/test_tutorial/test_separate_openapi_schemas/test_tutorial002.py +++ b/tests/test_tutorial/test_separate_openapi_schemas/test_tutorial002.py @@ -12,12 +12,14 @@ def get_client() -> TestClient: return client +@needs_pydanticv2 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_pydanticv2 def test_read_items(client: TestClient) -> None: response = client.get("/items/") assert response.status_code == 200, response.text 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 index 89c9ce977..9f97dc2ab 100644 --- a/tests/test_tutorial/test_separate_openapi_schemas/test_tutorial002_py310.py +++ b/tests/test_tutorial/test_separate_openapi_schemas/test_tutorial002_py310.py @@ -13,6 +13,7 @@ def get_client() -> TestClient: @needs_py310 +@needs_pydanticv2 def test_create_item(client: TestClient) -> None: response = client.post("/items/", json={"name": "Foo"}) assert response.status_code == 200, response.text @@ -20,6 +21,7 @@ def test_create_item(client: TestClient) -> None: @needs_py310 +@needs_pydanticv2 def test_read_items(client: TestClient) -> None: response = client.get("/items/") assert response.status_code == 200, response.text 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 index 6ac3d8f79..105e91ce9 100644 --- a/tests/test_tutorial/test_separate_openapi_schemas/test_tutorial002_py39.py +++ b/tests/test_tutorial/test_separate_openapi_schemas/test_tutorial002_py39.py @@ -13,6 +13,7 @@ def get_client() -> TestClient: @needs_py39 +@needs_pydanticv2 def test_create_item(client: TestClient) -> None: response = client.post("/items/", json={"name": "Foo"}) assert response.status_code == 200, response.text @@ -20,6 +21,7 @@ def test_create_item(client: TestClient) -> None: @needs_py39 +@needs_pydanticv2 def test_read_items(client: TestClient) -> None: response = client.get("/items/") assert response.status_code == 200, response.text