Coverage for docs_src/tutorial/fastapi/relationships/tutorial001_py310.py: 100%
124 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, Relationship, Session, SQLModel, create_engine, select 1abc
5class TeamBase(SQLModel): 1abc
6 name: str = Field(index=True) 1abc
7 headquarters: str 1abc
10class Team(TeamBase, table=True): 1abc
11 id: int | None = Field(default=None, primary_key=True) 1abc
13 heroes: list["Hero"] = Relationship(back_populates="team") 1abc
16class TeamCreate(TeamBase): 1abc
17 pass 1abc
20class TeamPublic(TeamBase): 1abc
21 id: int 1abc
24class TeamUpdate(SQLModel): 1abc
25 id: int | None = None 1abc
26 name: str | None = None 1abc
27 headquarters: str | None = None 1abc
30class HeroBase(SQLModel): 1abc
31 name: str = Field(index=True) 1abc
32 secret_name: str 1abc
33 age: int | None = Field(default=None, index=True) 1abc
35 team_id: int | None = Field(default=None, foreign_key="team.id") 1abc
38class Hero(HeroBase, table=True): 1abc
39 id: int | None = Field(default=None, primary_key=True) 1abc
41 team: Team | None = Relationship(back_populates="heroes") 1abc
44class HeroPublic(HeroBase): 1abc
45 id: int 1abc
48class HeroCreate(HeroBase): 1abc
49 pass 1abc
52class HeroUpdate(SQLModel): 1abc
53 name: str | None = None 1abc
54 secret_name: str | None = None 1abc
55 age: int | None = None 1abc
56 team_id: int | None = None 1abc
59class HeroPublicWithTeam(HeroPublic): 1abc
60 team: TeamPublic | None = None 1abc
63class TeamPublicWithHeroes(TeamPublic): 1abc
64 heroes: list[HeroPublic] = [] 1abc
67sqlite_file_name = "database.db" 1abc
68sqlite_url = f"sqlite:///{sqlite_file_name}" 1abc
70connect_args = {"check_same_thread": False} 1abc
71engine = create_engine(sqlite_url, echo=True, connect_args=connect_args) 1abc
74def create_db_and_tables(): 1abc
75 SQLModel.metadata.create_all(engine) 1abc
78def get_session(): 1abc
79 with Session(engine) as session: 1abc
80 yield session 1abc
83app = FastAPI() 1abc
86@app.on_event("startup") 1abc
87def on_startup(): 1abc
88 create_db_and_tables() 1abc
91@app.post("/heroes/", response_model=HeroPublic) 1abc
92def create_hero(*, session: Session = Depends(get_session), hero: HeroCreate): 1abc
93 db_hero = Hero.model_validate(hero) 1abc
94 session.add(db_hero) 1abc
95 session.commit() 1abc
96 session.refresh(db_hero) 1abc
97 return db_hero 1abc
100@app.get("/heroes/", response_model=list[HeroPublic]) 1abc
101def read_heroes( 1abc
102 *,
103 session: Session = Depends(get_session),
104 offset: int = 0,
105 limit: int = Query(default=100, le=100),
106):
107 heroes = session.exec(select(Hero).offset(offset).limit(limit)).all() 1abc
108 return heroes 1abc
111@app.get("/heroes/{hero_id}", response_model=HeroPublicWithTeam) 1abc
112def read_hero(*, session: Session = Depends(get_session), hero_id: int): 1abc
113 hero = session.get(Hero, hero_id) 1abc
114 if not hero: 1abc
115 raise HTTPException(status_code=404, detail="Hero not found") 1abc
116 return hero 1abc
119@app.patch("/heroes/{hero_id}", response_model=HeroPublic) 1abc
120def update_hero( 1abc
121 *, session: Session = Depends(get_session), hero_id: int, hero: HeroUpdate
122):
123 db_hero = session.get(Hero, hero_id) 1abc
124 if not db_hero: 1abc
125 raise HTTPException(status_code=404, detail="Hero not found") 1abc
126 hero_data = hero.model_dump(exclude_unset=True) 1abc
127 for key, value in hero_data.items(): 1abc
128 setattr(db_hero, key, value) 1abc
129 session.add(db_hero) 1abc
130 session.commit() 1abc
131 session.refresh(db_hero) 1abc
132 return db_hero 1abc
135@app.delete("/heroes/{hero_id}") 1abc
136def delete_hero(*, session: Session = Depends(get_session), hero_id: int): 1abc
137 hero = session.get(Hero, hero_id) 1abc
138 if not hero: 1abc
139 raise HTTPException(status_code=404, detail="Hero not found") 1abc
140 session.delete(hero) 1abc
141 session.commit() 1abc
142 return {"ok": True} 1abc
145@app.post("/teams/", response_model=TeamPublic) 1abc
146def create_team(*, session: Session = Depends(get_session), team: TeamCreate): 1abc
147 db_team = Team.model_validate(team) 1abc
148 session.add(db_team) 1abc
149 session.commit() 1abc
150 session.refresh(db_team) 1abc
151 return db_team 1abc
154@app.get("/teams/", response_model=list[TeamPublic]) 1abc
155def read_teams( 1abc
156 *,
157 session: Session = Depends(get_session),
158 offset: int = 0,
159 limit: int = Query(default=100, le=100),
160):
161 teams = session.exec(select(Team).offset(offset).limit(limit)).all() 1abc
162 return teams 1abc
165@app.get("/teams/{team_id}", response_model=TeamPublicWithHeroes) 1abc
166def read_team(*, team_id: int, session: Session = Depends(get_session)): 1abc
167 team = session.get(Team, team_id) 1abc
168 if not team: 1abc
169 raise HTTPException(status_code=404, detail="Team not found") 1abc
170 return team 1abc
173@app.patch("/teams/{team_id}", response_model=TeamPublic) 1abc
174def update_team( 1abc
175 *,
176 session: Session = Depends(get_session),
177 team_id: int,
178 team: TeamUpdate,
179):
180 db_team = session.get(Team, team_id) 1abc
181 if not db_team: 1abc
182 raise HTTPException(status_code=404, detail="Team not found") 1abc
183 team_data = team.model_dump(exclude_unset=True) 1abc
184 for key, value in team_data.items(): 1abc
185 setattr(db_team, key, value) 1abc
186 session.add(db_team) 1abc
187 session.commit() 1abc
188 session.refresh(db_team) 1abc
189 return db_team 1abc
192@app.delete("/teams/{team_id}") 1abc
193def delete_team(*, session: Session = Depends(get_session), team_id: int): 1abc
194 team = session.get(Team, team_id) 1abc
195 if not team: 1abc
196 raise HTTPException(status_code=404, detail="Team not found") 1abc
197 session.delete(team) 1abc
198 session.commit() 1abc
199 return {"ok": True} 1abc