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

32 statements  

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

1from dirty_equals import IsDict 1idefgh

2from fastapi.testclient import TestClient 1idefgh

3from sqlmodel import create_engine 1idefgh

4from sqlmodel.pool import StaticPool 1idefgh

5 

6from ....conftest import needs_py310 1idefgh

7 

8 

9@needs_py310 1idefgh

10def test_tutorial(clear_sqlmodel): 1defgh

11 from docs_src.tutorial.fastapi.read_one import tutorial001_py310 as mod 1abc

12 

13 mod.sqlite_url = "sqlite://" 1abc

14 mod.engine = create_engine( 1abc

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

16 ) 

17 

18 with TestClient(mod.app) as client: 1abc

19 hero1_data = {"name": "Deadpond", "secret_name": "Dive Wilson"} 1abc

20 hero2_data = { 1abc

21 "name": "Spider-Boy", 

22 "secret_name": "Pedro Parqueador", 

23 "id": 9000, 

24 } 

25 response = client.post("/heroes/", json=hero1_data) 1abc

26 assert response.status_code == 200, response.text 1abc

27 response = client.post("/heroes/", json=hero2_data) 1abc

28 assert response.status_code == 200, response.text 1abc

29 hero2 = response.json() 1abc

30 response = client.get("/heroes/") 1abc

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

32 data = response.json() 1abc

33 assert len(data) == 2 1abc

34 

35 hero_id = hero2["id"] 1abc

36 response = client.get(f"/heroes/{hero_id}") 1abc

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

38 data = response.json() 1abc

39 assert data == hero2 1abc

40 

41 response = client.get("/heroes/9000") 1abc

42 assert response.status_code == 404, response.text 1abc

43 

44 response = client.get("/openapi.json") 1abc

45 

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

47 

48 assert response.json() == { 1abc

49 "openapi": "3.1.0", 

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

51 "paths": { 

52 "/heroes/": { 

53 "get": { 

54 "summary": "Read Heroes", 

55 "operationId": "read_heroes_heroes__get", 

56 "responses": { 

57 "200": { 

58 "description": "Successful Response", 

59 "content": { 

60 "application/json": { 

61 "schema": { 

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

63 "type": "array", 

64 "items": { 

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

66 }, 

67 } 

68 } 

69 }, 

70 } 

71 }, 

72 }, 

73 "post": { 

74 "summary": "Create Hero", 

75 "operationId": "create_hero_heroes__post", 

76 "requestBody": { 

77 "content": { 

78 "application/json": { 

79 "schema": { 

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

81 } 

82 } 

83 }, 

84 "required": True, 

85 }, 

86 "responses": { 

87 "200": { 

88 "description": "Successful Response", 

89 "content": { 

90 "application/json": { 

91 "schema": { 

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

93 } 

94 } 

95 }, 

96 }, 

97 "422": { 

98 "description": "Validation Error", 

99 "content": { 

100 "application/json": { 

101 "schema": { 

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

103 } 

104 } 

105 }, 

106 }, 

107 }, 

108 }, 

109 }, 

110 "/heroes/{hero_id}": { 

111 "get": { 

112 "summary": "Read Hero", 

113 "operationId": "read_hero_heroes__hero_id__get", 

114 "parameters": [ 

115 { 

116 "required": True, 

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

118 "name": "hero_id", 

119 "in": "path", 

120 } 

121 ], 

122 "responses": { 

123 "200": { 

124 "description": "Successful Response", 

125 "content": { 

126 "application/json": { 

127 "schema": { 

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

129 } 

130 } 

131 }, 

132 }, 

133 "422": { 

134 "description": "Validation Error", 

135 "content": { 

136 "application/json": { 

137 "schema": { 

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

139 } 

140 } 

141 }, 

142 }, 

143 }, 

144 } 

145 }, 

146 }, 

147 "components": { 

148 "schemas": { 

149 "HTTPValidationError": { 

150 "title": "HTTPValidationError", 

151 "type": "object", 

152 "properties": { 

153 "detail": { 

154 "title": "Detail", 

155 "type": "array", 

156 "items": { 

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

158 }, 

159 } 

160 }, 

161 }, 

162 "HeroCreate": { 

163 "title": "HeroCreate", 

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

165 "type": "object", 

166 "properties": { 

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

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

169 "age": IsDict( 

170 { 

171 "title": "Age", 

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

173 } 

174 ) 

175 | IsDict( 

176 # TODO: remove when deprecating Pydantic v1 

177 {"title": "Age", "type": "integer"} 

178 ), 

179 }, 

180 }, 

181 "HeroPublic": { 

182 "title": "HeroPublic", 

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

184 "type": "object", 

185 "properties": { 

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

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

188 "age": IsDict( 

189 { 

190 "title": "Age", 

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

192 } 

193 ) 

194 | IsDict( 

195 # TODO: remove when deprecating Pydantic v1 

196 {"title": "Age", "type": "integer"} 

197 ), 

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

199 }, 

200 }, 

201 "ValidationError": { 

202 "title": "ValidationError", 

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

204 "type": "object", 

205 "properties": { 

206 "loc": { 

207 "title": "Location", 

208 "type": "array", 

209 "items": { 

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

211 }, 

212 }, 

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

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

215 }, 

216 }, 

217 } 

218 }, 

219 }