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

22 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-05-05 00:03 +0000

1import importlib 1abcdef

2 

3import pytest 1abcdef

4from dirty_equals import IsDict 1abcdef

5from fastapi.testclient import TestClient 1abcdef

6 

7from ...utils import needs_py39, needs_py310 1abcdef

8 

9 

10@pytest.fixture( 1abcdef

11 name="client", 

12 params=[ 

13 "tutorial001", 

14 pytest.param("tutorial001_py310", marks=needs_py310), 

15 "tutorial001_an", 

16 pytest.param("tutorial001_an_py39", marks=needs_py39), 

17 pytest.param("tutorial001_an_py310", marks=needs_py310), 

18 ], 

19) 

20def get_client(request: pytest.FixtureRequest): 1abcdef

21 mod = importlib.import_module(f"docs_src.extra_data_types.{request.param}") 1abcdef

22 

23 client = TestClient(mod.app) 1abcdef

24 return client 1abcdef

25 

26 

27def test_extra_types(client: TestClient): 1abcdef

28 item_id = "ff97dd87-a4a5-4a12-b412-cde99f33e00e" 1ghijkl

29 data = { 1ghijkl

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

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

32 "repeat_at": "15:30:00", 

33 "process_after": 300, 

34 } 

35 expected_response = data.copy() 1ghijkl

36 expected_response.update( 1ghijkl

37 { 

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

39 "duration": 176_100, 

40 "item_id": item_id, 

41 } 

42 ) 

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

44 assert response.status_code == 200, response.text 1ghijkl

45 assert response.json() == expected_response 1ghijkl

46 

47 

48def test_openapi_schema(client: TestClient): 1abcdef

49 response = client.get("/openapi.json") 1mnopqr

50 assert response.status_code == 200, response.text 1mnopqr

51 assert response.json() == { 1mnopqr

52 "openapi": "3.1.0", 

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

54 "paths": { 

55 "/items/{item_id}": { 

56 "put": { 

57 "responses": { 

58 "200": { 

59 "description": "Successful Response", 

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

61 }, 

62 "422": { 

63 "description": "Validation Error", 

64 "content": { 

65 "application/json": { 

66 "schema": { 

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

68 } 

69 } 

70 }, 

71 }, 

72 }, 

73 "summary": "Read Items", 

74 "operationId": "read_items_items__item_id__put", 

75 "parameters": [ 

76 { 

77 "required": True, 

78 "schema": { 

79 "title": "Item Id", 

80 "type": "string", 

81 "format": "uuid", 

82 }, 

83 "name": "item_id", 

84 "in": "path", 

85 } 

86 ], 

87 "requestBody": { 

88 "required": True, 

89 "content": { 

90 "application/json": { 

91 "schema": IsDict( 

92 { 

93 "allOf": [ 

94 { 

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

96 } 

97 ], 

98 "title": "Body", 

99 } 

100 ) 

101 | IsDict( 

102 # TODO: remove when deprecating Pydantic v1 

103 { 

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

105 } 

106 ) 

107 } 

108 }, 

109 }, 

110 } 

111 } 

112 }, 

113 "components": { 

114 "schemas": { 

115 "Body_read_items_items__item_id__put": { 

116 "title": "Body_read_items_items__item_id__put", 

117 "type": "object", 

118 "properties": { 

119 "start_datetime": { 

120 "title": "Start Datetime", 

121 "type": "string", 

122 "format": "date-time", 

123 }, 

124 "end_datetime": { 

125 "title": "End Datetime", 

126 "type": "string", 

127 "format": "date-time", 

128 }, 

129 "repeat_at": IsDict( 

130 { 

131 "title": "Repeat At", 

132 "anyOf": [ 

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

134 {"type": "null"}, 

135 ], 

136 } 

137 ) 

138 | IsDict( 

139 # TODO: remove when deprecating Pydantic v1 

140 { 

141 "title": "Repeat At", 

142 "type": "string", 

143 "format": "time", 

144 } 

145 ), 

146 "process_after": IsDict( 

147 { 

148 "title": "Process After", 

149 "type": "string", 

150 "format": "duration", 

151 } 

152 ) 

153 | IsDict( 

154 # TODO: remove when deprecating Pydantic v1 

155 { 

156 "title": "Process After", 

157 "type": "number", 

158 "format": "time-delta", 

159 } 

160 ), 

161 }, 

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

163 }, 

164 "ValidationError": { 

165 "title": "ValidationError", 

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

167 "type": "object", 

168 "properties": { 

169 "loc": { 

170 "title": "Location", 

171 "type": "array", 

172 "items": { 

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

174 }, 

175 }, 

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

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

178 }, 

179 }, 

180 "HTTPValidationError": { 

181 "title": "HTTPValidationError", 

182 "type": "object", 

183 "properties": { 

184 "detail": { 

185 "title": "Detail", 

186 "type": "array", 

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

188 } 

189 }, 

190 }, 

191 } 

192 }, 

193 }