Coverage for docs_src/tutorial/fastapi/relationships/tutorial001_py39.py: 100%
125 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 typing import Optional 1abcd
3from fastapi import Depends, FastAPI, HTTPException, Query 1abcd
4from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select 1abcd
7class TeamBase(SQLModel): 1abcd
8 name: str = Field(index=True) 1abcd
9 headquarters: str 1abcd
12class Team(TeamBase, table=True): 1abcd
13 id: Optional[int] = Field(default=None, primary_key=True) 1abcd
15 heroes: list["Hero"] = Relationship(back_populates="team") 1abcd
18class TeamCreate(TeamBase): 1abcd
19 pass 1abcd
22class TeamPublic(TeamBase): 1abcd
23 id: int 1abcd
26class TeamUpdate(SQLModel): 1abcd
27 id: Optional[int] = None 1abcd
28 name: Optional[str] = None 1abcd
29 headquarters: Optional[str] = None 1abcd
32class HeroBase(SQLModel): 1abcd
33 name: str = Field(index=True) 1abcd
34 secret_name: str 1abcd
35 age: Optional[int] = Field(default=None, index=True) 1abcd
37 team_id: Optional[int] = Field(default=None, foreign_key="team.id") 1abcd
40class Hero(HeroBase, table=True): 1abcd
41 id: Optional[int] = Field(default=None, primary_key=True) 1abcd
43 team: Optional[Team] = Relationship(back_populates="heroes") 1abcd
46class HeroPublic(HeroBase): 1abcd
47 id: int 1abcd
50class HeroCreate(HeroBase): 1abcd
51 pass 1abcd
54class HeroUpdate(SQLModel): 1abcd
55 name: Optional[str] = None 1abcd
56 secret_name: Optional[str] = None 1abcd
57 age: Optional[int] = None 1abcd
58 team_id: Optional[int] = None 1abcd
61class HeroPublicWithTeam(HeroPublic): 1abcd
62 team: Optional[TeamPublic] = None 1abcd
65class TeamPublicWithHeroes(TeamPublic): 1abcd
66 heroes: list[HeroPublic] = [] 1abcd
69sqlite_file_name = "database.db" 1abcd
70sqlite_url = f"sqlite:///{sqlite_file_name}" 1abcd
72connect_args = {"check_same_thread": False} 1abcd
73engine = create_engine(sqlite_url, echo=True, connect_args=connect_args) 1abcd
76def create_db_and_tables(): 1abcd
77 SQLModel.metadata.create_all(engine) 1abcd
80def get_session(): 1abcd
81 with Session(engine) as session: 1abcd
82 yield session 1abcd
85app = FastAPI() 1abcd
88@app.on_event("startup") 1abcd
89def on_startup(): 1abcd
90 create_db_and_tables() 1abcd
93@app.post("/heroes/", response_model=HeroPublic) 1abcd
94def create_hero(*, session: Session = Depends(get_session), hero: HeroCreate): 1abcd
95 db_hero = Hero.model_validate(hero) 1abcd
96 session.add(db_hero) 1abcd
97 session.commit() 1abcd
98 session.refresh(db_hero) 1abcd
99 return db_hero 1abcd
102@app.get("/heroes/", response_model=list[HeroPublic]) 1abcd
103def read_heroes( 1abcd
104 *,
105 session: Session = Depends(get_session),
106 offset: int = 0,
107 limit: int = Query(default=100, le=100),
108):
109 heroes = session.exec(select(Hero).offset(offset).limit(limit)).all() 1abcd
110 return heroes 1abcd
113@app.get("/heroes/{hero_id}", response_model=HeroPublicWithTeam) 1abcd
114def read_hero(*, session: Session = Depends(get_session), hero_id: int): 1abcd
115 hero = session.get(Hero, hero_id) 1abcd
116 if not hero: 1abcd
117 raise HTTPException(status_code=404, detail="Hero not found") 1abcd
118 return hero 1abcd
121@app.patch("/heroes/{hero_id}", response_model=HeroPublic) 1abcd
122def update_hero( 1abcd
123 *, session: Session = Depends(get_session), hero_id: int, hero: HeroUpdate
124):
125 db_hero = session.get(Hero, hero_id) 1abcd
126 if not db_hero: 1abcd
127 raise HTTPException(status_code=404, detail="Hero not found") 1abcd
128 hero_data = hero.model_dump(exclude_unset=True) 1abcd
129 for key, value in hero_data.items(): 1abcd
130 setattr(db_hero, key, value) 1abcd
131 session.add(db_hero) 1abcd
132 session.commit() 1abcd
133 session.refresh(db_hero) 1abcd
134 return db_hero 1abcd
137@app.delete("/heroes/{hero_id}") 1abcd
138def delete_hero(*, session: Session = Depends(get_session), hero_id: int): 1abcd
139 hero = session.get(Hero, hero_id) 1abcd
140 if not hero: 1abcd
141 raise HTTPException(status_code=404, detail="Hero not found") 1abcd
142 session.delete(hero) 1abcd
143 session.commit() 1abcd
144 return {"ok": True} 1abcd
147@app.post("/teams/", response_model=TeamPublic) 1abcd
148def create_team(*, session: Session = Depends(get_session), team: TeamCreate): 1abcd
149 db_team = Team.model_validate(team) 1abcd
150 session.add(db_team) 1abcd
151 session.commit() 1abcd
152 session.refresh(db_team) 1abcd
153 return db_team 1abcd
156@app.get("/teams/", response_model=list[TeamPublic]) 1abcd
157def read_teams( 1abcd
158 *,
159 session: Session = Depends(get_session),
160 offset: int = 0,
161 limit: int = Query(default=100, le=100),
162):
163 teams = session.exec(select(Team).offset(offset).limit(limit)).all() 1abcd
164 return teams 1abcd
167@app.get("/teams/{team_id}", response_model=TeamPublicWithHeroes) 1abcd
168def read_team(*, team_id: int, session: Session = Depends(get_session)): 1abcd
169 team = session.get(Team, team_id) 1abcd
170 if not team: 1abcd
171 raise HTTPException(status_code=404, detail="Team not found") 1abcd
172 return team 1abcd
175@app.patch("/teams/{team_id}", response_model=TeamPublic) 1abcd
176def update_team( 1abcd
177 *,
178 session: Session = Depends(get_session),
179 team_id: int,
180 team: TeamUpdate,
181):
182 db_team = session.get(Team, team_id) 1abcd
183 if not db_team: 1abcd
184 raise HTTPException(status_code=404, detail="Team not found") 1abcd
185 team_data = team.model_dump(exclude_unset=True) 1abcd
186 for key, value in team_data.items(): 1abcd
187 setattr(db_team, key, value) 1abcd
188 session.add(db_team) 1abcd
189 session.commit() 1abcd
190 session.refresh(db_team) 1abcd
191 return db_team 1abcd
194@app.delete("/teams/{team_id}") 1abcd
195def delete_team(*, session: Session = Depends(get_session), team_id: int): 1abcd
196 team = session.get(Team, team_id) 1abcd
197 if not team: 1abcd
198 raise HTTPException(status_code=404, detail="Team not found") 1abcd
199 session.delete(team) 1abcd
200 session.commit() 1abcd
201 return {"ok": True} 1abcd