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