Coverage for docs_src/tutorial/fastapi/app_testing/tutorial001_py39/test_main.py: 100%
81 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-09-09 00:02 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-09-09 00:02 +0000
1import pytest 1aibjckdl
2from fastapi.testclient import TestClient 1aibjckdl
3from sqlmodel import Session, SQLModel, create_engine 1aibjckdl
4from sqlmodel.pool import StaticPool 1aibjckdl
6from .main import Hero, app, get_session 1aibjckdl
9@pytest.fixture(name="session") 1aibjckdl
10def session_fixture(): 1aibjckdl
11 engine = create_engine( 1abcd
12 "sqlite://", connect_args={"check_same_thread": False}, poolclass=StaticPool
13 )
14 SQLModel.metadata.create_all(engine) 1abcd
15 with Session(engine) as session: 1abcd
16 yield session 1abcd
19@pytest.fixture(name="client") 1aibjckdl
20def client_fixture(session: Session): 1aibjckdl
21 def get_session_override(): 1abcd
22 return session 1KLMuCDvmenNOPwEFxofpQRSyGHzqgrTUVAIJBsht
24 app.dependency_overrides[get_session] = get_session_override 1abcd
25 client = TestClient(app) 1abcd
26 yield client 1abcd
27 app.dependency_overrides.clear() 1abcd
30def test_create_hero(client: TestClient): 1aibjckdl
31 response = client.post( 1uwyA
32 "/heroes/", json={"name": "Deadpond", "secret_name": "Dive Wilson"}
33 )
34 data = response.json() 1uwyA
36 assert response.status_code == 200 1uwyA
37 assert data["name"] == "Deadpond" 1uwyA
38 assert data["secret_name"] == "Dive Wilson" 1uwyA
39 assert data["age"] is None 1uwyA
40 assert data["id"] is not None 1uwyA
43def test_create_hero_incomplete(client: TestClient): 1aibjckdl
44 # No secret_name
45 response = client.post("/heroes/", json={"name": "Deadpond"}) 1CEGI
46 assert response.status_code == 422 1CEGI
49def test_create_hero_invalid(client: TestClient): 1aibjckdl
50 # secret_name has an invalid type
51 response = client.post( 1DFHJ
52 "/heroes/",
53 json={
54 "name": "Deadpond",
55 "secret_name": {"message": "Do you wanna know my secret identity?"},
56 },
57 )
58 assert response.status_code == 422 1DFHJ
61def test_read_heroes(session: Session, client: TestClient): 1aibjckdl
62 hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson") 1efgh
63 hero_2 = Hero(name="Rusty-Man", secret_name="Tommy Sharp", age=48) 1efgh
64 session.add(hero_1) 1efgh
65 session.add(hero_2) 1efgh
66 session.commit() 1efgh
68 response = client.get("/heroes/") 1efgh
69 data = response.json() 1efgh
71 assert response.status_code == 200 1efgh
73 assert len(data) == 2 1efgh
74 assert data[0]["name"] == hero_1.name 1efgh
75 assert data[0]["secret_name"] == hero_1.secret_name 1efgh
76 assert data[0]["age"] == hero_1.age 1efgh
77 assert data[0]["id"] == hero_1.id 1efgh
78 assert data[1]["name"] == hero_2.name 1efgh
79 assert data[1]["secret_name"] == hero_2.secret_name 1efgh
80 assert data[1]["age"] == hero_2.age 1efgh
81 assert data[1]["id"] == hero_2.id 1efgh
84def test_read_hero(session: Session, client: TestClient): 1aibjckdl
85 hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson") 1moqs
86 session.add(hero_1) 1moqs
87 session.commit() 1moqs
89 response = client.get(f"/heroes/{hero_1.id}") 1moqs
90 data = response.json() 1moqs
92 assert response.status_code == 200 1moqs
93 assert data["name"] == hero_1.name 1moqs
94 assert data["secret_name"] == hero_1.secret_name 1moqs
95 assert data["age"] == hero_1.age 1moqs
96 assert data["id"] == hero_1.id 1moqs
99def test_update_hero(session: Session, client: TestClient): 1aibjckdl
100 hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson") 1nprt
101 session.add(hero_1) 1nprt
102 session.commit() 1nprt
104 response = client.patch(f"/heroes/{hero_1.id}", json={"name": "Deadpuddle"}) 1nprt
105 data = response.json() 1nprt
107 assert response.status_code == 200 1nprt
108 assert data["name"] == "Deadpuddle" 1nprt
109 assert data["secret_name"] == "Dive Wilson" 1nprt
110 assert data["age"] is None 1nprt
111 assert data["id"] == hero_1.id 1nprt
114def test_delete_hero(session: Session, client: TestClient): 1aibjckdl
115 hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson") 1vxzB
116 session.add(hero_1) 1vxzB
117 session.commit() 1vxzB
119 response = client.delete(f"/heroes/{hero_1.id}") 1vxzB
121 hero_in_db = session.get(Hero, hero_1.id) 1vxzB
123 assert response.status_code == 200 1vxzB
125 assert hero_in_db is None 1vxzB