Coverage for tests/test_tutorial/test_additional_responses/test_tutorial001.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.additional_responses.tutorial001 import app 1abcde

4 

5client = TestClient(app) 1abcde

6 

7 

8def test_path_operation(): 1abcde

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

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

11 assert response.json() == {"id": "foo", "value": "there goes my hero"} 1abcde

12 

13 

14def test_path_operation_not_found(): 1abcde

15 response = client.get("/items/bar") 1abcde

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

17 assert response.json() == {"message": "Item not found"} 1abcde

18 

19 

20def test_openapi_schema(): 1abcde

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

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

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

24 "openapi": "3.1.0", 

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

26 "paths": { 

27 "/items/{item_id}": { 

28 "get": { 

29 "responses": { 

30 "404": { 

31 "description": "Not Found", 

32 "content": { 

33 "application/json": { 

34 "schema": {"$ref": "#/components/schemas/Message"} 

35 } 

36 }, 

37 }, 

38 "200": { 

39 "description": "Successful Response", 

40 "content": { 

41 "application/json": { 

42 "schema": {"$ref": "#/components/schemas/Item"} 

43 } 

44 }, 

45 }, 

46 "422": { 

47 "description": "Validation Error", 

48 "content": { 

49 "application/json": { 

50 "schema": { 

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

52 } 

53 } 

54 }, 

55 }, 

56 }, 

57 "summary": "Read Item", 

58 "operationId": "read_item_items__item_id__get", 

59 "parameters": [ 

60 { 

61 "required": True, 

62 "schema": {"title": "Item Id", "type": "string"}, 

63 "name": "item_id", 

64 "in": "path", 

65 } 

66 ], 

67 } 

68 } 

69 }, 

70 "components": { 

71 "schemas": { 

72 "Item": { 

73 "title": "Item", 

74 "required": ["id", "value"], 

75 "type": "object", 

76 "properties": { 

77 "id": {"title": "Id", "type": "string"}, 

78 "value": {"title": "Value", "type": "string"}, 

79 }, 

80 }, 

81 "Message": { 

82 "title": "Message", 

83 "required": ["message"], 

84 "type": "object", 

85 "properties": {"message": {"title": "Message", "type": "string"}}, 

86 }, 

87 "ValidationError": { 

88 "title": "ValidationError", 

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

90 "type": "object", 

91 "properties": { 

92 "loc": { 

93 "title": "Location", 

94 "type": "array", 

95 "items": { 

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

97 }, 

98 }, 

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

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

101 }, 

102 }, 

103 "HTTPValidationError": { 

104 "title": "HTTPValidationError", 

105 "type": "object", 

106 "properties": { 

107 "detail": { 

108 "title": "Detail", 

109 "type": "array", 

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

111 } 

112 }, 

113 }, 

114 } 

115 }, 

116 }