Coverage for tests / test_tutorial / test_fastapi / test_limit_and_offset / test_tutorial001.py: 100%

52 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2026-11-09 00:02 +0000

1import importlib 1jklmnopqr

2from types import ModuleType 1jklmnopqr

3 

4import pytest 1jklmnopqr

5from dirty_equals import IsOneOf 1jklmnopqr

6from fastapi.testclient import TestClient 1jklmnopqr

7from sqlalchemy import Engine 1jklmnopqr

8 

9 

10@pytest.fixture( 1jklmnopqr

11 name="module", 

12 params=[ 

13 pytest.param("tutorial001_py310"), 

14 ], 

15) 

16def get_module(request: pytest.FixtureRequest, database_engine: Engine) -> ModuleType: 1jklmnopqr

17 mod = importlib.import_module( 1jklmnopqr

18 f"docs_src.tutorial.fastapi.limit_and_offset.{request.param}" 

19 ) 

20 mod.engine = database_engine 1jklmnopqr

21 return mod 1jklmnopqr

22 

23 

24def test_tutorial(module: ModuleType): 1jklmnopqr

25 with TestClient(module.app) as client: 1abcdefghi

26 hero1_data = {"name": "Deadpond", "secret_name": "Dive Wilson"} 1abcdefghi

27 hero2_data = { 1abcdefghi

28 "name": "Spider-Boy", 

29 "secret_name": "Pedro Parqueador", 

30 "id": 9000, 

31 } 

32 hero3_data = { 1abcdefghi

33 "name": "Rusty-Man", 

34 "secret_name": "Tommy Sharp", 

35 "age": 48, 

36 } 

37 response = client.post("/heroes/", json=hero1_data) 1abcdefghi

38 assert response.status_code == 200, response.text 1abcdefghi

39 response = client.post("/heroes/", json=hero2_data) 1abcdefghi

40 assert response.status_code == 200, response.text 1abcdefghi

41 hero2 = response.json() 1abcdefghi

42 hero_id = hero2["id"] 1abcdefghi

43 response = client.post("/heroes/", json=hero3_data) 1abcdefghi

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

45 response = client.get(f"/heroes/{hero_id}") 1abcdefghi

46 assert response.status_code == 200, response.text 1abcdefghi

47 response = client.get("/heroes/9000") 1abcdefghi

48 assert response.status_code == 404, response.text 1abcdefghi

49 

50 response = client.get("/heroes/") 1abcdefghi

51 assert response.status_code == 200, response.text 1abcdefghi

52 data = response.json() 1abcdefghi

53 assert len(data) == 3 1abcdefghi

54 

55 response = client.get("/heroes/", params={"limit": 2}) 1abcdefghi

56 assert response.status_code == 200, response.text 1abcdefghi

57 data = response.json() 1abcdefghi

58 assert len(data) == 2 1abcdefghi

59 assert data[0]["name"] == hero1_data["name"] 1abcdefghi

60 assert data[1]["name"] == hero2_data["name"] 1abcdefghi

61 

62 response = client.get("/heroes/", params={"offset": 1}) 1abcdefghi

63 assert response.status_code == 200, response.text 1abcdefghi

64 data = response.json() 1abcdefghi

65 assert len(data) == 2 1abcdefghi

66 assert data[0]["name"] == hero2_data["name"] 1abcdefghi

67 assert data[1]["name"] == hero3_data["name"] 1abcdefghi

68 

69 response = client.get("/heroes/", params={"offset": 1, "limit": 1}) 1abcdefghi

70 assert response.status_code == 200, response.text 1abcdefghi

71 data = response.json() 1abcdefghi

72 assert len(data) == 1 1abcdefghi

73 assert data[0]["name"] == hero2_data["name"] 1abcdefghi

74 

75 response = client.get("/openapi.json") 1abcdefghi

76 assert response.status_code == 200, response.text 1abcdefghi

