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

20 statements  

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

1from fastapi.testclient import TestClient 1efgh

2from sqlmodel import Session, SQLModel, create_engine 1efgh

3from sqlmodel.pool import StaticPool # (1)! 1efgh

4 

5from .main import app, get_session 1efgh

6 

7 

8def test_create_hero(): 1efgh

9 engine = create_engine( 1abcd

10 "sqlite://", # (2)! 

11 connect_args={"check_same_thread": False}, 

12 poolclass=StaticPool, # (3)! 

13 ) 

14 SQLModel.metadata.create_all(engine) 1abcd

15 

16 with Session(engine) as session: 1abcd

17 

18 def get_session_override(): 1abcd

19 return session 1abcd

20 

21 app.dependency_overrides[get_session] = get_session_override 1abcd

22 

23 client = TestClient(app) 1abcd

24 

25 response = client.post( 1abcd

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

27 ) 

28 app.dependency_overrides.clear() 1abcd

29 data = response.json() 1abcd

30 

31 assert response.status_code == 200 1abcd

32 assert data["name"] == "Deadpond" 1abcd

33 assert data["secret_name"] == "Dive Wilson" 1abcd

34 assert data["age"] is None 1abcd

35 assert data["id"] is not None 1abcd