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

125 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 id: Optional[int] = None 1fabcde

28 name: Optional[str] = None 1fabcde

29 headquarters: Optional[str] = None 1fabcde

30 

31 

32class HeroBase(SQLModel): 1fabcde

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

34 secret_name: str 1fabcde

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

36 

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

38 

39 

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

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

42 

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

44 

45 

46class HeroPublic(HeroBase): 1fabcde

47 id: int 1fabcde

48 

49 

50class HeroCreate(HeroBase): 1fabcde

51 pass 1fabcde

52 

53 

54class HeroUpdate(SQLModel): 1fabcde

55 name: Optional[str] = None 1fabcde

56 secret_name: Optional[str] = None 1fabcde

57 age: Optional[int] = None 1fabcde

58 team_id: Optional[int] = None 1fabcde

59 

60 

61class HeroPublicWithTeam(HeroPublic): 1fabcde

62 team: Optional[TeamPublic] = None 1fabcde

63 

64 

65class TeamPublicWithHeroes(TeamPublic): 1fabcde

66 heroes: List[HeroPublic] = [] 1fabcde

67 

68 

69sqlite_file_name = "database.db" 1fabcde

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

71 

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

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

74 

75 

76def create_db_and_tables(): 1fabcde

77 SQLModel.metadata.create_all(engine) 1fabcde

78 

79 

80def get_session(): 1fabcde

81 with Session(engine) as session: 1fabcde

82 yield session 1fabcde

83 

84 

85app = FastAPI() 1fabcde

86 

87 

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

89def on_startup(): 1abcde

90 create_db_and_tables() 1fabcde

91 

92 

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

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

95 db_hero = Hero.model_validate(hero) 1fabcde

96 session.add(db_hero) 1fabcde

97 session.commit() 1fabcde

98 session.refresh(db_hero) 1fabcde

99 return db_hero 1fabcde

100 

101 

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

103def read_heroes( 1abcde

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

110 return heroes 1fabcde

111 

112 

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

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

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

116 if not hero: 1fabcde

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

118 return hero 1fabcde

119 

120 

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

122def update_hero( 1abcde

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

124): 

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

126 if not db_hero: 1fabcde

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

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

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

130 setattr(db_hero, key, value) 1fabcde

131 session.add(db_hero) 1fabcde

132 session.commit() 1fabcde

133 session.refresh(db_hero) 1fabcde

134 return db_hero 1fabcde

135 

136 

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

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

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

140 if not hero: 1fabcde

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

142 session.delete(hero) 1fabcde

143 session.commit() 1fabcde

144 return {"ok": True} 1fabcde

145 

146 

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

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

149 db_team = Team.model_validate(team) 1fabcde

150 session.add(db_team) 1fabcde

151 session.commit() 1fabcde

152 session.refresh(db_team) 1fabcde

153 return db_team 1fabcde

154 

155 

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

157def read_teams( 1abcde

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

164 return teams 1fabcde

165 

166 

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

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

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

170 if not team: 1fabcde

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

172 return team 1fabcde

173 

174 

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

176def update_team( 1abcde

177 *, 

178 session: Session = Depends(get_session), 

179 team_id: int, 

180 team: TeamUpdate, 

181): 

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

183 if not db_team: 1fabcde

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

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

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

187 setattr(db_team, key, value) 1fabcde

188 session.add(db_team) 1fabcde

189 session.commit() 1fabcde

190 session.refresh(db_team) 1fabcde

191 return db_team 1fabcde

192 

193 

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

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

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

197 if not team: 1fabcde

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

199 session.delete(team) 1fabcde

200 session.commit() 1fabcde

201 return {"ok": True} 1fabcde