Coverage for docs_src/tutorial/fastapi/app_testing/tutorial001_py310/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 1agbhci
2from fastapi.testclient import TestClient 1agbhci
3from sqlmodel import Session, SQLModel, create_engine 1agbhci
4from sqlmodel.pool import StaticPool 1agbhci
6from .main import Hero, app, get_session 1agbhci
9@pytest.fixture(name="session") 1agbhci
10def session_fixture(): 1agbhci
11 engine = create_engine( 1abc
12 "sqlite://", connect_args={"check_same_thread": False}, poolclass=StaticPool
13 )
14 SQLModel.metadata.create_all(engine) 1abc
15 with Session(engine) as session: 1abc
16 yield session 1abc
19@pytest.fixture(name="client") 1agbhci
20def client_fixture(session: Session): 1agbhci
21 def get_session_override(): 1abc
22 return session 1BCDpvwqjdkEFGrxyslemHIJtzAunfo
24 app.dependency_overrides[get_session] = get_session_override 1abc
25 client = TestClient(app) 1abc
26 yield client 1abc
27 app.dependency_overrides.clear() 1abc
30def test_create_hero(client: TestClient): 1agbhci
31 response = client.post( 1prt
32 "/heroes/", json={"name": "Deadpond", "secret_name": "Dive Wilson"}
33 )
34 data = response.json() 1prt
36 assert response.status_code == 200 1prt
37 assert data["name"] == "Deadpond" 1prt
38 assert data["secret_name"] == "Dive Wilson" 1prt
39 assert data["age"] is None 1prt
40 assert data["id"] is not None 1prt
43def test_create_hero_incomplete(client: TestClient): 1agbhci
44 # No secret_name
45 response = client.post("/heroes/", json={"name": "Deadpond"}) 1vxz
46 assert response.status_code == 422 1vxz
49def test_create_hero_invalid(client: TestClient): 1agbhci
50 # secret_name has an invalid type
51 response = client.post( 1wyA
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 1wyA
61def test_read_heroes(session: Session, client: TestClient): 1agbhci
62 hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson") 1def
63 hero_2 = Hero(name="Rusty-Man", secret_name="Tommy Sharp", age=48) 1def
64 session.add(hero_1) 1def
65 session.add(hero_2) 1def
66 session.commit() 1def
68 response = client.get("/heroes/") 1def
69 data = response.json() 1def
71 assert response.status_code == 200 1def
73 assert len(data) == 2 1def
74 assert data[0]["name"] == hero_1.name 1def
75 assert data[0]["secret_name"] == hero_1.secret_name 1def
76 assert data[0]["age"] == hero_1.age 1def
77 assert data[0]["id"] == hero_1.id 1def
78 assert data[1]["name"] == hero_2.name 1def
79 assert data[1]["secret_name"] == hero_2.secret_name 1def
80 assert data[1]["age"] == hero_2.age 1def
81 assert data[1]["id"] == hero_2.id 1def
84def test_read_hero(session: Session, client: TestClient): 1agbhci
85 hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson") 1jln
86 session.add(hero_1) 1jln
87 session.commit() 1jln
89 response = client.get(f"/heroes/{hero_1.id}") 1jln
90 data = response.json() 1jln
92 assert response.status_code == 200 1jln
93 assert data["name"] == hero_1.name 1jln
94 assert data["secret_name"] == hero_1.secret_name 1jln
95 assert data["age"] == hero_1.age 1jln
96 assert data["id"] == hero_1.id 1jln
99def test_update_hero(session: Session, client: TestClient): 1agbhci
100 hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson") 1kmo
101 session.add(hero_1) 1kmo
102 session.commit() 1kmo
104 response = client.patch(f"/heroes/{hero_1.id}", json={"name": "Deadpuddle"}) 1kmo
105 data = response.json() 1kmo
107 assert response.status_code == 200 1kmo
108 assert data["name"] == "Deadpuddle" 1kmo
109 assert data["secret_name"] == "Dive Wilson" 1kmo
110 assert data["age"] is None 1kmo
111 assert data["id"] == hero_1.id 1kmo
114def test_delete_hero(session: Session, client: TestClient): 1agbhci
115 hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson") 1qsu
116 session.add(hero_1) 1qsu
117 session.commit() 1qsu
119 response = client.delete(f"/heroes/{hero_1.id}") 1qsu
121 hero_in_db = session.get(Hero, hero_1.id) 1qsu
123 assert response.status_code == 200 1qsu
125 assert hero_in_db is None 1qsu