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

29 statements  

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

1import importlib 1abcdef

2from types import ModuleType 1abcdef

3 

4import pytest 1abcdef

5from fastapi.testclient import TestClient 1abcdef

6 

7from ...utils import needs_py39 1abcdef

8 

9 

10@pytest.fixture( 1abcdef

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): 1abcdef

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

20 

21 return mod 1abcdef

22 

23 

24def test_get_no_item(mod: ModuleType): 1abcdef

25 client = TestClient(mod.app) 1ghijkl

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

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

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

29 

30 

31def test_get(mod: ModuleType): 1abcdef

32 client = TestClient(mod.app) 1mnopqr

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

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

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

36 

37 

38def test_internal_error(mod: ModuleType): 1abcdef

39 client = TestClient(mod.app) 1stuvwx

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

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

42 assert ( 1stuvwx

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): 1abcdef

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

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

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

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