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

119 statements  

« 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

3 

4 

5class TeamBase(SQLModel): 1abc

6 name: str = Field(index=True) 1abc

7 headquarters: str 1abc

8 

9 

10class Team(TeamBase, table=True): 1abc

11 id: int | None = Field(default=None, primary_key=True) 1abc

12 

13 heroes: list["Hero"] = Relationship(back_populates="team") 1abc

14 

15 

16class TeamCreate(TeamBase): 1abc

17 pass 1abc

18 

19 

20class TeamPublic(TeamBase): 1abc

21 id: int 1abc

22 

23 

24class TeamUpdate(SQLModel): 1abc

25 name: str | None = None 1abc

26 headquarters: str | None = None 1abc

27 

28 

29class HeroBase(SQLModel): 1abc

30 name: str = Field(index=True) 1abc

31 secret_name: str 1abc

32 age: int | None = Field(default=None, index=True) 1abc

33 

34 team_id: int | None = Field(default=None, foreign_key="team.id") 1abc

35 

36 

37class Hero(HeroBase, table=True): 1abc

38 id: int | None = Field(default=None, primary_key=True) 1abc

39 

40 team: Team | None = Relationship(back_populates="heroes") 1abc

41 

42 

43class HeroPublic(HeroBase): 1abc

44 id: int 1abc

45 

46 

47class HeroCreate(HeroBase): 1abc

48 pass 1abc

49 

50 

51class HeroUpdate(SQLModel): 1abc

52 name: str | None = None 1abc

53 secret_name: str | None = None 1abc

54 age: int | None = None 1abc

55 team_id: int | None = None 1abc

56 

57 

58sqlite_file_name = "database.db" 1abc

59sqlite_url = f"sqlite:///{sqlite_file_name}" 1abc

60 

61connect_args = {"check_same_thread": False} 1abc

62engine = create_engine(sqlite_url, echo=True, connect_args=connect_args) 1abc

63 

64 

65def create_db_and_tables(): 1abc

66 SQLModel.metadata.create_all(engine) 1abc

67 

68 

69def get_session(): 1abc

70 with Session(engine) as session: 1abc

71 yield session 1abc

72 

73 

74app = FastAPI() 1abc

75 

76 

77@app.on_event("startup") 1abc

78def on_startup(): 1abc

79 create_db_and_tables() 1abc

80 

81 

82@app.post("/heroes/", response_model=HeroPublic) 1abc

83def create_hero(*, session: Session = Depends(get_session), hero: HeroCreate): 1abc

84 db_hero = Hero.model_validate(hero) 1abc

85 session.add(db_hero) 1abc

86 session.commit() 1abc

87 session.refresh(db_hero) 1abc

88 return db_hero 1abc

89 

90 

91@app.get("/heroes/", response_model=list[HeroPublic]) 1abc

92def read_heroes( 1abc

93 *, 

94 session: Session = Depends(get_session), 

95 offset: int = 0, 

96 limit: int = Query(default=100, le=100), 

97): 

98 heroes = session.exec(select(Hero).offset(offset).limit(limit)).all() 1abc

99 return heroes 1abc

100 

101 

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

103def read_hero(*, session: Session = Depends(get_session), hero_id: int): 1abc

104 hero = session.get(Hero, hero_id) 1abc

105 if not hero: 1abc

106 raise HTTPException(status_code=404, detail="Hero not found") 1abc

107 return hero 1abc

108 

109 

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

111def update_hero( 1abc

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

113): 

114 db_hero = session.get(Hero, hero_id) 1abc

115 if not db_hero: 1abc

116 raise HTTPException(status_code=404, detail="Hero not found") 1abc

117 hero_data = hero.model_dump(exclude_unset=True) 1abc

118 for key, value in hero_data.items(): 1abc

119 setattr(db_hero, key, value) 1abc

120 session.add(db_hero) 1abc

121 session.commit() 1abc

122 session.refresh(db_hero) 1abc

123 return db_hero 1abc

124 

125 

126@app.delete("/heroes/{hero_id}") 1abc

127def delete_hero(*, session: Session = Depends(get_session), hero_id: int): 1abc

128 hero = session.get(Hero, hero_id) 1abc

129 if not hero: 1abc

130 raise HTTPException(status_code=404, detail="Hero not found") 1abc

131 session.delete(hero) 1abc

132 session.commit() 1abc

133 return {"ok": True} 1abc

134 

135 

136@app.post("/teams/", response_model=TeamPublic) 1abc

137def create_team(*, session: Session = Depends(get_session), team: TeamCreate): 1abc

138 db_team = Team.model_validate(team) 1abc

139 session.add(db_team) 1abc

140 session.commit() 1abc

141 session.refresh(db_team) 1abc

142 return db_team 1abc

143 

144 

145@app.get("/teams/", response_model=list[TeamPublic]) 1abc

146def read_teams( 1abc

147 *, 

148 session: Session = Depends(get_session), 

149 offset: int = 0, 

150 limit: int = Query(default=100, le=100), 

151): 

152 teams = session.exec(select(Team).offset(offset).limit(limit)).all() 1abc

153 return teams 1abc

154 

155 

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

157def read_team(*, team_id: int, session: Session = Depends(get_session)): 1abc

158 team = session.get(Team, team_id) 1abc

159 if not team: 1abc

160 raise HTTPException(status_code=404, detail="Team not found") 1abc

161 return team 1abc

162 

163 

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

165def update_team( 1abc

166 *, 

167 session: Session = Depends(get_session), 

168 team_id: int, 

169 team: TeamUpdate, 

170): 

171 db_team = session.get(Team, team_id) 1abc

172 if not db_team: 1abc

173 raise HTTPException(status_code=404, detail="Team not found") 1abc

174 team_data = team.model_dump(exclude_unset=True) 1abc

175 for key, value in team_data.items(): 1abc

176 setattr(db_team, key, value) 1abc

177 session.add(db_team) 1abc

178 session.commit() 1abc

179 session.refresh(db_team) 1abc

180 return db_team 1abc

181 

182 

183@app.delete("/teams/{team_id}") 1abc

184def delete_team(*, session: Session = Depends(get_session), team_id: int): 1abc

185 team = session.get(Team, team_id) 1abc

186 if not team: 1abc

187 raise HTTPException(status_code=404, detail="Team not found") 1abc

188 session.delete(team) 1abc

189 session.commit() 1abc

190 return {"ok": True} 1abc