77 assert response.json() == { 1abcdefghi

78 "openapi": "3.1.0", 

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

80 "paths": { 

81 "/heroes/": { 

82 "get": { 

83 "summary": "Read Heroes", 

84 "operationId": "read_heroes_heroes__get", 

85 "parameters": [ 

86 { 

87 "required": False, 

88 "schema": { 

89 "title": "Offset", 

90 "type": "integer", 

91 "default": 0, 

92 }, 

93 "name": "offset", 

94 "in": "query", 

95 }, 

96 { 

97 "required": False, 

98 "schema": { 

99 "title": "Limit", 

100 "maximum": 100.0, 

101 "type": "integer", 

102 "default": 100, 

103 }, 

104 "name": "limit", 

105 "in": "query", 

106 }, 

107 ], 

108 "responses": { 

109 "200": { 

110 "description": "Successful Response", 

111 "content": { 

112 "application/json": { 

113 "schema": { 

114 "title": "Response Read Heroes Heroes Get", 

115 "type": "array", 

116 "items": { 

117 "$ref": "#/components/schemas/HeroPublic" 

118 }, 

119 } 

120 } 

121 }, 

122 }, 

123 "422": { 

124 "description": "Validation Error", 

125 "content": { 

126 "application/json": { 

127 "schema": { 

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

129 } 

130 } 

131 }, 

132 }, 

133 }, 

134 }, 

135 "post": { 

136 "summary": "Create Hero", 

137 "operationId": "create_hero_heroes__post", 

138 "requestBody": { 

139 "content": { 

140 "application/json": { 

141 "schema": { 

142 "$ref": "#/components/schemas/HeroCreate" 

143 } 

144 } 

145 }, 

146 "required": True, 

147 }, 

148 "responses": { 

149 "200": { 

150 "description": "Successful Response", 

151 "content": { 

152 "application/json": { 

153 "schema": { 

154 "$ref": "#/components/schemas/HeroPublic" 

155 } 

156 } 

157 }, 

158 }, 

159 "422": { 

160 "description": "Validation Error", 

161 "content": { 

162 "application/json": { 

163 "schema": { 

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

165 } 

166 } 

167 }, 

168 }, 

169 }, 

170 }, 

171 }, 

172 "/heroes/{hero_id}": { 

173 "get": { 

174 "summary": "Read Hero", 

175 "operationId": "read_hero_heroes__hero_id__get", 

176 "parameters": [ 

177 { 

178 "required": True, 

179 "schema": {"title": "Hero Id", "type": "integer"}, 

180 "name": "hero_id", 

181 "in": "path", 

182 } 

183 ], 

184 "responses": { 

185 "200": { 

186 "description": "Successful Response", 

187 "content": { 

188 "application/json": { 

189 "schema": { 

190 "$ref": "#/components/schemas/HeroPublic" 

191 } 

192 } 

193 }, 

194 }, 

195 "422": { 

196 "description": "Validation Error", 

197 "content": { 

198 "application/json": { 

199 "schema": { 

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

201 } 

202 } 

203 }, 

204 }, 

205 }, 

206 } 

207 }, 

208 }, 

209 "components": { 

210 "schemas": { 

211 "HTTPValidationError": { 

212 "title": "HTTPValidationError", 

213 "type": "object", 

214 "properties": { 

215 "detail": { 

216 "title": "Detail", 

217 "type": "array", 

218 "items": { 

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

220 }, 

221 } 

222 }, 

223 }, 

224 "HeroCreate": { 

225 "title": "HeroCreate", 

226 "required": ["name", "secret_name"], 

227 "type": "object", 

228 "properties": { 

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

230 "secret_name": {"title": "Secret Name", "type": "string"}, 

231 "age": { 

232 "title": "Age", 

233 "anyOf": [{"type": "integer"}, {"type": "null"}], 

234 }, 

235 }, 

236 }, 

237 "HeroPublic": { 

238 "title": "HeroPublic", 

239 "required": ["name", "secret_name", "id"], 

240 "type": "object", 

241 "properties": { 

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

243 "secret_name": {"title": "Secret Name", "type": "string"}, 

244 "age": { 

245 "title": "Age", 

246 "anyOf": [{"type": "integer"}, {"type": "null"}], 

247 }, 

248 "id": {"title": "Id", "type": "integer"}, 

249 }, 

250 }, 

251 "ValidationError": { 

252 "title": "ValidationError", 

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

254 "type": "object", 

255 "properties": IsOneOf( 

256 { 

257 "loc": { 

258 "title": "Location", 

259 "type": "array", 

260 "items": { 

261 "anyOf": [ 

262 {"type": "string"}, 

263 {"type": "integer"}, 

264 ] 

265 }, 

266 }, 

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

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

269 }, 

270 { 

271 "loc": { 

272 "title": "Location", 

273 "type": "array", 

274 "items": { 

275 "anyOf": [ 

276 {"type": "string"}, 

277 {"type": "integer"}, 

278 ] 

279 }, 

280 }, 

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

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

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

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

285 }, 

286 ), 

287 }, 

288 } 

289 }, 

290 }