Coverage for tests/test_tutorial/test_handling_errors/test_tutorial003.py: 100%

15 statements  

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

1from fastapi.testclient import TestClient 1abcde

2 

3from docs_src.handling_errors.tutorial003 import app 1abcde

4 

5client = TestClient(app) 1abcde

6 

7 

8def test_get(): 1abcde

9 response = client.get("/unicorns/shinny") 1abcde

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

11 assert response.json() == {"unicorn_name": "shinny"} 1abcde

12 

13 

14def test_get_exception(): 1abcde

15 response = client.get("/unicorns/yolo") 1abcde

16 assert response.status_code == 418, response.text 1abcde

17 assert response.json() == { 1abcde

18 "message": "Oops! yolo did something. There goes a rainbow..." 

19 } 

20 

21 

22def test_openapi_schema(): 1abcde

23 response = client.get("/openapi.json") 1abcde

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

25 assert response.json() == { 1abcde

26 "openapi": "3.1.0", 

27 "info": {"title": "FastAPI", "version": "0.1.0"}, 

28 "paths": { 

29 "/unicorns/{name}": { 

30 "get": { 

31 "responses": { 

32 "200": { 

33 "description": "Successful Response", 

34 "content": {"application/json": {"schema": {}}}, 

35 }, 

36 "422": { 

37 "description": "Validation Error", 

38 "content": { 

39 "application/json": { 

40 "schema": { 

41 "$ref": "#/components/schemas/HTTPValidationError" 

42 } 

43 } 

44 }, 

45 }, 

46 }, 

47 "summary": "Read Unicorn", 

48 "operationId": "read_unicorn_unicorns__name__get", 

49 "parameters": [ 

50 { 

51 "required": True, 

52 "schema": {"title": "Name", "type": "string"}, 

53 "name": "name", 

54 "in": "path", 

55 } 

56 ], 

57 } 

58 } 

59 }, 

60 "components": { 

61 "schemas": { 

62 "ValidationError": { 

63 "title": "ValidationError", 

64 "required": ["loc", "msg", "type"], 

65 "type": "object", 

66 "properties": { 

67 "loc": { 

68 "title": "Location", 

69 "type": "array", 

70 "items": { 

71 "anyOf": [{"type": "string"}, {"type": "integer"}] 

72 }, 

73 }, 

74 "msg": {"title": "Message", "type": "string"}, 

75 "type": {"title": "Error Type", "type": "string"}, 

76 }, 

77 }, 

78 "HTTPValidationError": { 

79 "title": "HTTPValidationError", 

80 "type": "object", 

81 "properties": { 

82 "detail": { 

83 "title": "Detail", 

84 "type": "array", 

85 "items": {"$ref": "#/components/schemas/ValidationError"}, 

86 } 

87 }, 

88 }, 

89 } 

90 }, 

91 }