Coverage for docs_src/tutorial/fastapi/app_testing/tutorial001_py39/test_main_005.py: 100%

24 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-09-09 00:02 +0000

1import pytest # (1)! 1abcd

2from fastapi.testclient import TestClient 1abcd

3from sqlmodel import Session, SQLModel, create_engine 1abcd

4from sqlmodel.pool import StaticPool 1abcd

5 

6from .main import app, get_session 1abcd

7 

8 

9@pytest.fixture(name="session") # (2)! 1abcd

10def session_fixture(): # (3)! 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 # (4)! 1abcd

17 

18 

19def test_create_hero(session: Session): # (5)! 1abcd

20 def get_session_override(): 1efgh

21 return session # (6)! 1efgh

22 

23 app.dependency_overrides[get_session] = get_session_override 1efgh

24 

25 client = TestClient(app) 1efgh

26 

27 response = client.post( 1efgh

28 "/heroes/", json={"name": "Deadpond", "secret_name": "Dive Wilson"} 

29 ) 

30 app.dependency_overrides.clear() 1efgh

31 data = response.json() 1efgh

32 

33 assert response.status_code == 200 1efgh

34 assert data["name"] == "Deadpond" 1efgh

35 assert data["secret_name"] == "Dive Wilson" 1efgh

36 assert data["age"] is None 1efgh

37 assert data["id"] is not None 1efgh