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

29 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.testclient import TestClient 1abcdefg

6 

7from ...utils import needs_py39 1abcdefg

8 

9 

10@pytest.fixture( 1abcdefg

11 name="mod", 

12 params=[ 

13 "tutorial008d", 

14 "tutorial008d_an", 

15 pytest.param("tutorial008d_an_py39", marks=needs_py39), 

16 ], 

17) 

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

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

20 

21 return mod 1abcdefg

22 

23 

24def test_get_no_item(mod: ModuleType): 1abcdefg

25 client = TestClient(mod.app) 1hijklmn

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

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

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

29 

30 

31def test_get(mod: ModuleType): 1abcdefg

32 client = TestClient(mod.app) 1opqrstu

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

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

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

36 

37 

38def test_internal_error(mod: ModuleType): 1abcdefg

39 client = TestClient(mod.app) 1vwxyzAB

40 with pytest.raises(mod.InternalError) as exc_info: 1vwxyzAB

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

42 assert ( 1vwxyzAB

43 exc_info.value.args[0] == "The portal gun is too dangerous to be owned by Rick" 

44 ) 

45 

46 

47def test_internal_server_error(mod: ModuleType): 1abcdefg

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

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

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

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