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

67 statements  

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

1from typing import Optional 1abcdefgh

2 

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

4from sqlmodel import Field, Session, SQLModel, create_engine, select 1abcdefgh

5 

6 

7class HeroBase(SQLModel): 1abcdefgh

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

9 secret_name: str 1abcdefgh

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

11 

12 

13class Hero(HeroBase, table=True): 1abcdefgh

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

15 

16 

17class HeroCreate(HeroBase): 1abcdefgh

18 pass 1abcdefgh

19 

20 

21class HeroPublic(HeroBase): 1abcdefgh

22 id: int 1abcdefgh

23 

24 

25class HeroUpdate(SQLModel): 1abcdefgh

26 name: Optional[str] = None 1abcdefgh

27 secret_name: Optional[str] = None 1abcdefgh

28 age: Optional[int] = None 1abcdefgh

29 

30 

31sqlite_file_name = "database.db" 1abcdefgh

32sqlite_url = f"sqlite:///{sqlite_file_name}" 1abcdefgh

33 

34connect_args = {"check_same_thread": False} 1abcdefgh

35engine = create_engine(sqlite_url, echo=True, connect_args=connect_args) 1abcdefgh

36 

37 

38def create_db_and_tables(): 1abcdefgh

39 SQLModel.metadata.create_all(engine) 189!#

40 

41 

42def get_session(): 1abcdefgh

43 with Session(engine) as session: 1$%'(

44 yield session 1$%'(

45 

46 

47app = FastAPI() 1abcdefgh

48 

49 

50@app.on_event("startup") 1abcdefgh

51def on_startup(): 1abcdefgh

52 create_db_and_tables() 189!#

53 

54 

55@app.post("/heroes/", response_model=HeroPublic) 1abcdefgh

56def create_hero(*, session: Session = Depends(get_session), hero: HeroCreate): 1abcdefgh

57 db_hero = Hero.model_validate(hero) 1mnopqrstuvwxyzABCDEFGHIJKLMN

58 session.add(db_hero) 1mnopqrstuvwxyzABCDEFGHIJKLMN

59 session.commit() 1mnopqrstuvwxyzABCDEFGHIJKLMN

60 session.refresh(db_hero) 1mnopqrstuvwxyzABCDEFGHIJKLMN

61 return db_hero 1mnopqrstuvwxyzABCDEFGHIJKLMN

62 

63 

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

65def read_heroes( 1abcdefgh

66 *, 

67 session: Session = Depends(get_session), 

68 offset: int = 0, 

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

70): 

71 heroes = session.exec(select(Hero).offset(offset).limit(limit)).all() 1)*+,

72 return heroes 1)*+,

73 

74 

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

76def read_hero(*, session: Session = Depends(get_session), hero_id: int): 1abcdefgh

77 hero = session.get(Hero, hero_id) 1STUVWXYZ

78 if not hero: 1STUVWXYZ

79 raise HTTPException(status_code=404, detail="Hero not found") 1SUWY

80 return hero 1TVXZ

81 

82 

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

84def update_hero( 1abcdefgh

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

86): 

87 db_hero = session.get(Hero, hero_id) 10i1j2k3l

88 if not db_hero: 10i1j2k3l

89 raise HTTPException(status_code=404, detail="Hero not found") 10123

90 hero_data = hero.model_dump(exclude_unset=True) 1ijkl

91 for key, value in hero_data.items(): 1ijkl

92 setattr(db_hero, key, value) 1ijkl

93 session.add(db_hero) 1ijkl

94 session.commit() 1ijkl

95 session.refresh(db_hero) 1ijkl

96 return db_hero 1ijkl

97 

98 

99@app.delete("/heroes/{hero_id}") 1abcdefgh

100def delete_hero(*, session: Session = Depends(get_session), hero_id: int): 1abcdefgh

101 hero = session.get(Hero, hero_id) 14O5P6Q7R

102 if not hero: 14O5P6Q7R

103 raise HTTPException(status_code=404, detail="Hero not found") 14567

104 session.delete(hero) 1OPQR

105 session.commit() 1OPQR

106 return {"ok": True} 1OPQR