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

66 statements  

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

1from fastapi import Depends, FastAPI, HTTPException, Query 1abcdef

2from sqlmodel import Field, Session, SQLModel, create_engine, select 1abcdef

3 

4 

5class HeroBase(SQLModel): 1abcdef

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

7 secret_name: str 1abcdef

8 age: int | None = Field(default=None, index=True) 1abcdef

9 

10 

11class Hero(HeroBase, table=True): 1abcdef

12 id: int | None = Field(default=None, primary_key=True) 1abcdef

13 

14 

15class HeroCreate(HeroBase): 1abcdef

16 pass 1abcdef

17 

18 

19class HeroPublic(HeroBase): 1abcdef

20 id: int 1abcdef

21 

22 

23class HeroUpdate(SQLModel): 1abcdef

24 name: str | None = None 1abcdef

25 secret_name: str | None = None 1abcdef

26 age: int | None = None 1abcdef

27 

28 

29sqlite_file_name = "database.db" 1abcdef

30sqlite_url = f"sqlite:///{sqlite_file_name}" 1abcdef

31 

32connect_args = {"check_same_thread": False} 1abcdef

33engine = create_engine(sqlite_url, echo=True, connect_args=connect_args) 1abcdef

34 

35 

36def create_db_and_tables(): 1abcdef

37 SQLModel.metadata.create_all(engine) 1TUV

38 

39 

40def get_session(): 1abcdef

41 with Session(engine) as session: 1WXY

42 yield session 1WXY

43 

44 

45app = FastAPI() 1abcdef

46 

47 

48@app.on_event("startup") 1abcdef

49def on_startup(): 1abcdef

50 create_db_and_tables() 1TUV

51 

52 

53@app.post("/heroes/", response_model=HeroPublic) 1abcdef

54def create_hero(*, session: Session = Depends(get_session), hero: HeroCreate): 1abcdef

55 db_hero = Hero.model_validate(hero) 1jklmnopqrstuvwxyzABCD

56 session.add(db_hero) 1jklmnopqrstuvwxyzABCD

57 session.commit() 1jklmnopqrstuvwxyzABCD

58 session.refresh(db_hero) 1jklmnopqrstuvwxyzABCD

59 return db_hero 1jklmnopqrstuvwxyzABCD

60 

61 

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

63def read_heroes( 1abcdef

64 *, 

65 session: Session = Depends(get_session), 

66 offset: int = 0, 

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

68): 

69 heroes = session.exec(select(Hero).offset(offset).limit(limit)).all() 1Z01

70 return heroes 1Z01

71 

72 

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

74def read_hero(*, session: Session = Depends(get_session), hero_id: int): 1abcdef

75 hero = session.get(Hero, hero_id) 1HIJKLM

76 if not hero: 1HIJKLM

77 raise HTTPException(status_code=404, detail="Hero not found") 1HJL

78 return hero 1IKM

79 

80 

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

82def update_hero( 1abcdef

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

84): 

85 db_hero = session.get(Hero, hero_id) 1NgOhPi

86 if not db_hero: 1NgOhPi

87 raise HTTPException(status_code=404, detail="Hero not found") 1NOP

88 hero_data = hero.model_dump(exclude_unset=True) 1ghi

89 for key, value in hero_data.items(): 1ghi

90 setattr(db_hero, key, value) 1ghi

91 session.add(db_hero) 1ghi

92 session.commit() 1ghi

93 session.refresh(db_hero) 1ghi

94 return db_hero 1ghi

95 

96 

97@app.delete("/heroes/{hero_id}") 1abcdef

98def delete_hero(*, session: Session = Depends(get_session), hero_id: int): 1abcdef

99 hero = session.get(Hero, hero_id) 1QERFSG

100 if not hero: 1QERFSG

101 raise HTTPException(status_code=404, detail="Hero not found") 1QRS

102 session.delete(hero) 1EFG

103 session.commit() 1EFG

104 return {"ok": True} 1EFG