Coverage for docs_src/tutorial/fastapi/app_testing/tutorial001/main.py: 100%
67 statements
« prev ^ index » next coverage.py v7.7.1, created at 2025-03-24 00:02 +0000
« prev ^ index » next coverage.py v7.7.1, created at 2025-03-24 00:02 +0000
1from typing import List, Optional 1abcdef
3from fastapi import Depends, FastAPI, HTTPException, Query 1abcdef
4from sqlmodel import Field, Session, SQLModel, create_engine, select 1abcdef
7class HeroBase(SQLModel): 1abcdef
8 name: str = Field(index=True) 1abcdef
9 secret_name: str 1abcdef
10 age: Optional[int] = Field(default=None, index=True) 1abcdef
13class Hero(HeroBase, table=True): 1abcdef
14 id: Optional[int] = Field(default=None, primary_key=True) 1abcdef
17class HeroCreate(HeroBase): 1abcdef
18 pass 1abcdef
21class HeroPublic(HeroBase): 1abcdef
22 id: int 1abcdef
25class HeroUpdate(SQLModel): 1abcdef
26 name: Optional[str] = None 1abcdef
27 secret_name: Optional[str] = None 1abcdef
28 age: Optional[int] = None 1abcdef
31sqlite_file_name = "database.db" 1abcdef
32sqlite_url = f"sqlite:///{sqlite_file_name}" 1abcdef
34connect_args = {"check_same_thread": False} 1abcdef
35engine = create_engine(sqlite_url, echo=True, connect_args=connect_args) 1abcdef
38def create_db_and_tables(): 1abcdef
39 SQLModel.metadata.create_all(engine) 2FbGbHbIbJbKb
42def get_session(): 1abcdef
43 with Session(engine) as session: 2LbMbNbObPbQb
44 yield session 2LbMbNbObPbQb
47app = FastAPI() 1abcdef
50@app.on_event("startup") 1abcdef
51def on_startup(): 1abcdef
52 create_db_and_tables() 2FbGbHbIbJbKb
55@app.post("/heroes/", response_model=HeroPublic) 1abcdef
56def create_hero(*, session: Session = Depends(get_session), hero: HeroCreate): 1abcdef
57 db_hero = Hero.model_validate(hero) 2m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? @ [ ] ^ _ ` { | } ~ ab
58 session.add(db_hero) 2m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? @ [ ] ^ _ ` { | } ~ ab
59 session.commit() 2m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? @ [ ] ^ _ ` { | } ~ ab
60 session.refresh(db_hero) 2m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? @ [ ] ^ _ ` { | } ~ ab
61 return db_hero 2m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? @ [ ] ^ _ ` { | } ~ ab
64@app.get("/heroes/", response_model=List[HeroPublic]) 1abcdef
65def read_heroes( 1abcdef
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() 2RbSbTbUbVbWb
72 return heroes 2RbSbTbUbVbWb
75@app.get("/heroes/{hero_id}", response_model=HeroPublic) 1abcdef
76def read_hero(*, session: Session = Depends(get_session), hero_id: int): 1abcdef
77 hero = session.get(Hero, hero_id) 2hbibjbkblbmbnbobpbqbrbsb
78 if not hero: 2hbibjbkblbmbnbobpbqbrbsb
79 raise HTTPException(status_code=404, detail="Hero not found") 2hbjblbnbpbrb
80 return hero 2ibkbmbobqbsb
83@app.patch("/heroes/{hero_id}", response_model=HeroPublic) 1abcdef
84def update_hero( 1abcdef
85 *, session: Session = Depends(get_session), hero_id: int, hero: HeroUpdate
86):
87 db_hero = session.get(Hero, hero_id) 2tbg ubh vbi wbj xbk ybl
88 if not db_hero: 2tbg ubh vbi wbj xbk ybl
89 raise HTTPException(status_code=404, detail="Hero not found") 2tbubvbwbxbyb
90 hero_data = hero.model_dump(exclude_unset=True) 1ghijkl
91 for key, value in hero_data.items(): 1ghijkl
92 setattr(db_hero, key, value) 1ghijkl
93 session.add(db_hero) 1ghijkl
94 session.commit() 1ghijkl
95 session.refresh(db_hero) 1ghijkl
96 return db_hero 1ghijkl
99@app.delete("/heroes/{hero_id}") 1abcdef
100def delete_hero(*, session: Session = Depends(get_session), hero_id: int): 1abcdef
101 hero = session.get(Hero, hero_id) 2zbbbAbcbBbdbCbebDbfbEbgb
102 if not hero: 2zbbbAbcbBbdbCbebDbfbEbgb
103 raise HTTPException(status_code=404, detail="Hero not found") 2zbAbBbCbDbEb
104 session.delete(hero) 2bbcbdbebfbgb
105 session.commit() 2bbcbdbebfbgb
106 return {"ok": True} 2bbcbdbebfbgb