Coverage for docs_src/tutorial/fastapi/app_testing/tutorial001_py310/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)! 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") # (2)! 1abc
10def session_fixture(): # (3)! 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 # (4)! 1abc
19def test_create_hero(session: Session): # (5)! 1abc
20 def get_session_override(): 1def
21 return session # (6)! 1def
23 app.dependency_overrides[get_session] = get_session_override 1def
25 client = TestClient(app) 1def
27 response = client.post( 1def
28 "/heroes/", json={"name": "Deadpond", "secret_name": "Dive Wilson"}
29 )
30 app.dependency_overrides.clear() 1def
31 data = response.json() 1def
33 assert response.status_code == 200 1def
34 assert data["name"] == "Deadpond" 1def
35 assert data["secret_name"] == "Dive Wilson" 1def
36 assert data["age"] is None 1def
37 assert data["id"] is not None 1def