Coverage for tests/test_tutorial/test_openapi_callbacks/test_tutorial001.py: 100%

14 statements  

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

1from dirty_equals import IsDict 1abcde

2from fastapi.testclient import TestClient 1abcde

3 

4from docs_src.openapi_callbacks.tutorial001 import app, invoice_notification 1abcde

5 

6client = TestClient(app) 1abcde

7 

8 

9def test_get(): 1abcde

10 response = client.post( 1abcde

11 "/invoices/", json={"id": "fooinvoice", "customer": "John", "total": 5.3} 

12 ) 

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

14 assert response.json() == {"msg": "Invoice received"} 1abcde

15 

16 

17def test_dummy_callback(): 1abcde

18 # Just for coverage 

19 invoice_notification({}) 1abcde

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 "/invoices/": { 

30 "post": { 

31 "summary": "Create Invoice", 

32 "description": 'Create an invoice.\n\nThis will (let\'s imagine) let the API user (some external developer) create an\ninvoice.\n\nAnd this path operation will:\n\n* Send the invoice to the client.\n* Collect the money from the client.\n* Send a notification back to the API user (the external developer), as a callback.\n * At this point is that the API will somehow send a POST request to the\n external API with the notification of the invoice event\n (e.g. "payment successful").', 

33 "operationId": "create_invoice_invoices__post", 

34 "parameters": [ 

35 { 

36 "required": False, 

37 "schema": IsDict( 

38 { 

39 "anyOf": [ 

40 { 

41 "type": "string", 

42 "format": "uri", 

43 "minLength": 1, 

44 "maxLength": 2083, 

45 }, 

46 {"type": "null"}, 

47 ], 

48 "title": "Callback Url", 

49 } 

50 ) 

51 | IsDict( 

52 # TODO: remove when deprecating Pydantic v1 

53 { 

54 "title": "Callback Url", 

55 "maxLength": 2083, 

56 "minLength": 1, 

57 "type": "string", 

58 "format": "uri", 

59 } 

60 ), 

61 "name": "callback_url", 

62 "in": "query", 

63 } 

64 ], 

65 "requestBody": { 

66 "content": { 

67 "application/json": { 

68 "schema": {"$ref": "#/components/schemas/Invoice"} 

69 } 

70 }, 

71 "required": True, 

72 }, 

73 "responses": { 

74 "200": { 

75 "description": "Successful Response", 

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

77 }, 

78 "422": { 

79 "description": "Validation Error", 

80 "content": { 

81 "application/json": { 

82 "schema": { 

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

84 } 

85 } 

86 }, 

87 }, 

88 }, 

89 "callbacks": { 

90 "invoice_notification": { 

91 "{$callback_url}/invoices/{$request.body.id}": { 

92 "post": { 

93 "summary": "Invoice Notification", 

94 "operationId": "invoice_notification__callback_url__invoices___request_body_id__post", 

95 "requestBody": { 

96 "required": True, 

97 "content": { 

98 "application/json": { 

99 "schema": { 

100 "$ref": "#/components/schemas/InvoiceEvent" 

101 } 

102 } 

103 }, 

104 }, 

105 "responses": { 

106 "200": { 

107 "description": "Successful Response", 

108 "content": { 

109 "application/json": { 

110 "schema": { 

111 "$ref": "#/components/schemas/InvoiceEventReceived" 

112 } 

113 } 

114 }, 

115 }, 

116 "422": { 

117 "description": "Validation Error", 

118 "content": { 

119 "application/json": { 

120 "schema": { 

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

122 } 

123 } 

124 }, 

125 }, 

126 }, 

127 } 

128 } 

129 } 

130 }, 

131 } 

132 } 

133 }, 

134 "components": { 

135 "schemas": { 

136 "HTTPValidationError": { 

137 "title": "HTTPValidationError", 

138 "type": "object", 

139 "properties": { 

140 "detail": { 

141 "title": "Detail", 

142 "type": "array", 

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

144 } 

145 }, 

146 }, 

147 "Invoice": { 

148 "title": "Invoice", 

149 "required": ["id", "customer", "total"], 

150 "type": "object", 

151 "properties": { 

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

153 "title": IsDict( 

154 { 

155 "title": "Title", 

156 "anyOf": [{"type": "string"}, {"type": "null"}], 

157 } 

158 ) 

159 | IsDict( 

160 # TODO: remove when deprecating Pydantic v1 

161 {"title": "Title", "type": "string"} 

162 ), 

163 "customer": {"title": "Customer", "type": "string"}, 

164 "total": {"title": "Total", "type": "number"}, 

165 }, 

166 }, 

167 "InvoiceEvent": { 

168 "title": "InvoiceEvent", 

169 "required": ["description", "paid"], 

170 "type": "object", 

171 "properties": { 

172 "description": {"title": "Description", "type": "string"}, 

173 "paid": {"title": "Paid", "type": "boolean"}, 

174 }, 

175 }, 

176 "InvoiceEventReceived": { 

177 "title": "InvoiceEventReceived", 

178 "required": ["ok"], 

179 "type": "object", 

180 "properties": {"ok": {"title": "Ok", "type": "boolean"}}, 

181 }, 

182 "ValidationError": { 

183 "title": "ValidationError", 

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

185 "type": "object", 

186 "properties": { 

187 "loc": { 

188 "title": "Location", 

189 "type": "array", 

190 "items": { 

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

192 }, 

193 }, 

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

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

196 }, 

197 }, 

198 } 

199 }, 

200 }