Browse Source

Update tests to work with Pydantic V2

pull/9319/head
Yurii Motov 1 week ago
parent
commit
f355a5e582
  1. 67
      tests/test_dependency_body_duplicated_variable.py

67
tests/test_dependency_body_duplicated_variable.py

@ -1,7 +1,6 @@
from typing import Awaitable, Callable, List from typing import Awaitable, Callable, List
from unittest.mock import ANY from unittest.mock import ANY
import pytest
from fastapi import Body, Depends, FastAPI from fastapi import Body, Depends, FastAPI
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
@ -18,15 +17,15 @@ def make_field(name: str) -> Callable[..., Awaitable[str]]:
@app.post("/example") @app.post("/example")
def example( def example(
field_0: str = Body(...), field_0: str = Body(...),
_field_1: str = Body(..., alias="field_1"), field_1_: str = Body(..., alias="field_1"),
_field_2: str = Depends(make_field("field_2")), field_2_: str = Depends(make_field("field_2")),
_field_3: str = Depends(make_field("field_3")), field_3_: str = Depends(make_field("field_3")),
) -> List[str]: ) -> List[str]:
return [field_0, _field_1, _field_2, _field_3] return [field_0, field_1_, field_2_, field_3_]
openapi_schema = { openapi_schema = {
"openapi": "3.0.2", "openapi": "3.1.0",
"info": {"title": "FastAPI", "version": "0.1.0"}, "info": {"title": "FastAPI", "version": "0.1.0"},
"paths": { "paths": {
"/example": { "/example": {
@ -90,37 +89,25 @@ def test_openapi_schema():
assert response.json() == openapi_schema assert response.json() == openapi_schema
def _field_missing(name): def test_valid():
return { response = client.post(
"loc": ["body", name], "/example/",
"msg": "field required", json={"field_0": "a", "field_1": "b", "field_2": "c", "field_3": "d"},
"type": "value_error.missing", )
} assert response.status_code == 200, response.text
assert response.json() == ["a", "b", "c", "d"]
@pytest.mark.parametrize(
"body_json,expected_status,expected_response", def test_missing():
[ response = client.post("/example/", json={})
[ assert response.status_code == 422, response.text
{}, resp_json = response.json()
422, assert len(resp_json["detail"]) == 4
{ assert resp_json["detail"][0]["loc"] == ["body", "field_2"]
"detail": [ assert str(resp_json["detail"][0]["msg"]).lower() == "field required"
_field_missing("field_2"), assert resp_json["detail"][1]["loc"] == ["body", "field_3"]
_field_missing("field_3"), assert str(resp_json["detail"][1]["msg"]).lower() == "field required"
_field_missing("field_0"), assert resp_json["detail"][2]["loc"] == ["body", "field_0"]
_field_missing("field_1"), assert str(resp_json["detail"][2]["msg"]).lower() == "field required"
], assert resp_json["detail"][3]["loc"] == ["body", "field_1"]
}, assert str(resp_json["detail"][3]["msg"]).lower() == "field required"
],
[
{"field_0": "a", "field_1": "b", "field_2": "c", "field_3": "d"},
200,
["a", "b", "c", "d"],
],
],
)
def test_endpoint(body_json, expected_status, expected_response):
response = client.post("/example/", json=body_json)
assert response.status_code == expected_status, response.text
assert response.json() == expected_response

Loading…
Cancel
Save