Coverage for docs_src/tutorial/fastapi/app_testing/tutorial001/test_main_005.py: 100%
24 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 # (1)! 1rabcde
2from fastapi.testclient import TestClient 1rabcde
3from sqlmodel import Session, SQLModel, create_engine 1rabcde
4from sqlmodel.pool import StaticPool 1rabcde
6from .main import app, get_session 1rabcde
9@pytest.fixture(name="session") # (2)! 1rabcde
10def session_fixture(): # (3)! 1abcde
11 engine = create_engine( 1rabcde
12 "sqlite://", connect_args={"check_same_thread": False}, poolclass=StaticPool
13 )
14 SQLModel.metadata.create_all(engine) 1rabcde
15 with Session(engine) as session: 1rabcde
16 yield session # (4)! 1rabcde
19def test_create_hero(session: Session): # (5)! 1rabcde
20 def get_session_override(): 1fghijklmnopq
21 return session # (6)! 1fghijklmnopq
23 app.dependency_overrides[get_session] = get_session_override 1fghijklmnopq
25 client = TestClient(app) 1fghijklmnopq
27 response = client.post( 1fghijklmnopq
28 "/heroes/", json={"name": "Deadpond", "secret_name": "Dive Wilson"}
29 )
30 app.dependency_overrides.clear() 1fghijklmnopq
31 data = response.json() 1fghijklmnopq
33 assert response.status_code == 200 1fghijklmnopq
34 assert data["name"] == "Deadpond" 1fghijklmnopq
35 assert data["secret_name"] == "Dive Wilson" 1fghijklmnopq
36 assert data["age"] is None 1fghijklmnopq
37 assert data["id"] is not None 1fghijklmnopq