Coverage for tests/test_tutorial/test_extra_data_types/test_tutorial001_an.py: 100%

16 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.extra_data_types.tutorial001_an import app 1abcde

5 

6client = TestClient(app) 1abcde

7 

8 

9def test_extra_types(): 1abcde

10 item_id = "ff97dd87-a4a5-4a12-b412-cde99f33e00e" 1abcde

11 data = { 1abcde

12 "start_datetime": "2018-12-22T14:00:00+00:00", 

13 "end_datetime": "2018-12-24T15:00:00+00:00", 

14 "repeat_at": "15:30:00", 

15 "process_after": 300, 

16 } 

17 expected_response = data.copy() 1abcde

18 expected_response.update( 1abcde

19 { 

20 "start_process": "2018-12-22T14:05:00+00:00", 

21 "duration": 176_100, 

22 "item_id": item_id, 

23 } 

24 ) 

25 response = client.put(f"/items/{item_id}", json=data) 1abcde

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

27 assert response.json() == expected_response 1abcde

28 

29 

30def test_openapi_schema(): 1abcde

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

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

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

34 "openapi": "3.1.0", 

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

36 "paths": { 

37 "/items/{item_id}": { 

38 "put": { 

39 "responses": { 

40 "200": { 

41 "description": "Successful Response", 

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

43 }, 

44 "422": { 

45 "description": "Validation Error", 

46 "content": { 

47 "application/json": { 

48 "schema": { 

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

50 } 

51 } 

52 }, 

53 }, 

54 }, 

55 "summary": "Read Items", 

56 "operationId": "read_items_items__item_id__put", 

57 "parameters": [ 

58 { 

59 "required": True, 

60 "schema": { 

61 "title": "Item Id", 

62 "type": "string", 

63 "format": "uuid", 

64 }, 

65 "name": "item_id", 

66 "in": "path", 

67 } 

68 ], 

69 "requestBody": { 

70 "required": True, 

71 "content": { 

72 "application/json": { 

73 "schema": IsDict( 

74 { 

75 "allOf": [ 

76 { 

77 "$ref": "#/components/schemas/Body_read_items_items__item_id__put" 

78 } 

79 ], 

80 "title": "Body", 

81 } 

82 ) 

83 | IsDict( 

84 # TODO: remove when deprecating Pydantic v1 

85 { 

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

87 } 

88 ) 

89 } 

90 }, 

91 }, 

92 } 

93 } 

94 }, 

95 "components": { 

96 "schemas": { 

97 "Body_read_items_items__item_id__put": { 

98 "title": "Body_read_items_items__item_id__put", 

99 "type": "object", 

100 "properties": { 

101 "start_datetime": { 

102 "title": "Start Datetime", 

103 "type": "string", 

104 "format": "date-time", 

105 }, 

106 "end_datetime": { 

107 "title": "End Datetime", 

108 "type": "string", 

109 "format": "date-time", 

110 }, 

111 "repeat_at": IsDict( 

112 { 

113 "title": "Repeat At", 

114 "anyOf": [ 

115 {"type": "string", "format": "time"}, 

116 {"type": "null"}, 

117 ], 

118 } 

119 ) 

120 | IsDict( 

121 # TODO: remove when deprecating Pydantic v1 

122 { 

123 "title": "Repeat At", 

124 "type": "string", 

125 "format": "time", 

126 } 

127 ), 

128 "process_after": IsDict( 

129 { 

130 "title": "Process After", 

131 "type": "string", 

132 "format": "duration", 

133 } 

134 ) 

135 | IsDict( 

136 # TODO: remove when deprecating Pydantic v1 

137 { 

138 "title": "Process After", 

139 "type": "number", 

140 "format": "time-delta", 

141 } 

142 ), 

143 }, 

144 "required": ["start_datetime", "end_datetime", "process_after"], 

145 }, 

146 "ValidationError": { 

147 "title": "ValidationError", 

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

149 "type": "object", 

150 "properties": { 

151 "loc": { 

152 "title": "Location", 

153 "type": "array", 

154 "items": { 

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

156 }, 

157 }, 

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

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

160 }, 

161 }, 

162 "HTTPValidationError": { 

163 "title": "HTTPValidationError", 

164 "type": "object", 

165 "properties": { 

166 "detail": { 

167 "title": "Detail", 

168 "type": "array", 

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

170 } 

171 }, 

172 }, 

173 } 

174 }, 

175 }