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

23 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-02-12 18:15 +0000

1import importlib 1abdc

2 

3import pytest 1abdc

4from fastapi.testclient import TestClient 1abdc

5from inline_snapshot import snapshot 1abdc

6 

7from ...utils import needs_py310 1abdc

8 

9 

10@pytest.fixture( 1abdc

11 name="client", 

12 params=[ 

13 pytest.param("tutorial003_py310", marks=needs_py310), 

14 ], 

15) 

16def get_client(request: pytest.FixtureRequest): 1abdc

17 mod = importlib.import_module(f"docs_src.dataclasses_.{request.param}") 1abc

18 

19 client = TestClient(mod.app) 1abc

20 client.headers.clear() 1abc

21 return client 1abc

22 

23 

24def test_post_authors_item(client: TestClient): 1abdc

25 response = client.post( 1efg

26 "/authors/foo/items/", 

27 json=[{"name": "Bar"}, {"name": "Baz", "description": "Drop the Baz"}], 

28 ) 

29 assert response.status_code == 200 1efg

30 assert response.json() == { 1efg

31 "name": "foo", 

32 "items": [ 

33 {"name": "Bar", "description": None}, 

34 {"name": "Baz", "description": "Drop the Baz"}, 

35 ], 

36 } 

37 

38 

39def test_get_authors(client: TestClient): 1abdc

40 response = client.get("/authors/") 1hij

41 assert response.status_code == 200 1hij

42 assert response.json() == [ 1hij

43 { 

44 "name": "Breaters", 

45 "items": [ 

46 { 

47 "name": "Island In The Moon", 

48 "description": "A place to be playin' and havin' fun", 

49 }, 

50 {"name": "Holy Buddies", "description": None}, 

51 ], 

52 }, 

53 { 

54 "name": "System of an Up", 

55 "items": [ 

56 { 

57 "name": "Salt", 

58 "description": "The kombucha mushroom people's favorite", 

59 }, 

60 {"name": "Pad Thai", "description": None}, 

61 { 

62 "name": "Lonely Night", 

63 "description": "The mostests lonliest nightiest of allest", 

64 }, 

65 ], 

66 }, 

67 ] 

68 

69 

70def test_openapi_schema(client: TestClient): 1abdc

71 response = client.get("/openapi.json") 1klm

72 assert response.status_code == 200 1klm

73 assert response.json() == snapshot( 1klm

74 { 

75 "openapi": "3.1.0", 

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

77 "paths": { 

78 "/authors/{author_id}/items/": { 

79 "post": { 

80 "summary": "Create Author Items", 

81 "operationId": "create_author_items_authors__author_id__items__post", 

82 "parameters": [ 

83 { 

84 "required": True, 

85 "schema": {"title": "Author Id", "type": "string"}, 

86 "name": "author_id", 

87 "in": "path", 

88 } 

89 ], 

90 "requestBody": { 

91 "content": { 

92 "application/json": { 

93 "schema": { 

94 "title": "Items", 

95 "type": "array", 

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

97 } 

98 } 

99 }, 

100 "required": True, 

101 }, 

102 "responses": { 

103 "200": { 

104 "description": "Successful Response", 

105 "content": { 

106 "application/json": { 

107 "schema": { 

108 "$ref": "#/components/schemas/Author" 

109 } 

110 } 

111 }, 

112 }, 

113 "422": { 

114 "description": "Validation Error", 

115 "content": { 

116 "application/json": { 

117 "schema": { 

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

119 } 

120 } 

121 }, 

122 }, 

123 }, 

124 } 

125 }, 

126 "/authors/": { 

127 "get": { 

128 "summary": "Get Authors", 

129 "operationId": "get_authors_authors__get", 

130 "responses": { 

131 "200": { 

132 "description": "Successful Response", 

133 "content": { 

134 "application/json": { 

135 "schema": { 

136 "title": "Response Get Authors Authors Get", 

137 "type": "array", 

138 "items": { 

139 "$ref": "#/components/schemas/Author" 

140 }, 

141 } 

142 } 

143 }, 

144 } 

145 }, 

146 } 

147 }, 

148 }, 

149 "components": { 

150 "schemas": { 

151 "Author": { 

152 "title": "Author", 

153 "required": ["name"], 

154 "type": "object", 

155 "properties": { 

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

157 "items": { 

158 "title": "Items", 

159 "type": "array", 

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

161 }, 

162 }, 

163 }, 

164 "HTTPValidationError": { 

165 "title": "HTTPValidationError", 

166 "type": "object", 

167 "properties": { 

168 "detail": { 

169 "title": "Detail", 

170 "type": "array", 

171 "items": { 

172 "$ref": "#/components/schemas/ValidationError" 

173 }, 

174 } 

175 }, 

176 }, 

177 "Item": { 

178 "title": "Item", 

179 "required": ["name"], 

180 "type": "object", 

181 "properties": { 

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

183 "description": { 

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

185 "title": "Description", 

186 }, 

187 }, 

188 }, 

189 "ValidationError": { 

190 "title": "ValidationError", 

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

192 "type": "object", 

193 "properties": { 

194 "loc": { 

195 "title": "Location", 

196 "type": "array", 

197 "items": { 

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

199 }, 

200 }, 

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

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

203 "input": {"title": "Input"}, 

204 "ctx": {"title": "Context", "type": "object"}, 

205 }, 

206 }, 

207 } 

208 }, 

209 } 

210 )