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

34 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(f"docs_src.tutorial.fastapi.read_one.{request.param}") 1jklmnopqr

18 mod.engine = database_engine 1jklmnopqr

19 return mod 1jklmnopqr

20 

21 

22def test_tutorial(module: ModuleType): 1jklmnopqr

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

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

25 hero2_data = { 1abcdefghi

26 "name": "Spider-Boy", 

27 "secret_name": "Pedro Parqueador", 

28 "id": 9000, 

29 } 

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

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

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

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

34 hero2 = response.json() 1abcdefghi

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

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

37 data = response.json() 1abcdefghi

38 assert len(data) == 2 1abcdefghi

39 

40 hero_id = hero2["id"] 1abcdefghi

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

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

43 data = response.json() 1abcdefghi

44 assert data == hero2 1abcdefghi

45 

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

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

48 

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

50 

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

52 

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

54 "openapi": "3.1.0", 

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

56 "paths": { 

57 "/heroes/": { 

58 "get": { 

59 "summary": "Read Heroes", 

60 "operationId": "read_heroes_heroes__get", 

61 "responses": { 

62 "200": { 

63 "description": "Successful Response", 

64 "content": { 

65 "application/json": { 

66 "schema": { 

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

68 "type": "array", 

69 "items": { 

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

71 }, 

72 } 

73 } 

74 }, 

75 } 

76 }, 

77 }, 

78 "post": { 

79 "summary": "Create Hero", 

80 "operationId": "create_hero_heroes__post", 

81 "requestBody": { 

82 "content": { 

83 "application/json": { 

84 "schema": { 

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

86 } 

87 } 

88 }, 

89 "required": True, 

90 }, 

91 "responses": { 

92 "200": { 

93 "description": "Successful Response", 

94 "content": { 

95 "application/json": { 

96 "schema": { 

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

98 } 

99 } 

100 }, 

101 }, 

102 "422": { 

103 "description": "Validation Error", 

104 "content": { 

105 "application/json": { 

106 "schema": { 

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

108 } 

109 } 

110 }, 

111 }, 

112 }, 

113 }, 

114 }, 

115 "/heroes/{hero_id}": { 

116 "get": { 

117 "summary": "Read Hero", 

118 "operationId": "read_hero_heroes__hero_id__get", 

119 "parameters": [ 

120 { 

121 "required": True, 

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

123 "name": "hero_id", 

124 "in": "path", 

125 } 

126 ], 

127 "responses": { 

128 "200": { 

129 "description": "Successful Response", 

130 "content": { 

131 "application/json": { 

132 "schema": { 

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

134 } 

135 } 

136 }, 

137 }, 

138 "422": { 

139 "description": "Validation Error", 

140 "content": { 

141 "application/json": { 

142 "schema": { 

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

144 } 

145 } 

146 }, 

147 }, 

148 }, 

149 } 

150 }, 

151 }, 

152 "components": { 

153 "schemas": { 

154 "HTTPValidationError": { 

155 "title": "HTTPValidationError", 

156 "type": "object", 

157 "properties": { 

158 "detail": { 

159 "title": "Detail", 

160 "type": "array", 

161 "items": { 

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

163 }, 

164 } 

165 }, 

166 }, 

167 "HeroCreate": { 

168 "title": "HeroCreate", 

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

170 "type": "object", 

171 "properties": { 

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

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

174 "age": { 

175 "title": "Age", 

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

177 }, 

178 }, 

179 }, 

180 "HeroPublic": { 

181 "title": "HeroPublic", 

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

183 "type": "object", 

184 "properties": { 

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

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

187 "age": { 

188 "title": "Age", 

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

190 }, 

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

192 }, 

193 }, 

194 "ValidationError": { 

195 "title": "ValidationError", 

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

197 "type": "object", 

198 "properties": IsOneOf( 

199 { 

200 "loc": { 

201 "title": "Location", 

202 "type": "array", 

203 "items": { 

204 "anyOf": [ 

205 {"type": "string"}, 

206 {"type": "integer"}, 

207 ] 

208 }, 

209 }, 

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

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

212 }, 

213 { 

214 "loc": { 

215 "title": "Location", 

216 "type": "array", 

217 "items": { 

218 "anyOf": [ 

219 {"type": "string"}, 

220 {"type": "integer"}, 

221 ] 

222 }, 

223 }, 

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

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

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

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

228 }, 

229 ), 

230 }, 

231 } 

232 }, 

233 }