Coverage for docs_src/tutorial/fastapi/app_testing/tutorial001_py39/test_main_006.py: 100%
27 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 1abcd
2from fastapi.testclient import TestClient 1abcd
3from sqlmodel import Session, SQLModel, create_engine 1abcd
4from sqlmodel.pool import StaticPool 1abcd
6from .main import app, get_session 1abcd
9@pytest.fixture(name="session") 1abcd
10def session_fixture(): 1abcd
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") # (1)! 1abcd
20def client_fixture(session: Session): # (2)! 1abcd
21 def get_session_override(): # (3)! 1abcd
22 return session 1efgh
24 app.dependency_overrides[get_session] = get_session_override # (4)! 1abcd
26 client = TestClient(app) # (5)! 1abcd
27 yield client # (6)! 1abcd
28 app.dependency_overrides.clear() # (7)! 1abcd
31def test_create_hero(client: TestClient): # (8)! 1abcd
32 response = client.post( 1efgh
33 "/heroes/", json={"name": "Deadpond", "secret_name": "Dive Wilson"}
34 )
35 data = response.json() 1efgh
37 assert response.status_code == 200 1efgh
38 assert data["name"] == "Deadpond" 1efgh
39 assert data["secret_name"] == "Dive Wilson" 1efgh
40 assert data["age"] is None 1efgh
41 assert data["id"] is not None 1efgh