Coverage for tests/test_tutorial/test_dependencies/test_tutorial008c.py: 100%

30 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-12-04 08:29 +0000

1import importlib 1abcdefg

2from types import ModuleType 1abcdefg

3 

4import pytest 1abcdefg

5from fastapi.exceptions import FastAPIError 1abcdefg

6from fastapi.testclient import TestClient 1abcdefg

7 

8from ...utils import needs_py39 1abcdefg

9 

10 

11@pytest.fixture( 1abcdefg

12 name="mod", 

13 params=[ 

14 "tutorial008c", 

15 "tutorial008c_an", 

16 pytest.param("tutorial008c_an_py39", marks=needs_py39), 

17 ], 

18) 

19def get_mod(request: pytest.FixtureRequest): 1abcdefg

20 mod = importlib.import_module(f"docs_src.dependencies.{request.param}") 1abcdefg

21 

22 return mod 1abcdefg

23 

24 

25def test_get_no_item(mod: ModuleType): 1abcdefg

26 client = TestClient(mod.app) 1hijklmn

27 response = client.get("/items/foo") 1hijklmn

28 assert response.status_code == 404, response.text 1hijklmn

29 assert response.json() == {"detail": "Item not found, there's only a plumbus here"} 1hijklmn

30 

31 

32def test_get(mod: ModuleType): 1abcdefg

33 client = TestClient(mod.app) 1opqrstu

34 response = client.get("/items/plumbus") 1opqrstu

35 assert response.status_code == 200, response.text 1opqrstu

36 assert response.json() == "plumbus" 1opqrstu

37 

38 

39def test_fastapi_error(mod: ModuleType): 1abcdefg

40 client = TestClient(mod.app) 1vwxyzAB

41 with pytest.raises(FastAPIError) as exc_info: 1vwxyzAB

42 client.get("/items/portal-gun") 1vwxyzAB

43 assert "raising an exception and a dependency with yield" in exc_info.value.args[0] 1vwxyzAB

44 

45 

46def test_internal_server_error(mod: ModuleType): 1abcdefg

47 client = TestClient(mod.app, raise_server_exceptions=False) 1CDEFGHI

48 response = client.get("/items/portal-gun") 1CDEFGHI

49 assert response.status_code == 500, response.text 1CDEFGHI

50 assert response.text == "Internal Server Error" 1CDEFGHI