Browse Source

Simplify test_validate_response_recursive #13150

pull/13507/head
Valentyn Druzhynin 2 weeks ago
parent
commit
309c107457
  1. 12
      tests/test_validate_response_recursive/app.py
  2. 51
      tests/test_validate_response_recursive/app_pv2.py
  3. 5
      tests/test_validate_response_recursive/test_validate_response_recursive.py
  4. 33
      tests/test_validate_response_recursive/test_validate_response_recursive_pv2.py

12
tests/test_validate_response_recursive/app_pv1.py → tests/test_validate_response_recursive/app.py

@ -1,6 +1,7 @@
from typing import List
from fastapi import FastAPI
from fastapi._compat import PYDANTIC_V2
from pydantic import BaseModel
app = FastAPI()
@ -11,9 +12,6 @@ class RecursiveItem(BaseModel):
name: str
RecursiveItem.update_forward_refs()
class RecursiveSubitemInSubmodel(BaseModel):
sub_items2: List["RecursiveItemViaSubmodel"] = []
name: str
@ -24,7 +22,13 @@ class RecursiveItemViaSubmodel(BaseModel):
name: str
RecursiveSubitemInSubmodel.update_forward_refs()
if PYDANTIC_V2:
RecursiveItem.model_rebuild()
RecursiveSubitemInSubmodel.model_rebuild()
RecursiveItemViaSubmodel.model_rebuild()
else:
RecursiveItem.update_forward_refs()
RecursiveSubitemInSubmodel.update_forward_refs()
@app.get("/items/recursive", response_model=RecursiveItem)

51
tests/test_validate_response_recursive/app_pv2.py

@ -1,51 +0,0 @@
from typing import List
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class RecursiveItem(BaseModel):
sub_items: List["RecursiveItem"] = []
name: str
RecursiveItem.model_rebuild()
class RecursiveSubitemInSubmodel(BaseModel):
sub_items2: List["RecursiveItemViaSubmodel"] = []
name: str
class RecursiveItemViaSubmodel(BaseModel):
sub_items1: List[RecursiveSubitemInSubmodel] = []
name: str
RecursiveSubitemInSubmodel.model_rebuild()
RecursiveItemViaSubmodel.model_rebuild()
@app.get("/items/recursive", response_model=RecursiveItem)
def get_recursive():
return {"name": "item", "sub_items": [{"name": "subitem", "sub_items": []}]}
@app.get("/items/recursive-submodel", response_model=RecursiveItemViaSubmodel)
def get_recursive_submodel():
return {
"name": "item",
"sub_items1": [
{
"name": "subitem",
"sub_items2": [
{
"name": "subsubitem",
"sub_items1": [{"name": "subsubsubitem", "sub_items2": []}],
}
],
}
],
}

5
tests/test_validate_response_recursive/test_validate_response_recursive_pv1.py → tests/test_validate_response_recursive/test_validate_response_recursive.py

@ -1,12 +1,9 @@
from fastapi.testclient import TestClient
from ..utils import needs_pydanticv1
from .app import app
@needs_pydanticv1
def test_recursive():
from .app_pv1 import app
client = TestClient(app)
response = client.get("/items/recursive")
assert response.status_code == 200, response.text

33
tests/test_validate_response_recursive/test_validate_response_recursive_pv2.py

@ -1,33 +0,0 @@
from fastapi.testclient import TestClient
from ..utils import needs_pydanticv2
@needs_pydanticv2
def test_recursive():
from .app_pv2 import app
client = TestClient(app)
response = client.get("/items/recursive")
assert response.status_code == 200, response.text
assert response.json() == {
"sub_items": [{"name": "subitem", "sub_items": []}],
"name": "item",
}
response = client.get("/items/recursive-submodel")
assert response.status_code == 200, response.text
assert response.json() == {
"name": "item",
"sub_items1": [
{
"name": "subitem",
"sub_items2": [
{
"name": "subsubitem",
"sub_items1": [{"name": "subsubsubitem", "sub_items2": []}],
}
],
}
],
}
Loading…
Cancel
Save