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

69 statements  

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

1from fastapi import FastAPI, HTTPException, Query 1abc

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

3 

4 

5class HeroBase(SQLModel): 1abc

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

7 secret_name: str 1abc

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

9 

10 

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

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

13 hashed_password: str = Field() 1abc

14 

15 

16class HeroCreate(HeroBase): 1abc

17 password: str 1abc

18 

19 

20class HeroPublic(HeroBase): 1abc

21 id: int 1abc

22 

23 

24class HeroUpdate(SQLModel): 1abc

25 name: str | None = None 1abc

26 secret_name: str | None = None 1abc

27 age: int | None = None 1abc

28 password: str | None = None 1abc

29 

30 

31sqlite_file_name = "database.db" 1abc

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

33 

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

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

36 

37 

38def create_db_and_tables(): 1abc

39 SQLModel.metadata.create_all(engine) 1abc

40 

41 

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

43 # Use something like passlib here 

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

45 

46 

47app = FastAPI() 1abc

48 

49 

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

51def on_startup(): 1abc

52 create_db_and_tables() 1abc

53 

54 

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

56def create_hero(hero: HeroCreate): 1abc

57 hashed_password = hash_password(hero.password) 1abc

58 with Session(engine) as session: 1abc

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

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

61 session.add(db_hero) 1abc

62 session.commit() 1abc

63 session.refresh(db_hero) 1abc

64 return db_hero 1abc

65 

66 

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

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

69 with Session(engine) as session: 1abc

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

71 return heroes 1abc

72 

73 

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

75def read_hero(hero_id: int): 1abc

76 with Session(engine) as session: 1abc

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

78 if not hero: 1abc

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

80 return hero 1abc

81 

82 

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

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

85 with Session(engine) as session: 1abc

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

87 if not db_hero: 1abc

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

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

90 extra_data = {} 1abc

91 if "password" in hero_data: 1abc

92 password = hero_data["password"] 1abc

93 hashed_password = hash_password(password) 1abc

94 extra_data["hashed_password"] = hashed_password 1abc

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

96 session.add(db_hero) 1abc

97 session.commit() 1abc

98 session.refresh(db_hero) 1abc

99 return db_hero 1abc