Browse Source

Update tests to filter warnings

pull/14583/head
Sebastián Ramírez 7 months ago
parent
commit
247e86daf2
  1. 10
      tests/test_datetime_custom_encoder.py
  2. 20
      tests/test_filter_pydantic_sub_model/app_pv1.py
  3. 30
      tests/test_get_model_definitions_formfeed_escape.py
  4. 12
      tests/test_read_with_orm_mode.py
  5. 12
      tests/test_response_model_as_return_annotation.py
  6. 9
      tests/test_tutorial/test_schema_extra_example/test_tutorial001_pv1.py

10
tests/test_datetime_custom_encoder.py

@ -1,3 +1,4 @@
import warnings
from datetime import datetime, timezone from datetime import datetime, timezone
from fastapi import FastAPI from fastapi import FastAPI
@ -48,9 +49,12 @@ def test_pydanticv1():
app = FastAPI() app = FastAPI()
model = ModelWithDatetimeField(dt_field=datetime(2019, 1, 1, 8)) model = ModelWithDatetimeField(dt_field=datetime(2019, 1, 1, 8))
@app.get("/model", response_model=ModelWithDatetimeField) with warnings.catch_warnings(record=True):
def get_model(): warnings.simplefilter("always")
return model
@app.get("/model", response_model=ModelWithDatetimeField)
def get_model():
return model
client = TestClient(app) client = TestClient(app)
with client: with client:

20
tests/test_filter_pydantic_sub_model/app_pv1.py

@ -1,3 +1,4 @@
import warnings
from typing import Optional from typing import Optional
from fastapi import Depends, FastAPI from fastapi import Depends, FastAPI
@ -31,11 +32,14 @@ async def get_model_c() -> ModelC:
return ModelC(username="test-user", password="test-password") return ModelC(username="test-user", password="test-password")
@app.get("/model/{name}", response_model=ModelA) with warnings.catch_warnings(record=True):
async def get_model_a(name: str, model_c=Depends(get_model_c)): warnings.simplefilter("always")
return {
"name": name, @app.get("/model/{name}", response_model=ModelA)
"description": "model-a-desc", async def get_model_a(name: str, model_c=Depends(get_model_c)):
"model_b": model_c, return {
"tags": {"key1": "value1", "key2": "value2"}, "name": name,
} "description": "model-a-desc",
"model_b": model_c,
"tags": {"key1": "value1", "key2": "value2"},
}

30
tests/test_get_model_definitions_formfeed_escape.py

@ -1,3 +1,5 @@
import warnings
import pytest import pytest
from fastapi import FastAPI from fastapi import FastAPI
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
@ -36,12 +38,28 @@ def client_fixture(request: pytest.FixtureRequest) -> TestClient:
app = FastAPI() app = FastAPI()
@app.get("/facilities/{facility_id}") if request.param == "pydantic-v1":
def get_facility(facility_id: str) -> Facility: with warnings.catch_warnings(record=True):
return Facility( warnings.simplefilter("always")
id=facility_id,
address=Address(line_1="123 Main St", city="Anytown", state_province="CA"), @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) client = TestClient(app)
return client return client

12
tests/test_read_with_orm_mode.py

@ -1,3 +1,4 @@
import warnings
from typing import Any from typing import Any
from fastapi import FastAPI from fastapi import FastAPI
@ -73,10 +74,13 @@ def test_read_with_orm_mode_pv1() -> None:
app = FastAPI() app = FastAPI()
@app.post("/people/", response_model=PersonRead) with warnings.catch_warnings(record=True):
def create_person(person: PersonCreate) -> Any: warnings.simplefilter("always")
db_person = Person.from_orm(person)
return db_person @app.post("/people/", response_model=PersonRead)
def create_person(person: PersonCreate) -> Any:
db_person = Person.from_orm(person)
return db_person
client = TestClient(app) client = TestClient(app)

12
tests/test_response_model_as_return_annotation.py

@ -1,3 +1,4 @@
import warnings
from typing import Union from typing import Union
import pytest import pytest
@ -521,11 +522,14 @@ def test_invalid_response_model_field_pv1():
class Model(v1.BaseModel): class Model(v1.BaseModel):
foo: str foo: str
with pytest.raises(FastAPIError) as e: with warnings.catch_warnings(record=True):
warnings.simplefilter("always")
@app.get("/") with pytest.raises(FastAPIError) as e:
def read_root() -> Union[Response, Model, None]:
return Response(content="Foo") # pragma: no cover @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 "valid Pydantic field type" in e.value.args[0]
assert "parameter response_model=None" in e.value.args[0] assert "parameter response_model=None" in e.value.args[0]

9
tests/test_tutorial/test_schema_extra_example/test_tutorial001_pv1.py

@ -1,4 +1,5 @@
import importlib import importlib
import warnings
import pytest import pytest
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
@ -15,7 +16,13 @@ from ...utils import needs_py310, needs_pydanticv1
], ],
) )
def get_client(request: pytest.FixtureRequest): 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) client = TestClient(mod.app)
return client return client

Loading…
Cancel
Save