Coverage for docs_src/tutorial/fastapi/app_testing/tutorial001/test_main_001.py: 100%

19 statements  

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

1from fastapi.testclient import TestClient 1mnopqr

2from sqlmodel import Session, SQLModel, create_engine 1mnopqr

3 

4from .main import app, get_session # (1)! 1mnopqr

5 

6 

7def test_create_hero(): 1mnopqr

8 engine = create_engine( 1abcdefghijkl

9 "sqlite:///testing.db", connect_args={"check_same_thread": False} 

10 ) 

11 SQLModel.metadata.create_all(engine) 1abcdefghijkl

12 

13 with Session(engine) as session: 1abcdefghijkl

14 

15 def get_session_override(): 1abcdefghijkl

16 return session 1abcdefghijkl

17 

18 app.dependency_overrides[get_session] = get_session_override 1abcdefghijkl

19 

20 client = TestClient(app) # (2)! 1abcdefghijkl

21 

22 response = client.post( # (3)! 1abcdefghijkl

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

24 ) 

25 app.dependency_overrides.clear() 1abcdefghijkl

26 data = response.json() # (4)! 1abcdefghijkl

27 

28 assert response.status_code == 200 # (5)! 1abcdefghijkl

29 assert data["name"] == "Deadpond" # (6)! 1abcdefghijkl

30 assert data["secret_name"] == "Dive Wilson" # (7)! 1abcdefghijkl

31 assert data["age"] is None # (8)! 1abcdefghijkl

32 assert data["id"] is not None # (9)! 1abcdefghijkl