Coverage for tests/test_response_model_sub_types.py: 100%

33 statements  

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

1from typing import List 1abcde

2 

3from fastapi import FastAPI 1abcde

4from fastapi.testclient import TestClient 1abcde

5from pydantic import BaseModel 1abcde

6 

7 

8class Model(BaseModel): 1abcde

9 name: str 1abcde

10 

11 

12app = FastAPI() 1abcde

13 

14 

15@app.get("/valid1", responses={"500": {"model": int}}) 1abcde

16def valid1(): 1abcde

17 pass 1abcde

18 

19 

20@app.get("/valid2", responses={"500": {"model": List[int]}}) 1abcde

21def valid2(): 1abcde

22 pass 1abcde

23 

24 

25@app.get("/valid3", responses={"500": {"model": Model}}) 1abcde

26def valid3(): 1abcde

27 pass 1abcde

28 

29 

30@app.get("/valid4", responses={"500": {"model": List[Model]}}) 1abcde

31def valid4(): 1abcde

32 pass 1abcde

33 

34 

35client = TestClient(app) 1abcde

36 

37 

38def test_path_operations(): 1abcde

39 response = client.get("/valid1") 1abcde

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

41 response = client.get("/valid2") 1abcde

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

43 response = client.get("/valid3") 1abcde

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

45 response = client.get("/valid4") 1abcde

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

47 

48 

49def test_openapi_schema(): 1abcde

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

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

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

53 "openapi": "3.1.0", 

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

55 "paths": { 

56 "/valid1": { 

57 "get": { 

58 "summary": "Valid1", 

59 "operationId": "valid1_valid1_get", 

60 "responses": { 

61 "200": { 

62 "description": "Successful Response", 

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

64 }, 

65 "500": { 

66 "description": "Internal Server Error", 

67 "content": { 

68 "application/json": { 

69 "schema": { 

70 "title": "Response 500 Valid1 Valid1 Get", 

71 "type": "integer", 

72 } 

73 } 

74 }, 

75 }, 

76 }, 

77 } 

78 }, 

79 "/valid2": { 

80 "get": { 

81 "summary": "Valid2", 

82 "operationId": "valid2_valid2_get", 

83 "responses": { 

84 "200": { 

85 "description": "Successful Response", 

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

87 }, 

88 "500": { 

89 "description": "Internal Server Error", 

90 "content": { 

91 "application/json": { 

92 "schema": { 

93 "title": "Response 500 Valid2 Valid2 Get", 

94 "type": "array", 

95 "items": {"type": "integer"}, 

96 } 

97 } 

98 }, 

99 }, 

100 }, 

101 } 

102 }, 

103 "/valid3": { 

104 "get": { 

105 "summary": "Valid3", 

106 "operationId": "valid3_valid3_get", 

107 "responses": { 

108 "200": { 

109 "description": "Successful Response", 

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

111 }, 

112 "500": { 

113 "description": "Internal Server Error", 

114 "content": { 

115 "application/json": { 

116 "schema": {"$ref": "#/components/schemas/Model"} 

117 } 

118 }, 

119 }, 

120 }, 

121 } 

122 }, 

123 "/valid4": { 

124 "get": { 

125 "summary": "Valid4", 

126 "operationId": "valid4_valid4_get", 

127 "responses": { 

128 "200": { 

129 "description": "Successful Response", 

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

131 }, 

132 "500": { 

133 "description": "Internal Server Error", 

134 "content": { 

135 "application/json": { 

136 "schema": { 

137 "title": "Response 500 Valid4 Valid4 Get", 

138 "type": "array", 

139 "items": {"$ref": "#/components/schemas/Model"}, 

140 } 

141 } 

142 }, 

143 }, 

144 }, 

145 } 

146 }, 

147 }, 

148 "components": { 

149 "schemas": { 

150 "Model": { 

151 "title": "Model", 

152 "required": ["name"], 

153 "type": "object", 

154 "properties": {"name": {"title": "Name", "type": "string"}}, 

155 } 

156 } 

157 }, 

158 }