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

26 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-10-27 00:03 +0000

1from fastapi.testclient import TestClient 1abcdefghij

2from sqlalchemy import Inspector, inspect 1abcdefghij

3from sqlmodel import Session, create_engine 1abcdefghij

4 

5from . import main as app_mod 1abcdefghij

6from .test_main import client_fixture, session_fixture 1abcdefghij

7 

8assert client_fixture, "This keeps the client fixture used below" 1abcdefghij

9assert session_fixture, "This keeps the session fixture used by client_fixture" 1abcdefghij

10 

11 

12def test_startup(): 1abcdefghij

13 app_mod.engine = create_engine("sqlite://") 1klmnopqrst

14 app_mod.on_startup() 1klmnopqrst

15 insp: Inspector = inspect(app_mod.engine) 1klmnopqrst

16 assert insp.has_table(str(app_mod.Hero.__tablename__)) 1klmnopqrst

17 

18 

19def test_get_session(): 1abcdefghij

20 app_mod.engine = create_engine("sqlite://") 1uvwxyzABCD

21 for session in app_mod.get_session(): 1uvwxyzABCD

22 assert isinstance(session, Session) 1uvwxyzABCD

23 assert session.bind == app_mod.engine 1uvwxyzABCD

24 

25 

26def test_read_hero_not_found(client: TestClient): 1abcdefghij

27 response = client.get("/heroes/9000") 1EFGHIJKLMN

28 assert response.status_code == 404 1EFGHIJKLMN

29 

30 

31def test_update_hero_not_found(client: TestClient): 1abcdefghij

32 response = client.patch("/heroes/9000", json={"name": "Very-Rusty-Man"}) 1OPQRSTUVWX

33 assert response.status_code == 404 1OPQRSTUVWX

34 

35 

36def test_delete_hero_not_found(client: TestClient): 1abcdefghij

37 response = client.delete("/heroes/9000") 1YZ01234567

38 assert response.status_code == 404 1YZ01234567