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

26 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-08-08 03:53 +0000

1import pytest 1abcde

2from fastapi.testclient import TestClient 1abcde

3 

4 

5@pytest.fixture(name="client") 1abcde

6def get_client(): 1abcde

7 from docs_src.dependencies.tutorial008d import app 1abcde

8 

9 client = TestClient(app) 1abcde

10 return client 1abcde

11 

12 

13def test_get_no_item(client: TestClient): 1abcde

14 response = client.get("/items/foo") 1abcde

15 assert response.status_code == 404, response.text 1abcde

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

17 

18 

19def test_get(client: TestClient): 1abcde

20 response = client.get("/items/plumbus") 1abcde

21 assert response.status_code == 200, response.text 1abcde

22 assert response.json() == "plumbus" 1abcde

23 

24 

25def test_internal_error(client: TestClient): 1abcde

26 from docs_src.dependencies.tutorial008d import InternalError 1abcde

27 

28 with pytest.raises(InternalError) as exc_info: 1abcde

29 client.get("/items/portal-gun") 1abcde

30 assert ( 1abcde

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

32 ) 

33 

34 

35def test_internal_server_error(): 1abcde

36 from docs_src.dependencies.tutorial008d import app 1abcde

37 

38 client = TestClient(app, raise_server_exceptions=False) 1abcde

39 response = client.get("/items/portal-gun") 1abcde

40 assert response.status_code == 500, response.text 1abcde

41 assert response.text == "Internal Server Error" 1abcde