Coverage for docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py: 100%
66 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-09-09 00:02 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-09-09 00:02 +0000
1from fastapi import Depends, FastAPI, HTTPException, Query 1abc
2from sqlmodel import Field, Session, SQLModel, create_engine, select 1abc
5class HeroBase(SQLModel): 1abc
6 name: str = Field(index=True) 1abc
7 secret_name: str 1abc
8 age: int | None = Field(default=None, index=True) 1abc
11class Hero(HeroBase, table=True): 1abc
12 id: int | None = Field(default=None, primary_key=True) 1abc
15class HeroCreate(HeroBase): 1abc
16 pass 1abc
19class HeroPublic(HeroBase): 1abc
20 id: int 1abc
23class HeroUpdate(SQLModel): 1abc
24 name: str | None = None 1abc
25 secret_name: str | None = None 1abc
26 age: int | None = None 1abc
29sqlite_file_name = "database.db" 1abc
30sqlite_url = f"sqlite:///{sqlite_file_name}" 1abc
32connect_args = {"check_same_thread": False} 1abc
33engine = create_engine(sqlite_url, echo=True, connect_args=connect_args) 1abc
36def create_db_and_tables(): 1abc
37 SQLModel.metadata.create_all(engine) 1abc
40def get_session(): 1abc
41 with Session(engine) as session: 1abc
42 yield session 1abc
45app = FastAPI() 1abc
48@app.on_event("startup") 1abc
49def on_startup(): 1abc
50 create_db_and_tables() 1abc
53@app.post("/heroes/", response_model=HeroPublic) 1abc
54def create_hero(*, session: Session = Depends(get_session), hero: HeroCreate): 1abc
55 db_hero = Hero.model_validate(hero) 1abc
56 session.add(db_hero) 1abc
57 session.commit() 1abc
58 session.refresh(db_hero) 1abc
59 return db_hero 1abc
62@app.get("/heroes/", response_model=list[HeroPublic]) 1abc
63def read_heroes( 1abc
64 *,
65 session: Session = Depends(get_session),
66 offset: int = 0,
67 limit: int = Query(default=100, le=100),
68):
69 heroes = session.exec(select(Hero).offset(offset).limit(limit)).all() 1abc
70 return heroes 1abc
73@app.get("/heroes/{hero_id}", response_model=HeroPublic) 1abc
74def read_hero(*, session: Session = Depends(get_session), hero_id: int): 1abc
75 hero = session.get(Hero, hero_id) 1abc
76 if not hero: 1abc
77 raise HTTPException(status_code=404, detail="Hero not found") 1abc
78 return hero 1abc
81@app.patch("/heroes/{hero_id}", response_model=HeroPublic) 1abc
82def update_hero( 1abc
83 *, session: Session = Depends(get_session), hero_id: int, hero: HeroUpdate
84):
85 db_hero = session.get(Hero, hero_id) 1abc
86 if not db_hero: 1abc
87 raise HTTPException(status_code=404, detail="Hero not found") 1abc
88 hero_data = hero.model_dump(exclude_unset=True) 1abc
89 for key, value in hero_data.items(): 1abc
90 setattr(db_hero, key, value) 1abc
91 session.add(db_hero) 1abc
92 session.commit() 1abc
93 session.refresh(db_hero) 1abc
94 return db_hero 1abc
97@app.delete("/heroes/{hero_id}") 1abc
98def delete_hero(*, session: Session = Depends(get_session), hero_id: int): 1abc
99 hero = session.get(Hero, hero_id) 1abc
100 if not hero: 1abc
101 raise HTTPException(status_code=404, detail="Hero not found") 1abc
102 session.delete(hero) 1abc
103 session.commit() 1abc
104 return {"ok": True} 1abc