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

19 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.generate_clients.tutorial003 import app 1abcde

4 

5client = TestClient(app) 1abcde

6 

7 

8def test_post_items(): 1abcde

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

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

11 assert response.json() == {"message": "Item received"} 1abcde

12 

13 

14def test_post_users(): 1abcde

15 response = client.post( 1abcde

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

17 ) 

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

19 assert response.json() == {"message": "User received"} 1abcde

20 

21 

22def test_get_items(): 1abcde

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

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

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

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

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

28 ] 

29 

30 

31def test_openapi_schema(): 1abcde

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

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

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

35 "openapi": "3.1.0", 

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

37 "paths": { 

38 "/items/": { 

39 "get": { 

40 "tags": ["items"], 

41 "summary": "Get Items", 

42 "operationId": "items-get_items", 

43 "responses": { 

44 "200": { 

45 "description": "Successful Response", 

46 "content": { 

47 "application/json": { 

48 "schema": { 

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

50 "type": "array", 

51 "items": {"$ref": "#/components/schemas/Item"}, 

52 } 

53 } 

54 }, 

55 } 

56 }, 

57 }, 

58 "post": { 

59 "tags": ["items"], 

60 "summary": "Create Item", 

61 "operationId": "items-create_item", 

62 "requestBody": { 

63 "content": { 

64 "application/json": { 

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

66 } 

67 }, 

68 "required": True, 

69 }, 

70 "responses": { 

71 "200": { 

72 "description": "Successful Response", 

73 "content": { 

74 "application/json": { 

75 "schema": { 

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

77 } 

78 } 

79 }, 

80 }, 

81 "422": { 

82 "description": "Validation Error", 

83 "content": { 

84 "application/json": { 

85 "schema": { 

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

87 } 

88 } 

89 }, 

90 }, 

91 }, 

92 }, 

93 }, 

94 "/users/": { 

95 "post": { 

96 "tags": ["users"], 

97 "summary": "Create User", 

98 "operationId": "users-create_user", 

99 "requestBody": { 

100 "content": { 

101 "application/json": { 

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

103 } 

104 }, 

105 "required": True, 

106 }, 

107 "responses": { 

108 "200": { 

109 "description": "Successful Response", 

110 "content": { 

111 "application/json": { 

112 "schema": { 

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

114 } 

115 } 

116 }, 

117 }, 

118 "422": { 

119 "description": "Validation Error", 

120 "content": { 

121 "application/json": { 

122 "schema": { 

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

124 } 

125 } 

126 }, 

127 }, 

128 }, 

129 } 

130 }, 

131 }, 

132 "components": { 

133 "schemas": { 

134 "HTTPValidationError": { 

135 "title": "HTTPValidationError", 

136 "type": "object", 

137 "properties": { 

138 "detail": { 

139 "title": "Detail", 

140 "type": "array", 

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

142 } 

143 }, 

144 }, 

145 "Item": { 

146 "title": "Item", 

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

148 "type": "object", 

149 "properties": { 

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

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

152 }, 

153 }, 

154 "ResponseMessage": { 

155 "title": "ResponseMessage", 

156 "required": ["message"], 

157 "type": "object", 

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

159 }, 

160 "User": { 

161 "title": "User", 

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

163 "type": "object", 

164 "properties": { 

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

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

167 }, 

168 }, 

169 "ValidationError": { 

170 "title": "ValidationError", 

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

172 "type": "object", 

173 "properties": { 

174 "loc": { 

175 "title": "Location", 

176 "type": "array", 

177 "items": { 

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

179 }, 

180 }, 

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

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

183 }, 

184 }, 

185 } 

186 }, 

187 }