Browse Source

refactored - replaced deprecated on_event with lifespan in SQL tutorials

pull/14620/head
Ads2024 7 months ago
parent
commit
5f036d6319
  1. 15
      docs_src/sql_databases/tutorial001_an_py310.py
  2. 14
      docs_src/sql_databases/tutorial001_an_py39.py
  3. 11
      docs_src/sql_databases/tutorial001_py310.py
  4. 12
      docs_src/sql_databases/tutorial001_py39.py
  5. 5
      tests/test_tutorial/test_sql_databases/test_tutorial001.py

15
docs_src/sql_databases/tutorial001_an_py310.py

@ -1,5 +1,5 @@
from typing import Annotated from typing import Annotated
from contextlib import asynccontextmanager
from fastapi import Depends, FastAPI, HTTPException, Query from fastapi import Depends, FastAPI, HTTPException, Query
from sqlmodel import Field, Session, SQLModel, create_engine, select from sqlmodel import Field, Session, SQLModel, create_engine, select
@ -27,14 +27,15 @@ def get_session():
yield session yield session
SessionDep = Annotated[Session, Depends(get_session)] @asynccontextmanager
async def lifespan(app: FastAPI):
app = FastAPI() create_db_and_tables()
yield
# will leave this as is for now
SessionDep = Annotated[Session, Depends(get_session)]
@app.on_event("startup") app = FastAPI(lifespan=lifespan)
def on_startup():
create_db_and_tables()
@app.post("/heroes/") @app.post("/heroes/")

14
docs_src/sql_databases/tutorial001_an_py39.py

@ -1,5 +1,5 @@
from typing import Annotated, Union from typing import Annotated, Union
from contextlib import asynccontextmanager
from fastapi import Depends, FastAPI, HTTPException, Query from fastapi import Depends, FastAPI, HTTPException, Query
from sqlmodel import Field, Session, SQLModel, create_engine, select from sqlmodel import Field, Session, SQLModel, create_engine, select
@ -27,14 +27,16 @@ def get_session():
yield session yield session
SessionDep = Annotated[Session, Depends(get_session)] @asynccontextmanager
async def lifespan(app: FastAPI):
create_db_and_tables()
yield
# will leave this as is for now
app = FastAPI()
SessionDep = Annotated[Session, Depends(get_session)]
@app.on_event("startup") app = FastAPI(lifespan=lifespan)
def on_startup():
create_db_and_tables()
@app.post("/heroes/") @app.post("/heroes/")

11
docs_src/sql_databases/tutorial001_py310.py

@ -1,5 +1,6 @@
from fastapi import Depends, FastAPI, HTTPException, Query from fastapi import Depends, FastAPI, HTTPException, Query
from sqlmodel import Field, Session, SQLModel, create_engine, select from sqlmodel import Field, Session, SQLModel, create_engine, select
from contextlib import asynccontextmanager
class Hero(SQLModel, table=True): class Hero(SQLModel, table=True):
@ -25,12 +26,14 @@ def get_session():
yield session yield session
app = FastAPI() @asynccontextmanager
async def lifespan(app: FastAPI):
create_db_and_tables()
yield
# will leave this as is for now
@app.on_event("startup") app = FastAPI(lifespan=lifespan)
def on_startup():
create_db_and_tables()
@app.post("/heroes/") @app.post("/heroes/")

12
docs_src/sql_databases/tutorial001_py39.py

@ -1,5 +1,5 @@
from typing import Union from typing import Union
from contextlib import asynccontextmanager
from fastapi import Depends, FastAPI, HTTPException, Query from fastapi import Depends, FastAPI, HTTPException, Query
from sqlmodel import Field, Session, SQLModel, create_engine, select from sqlmodel import Field, Session, SQLModel, create_engine, select
@ -26,13 +26,15 @@ def get_session():
with Session(engine) as session: with Session(engine) as session:
yield session yield session
@asynccontextmanager
async def lifespan(app: FastAPI):
create_db_and_tables()
yield
# will leave this as is for now
app = FastAPI()
app = FastAPI(lifespan=lifespan)
@app.on_event("startup")
def on_startup():
create_db_and_tables()
@app.post("/heroes/") @app.post("/heroes/")

5
tests/test_tutorial/test_sql_databases/test_tutorial001.py

@ -30,8 +30,7 @@ def clear_sqlmodel():
) )
def get_client(request: pytest.FixtureRequest): def get_client(request: pytest.FixtureRequest):
clear_sqlmodel() clear_sqlmodel()
# TODO: remove when updating SQL tutorial to use new lifespan API with warnings.catch_warnings():
with warnings.catch_warnings(record=True):
warnings.simplefilter("always") warnings.simplefilter("always")
mod = importlib.import_module(f"docs_src.sql_databases.{request.param}") mod = importlib.import_module(f"docs_src.sql_databases.{request.param}")
clear_sqlmodel() clear_sqlmodel()
@ -50,7 +49,7 @@ def get_client(request: pytest.FixtureRequest):
def test_crud_app(client: TestClient): def test_crud_app(client: TestClient):
# TODO: this warns that SQLModel.from_orm is deprecated in Pydantic v1, refactor # TODO: this warns that SQLModel.from_orm is deprecated in Pydantic v1, refactor
# this if using obj.model_validate becomes independent of Pydantic v2 # this if using obj.model_validate becomes independent of Pydantic v2
with warnings.catch_warnings(record=True): with warnings.catch_warnings():
warnings.simplefilter("always") warnings.simplefilter("always")
# No heroes before creating # No heroes before creating
response = client.get("heroes/") response = client.get("heroes/")

Loading…
Cancel
Save