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

69 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 FastAPI, HTTPException, Query 1fabcde

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

5 

6 

7class HeroBase(SQLModel): 1fabcde

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

9 secret_name: str 1fabcde

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

11 

12 

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

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

15 

16 

17class HeroCreate(HeroBase): 1fabcde

18 pass 1fabcde

19 

20 

21class HeroPublic(HeroBase): 1fabcde

22 id: int 1fabcde

23 

24 

25class HeroUpdate(SQLModel): 1fabcde

26 name: Optional[str] = None 1fabcde

27 secret_name: Optional[str] = None 1fabcde

28 age: Optional[int] = None 1fabcde

29 

30 

31sqlite_file_name = "database.db" 1fabcde

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

33 

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

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

36 

37 

38def create_db_and_tables(): 1fabcde

39 SQLModel.metadata.create_all(engine) 1fabcde

40 

41 

42app = FastAPI() 1fabcde

43 

44 

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

46def on_startup(): 1abcde

47 create_db_and_tables() 1fabcde

48 

49 

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

51def create_hero(hero: HeroCreate): 1fabcde

52 with Session(engine) as session: 1fabcde

53 db_hero = Hero.model_validate(hero) 1fabcde

54 session.add(db_hero) 1fabcde

55 session.commit() 1fabcde

56 session.refresh(db_hero) 1fabcde

57 return db_hero 1fabcde

58 

59 

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

61def read_heroes(offset: int = 0, limit: int = Query(default=100, le=100)): 1fabcde

62 with Session(engine) as session: 1fabcde

63 heroes = session.exec(select(Hero).offset(offset).limit(limit)).all() 1fabcde

64 return heroes 1fabcde

65 

66 

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

68def read_hero(hero_id: int): 1fabcde

69 with Session(engine) as session: 1fabcde

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

71 if not hero: 1fabcde

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

73 return hero 1fabcde

74 

75 

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

77def update_hero(hero_id: int, hero: HeroUpdate): 1fabcde

78 with Session(engine) as session: 1fabcde

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

80 if not db_hero: 1fabcde

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

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

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

84 setattr(db_hero, key, value) 1fabcde

85 session.add(db_hero) 1fabcde

86 session.commit() 1fabcde

87 session.refresh(db_hero) 1fabcde

88 return db_hero 1fabcde

89 

90 

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

92def delete_hero(hero_id: int): 1fabcde

93 with Session(engine) as session: 1fabcde

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

95 if not hero: 1fabcde

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

97 session.delete(hero) 1fabcde

98 session.commit() 1fabcde

99 return {"ok": True} 1fabcde