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

23 statements  

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

1import pytest 1deabc

2from dirty_equals import IsDict 1deabc

3from fastapi.testclient import TestClient 1deabc

4 

5from ...utils import needs_py310 1deabc

6 

7 

8@pytest.fixture(name="client") 1deabc

9def get_client(): 1deabc

10 from docs_src.extra_data_types.tutorial001_py310 import app 1abc

11 

12 client = TestClient(app) 1abc

13 return client 1abc

14 

15 

16@needs_py310 1deabc

17def test_extra_types(client: TestClient): 1deabc

18 item_id = "ff97dd87-a4a5-4a12-b412-cde99f33e00e" 1abc

19 data = { 1abc

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

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

22 "repeat_at": "15:30:00", 

23 "process_after": 300, 

24 } 

25 expected_response = data.copy() 1abc

26 expected_response.update( 1abc

27 { 

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

29 "duration": 176_100, 

30 "item_id": item_id, 

31 } 

32 ) 

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

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

35 assert response.json() == expected_response 1abc

36 

37 

38@needs_py310 1deabc

39def test_openapi_schema(client: TestClient): 1deabc

40 response = client.get("/openapi.json") 1abc

41 assert response.status_code == 200, response.text 1abc

42 assert response.json() == { 1abc

43 "openapi": "3.1.0", 

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

45 "paths": { 

46 "/items/{item_id}": { 

47 "put": { 

48 "responses": { 

49 "200": { 

50 "description": "Successful Response", 

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

52 }, 

53 "422": { 

54 "description": "Validation Error", 

55 "content": { 

56 "application/json": { 

57 "schema": { 

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

59 } 

60 } 

61 }, 

62 }, 

63 }, 

64 "summary": "Read Items", 

65 "operationId": "read_items_items__item_id__put", 

66 "parameters": [ 

67 { 

68 "required": True, 

69 "schema": { 

70 "title": "Item Id", 

71 "type": "string", 

72 "format": "uuid", 

73 }, 

74 "name": "item_id", 

75 "in": "path", 

76 } 

77 ], 

78 "requestBody": { 

79 "required": True, 

80 "content": { 

81 "application/json": { 

82 "schema": IsDict( 

83 { 

84 "allOf": [ 

85 { 

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

87 } 

88 ], 

89 "title": "Body", 

90 } 

91 ) 

92 | IsDict( 

93 # TODO: remove when deprecating Pydantic v1 

94 { 

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

96 } 

97 ) 

98 } 

99 }, 

100 }, 

101 } 

102 } 

103 }, 

104 "components": { 

105 "schemas": { 

106 "Body_read_items_items__item_id__put": { 

107 "title": "Body_read_items_items__item_id__put", 

108 "type": "object", 

109 "properties": { 

110 "start_datetime": { 

111 "title": "Start Datetime", 

112 "type": "string", 

113 "format": "date-time", 

114 }, 

115 "end_datetime": { 

116 "title": "End Datetime", 

117 "type": "string", 

118 "format": "date-time", 

119 }, 

120 "repeat_at": IsDict( 

121 { 

122 "title": "Repeat At", 

123 "anyOf": [ 

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

125 {"type": "null"}, 

126 ], 

127 } 

128 ) 

129 | IsDict( 

130 # TODO: remove when deprecating Pydantic v1 

131 { 

132 "title": "Repeat At", 

133 "type": "string", 

134 "format": "time", 

135 } 

136 ), 

137 "process_after": IsDict( 

138 { 

139 "title": "Process After", 

140 "type": "string", 

141 "format": "duration", 

142 } 

143 ) 

144 | IsDict( 

145 # TODO: remove when deprecating Pydantic v1 

146 { 

147 "title": "Process After", 

148 "type": "number", 

149 "format": "time-delta", 

150 } 

151 ), 

152 }, 

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

154 }, 

155 "ValidationError": { 

156 "title": "ValidationError", 

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

158 "type": "object", 

159 "properties": { 

160 "loc": { 

161 "title": "Location", 

162 "type": "array", 

163 "items": { 

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

165 }, 

166 }, 

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

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

169 }, 

170 }, 

171 "HTTPValidationError": { 

172 "title": "HTTPValidationError", 

173 "type": "object", 

174 "properties": { 

175 "detail": { 

176 "title": "Detail", 

177 "type": "array", 

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

179 } 

180 }, 

181 }, 

182 } 

183 }, 

184 }