diff --git a/tests/test_datetime_custom_encoder.py b/tests/test_datetime_custom_encoder.py index 822651f4f..56b6780f0 100644 --- a/tests/test_datetime_custom_encoder.py +++ b/tests/test_datetime_custom_encoder.py @@ -1,3 +1,4 @@ +import warnings from datetime import datetime, timezone from fastapi import FastAPI @@ -48,9 +49,12 @@ def test_pydanticv1(): app = FastAPI() model = ModelWithDatetimeField(dt_field=datetime(2019, 1, 1, 8)) - @app.get("/model", response_model=ModelWithDatetimeField) - def get_model(): - return model + with warnings.catch_warnings(record=True): + warnings.simplefilter("always") + + @app.get("/model", response_model=ModelWithDatetimeField) + def get_model(): + return model client = TestClient(app) with client: diff --git a/tests/test_filter_pydantic_sub_model/app_pv1.py b/tests/test_filter_pydantic_sub_model/app_pv1.py index 0b6ab53e0..d6f2ce7d2 100644 --- a/tests/test_filter_pydantic_sub_model/app_pv1.py +++ b/tests/test_filter_pydantic_sub_model/app_pv1.py @@ -1,3 +1,4 @@ +import warnings from typing import Optional from fastapi import Depends, FastAPI @@ -31,11 +32,14 @@ async def get_model_c() -> ModelC: return ModelC(username="test-user", password="test-password") -@app.get("/model/{name}", response_model=ModelA) -async def get_model_a(name: str, model_c=Depends(get_model_c)): - return { - "name": name, - "description": "model-a-desc", - "model_b": model_c, - "tags": {"key1": "value1", "key2": "value2"}, - } +with warnings.catch_warnings(record=True): + warnings.simplefilter("always") + + @app.get("/model/{name}", response_model=ModelA) + async def get_model_a(name: str, model_c=Depends(get_model_c)): + return { + "name": name, + "description": "model-a-desc", + "model_b": model_c, + "tags": {"key1": "value1", "key2": "value2"}, + } diff --git a/tests/test_get_model_definitions_formfeed_escape.py b/tests/test_get_model_definitions_formfeed_escape.py index 50d799a57..dee595554 100644 --- a/tests/test_get_model_definitions_formfeed_escape.py +++ b/tests/test_get_model_definitions_formfeed_escape.py @@ -1,3 +1,5 @@ +import warnings + import pytest from fastapi import FastAPI from fastapi.testclient import TestClient @@ -36,12 +38,28 @@ def client_fixture(request: pytest.FixtureRequest) -> TestClient: app = FastAPI() - @app.get("/facilities/{facility_id}") - def get_facility(facility_id: str) -> Facility: - return Facility( - id=facility_id, - address=Address(line_1="123 Main St", city="Anytown", state_province="CA"), - ) + if request.param == "pydantic-v1": + with warnings.catch_warnings(record=True): + warnings.simplefilter("always") + + @app.get("/facilities/{facility_id}") + def get_facility(facility_id: str) -> Facility: + return Facility( + id=facility_id, + address=Address( + line_1="123 Main St", city="Anytown", state_province="CA" + ), + ) + else: + + @app.get("/facilities/{facility_id}") + def get_facility(facility_id: str) -> Facility: + return Facility( + id=facility_id, + address=Address( + line_1="123 Main St", city="Anytown", state_province="CA" + ), + ) client = TestClient(app) return client diff --git a/tests/test_read_with_orm_mode.py b/tests/test_read_with_orm_mode.py index 5858f8e80..a195634b8 100644 --- a/tests/test_read_with_orm_mode.py +++ b/tests/test_read_with_orm_mode.py @@ -1,3 +1,4 @@ +import warnings from typing import Any from fastapi import FastAPI @@ -73,10 +74,13 @@ def test_read_with_orm_mode_pv1() -> None: app = FastAPI() - @app.post("/people/", response_model=PersonRead) - def create_person(person: PersonCreate) -> Any: - db_person = Person.from_orm(person) - return db_person + with warnings.catch_warnings(record=True): + warnings.simplefilter("always") + + @app.post("/people/", response_model=PersonRead) + def create_person(person: PersonCreate) -> Any: + db_person = Person.from_orm(person) + return db_person client = TestClient(app) diff --git a/tests/test_response_model_as_return_annotation.py b/tests/test_response_model_as_return_annotation.py index 44e882a76..9e527d6a0 100644 --- a/tests/test_response_model_as_return_annotation.py +++ b/tests/test_response_model_as_return_annotation.py @@ -1,3 +1,4 @@ +import warnings from typing import Union import pytest @@ -521,11 +522,14 @@ def test_invalid_response_model_field_pv1(): class Model(v1.BaseModel): foo: str - with pytest.raises(FastAPIError) as e: + with warnings.catch_warnings(record=True): + warnings.simplefilter("always") - @app.get("/") - def read_root() -> Union[Response, Model, None]: - return Response(content="Foo") # pragma: no cover + with pytest.raises(FastAPIError) as e: + + @app.get("/") + def read_root() -> Union[Response, Model, None]: + return Response(content="Foo") # pragma: no cover assert "valid Pydantic field type" in e.value.args[0] assert "parameter response_model=None" in e.value.args[0] diff --git a/tests/test_tutorial/test_schema_extra_example/test_tutorial001_pv1.py b/tests/test_tutorial/test_schema_extra_example/test_tutorial001_pv1.py index 605996289..c5526b19c 100644 --- a/tests/test_tutorial/test_schema_extra_example/test_tutorial001_pv1.py +++ b/tests/test_tutorial/test_schema_extra_example/test_tutorial001_pv1.py @@ -1,4 +1,5 @@ import importlib +import warnings import pytest from fastapi.testclient import TestClient @@ -15,7 +16,13 @@ from ...utils import needs_py310, needs_pydanticv1 ], ) def get_client(request: pytest.FixtureRequest): - mod = importlib.import_module(f"docs_src.schema_extra_example.{request.param}") + with warnings.catch_warnings(record=True): + warnings.filterwarnings( + "ignore", + message=r"pydantic\.v1 is deprecated and will soon stop being supported by FastAPI\..*", + category=DeprecationWarning, + ) + mod = importlib.import_module(f"docs_src.schema_extra_example.{request.param}") client = TestClient(mod.app) return client