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

31 statements  

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

1import pytest 1eabcd

2from fastapi.exceptions import FastAPIError 1eabcd

3from fastapi.testclient import TestClient 1eabcd

4 

5from ...utils import needs_py39 1eabcd

6 

7 

8@pytest.fixture(name="client") 1eabcd

9def get_client(): 1eabcd

10 from docs_src.dependencies.tutorial008c_an_py39 import app 1abcd

11 

12 client = TestClient(app) 1abcd

13 return client 1abcd

14 

15 

16@needs_py39 1eabcd

17def test_get_no_item(client: TestClient): 1eabcd

18 response = client.get("/items/foo") 1abcd

19 assert response.status_code == 404, response.text 1abcd

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

21 

22 

23@needs_py39 1eabcd

24def test_get(client: TestClient): 1eabcd

25 response = client.get("/items/plumbus") 1abcd

26 assert response.status_code == 200, response.text 1abcd

27 assert response.json() == "plumbus" 1abcd

28 

29 

30@needs_py39 1eabcd

31def test_fastapi_error(client: TestClient): 1eabcd

32 with pytest.raises(FastAPIError) as exc_info: 1abcd

33 client.get("/items/portal-gun") 1abcd

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

35 

36 

37@needs_py39 1eabcd

38def test_internal_server_error(): 1eabcd

39 from docs_src.dependencies.tutorial008c_an_py39 import app 1abcd

40 

41 client = TestClient(app, raise_server_exceptions=False) 1abcd

42 response = client.get("/items/portal-gun") 1abcd

43 assert response.status_code == 500, response.text 1abcd

44 assert response.text == "Internal Server Error" 1abcd