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

30 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.exceptions import FastAPIError 1abcdef

6from fastapi.testclient import TestClient 1abcdef

7 

8from ...utils import needs_py39 1abcdef

9 

10 

11@pytest.fixture( 1abcdef

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

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

21 

22 return mod 1abcdef

23 

24 

25def test_get_no_item(mod: ModuleType): 1abcdef

26 client = TestClient(mod.app) 1ghijkl

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

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

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

30 

31 

32def test_get(mod: ModuleType): 1abcdef

33 client = TestClient(mod.app) 1mnopqr

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

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

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

37 

38 

39def test_fastapi_error(mod: ModuleType): 1abcdef

40 client = TestClient(mod.app) 1stuvwx

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

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

43 assert "No response object was returned" in exc_info.value.args[0] 1stuvwx

44 

45 

46def test_internal_server_error(mod: ModuleType): 1abcdef

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

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

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

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