Coverage for docs_src/tutorial/fastapi/update/tutorial002_py310.py: 100%

69 statements  

« prev     ^ index     » next       coverage.py v7.7.1, created at 2025-03-24 00:02 +0000

1from fastapi import FastAPI, HTTPException, Query 1abcd

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

3 

4 

5class HeroBase(SQLModel): 1abcd

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

7 secret_name: str 1abcd

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

9 

10 

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

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

13 hashed_password: str = Field() 1abcd

14 

15 

16class HeroCreate(HeroBase): 1abcd

17 password: str 1abcd

18 

19 

20class HeroPublic(HeroBase): 1abcd

21 id: int 1abcd

22 

23 

24class HeroUpdate(SQLModel): 1abcd

25 name: str | None = None 1abcd

26 secret_name: str | None = None 1abcd

27 age: int | None = None 1abcd

28 password: str | None = None 1abcd

29 

30 

31sqlite_file_name = "database.db" 1abcd

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

33 

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

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

36 

37 

38def create_db_and_tables(): 1abcd

39 SQLModel.metadata.create_all(engine) 1abcd

40 

41 

42def hash_password(password: str) -> str: 1abcd

43 # Use something like passlib here 

44 return f"not really hashed {password} hehehe" 1abcd

45 

46 

47app = FastAPI() 1abcd

48 

49 

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

51def on_startup(): 1abcd

52 create_db_and_tables() 1abcd

53 

54 

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

56def create_hero(hero: HeroCreate): 1abcd

57 hashed_password = hash_password(hero.password) 1abcd

58 with Session(engine) as session: 1abcd

59 extra_data = {"hashed_password": hashed_password} 1abcd

60 db_hero = Hero.model_validate(hero, update=extra_data) 1abcd

61 session.add(db_hero) 1abcd

62 session.commit() 1abcd

63 session.refresh(db_hero) 1abcd

64 return db_hero 1abcd

65 

66 

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

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

69 with Session(engine) as session: 1abcd

70 heroes = session.exec(select(Hero).offset(offset).limit(limit)).all() 1abcd

71 return heroes 1abcd

72 

73 

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

75def read_hero(hero_id: int): 1abcd

76 with Session(engine) as session: 1abcd

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

78 if not hero: 1abcd

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

80 return hero 1abcd

81 

82 

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

84def update_hero(hero_id: int, hero: HeroUpdate): 1abcd

85 with Session(engine) as session: 1abcd

86 db_hero = session.get(Hero, hero_id) 1abcd

87 if not db_hero: 1abcd

88 raise HTTPException(status_code=404, detail="Hero not found") 1abcd

89 hero_data = hero.model_dump(exclude_unset=True) 1abcd

90 extra_data = {} 1abcd

91 if "password" in hero_data: 1abcd

92 password = hero_data["password"] 1abcd

93 hashed_password = hash_password(password) 1abcd

94 extra_data["hashed_password"] = hashed_password 1abcd

95 db_hero.sqlmodel_update(hero_data, update=extra_data) 1abcd

96 session.add(db_hero) 1abcd

97 session.commit() 1abcd

98 session.refresh(db_hero) 1abcd

99 return db_hero 1abcd