Coverage for docs_src/tutorial/fastapi/teams/tutorial001.py: 100%

120 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-09-09 00:02 +0000

1from typing import List, Optional 1fabcde

2 

3from fastapi import Depends, FastAPI, HTTPException, Query 1fabcde

4from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select 1fabcde

5 

6 

7class TeamBase(SQLModel): 1fabcde

8 name: str = Field(index=True) 1fabcde

9 headquarters: str 1fabcde

10 

11 

12class Team(TeamBase, table=True): 1fabcde

13 id: Optional[int] = Field(default=None, primary_key=True) 1fabcde

14 

15 heroes: List["Hero"] = Relationship(back_populates="team") 1fabcde

16 

17 

18class TeamCreate(TeamBase): 1fabcde

19 pass 1fabcde

20 

21 

22class TeamPublic(TeamBase): 1fabcde

23 id: int 1fabcde

24 

25 

26class TeamUpdate(SQLModel): 1fabcde

27 name: Optional[str] = None 1fabcde

28 headquarters: Optional[str] = None 1fabcde

29 

30 

31class HeroBase(SQLModel): 1fabcde

32 name: str = Field(index=True) 1fabcde

33 secret_name: str 1fabcde

34 age: Optional[int] = Field(default=None, index=True) 1fabcde

35 

36 team_id: Optional[int] = Field(default=None, foreign_key="team.id") 1fabcde

37 

38 

39class Hero(HeroBase, table=True): 1fabcde

40 id: Optional[int] = Field(default=None, primary_key=True) 1fabcde

41 

42 team: Optional[Team] = Relationship(back_populates="heroes") 1fabcde

43 

44 

45class HeroPublic(HeroBase): 1fabcde

46 id: int 1fabcde

47 

48 

49class HeroCreate(HeroBase): 1fabcde

50 pass 1fabcde

51 

52 

53class HeroUpdate(SQLModel): 1fabcde

54 name: Optional[str] = None 1fabcde

55 secret_name: Optional[str] = None 1fabcde

56 age: Optional[int] = None 1fabcde

57 team_id: Optional[int] = None 1fabcde

58 

59 

60sqlite_file_name = "database.db" 1fabcde

61sqlite_url = f"sqlite:///{sqlite_file_name}" 1fabcde

62 

63connect_args = {"check_same_thread": False} 1fabcde

64engine = create_engine(sqlite_url, echo=True, connect_args=connect_args) 1fabcde

65 

66 

67def create_db_and_tables(): 1fabcde

68 SQLModel.metadata.create_all(engine) 1fabcde

69 

70 

71def get_session(): 1fabcde

72 with Session(engine) as session: 1fabcde

73 yield session 1fabcde

74 

75 

76app = FastAPI() 1fabcde

77 

78 

79@app.on_event("startup") 1fabcde

80def on_startup(): 1abcde

81 create_db_and_tables() 1fabcde

82 

83 

84@app.post("/heroes/", response_model=HeroPublic) 1fabcde

85def create_hero(*, session: Session = Depends(get_session), hero: HeroCreate): 1fabcde

86 db_hero = Hero.model_validate(hero) 1fabcde

87 session.add(db_hero) 1fabcde

88 session.commit() 1fabcde

89 session.refresh(db_hero) 1fabcde

90 return db_hero 1fabcde

91 

92 

93@app.get("/heroes/", response_model=List[HeroPublic]) 1fabcde

94def read_heroes( 1abcde

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() 1fabcde

101 return heroes 1fabcde

102 

103 

104@app.get("/heroes/{hero_id}", response_model=HeroPublic) 1fabcde

105def read_hero(*, session: Session = Depends(get_session), hero_id: int): 1fabcde

106 hero = session.get(Hero, hero_id) 1fabcde

107 if not hero: 1fabcde

108 raise HTTPException(status_code=404, detail="Hero not found") 1fabcde

109 return hero 1fabcde

110 

111 

112@app.patch("/heroes/{hero_id}", response_model=HeroPublic) 1fabcde

113def update_hero( 1abcde

114 *, session: Session = Depends(get_session), hero_id: int, hero: HeroUpdate 

115): 

116 db_hero = session.get(Hero, hero_id) 1fabcde

117 if not db_hero: 1fabcde

118 raise HTTPException(status_code=404, detail="Hero not found") 1fabcde

119 hero_data = hero.model_dump(exclude_unset=True) 1fabcde

120 for key, value in hero_data.items(): 1fabcde

121 setattr(db_hero, key, value) 1fabcde

122 session.add(db_hero) 1fabcde

123 session.commit() 1fabcde

124 session.refresh(db_hero) 1fabcde

125 return db_hero 1fabcde

126 

127 

128@app.delete("/heroes/{hero_id}") 1fabcde

129def delete_hero(*, session: Session = Depends(get_session), hero_id: int): 1fabcde

130 hero = session.get(Hero, hero_id) 1fabcde

131 if not hero: 1fabcde

132 raise HTTPException(status_code=404, detail="Hero not found") 1fabcde

133 session.delete(hero) 1fabcde

134 session.commit() 1fabcde

135 return {"ok": True} 1fabcde

136 

137 

138@app.post("/teams/", response_model=TeamPublic) 1fabcde

139def create_team(*, session: Session = Depends(get_session), team: TeamCreate): 1fabcde

140 db_team = Team.model_validate(team) 1fabcde

141 session.add(db_team) 1fabcde

142 session.commit() 1fabcde

143 session.refresh(db_team) 1fabcde

144 return db_team 1fabcde

145 

146 

147@app.get("/teams/", response_model=List[TeamPublic]) 1fabcde

148def read_teams( 1abcde

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() 1fabcde

155 return teams 1fabcde

156 

157 

158@app.get("/teams/{team_id}", response_model=TeamPublic) 1fabcde

159def read_team(*, team_id: int, session: Session = Depends(get_session)): 1fabcde

160 team = session.get(Team, team_id) 1fabcde

161 if not team: 1fabcde

162 raise HTTPException(status_code=404, detail="Team not found") 1fabcde

163 return team 1fabcde

164 

165 

166@app.patch("/teams/{team_id}", response_model=TeamPublic) 1fabcde

167def update_team( 1abcde

168 *, 

169 session: Session = Depends(get_session), 

170 team_id: int, 

171 team: TeamUpdate, 

172): 

173 db_team = session.get(Team, team_id) 1fabcde

174 if not db_team: 1fabcde

175 raise HTTPException(status_code=404, detail="Team not found") 1fabcde

176 team_data = team.model_dump(exclude_unset=True) 1fabcde

177 for key, value in team_data.items(): 1fabcde

178 setattr(db_team, key, value) 1fabcde

179 session.add(db_team) 1fabcde

180 session.commit() 1fabcde

181 session.refresh(db_team) 1fabcde

182 return db_team 1fabcde

183 

184 

185@app.delete("/teams/{team_id}") 1fabcde

186def delete_team(*, session: Session = Depends(get_session), team_id: int): 1fabcde

187 team = session.get(Team, team_id) 1fabcde

188 if not team: 1fabcde

189 raise HTTPException(status_code=404, detail="Team not found") 1fabcde

190 session.delete(team) 1fabcde

191 session.commit() 1fabcde

192 return {"ok": True} 1fabcde