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