Coverage for docs_src/tutorial/fastapi/teams/tutorial001_py39.py: 100%
120 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 name: Optional[str] = None 1abcd
28 headquarters: Optional[str] = None 1abcd
31class HeroBase(SQLModel): 1abcd
32 name: str = Field(index=True) 1abcd
33 secret_name: str 1abcd
34 age: Optional[int] = Field(default=None, index=True) 1abcd
36 team_id: Optional[int] = Field(default=None, foreign_key="team.id") 1abcd
39class Hero(HeroBase, table=True): 1abcd
40 id: Optional[int] = Field(default=None, primary_key=True) 1abcd
42 team: Optional[Team] = Relationship(back_populates="heroes") 1abcd
45class HeroPublic(HeroBase): 1abcd
46 id: int 1abcd
49class HeroCreate(HeroBase): 1abcd
50 pass 1abcd
53class HeroUpdate(SQLModel): 1abcd
54 name: Optional[str] = None 1abcd
55 secret_name: Optional[str] = None 1abcd
56 age: Optional[int] = None 1abcd
57 team_id: Optional[int] = None 1abcd
60sqlite_file_name = "database.db" 1abcd
61sqlite_url = f"sqlite:///{sqlite_file_name}" 1abcd
63connect_args = {"check_same_thread": False} 1abcd
64engine = create_engine(sqlite_url, echo=True, connect_args=connect_args) 1abcd
67def create_db_and_tables(): 1abcd
68 SQLModel.metadata.create_all(engine) 1abcd
71def get_session(): 1abcd
72 with Session(engine) as session: 1abcd
73 yield session 1abcd
76app = FastAPI() 1abcd
79@app.on_event("startup") 1abcd
80def on_startup(): 1abcd
81 create_db_and_tables() 1abcd
84@app.post("/heroes/", response_model=HeroPublic) 1abcd
85def create_hero(*, session: Session = Depends(get_session), hero: HeroCreate): 1abcd
86 db_hero = Hero.model_validate(hero) 1abcd
87 session.add(db_hero) 1abcd
88 session.commit() 1abcd
89 session.refresh(db_hero) 1abcd
90 return db_hero 1abcd
93@app.get("/heroes/", response_model=list[HeroPublic]) 1abcd
94def read_heroes( 1abcd
95 *,
96 session: Session = Depends(get_session),
97 offset: int = 0,
98 limit: int = Query(default=100, le=100),
99):
100 heroes = session.exec(select(Hero).offset(offset).limit(limit)).all() 1abcd
101 return heroes 1abcd
104@app.get("/heroes/{hero_id}", response_model=HeroPublic) 1abcd
105def read_hero(*, session: Session = Depends(get_session), hero_id: int): 1abcd
106 hero = session.get(Hero, hero_id) 1abcd
107 if not hero: 1abcd
108 raise HTTPException(status_code=404, detail="Hero not found") 1abcd
109 return hero 1abcd
112@app.patch("/heroes/{hero_id}", response_model=HeroPublic) 1abcd
113def update_hero( 1abcd
114 *, session: Session = Depends(get_session), hero_id: int, hero: HeroUpdate
115):
116 db_hero = session.get(Hero, hero_id) 1abcd
117 if not db_hero: 1abcd
118 raise HTTPException(status_code=404, detail="Hero not found") 1abcd
119 hero_data = hero.model_dump(exclude_unset=True) 1abcd
120 for key, value in hero_data.items(): 1abcd
121 setattr(db_hero, key, value) 1abcd
122 session.add(db_hero) 1abcd
123 session.commit() 1abcd
124 session.refresh(db_hero) 1abcd
125 return db_hero 1abcd
128@app.delete("/heroes/{hero_id}") 1abcd
129def delete_hero(*, session: Session = Depends(get_session), hero_id: int): 1abcd
130 hero = session.get(Hero, hero_id) 1abcd
131 if not hero: 1abcd
132 raise HTTPException(status_code=404, detail="Hero not found") 1abcd
133 session.delete(hero) 1abcd
134 session.commit() 1abcd
135 return {"ok": True} 1abcd
138@app.post("/teams/", response_model=TeamPublic) 1abcd
139def create_team(*, session: Session = Depends(get_session), team: TeamCreate): 1abcd
140 db_team = Team.model_validate(team) 1abcd
141 session.add(db_team) 1abcd
142 session.commit() 1abcd
143 session.refresh(db_team) 1abcd
144 return db_team 1abcd
147@app.get("/teams/", response_model=list[TeamPublic]) 1abcd
148def read_teams( 1abcd
149 *,
150 session: Session = Depends(get_session),
151 offset: int = 0,
152 limit: int = Query(default=100, le=100),
153):
154 teams = session.exec(select(Team).offset(offset).limit(limit)).all() 1abcd
155 return teams 1abcd
158@app.get("/teams/{team_id}", response_model=TeamPublic) 1abcd
159def read_team(*, team_id: int, session: Session = Depends(get_session)): 1abcd
160 team = session.get(Team, team_id) 1abcd
161 if not team: 1abcd
162 raise HTTPException(status_code=404, detail="Team not found") 1abcd
163 return team 1abcd
166@app.patch("/teams/{team_id}", response_model=TeamPublic) 1abcd
167def update_team( 1abcd
168 *,
169 session: Session = Depends(get_session),
170 team_id: int,
171 team: TeamUpdate,
172):
173 db_team = session.get(Team, team_id) 1abcd
174 if not db_team: 1abcd
175 raise HTTPException(status_code=404, detail="Team not found") 1abcd
176 team_data = team.model_dump(exclude_unset=True) 1abcd
177 for key, value in team_data.items(): 1abcd
178 setattr(db_team, key, value) 1abcd
179 session.add(db_team) 1abcd
180 session.commit() 1abcd
181 session.refresh(db_team) 1abcd
182 return db_team 1abcd
185@app.delete("/teams/{team_id}") 1abcd
186def delete_team(*, session: Session = Depends(get_session), team_id: int): 1abcd
187 team = session.get(Team, team_id) 1abcd
188 if not team: 1abcd
189 raise HTTPException(status_code=404, detail="Team not found") 1abcd
190 session.delete(team) 1abcd
191 session.commit() 1abcd
192 return {"ok": True} 1abcd