From 5700a25c15e7d1ce2d57d0e3728ae0e4590e615f Mon Sep 17 00:00:00 2001 From: alv2017 Date: Tue, 18 Feb 2025 23:30:50 +0200 Subject: [PATCH] refactoring: tests/test_openapi_separate_input_output_schemas.py --- ...t_openapi_separate_input_output_schemas.py | 126 +++++++++--------- 1 file changed, 61 insertions(+), 65 deletions(-) diff --git a/tests/test_openapi_separate_input_output_schemas.py b/tests/test_openapi_separate_input_output_schemas.py index f7e045259..f94e0c2d9 100644 --- a/tests/test_openapi_separate_input_output_schemas.py +++ b/tests/test_openapi_separate_input_output_schemas.py @@ -1,5 +1,6 @@ from typing import List, Optional +import pytest from fastapi import FastAPI from fastapi.testclient import TestClient from pydantic import BaseModel @@ -23,7 +24,8 @@ class Item(BaseModel): model_config = {"json_schema_serialization_defaults_required": True} -def get_app_client(separate_input_output_schemas: bool = True) -> TestClient: +@pytest.fixture(name="app") +def custom_app(separate_input_output_schemas: bool) -> FastAPI: app = FastAPI(separate_input_output_schemas=separate_input_output_schemas) @app.post("/items/", responses={402: {"model": Item}}) @@ -45,47 +47,40 @@ def get_app_client(separate_input_output_schemas: bool = True) -> TestClient: Item(name="Plumbus"), ] - client = TestClient(app) - return client + return app -def test_create_item(): - client = get_app_client() - client_no = get_app_client(separate_input_output_schemas=False) +@pytest.mark.parametrize("separate_input_output_schemas", [True, False]) +def test_create_item(app: FastAPI, separate_input_output_schemas: bool): + client = TestClient(app) response = client.post("/items/", json={"name": "Plumbus"}) - response2 = client_no.post("/items/", json={"name": "Plumbus"}) - assert response.status_code == response2.status_code == 200, response.text - assert ( - response.json() - == response2.json() - == {"name": "Plumbus", "description": None, "sub": None} - ) + assert app.separate_input_output_schemas == separate_input_output_schemas + assert response.status_code == 200, response.text + assert response.json() == {"name": "Plumbus", "description": None, "sub": None} -def test_create_item_with_sub(): - client = get_app_client() - client_no = get_app_client(separate_input_output_schemas=False) + +@pytest.mark.parametrize("separate_input_output_schemas", [True, False]) +def test_create_item_with_sub(app: FastAPI, separate_input_output_schemas: bool): + client = TestClient(app) data = { "name": "Plumbus", "sub": {"subname": "SubPlumbus", "sub_description": "Sub WTF"}, } response = client.post("/items/", json=data) - response2 = client_no.post("/items/", json=data) - assert response.status_code == response2.status_code == 200, response.text - assert ( - response.json() - == response2.json() - == { - "name": "Plumbus", - "description": None, - "sub": {"subname": "SubPlumbus", "sub_description": "Sub WTF", "tags": []}, - } - ) + assert app.separate_input_output_schemas == separate_input_output_schemas + assert response.status_code == 200, response.text + assert response.json() == { + "name": "Plumbus", + "description": None, + "sub": {"subname": "SubPlumbus", "sub_description": "Sub WTF", "tags": []}, + } -def test_create_item_list(): - client = get_app_client() - client_no = get_app_client(separate_input_output_schemas=False) + +@pytest.mark.parametrize("separate_input_output_schemas", [True, False]) +def test_create_item_list(app: FastAPI, separate_input_output_schemas: bool): + client = TestClient(app) data = [ {"name": "Plumbus"}, { @@ -94,46 +89,44 @@ def test_create_item_list(): }, ] response = client.post("/items-list/", json=data) - response2 = client_no.post("/items-list/", json=data) - assert response.status_code == response2.status_code == 200, response.text - assert ( - response.json() - == response2.json() - == [ - {"name": "Plumbus", "description": None, "sub": None}, - { - "name": "Portal Gun", - "description": "Device to travel through the multi-rick-verse", - "sub": None, - }, - ] - ) + + assert app.separate_input_output_schemas == separate_input_output_schemas + assert response.status_code == 200, response.text + assert response.json() == [ + {"name": "Plumbus", "description": None, "sub": None}, + { + "name": "Portal Gun", + "description": "Device to travel through the multi-rick-verse", + "sub": None, + }, + ] -def test_read_items(): - client = get_app_client() - client_no = get_app_client(separate_input_output_schemas=False) +@pytest.mark.parametrize("separate_input_output_schemas", [True, False]) +def test_read_items(app: FastAPI, separate_input_output_schemas: bool): + client = TestClient(app) response = client.get("/items/") - response2 = client_no.get("/items/") - assert response.status_code == response2.status_code == 200, response.text - assert ( - response.json() - == response2.json() - == [ - { - "name": "Portal Gun", - "description": "Device to travel through the multi-rick-verse", - "sub": {"subname": "subname", "sub_description": None, "tags": []}, - }, - {"name": "Plumbus", "description": None, "sub": None}, - ] - ) + + assert hasattr(app, "separate_input_output_schemas") + assert app.separate_input_output_schemas == separate_input_output_schemas + assert response.status_code == 200, response.text + assert response.json() == [ + { + "name": "Portal Gun", + "description": "Device to travel through the multi-rick-verse", + "sub": {"subname": "subname", "sub_description": None, "tags": []}, + }, + {"name": "Plumbus", "description": None, "sub": None}, + ] @needs_pydanticv2 -def test_openapi_schema(): - client = get_app_client() +@pytest.mark.parametrize("separate_input_output_schemas", [True]) +def test_openapi_schema(app: FastAPI, separate_input_output_schemas: bool): + client = TestClient(app) response = client.get("/openapi.json") + + assert app.separate_input_output_schemas == separate_input_output_schemas assert response.status_code == 200, response.text assert response.json() == { "openapi": "3.1.0", @@ -349,9 +342,12 @@ def test_openapi_schema(): @needs_pydanticv2 -def test_openapi_schema_no_separate(): - client = get_app_client(separate_input_output_schemas=False) +@pytest.mark.parametrize("separate_input_output_schemas", [False]) +def test_openapi_schema_no_separate(app: FastAPI, separate_input_output_schemas: bool): + client = TestClient(app) response = client.get("/openapi.json") + + assert app.separate_input_output_schemas == separate_input_output_schemas assert response.status_code == 200, response.text assert response.json() == { "openapi": "3.1.0",