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

28 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-02-12 18:15 +0000

1import importlib 1abdc

2from types import ModuleType 1abdc

3 

4import pytest 1abdc

5from fastapi.testclient import TestClient 1abdc

6 

7 

8@pytest.fixture( 1abdc

9 name="mod", 

10 params=[ 

11 pytest.param("tutorial008d_py310"), 

12 pytest.param("tutorial008d_an_py310"), 

13 ], 

14) 

15def get_mod(request: pytest.FixtureRequest): 1abdc

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

17 

18 return mod 1abc

19 

20 

21def test_get_no_item(mod: ModuleType): 1abdc

22 client = TestClient(mod.app) 1efg

23 response = client.get("/items/foo") 1efg

24 assert response.status_code == 404, response.text 1efg

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

26 

27 

28def test_get(mod: ModuleType): 1abdc

29 client = TestClient(mod.app) 1hij

30 response = client.get("/items/plumbus") 1hij

31 assert response.status_code == 200, response.text 1hij

32 assert response.json() == "plumbus" 1hij

33 

34 

35def test_internal_error(mod: ModuleType): 1abdc

36 client = TestClient(mod.app) 1klp

37 with pytest.raises(mod.InternalError) as exc_info: 1klp

38 client.get("/items/portal-gun") 1klp

39 assert ( 1kl

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

41 ) 

42 

43 

44def test_internal_server_error(mod: ModuleType): 1abdc

45 client = TestClient(mod.app, raise_server_exceptions=False) 1mno

46 response = client.get("/items/portal-gun") 1mno

47 assert response.status_code == 500, response.text 1mno

48 assert response.text == "Internal Server Error" 1mno