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

36 statements  

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

1import importlib 1ijklmnop

2from types import ModuleType 1ijklmnop

3 

4import pytest 1ijklmnop

5from dirty_equals import IsOneOf 1ijklmnop

6from fastapi.testclient import TestClient 1ijklmnop

7from sqlmodel import create_engine 1ijklmnop

8from sqlmodel.pool import StaticPool 1ijklmnop

9 

10 

11@pytest.fixture( 1ijklmnop

12 name="module", 

13 params=[ 

14 pytest.param("tutorial001_py310"), 

15 ], 

16) 

17def get_module(request: pytest.FixtureRequest) -> ModuleType: 1ijklmnop

18 mod = importlib.import_module(f"docs_src.tutorial.fastapi.read_one.{request.param}") 1ijklmnop

19 mod.sqlite_url = "sqlite://" 1ijklmnop

20 mod.engine = create_engine( 1ijklmnop

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

22 ) 

23 return mod 1ijklmnop

24 

25 

26def test_tutorial(module: ModuleType): 1ijklmnop

27 with TestClient(module.app) as client: 1abcdefgh

28 hero1_data = {"name": "Deadpond", "secret_name": "Dive Wilson"} 1abcdefgh

29 hero2_data = { 1abcdefgh

30 "name": "Spider-Boy", 

31 "secret_name": "Pedro Parqueador", 

32 "id": 9000, 

33 } 

34 response = client.post("/heroes/", json=hero1_data) 1abcdefgh

35 assert response.status_code == 200, response.text 1abcdefgh

36 response = client.post("/heroes/", json=hero2_data) 1abcdefgh

37 assert response.status_code == 200, response.text 1abcdefgh

38 hero2 = response.json() 1abcdefgh

39 response = client.get("/heroes/") 1abcdefgh

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

41 data = response.json() 1abcdefgh

42 assert len(data) == 2 1abcdefgh

43 

44 hero_id = hero2["id"] 1abcdefgh

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

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

47 data = response.json() 1abcdefgh

48 assert data == hero2 1abcdefgh

49 

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

51 assert response.status_code == 404, response.text 1abcdefgh

52 

53 response = client.get("/openapi.json") 1abcdefgh

54 

55 assert response.status_code == 200, response.text 1abcdefgh

56 

57 assert response.json() == { 1abcdefgh

58 "openapi": "3.1.0", 

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

60 "paths": { 

61 "/heroes/": { 

62 "get": { 

63 "summary": "Read Heroes", 

64 "operationId": "read_heroes_heroes__get", 

65 "responses": { 

66 "200": { 

67 "description": "Successful Response", 

68 "content": { 

69 "application/json": { 

70 "schema": { 

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

72 "type": "array", 

73 "items": { 

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

75 }, 

76 } 

77 } 

78 }, 

79 } 

80 }, 

81 }, 

82 "post": { 

83 "summary": "Create Hero", 

84 "operationId": "create_hero_heroes__post", 

85 "requestBody": { 

86 "content": { 

87 "application/json": { 

88 "schema": { 

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

90 } 

91 } 

92 }, 

93 "required": True, 

94 }, 

95 "responses": { 

96 "200": { 

97 "description": "Successful Response", 

98 "content": { 

99 "application/json": { 

100 "schema": { 

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

102 } 

103 } 

104 }, 

105 }, 

106 "422": { 

107 "description": "Validation Error", 

108 "content": { 

109 "application/json": { 

110 "schema": { 

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

112 } 

113 } 

114 }, 

115 }, 

116 }, 

117 }, 

118 }, 

119 "/heroes/{hero_id}": { 

120 "get": { 

121 "summary": "Read Hero", 

122 "operationId": "read_hero_heroes__hero_id__get", 

123 "parameters": [ 

124 { 

125 "required": True, 

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

127 "name": "hero_id", 

128 "in": "path", 

129 } 

130 ], 

131 "responses": { 

132 "200": { 

133 "description": "Successful Response", 

134 "content": { 

135 "application/json": { 

136 "schema": { 

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

138 } 

139 } 

140 }, 

141 }, 

142 "422": { 

143 "description": "Validation Error", 

144 "content": { 

145 "application/json": { 

146 "schema": { 

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

148 } 

149 } 

150 }, 

151 }, 

152 }, 

153 } 

154 }, 

155 }, 

156 "components": { 

157 "schemas": { 

158 "HTTPValidationError": { 

159 "title": "HTTPValidationError", 

160 "type": "object", 

161 "properties": { 

162 "detail": { 

163 "title": "Detail", 

164 "type": "array", 

165 "items": { 

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

167 }, 

168 } 

169 }, 

170 }, 

171 "HeroCreate": { 

172 "title": "HeroCreate", 

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

174 "type": "object", 

175 "properties": { 

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

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

178 "age": { 

179 "title": "Age", 

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

181 }, 

182 }, 

183 }, 

184 "HeroPublic": { 

185 "title": "HeroPublic", 

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

187 "type": "object", 

188 "properties": { 

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

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

191 "age": { 

192 "title": "Age", 

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

194 }, 

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

196 }, 

197 }, 

198 "ValidationError": { 

199 "title": "ValidationError", 

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

201 "type": "object", 

202 "properties": IsOneOf( 

203 { 

204 "loc": { 

205 "title": "Location", 

206 "type": "array", 

207 "items": { 

208 "anyOf": [ 

209 {"type": "string"}, 

210 {"type": "integer"}, 

211 ] 

212 }, 

213 }, 

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

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

216 }, 

217 { 

218 "loc": { 

219 "title": "Location", 

220 "type": "array", 

221 "items": { 

222 "anyOf": [ 

223 {"type": "string"}, 

224 {"type": "integer"}, 

225 ] 

226 }, 

227 }, 

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

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

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

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

232 }, 

233 ), 

234 }, 

235 } 

236 }, 

237 }