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

28 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-04-06 01:24 +0000

1import importlib 1abcd

2from types import ModuleType 1abcd

3 

4import pytest 1abcd

5from fastapi.testclient import TestClient 1abcd

6 

7 

8@pytest.fixture( 1abcd

9 name="mod", 

10 params=[ 

11 pytest.param("tutorial008d_py310"), 

12 pytest.param("tutorial008d_an_py310"), 

13 ], 

14) 

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

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

17 

18 return mod 1JKLMNOPQRSTUVWXYZ012345678

19 

20 

21def test_get_no_item(mod: ModuleType): 1abcd

22 client = TestClient(mod.app) 1efghijkl

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

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

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

26 

27 

28def test_get(mod: ModuleType): 1abcd

29 client = TestClient(mod.app) 1mnopqrst

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

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

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

33 

34 

35def test_internal_error(mod: ModuleType): 1abcd

36 client = TestClient(mod.app) 1uvwxyHI

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

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

39 assert ( 1uvwxy

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

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

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

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

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