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

54 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2026-01-06 21:09 +0000

1import importlib 1jklmnopqr

2from types import ModuleType 1jklmnopqr

3 

4import pytest 1jklmnopqr

5from fastapi.testclient import TestClient 1jklmnopqr

6from sqlmodel import create_engine 1jklmnopqr

7from sqlmodel.pool import StaticPool 1jklmnopqr

8 

9from tests.conftest import needs_py310 1jklmnopqr

10 

11 

12@pytest.fixture( 1jklmnopqr

13 name="module", 

14 params=[ 

15 pytest.param("tutorial001_py39"), 

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

17 ], 

18) 

19def get_module(request: pytest.FixtureRequest) -> ModuleType: 1jklmnopqr

20 mod = importlib.import_module( 1jklmnopqr

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

22 ) 

23 mod.sqlite_url = "sqlite://" 1jklmnopqr

24 mod.engine = create_engine( 1jklmnopqr

25 mod.sqlite_url, connect_args=mod.connect_args, poolclass=StaticPool 

26 ) 

27 return mod 1jklmnopqr

28 

29 

30def test_tutorial(module: ModuleType): 1jklmnopqr

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

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

33 hero2_data = { 1abcdefghi

34 "name": "Spider-Boy", 

35 "secret_name": "Pedro Parqueador", 

36 "id": 9000, 

37 } 

38 hero3_data = { 1abcdefghi

39 "name": "Rusty-Man", 

40 "secret_name": "Tommy Sharp", 

41 "age": 48, 

42 } 

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

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

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

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

47 hero2 = response.json() 1abcdefghi

48 hero_id = hero2["id"] 1abcdefghi

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

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

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

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

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

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

55 

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

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

58 data = response.json() 1abcdefghi

59 assert len(data) == 3 1abcdefghi

60 

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

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

63 data = response.json() 1abcdefghi

64 assert len(data) == 2 1abcdefghi

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

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

67 

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

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

70 data = response.json() 1abcdefghi

71 assert len(data) == 2 1abcdefghi

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

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

74 

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

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

77 data = response.json() 1abcdefghi

78 assert len(data) == 1 1abcdefghi

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

80 

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

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

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

84 "openapi": "3.1.0", 

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

86 "paths": { 

87 "/heroes/": { 

88 "get": { 

89 "summary": "Read Heroes", 

90 "operationId": "read_heroes_heroes__get", 

91 "parameters": [ 

92 { 

93 "required": False, 

94 "schema": { 

95 "title": "Offset", 

96 "type": "integer", 

97 "default": 0, 

98 }, 

99 "name": "offset", 

100 "in": "query", 

101 }, 

102 { 

103 "required": False, 

104 "schema": { 

105 "title": "Limit", 

106 "maximum": 100.0, 

107 "type": "integer", 

108 "default": 100, 

109 }, 

110 "name": "limit", 

111 "in": "query", 

112 }, 

113 ], 

114 "responses": { 

115 "200": { 

116 "description": "Successful Response", 

117 "content": { 

118 "application/json": { 

119 "schema": { 

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

121 "type": "array", 

122 "items": { 

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

124 }, 

125 } 

126 } 

127 }, 

128 }, 

129 "422": { 

130 "description": "Validation Error", 

131 "content": { 

132 "application/json": { 

133 "schema": { 

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

135 } 

136 } 

137 }, 

138 }, 

139 }, 

140 }, 

141 "post": { 

142 "summary": "Create Hero", 

143 "operationId": "create_hero_heroes__post", 

144 "requestBody": { 

145 "content": { 

146 "application/json": { 

147 "schema": { 

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

149 } 

150 } 

151 }, 

152 "required": True, 

153 }, 

154 "responses": { 

155 "200": { 

156 "description": "Successful Response", 

157 "content": { 

158 "application/json": { 

159 "schema": { 

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

161 } 

162 } 

163 }, 

164 }, 

165 "422": { 

166 "description": "Validation Error", 

167 "content": { 

168 "application/json": { 

169 "schema": { 

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

171 } 

172 } 

173 }, 

174 }, 

175 }, 

176 }, 

177 }, 

178 "/heroes/{hero_id}": { 

179 "get": { 

180 "summary": "Read Hero", 

181 "operationId": "read_hero_heroes__hero_id__get", 

182 "parameters": [ 

183 { 

184 "required": True, 

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

186 "name": "hero_id", 

187 "in": "path", 

188 } 

189 ], 

190 "responses": { 

191 "200": { 

192 "description": "Successful Response", 

193 "content": { 

194 "application/json": { 

195 "schema": { 

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

197 } 

198 } 

199 }, 

200 }, 

201 "422": { 

202 "description": "Validation Error", 

203 "content": { 

204 "application/json": { 

205 "schema": { 

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

207 } 

208 } 

209 }, 

210 }, 

211 }, 

212 } 

213 }, 

214 }, 

215 "components": { 

216 "schemas": { 

217 "HTTPValidationError": { 

218 "title": "HTTPValidationError", 

219 "type": "object", 

220 "properties": { 

221 "detail": { 

222 "title": "Detail", 

223 "type": "array", 

224 "items": { 

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

226 }, 

227 } 

228 }, 

229 }, 

230 "HeroCreate": { 

231 "title": "HeroCreate", 

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

233 "type": "object", 

234 "properties": { 

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

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

237 "age": { 

238 "title": "Age", 

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

240 }, 

241 }, 

242 }, 

243 "HeroPublic": { 

244 "title": "HeroPublic", 

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

246 "type": "object", 

247 "properties": { 

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

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

250 "age": { 

251 "title": "Age", 

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

253 }, 

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

255 }, 

256 }, 

257 "ValidationError": { 

258 "title": "ValidationError", 

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

260 "type": "object", 

261 "properties": { 

262 "loc": { 

263 "title": "Location", 

264 "type": "array", 

265 "items": { 

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

267 }, 

268 }, 

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

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

271 }, 

272 }, 

273 } 

274 }, 

275 }