| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,13 +1,12 @@ | |||
| 1 | 1 | import json | |
| 2 | 2 | import sys | |
| 3 | - import warnings | ||
| 4 | 3 | from collections.abc import Iterator | |
| 5 | 4 | from typing import Annotated, Any | |
| 6 | 5 | ||
| 7 | 6 | import pytest | |
| 8 | 7 | from fastapi import Depends, FastAPI | |
| 9 | - from fastapi.exceptions import FastAPIDeprecationWarning | ||
| 10 | 8 | from fastapi.testclient import TestClient | |
| 9 | + from pydantic import BaseModel | ||
| 11 | 10 | ||
| 12 | 11 | if "--codspeed" not in sys.argv: | |
| 13 | 12 | pytest.skip( | |
@@ -47,148 +46,143 @@ def dep_b(a: Annotated[int, Depends(dep_a)]): | |||
| 47 | 46 | return a + 2 | |
| 48 | 47 | ||
| 49 | 48 | ||
| 50 | - @pytest.fixture( | ||
| 51 | - scope="module", | ||
| 52 | - params=[ | ||
| 53 | - "pydantic-v2", | ||
| 54 | - "pydantic-v1", | ||
| 55 | - ], | ||
| 56 | - ) | ||
| 57 | - def basemodel_class(request: pytest.FixtureRequest) -> type[Any]: | ||
| 58 | - if request.param == "pydantic-v2": | ||
| 59 | - from pydantic import BaseModel | ||
| 49 | + class ItemIn(BaseModel): | ||
| 50 | + name: str | ||
| 51 | + value: int | ||
| 60 | 52 | ||
| 61 | - return BaseModel | ||
| 62 | - else: | ||
| 63 | - from pydantic.v1 import BaseModel | ||
| 64 | 53 | ||
| 65 | - return BaseModel | ||
| 54 | + class ItemOut(BaseModel): | ||
| 55 | + name: str | ||
| 56 | + value: int | ||
| 57 | + dep: int | ||
| 66 | 58 | ||
| 67 | 59 | ||
| 68 | - @pytest.fixture(scope="module") | ||
| 69 | - def app(basemodel_class: type[Any]) -> FastAPI: | ||
| 70 | - class ItemIn(basemodel_class): | ||
| 71 | - name: str | ||
| 72 | - value: int | ||
| 73 | - | ||
| 74 | - class ItemOut(basemodel_class): | ||
| 75 | - name: str | ||
| 76 | - value: int | ||
| 77 | - dep: int | ||
| 78 | - | ||
| 79 | - class LargeIn(basemodel_class): | ||
| 80 | - items: list[dict[str, Any]] | ||
| 81 | - metadata: dict[str, Any] | ||
| 82 | - | ||
| 83 | - class LargeOut(basemodel_class): | ||
| 84 | - items: list[dict[str, Any]] | ||
| 85 | - metadata: dict[str, Any] | ||
| 86 | - | ||
| 87 | - app = FastAPI() | ||
| 88 | - | ||
| 89 | - with warnings.catch_warnings(record=True): | ||
| 90 | - warnings.filterwarnings( | ||
| 91 | - "ignore", | ||
| 92 | - message=r"pydantic\.v1 is deprecated and will soon stop being supported by FastAPI\..*", | ||
| 93 | - category=FastAPIDeprecationWarning, | ||
| 94 | - ) | ||
| 95 | - | ||
| 96 | - @app.post("/sync/validated", response_model=ItemOut) | ||
| 97 | - def sync_validated(item: ItemIn, dep: Annotated[int, Depends(dep_b)]): | ||
| 98 | - return ItemOut(name=item.name, value=item.value, dep=dep) | ||
| 99 | - | ||
| 100 | - @app.get("/sync/dict-no-response-model") | ||
| 101 | - def sync_dict_no_response_model(): | ||
| 102 | - return {"name": "foo", "value": 123} | ||
| 103 | - | ||
| 104 | - @app.get("/sync/dict-with-response-model", response_model=ItemOut) | ||
| 105 | - def sync_dict_with_response_model( | ||
| 106 | - dep: Annotated[int, Depends(dep_b)], | ||
| 107 | - ): | ||
| 108 | - return {"name": "foo", "value": 123, "dep": dep} | ||
| 109 | - | ||
| 110 | - @app.get("/sync/model-no-response-model") | ||
| 111 | - def sync_model_no_response_model(dep: Annotated[int, Depends(dep_b)]): | ||
| 112 | - return ItemOut(name="foo", value=123, dep=dep) | ||
| 113 | - | ||
| 114 | - @app.get("/sync/model-with-response-model", response_model=ItemOut) | ||
| 115 | - def sync_model_with_response_model(dep: Annotated[int, Depends(dep_b)]): | ||
| 116 | - return ItemOut(name="foo", value=123, dep=dep) | ||
| 117 | - | ||
| 118 | - @app.post("/async/validated", response_model=ItemOut) | ||
| 119 | - async def async_validated( | ||
| 120 | - item: ItemIn, | ||
| 121 | - dep: Annotated[int, Depends(dep_b)], | ||
| 122 | - ): | ||
| 123 | - return ItemOut(name=item.name, value=item.value, dep=dep) | ||
| 124 | - | ||
| 125 | - @app.post("/sync/large-receive") | ||
| 126 | - def sync_large_receive(payload: LargeIn): | ||
| 127 | - return {"received": len(payload.items)} | ||
| 128 | - | ||
| 129 | - @app.post("/async/large-receive") | ||
| 130 | - async def async_large_receive(payload: LargeIn): | ||
| 131 | - return {"received": len(payload.items)} | ||
| 132 | - | ||
| 133 | - @app.get("/sync/large-dict-no-response-model") | ||
| 134 | - def sync_large_dict_no_response_model(): | ||
| 135 | - return LARGE_PAYLOAD | ||
| 136 | - | ||
| 137 | - @app.get("/sync/large-dict-with-response-model", response_model=LargeOut) | ||
| 138 | - def sync_large_dict_with_response_model(): | ||
| 139 | - return LARGE_PAYLOAD | ||
| 140 | - | ||
| 141 | - @app.get("/sync/large-model-no-response-model") | ||
| 142 | - def sync_large_model_no_response_model(): | ||
| 143 | - return LargeOut(items=LARGE_ITEMS, metadata=LARGE_METADATA) | ||
| 144 | - | ||
| 145 | - @app.get("/sync/large-model-with-response-model", response_model=LargeOut) | ||
| 146 | - def sync_large_model_with_response_model(): | ||
| 147 | - return LargeOut(items=LARGE_ITEMS, metadata=LARGE_METADATA) | ||
| 148 | - | ||
| 149 | - @app.get("/async/large-dict-no-response-model") | ||
| 150 | - async def async_large_dict_no_response_model(): | ||
| 151 | - return LARGE_PAYLOAD | ||
| 152 | - | ||
| 153 | - @app.get("/async/large-dict-with-response-model", response_model=LargeOut) | ||
| 154 | - async def async_large_dict_with_response_model(): | ||
| 155 | - return LARGE_PAYLOAD | ||
| 156 | - | ||
| 157 | - @app.get("/async/large-model-no-response-model") | ||
| 158 | - async def async_large_model_no_response_model(): | ||
| 159 | - return LargeOut(items=LARGE_ITEMS, metadata=LARGE_METADATA) | ||
| 160 | - | ||
| 161 | - @app.get("/async/large-model-with-response-model", response_model=LargeOut) | ||
| 162 | - async def async_large_model_with_response_model(): | ||
| 163 | - return LargeOut(items=LARGE_ITEMS, metadata=LARGE_METADATA) | ||
| 164 | - | ||
| 165 | - @app.get("/async/dict-no-response-model") | ||
| 166 | - async def async_dict_no_response_model(): | ||
| 167 | - return {"name": "foo", "value": 123} | ||
| 168 | - | ||
| 169 | - @app.get("/async/dict-with-response-model", response_model=ItemOut) | ||
| 170 | - async def async_dict_with_response_model( | ||
| 171 | - dep: Annotated[int, Depends(dep_b)], | ||
| 172 | - ): | ||
| 173 | - return {"name": "foo", "value": 123, "dep": dep} | ||
| 174 | - | ||
| 175 | - @app.get("/async/model-no-response-model") | ||
| 176 | - async def async_model_no_response_model( | ||
| 177 | - dep: Annotated[int, Depends(dep_b)], | ||
| 178 | - ): | ||
| 179 | - return ItemOut(name="foo", value=123, dep=dep) | ||
| 180 | - | ||
| 181 | - @app.get("/async/model-with-response-model", response_model=ItemOut) | ||
| 182 | - async def async_model_with_response_model( | ||
| 183 | - dep: Annotated[int, Depends(dep_b)], | ||
| 184 | - ): | ||
| 185 | - return ItemOut(name="foo", value=123, dep=dep) | ||
| 186 | - | ||
| 187 | - return app | ||
| 60 | + class LargeIn(BaseModel): | ||
| 61 | + items: list[dict[str, Any]] | ||
| 62 | + metadata: dict[str, Any] | ||
| 63 | + | ||
| 64 | + | ||
| 65 | + class LargeOut(BaseModel): | ||
| 66 | + items: list[dict[str, Any]] | ||
| 67 | + metadata: dict[str, Any] | ||
| 68 | + | ||
| 69 | + | ||
| 70 | + app = FastAPI() | ||
| 71 | + | ||
| 72 | + | ||
| 73 | + @app.post("/sync/validated", response_model=ItemOut) | ||
| 74 | + def sync_validated(item: ItemIn, dep: Annotated[int, Depends(dep_b)]): | ||
| 75 | + return ItemOut(name=item.name, value=item.value, dep=dep) | ||
| 76 | + | ||
| 77 | + | ||
| 78 | + @app.get("/sync/dict-no-response-model") | ||
| 79 | + def sync_dict_no_response_model(): | ||
| 80 | + return {"name": "foo", "value": 123} | ||
| 81 | + | ||
| 82 | + | ||
| 83 | + @app.get("/sync/dict-with-response-model", response_model=ItemOut) | ||
| 84 | + def sync_dict_with_response_model( | ||
| 85 | + dep: Annotated[int, Depends(dep_b)], | ||
| 86 | + ): | ||
| 87 | + return {"name": "foo", "value": 123, "dep": dep} | ||
| 88 | + | ||
| 89 | + | ||
| 90 | + @app.get("/sync/model-no-response-model") | ||
| 91 | + def sync_model_no_response_model(dep: Annotated[int, Depends(dep_b)]): | ||
| 92 | + return ItemOut(name="foo", value=123, dep=dep) | ||
| 93 | + | ||
| 94 | + | ||
| 95 | + @app.get("/sync/model-with-response-model", response_model=ItemOut) | ||
| 96 | + def sync_model_with_response_model(dep: Annotated[int, Depends(dep_b)]): | ||
| 97 | + return ItemOut(name="foo", value=123, dep=dep) | ||
| 98 | + | ||
| 99 | + | ||
| 100 | + @app.post("/async/validated", response_model=ItemOut) | ||
| 101 | + async def async_validated( | ||
| 102 | + item: ItemIn, | ||
| 103 | + dep: Annotated[int, Depends(dep_b)], | ||
| 104 | + ): | ||
| 105 | + return ItemOut(name=item.name, value=item.value, dep=dep) | ||
| 106 | + | ||
| 107 | + | ||
| 108 | + @app.post("/sync/large-receive") | ||
| 109 | + def sync_large_receive(payload: LargeIn): | ||
| 110 | + return {"received": len(payload.items)} | ||
| 111 | + | ||
| 112 | + | ||
| 113 | + @app.post("/async/large-receive") | ||
| 114 | + async def async_large_receive(payload: LargeIn): | ||
| 115 | + return {"received": len(payload.items)} | ||
| 116 | + | ||
| 117 | + | ||
| 118 | + @app.get("/sync/large-dict-no-response-model") | ||
| 119 | + def sync_large_dict_no_response_model(): | ||
| 120 | + return LARGE_PAYLOAD | ||
| 121 | + | ||
| 122 | + | ||
| 123 | + @app.get("/sync/large-dict-with-response-model", response_model=LargeOut) | ||
| 124 | + def sync_large_dict_with_response_model(): | ||
| 125 | + return LARGE_PAYLOAD | ||
| 126 | + | ||
| 127 | + | ||
| 128 | + @app.get("/sync/large-model-no-response-model") | ||
| 129 | + def sync_large_model_no_response_model(): | ||
| 130 | + return LargeOut(items=LARGE_ITEMS, metadata=LARGE_METADATA) | ||
| 131 | + | ||
| 132 | + | ||
| 133 | + @app.get("/sync/large-model-with-response-model", response_model=LargeOut) | ||
| 134 | + def sync_large_model_with_response_model(): | ||
| 135 | + return LargeOut(items=LARGE_ITEMS, metadata=LARGE_METADATA) | ||
| 136 | + | ||
| 137 | + | ||
| 138 | + @app.get("/async/large-dict-no-response-model") | ||
| 139 | + async def async_large_dict_no_response_model(): | ||
| 140 | + return LARGE_PAYLOAD | ||
| 141 | + | ||
| 142 | + | ||
| 143 | + @app.get("/async/large-dict-with-response-model", response_model=LargeOut) | ||
| 144 | + async def async_large_dict_with_response_model(): | ||
| 145 | + return LARGE_PAYLOAD | ||
| 146 | + | ||
| 147 | + | ||
| 148 | + @app.get("/async/large-model-no-response-model") | ||
| 149 | + async def async_large_model_no_response_model(): | ||
| 150 | + return LargeOut(items=LARGE_ITEMS, metadata=LARGE_METADATA) | ||
| 151 | + | ||
| 152 | + | ||
| 153 | + @app.get("/async/large-model-with-response-model", response_model=LargeOut) | ||
| 154 | + async def async_large_model_with_response_model(): | ||
| 155 | + return LargeOut(items=LARGE_ITEMS, metadata=LARGE_METADATA) | ||
| 156 | + | ||
| 157 | + | ||
| 158 | + @app.get("/async/dict-no-response-model") | ||
| 159 | + async def async_dict_no_response_model(): | ||
| 160 | + return {"name": "foo", "value": 123} | ||
| 161 | + | ||
| 162 | + | ||
| 163 | + @app.get("/async/dict-with-response-model", response_model=ItemOut) | ||
| 164 | + async def async_dict_with_response_model( | ||
| 165 | + dep: Annotated[int, Depends(dep_b)], | ||
| 166 | + ): | ||
| 167 | + return {"name": "foo", "value": 123, "dep": dep} | ||
| 168 | + | ||
| 169 | + | ||
| 170 | + @app.get("/async/model-no-response-model") | ||
| 171 | + async def async_model_no_response_model( | ||
| 172 | + dep: Annotated[int, Depends(dep_b)], | ||
| 173 | + ): | ||
| 174 | + return ItemOut(name="foo", value=123, dep=dep) | ||
| 175 | + | ||
| 176 | + | ||
| 177 | + @app.get("/async/model-with-response-model", response_model=ItemOut) | ||
| 178 | + async def async_model_with_response_model( | ||
| 179 | + dep: Annotated[int, Depends(dep_b)], | ||
| 180 | + ): | ||
| 181 | + return ItemOut(name="foo", value=123, dep=dep) | ||
| 188 | 182 | ||
| 189 | 183 | ||
| 190 | 184 | @pytest.fixture(scope="module") | |
| 191 | - def client(app: FastAPI) -> Iterator[TestClient]: | ||
| 185 | + def client() -> Iterator[TestClient]: | ||
| 192 | 186 | with TestClient(app) as client: | |
| 193 | 187 | yield client | |
| 194 | 188 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments