mirror of https://github.com/tiangolo/fastapi.git
Fix coverage
This commit is contained in:
parent
c50589dc59
commit
a8bbcebcb4
|
|
@ -176,7 +176,7 @@ def _model_has_computed_fields(model_or_enum: Any) -> bool:
|
||||||
model_schema = model_or_enum.__pydantic_core_schema__.get("schema", {})
|
model_schema = model_or_enum.__pydantic_core_schema__.get("schema", {})
|
||||||
computed_fields = model_schema.get("computed_fields", [])
|
computed_fields = model_schema.get("computed_fields", [])
|
||||||
return len(computed_fields) > 0
|
return len(computed_fields) > 0
|
||||||
return False
|
return False # pragma: no cover
|
||||||
|
|
||||||
|
|
||||||
def _has_computed_fields(field: ModelField) -> bool:
|
def _has_computed_fields(field: ModelField) -> bool:
|
||||||
|
|
|
||||||
|
|
@ -8,8 +8,8 @@ from pydantic import BaseModel
|
||||||
from .utils import needs_pydanticv2
|
from .utils import needs_pydanticv2
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(name="client")
|
@pytest.fixture(name="client", params=[True, False])
|
||||||
def get_client():
|
def get_client(request: pytest.FixtureRequest):
|
||||||
from pydantic import computed_field
|
from pydantic import computed_field
|
||||||
|
|
||||||
class MyModel(BaseModel):
|
class MyModel(BaseModel):
|
||||||
|
|
@ -22,11 +22,7 @@ def get_client():
|
||||||
def is_adult(self) -> bool:
|
def is_adult(self) -> bool:
|
||||||
return self.age >= 18
|
return self.age >= 18
|
||||||
|
|
||||||
app = FastAPI(separate_input_output_schemas=False)
|
app = FastAPI(separate_input_output_schemas=request.param)
|
||||||
|
|
||||||
@app.get("/item")
|
|
||||||
def get_item() -> MyModel:
|
|
||||||
return MyModel(id=1, name="Alice", age=30)
|
|
||||||
|
|
||||||
@app.get("/list")
|
@app.get("/list")
|
||||||
def get_items() -> List[MyModel]:
|
def get_items() -> List[MyModel]:
|
||||||
|
|
@ -39,6 +35,26 @@ def get_client():
|
||||||
yield TestClient(app)
|
yield TestClient(app)
|
||||||
|
|
||||||
|
|
||||||
|
@needs_pydanticv2
|
||||||
|
def test_create_item(client: TestClient):
|
||||||
|
response = client.post(
|
||||||
|
"/item",
|
||||||
|
json={"id": 1, "name": "Alice", "age": 30},
|
||||||
|
)
|
||||||
|
assert response.status_code == 200, response.text
|
||||||
|
assert response.json() == {"id": 1, "name": "Alice", "age": 30, "is_adult": True}
|
||||||
|
|
||||||
|
|
||||||
|
@needs_pydanticv2
|
||||||
|
def test_get_items(client: TestClient):
|
||||||
|
response = client.get("/list")
|
||||||
|
assert response.status_code == 200, response.text
|
||||||
|
assert response.json() == [
|
||||||
|
{"id": 1, "name": "Alice", "age": 30, "is_adult": True},
|
||||||
|
{"id": 2, "name": "Bob", "age": 17, "is_adult": False},
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
@needs_pydanticv2
|
@needs_pydanticv2
|
||||||
def test_openapi(client: TestClient):
|
def test_openapi(client: TestClient):
|
||||||
response = client.get("/openapi.json")
|
response = client.get("/openapi.json")
|
||||||
|
|
@ -51,22 +67,6 @@ def test_openapi(client: TestClient):
|
||||||
"openapi": "3.1.0",
|
"openapi": "3.1.0",
|
||||||
"paths": {
|
"paths": {
|
||||||
"/item": {
|
"/item": {
|
||||||
"get": {
|
|
||||||
"operationId": "get_item_item_get",
|
|
||||||
"responses": {
|
|
||||||
"200": {
|
|
||||||
"content": {
|
|
||||||
"application/json": {
|
|
||||||
"schema": {
|
|
||||||
"$ref": "#/components/schemas/MyModel-Output",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"description": "Successful Response",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"summary": "Get Item",
|
|
||||||
},
|
|
||||||
"post": {
|
"post": {
|
||||||
"operationId": "create_item_item_post",
|
"operationId": "create_item_item_post",
|
||||||
"requestBody": {
|
"requestBody": {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue