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

20 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-02-12 18:15 +0000

1from fastapi.testclient import TestClient 1abcd

2from inline_snapshot import snapshot 1abcd

3 

4from docs_src.generate_clients.tutorial003_py310 import app 1abcd

5 

6client = TestClient(app) 1abcd

7 

8 

9def test_post_items(): 1abcd

10 response = client.post("/items/", json={"name": "Foo", "price": 5}) 1efg

11 assert response.status_code == 200, response.text 1efg

12 assert response.json() == {"message": "Item received"} 1efg

13 

14 

15def test_post_users(): 1abcd

16 response = client.post( 1hij

17 "/users/", json={"username": "Foo", "email": "foo@example.com"} 

18 ) 

19 assert response.status_code == 200, response.text 1hij

20 assert response.json() == {"message": "User received"} 1hij

21 

22 

23def test_get_items(): 1abcd

24 response = client.get("/items/") 1klm

25 assert response.status_code == 200, response.text 1klm

26 assert response.json() == [ 1klm

27 {"name": "Plumbus", "price": 3}, 

28 {"name": "Portal Gun", "price": 9001}, 

29 ] 

30 

31 

32def test_openapi_schema(): 1abcd

33 response = client.get("/openapi.json") 1nop

34 assert response.status_code == 200, response.text 1nop

35 assert response.json() == snapshot( 1nop

36 { 

37 "openapi": "3.1.0", 

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

39 "paths": { 

40 "/items/": { 

41 "get": { 

42 "tags": ["items"], 

43 "summary": "Get Items", 

44 "operationId": "items-get_items", 

45 "responses": { 

46 "200": { 

47 "description": "Successful Response", 

48 "content": { 

49 "application/json": { 

50 "schema": { 

51 "title": "Response Items-Get Items", 

52 "type": "array", 

53 "items": { 

54 "$ref": "#/components/schemas/Item" 

55 }, 

56 } 

57 } 

58 }, 

59 } 

60 }, 

61 }, 

62 "post": { 

63 "tags": ["items"], 

64 "summary": "Create Item", 

65 "operationId": "items-create_item", 

66 "requestBody": { 

67 "content": { 

68 "application/json": { 

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

70 } 

71 }, 

72 "required": True, 

73 }, 

74 "responses": { 

75 "200": { 

76 "description": "Successful Response", 

77 "content": { 

78 "application/json": { 

79 "schema": { 

80 "$ref": "#/components/schemas/ResponseMessage" 

81 } 

82 } 

83 }, 

84 }, 

85 "422": { 

86 "description": "Validation Error", 

87 "content": { 

88 "application/json": { 

89 "schema": { 

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

91 } 

92 } 

93 }, 

94 }, 

95 }, 

96 }, 

97 }, 

98 "/users/": { 

99 "post": { 

100 "tags": ["users"], 

101 "summary": "Create User", 

102 "operationId": "users-create_user", 

103 "requestBody": { 

104 "content": { 

105 "application/json": { 

106 "schema": {"$ref": "#/components/schemas/User"} 

107 } 

108 }, 

109 "required": True, 

110 }, 

111 "responses": { 

112 "200": { 

113 "description": "Successful Response", 

114 "content": { 

115 "application/json": { 

116 "schema": { 

117 "$ref": "#/components/schemas/ResponseMessage" 

118 } 

119 } 

120 }, 

121 }, 

122 "422": { 

123 "description": "Validation Error", 

124 "content": { 

125 "application/json": { 

126 "schema": { 

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

128 } 

129 } 

130 }, 

131 }, 

132 }, 

133 } 

134 }, 

135 }, 

136 "components": { 

137 "schemas": { 

138 "HTTPValidationError": { 

139 "title": "HTTPValidationError", 

140 "type": "object", 

141 "properties": { 

142 "detail": { 

143 "title": "Detail", 

144 "type": "array", 

145 "items": { 

146 "$ref": "#/components/schemas/ValidationError" 

147 }, 

148 } 

149 }, 

150 }, 

151 "Item": { 

152 "title": "Item", 

153 "required": ["name", "price"], 

154 "type": "object", 

155 "properties": { 

156 "name": {"title": "Name", "type": "string"}, 

157 "price": {"title": "Price", "type": "number"}, 

158 }, 

159 }, 

160 "ResponseMessage": { 

161 "title": "ResponseMessage", 

162 "required": ["message"], 

163 "type": "object", 

164 "properties": { 

165 "message": {"title": "Message", "type": "string"} 

166 }, 

167 }, 

168 "User": { 

169 "title": "User", 

170 "required": ["username", "email"], 

171 "type": "object", 

172 "properties": { 

173 "username": {"title": "Username", "type": "string"}, 

174 "email": {"title": "Email", "type": "string"}, 

175 }, 

176 }, 

177 "ValidationError": { 

178 "title": "ValidationError", 

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

180 "type": "object", 

181 "properties": { 

182 "loc": { 

183 "title": "Location", 

184 "type": "array", 

185 "items": { 

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

187 }, 

188 }, 

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

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

191 "input": {"title": "Input"}, 

192 "ctx": {"title": "Context", "type": "object"}, 

193 }, 

194 }, 

195 } 

196 }, 

197 } 

198 )