Coverage for tests / test_tutorial / test_body_updates / test_tutorial002.py: 100%

24 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-02-12 18:15 +0000

1import importlib 1abdc

2 

3import pytest 1abdc

4from fastapi.testclient import TestClient 1abdc

5from inline_snapshot import snapshot 1abdc

6 

7from ...utils import needs_py310 1abdc

8 

9 

10@pytest.fixture( 1abdc

11 name="client", 

12 params=[ 

13 pytest.param("tutorial002_py310", marks=needs_py310), 

14 ], 

15) 

16def get_client(request: pytest.FixtureRequest): 1abdc

17 mod = importlib.import_module(f"docs_src.body_updates.{request.param}") 1abc

18 

19 client = TestClient(mod.app) 1abc

20 return client 1abc

21 

22 

23def test_get(client: TestClient): 1abdc

24 response = client.get("/items/baz") 1efg

25 assert response.status_code == 200, response.text 1efg

26 assert response.json() == { 1efg

27 "name": "Baz", 

28 "description": None, 

29 "price": 50.2, 

30 "tax": 10.5, 

31 "tags": [], 

32 } 

33 

34 

35def test_patch_all(client: TestClient): 1abdc

36 response = client.patch( 1klm

37 "/items/foo", 

38 json={ 

39 "name": "Fooz", 

40 "description": "Item description", 

41 "price": 3, 

42 "tax": 10.5, 

43 "tags": ["tag1", "tag2"], 

44 }, 

45 ) 

46 assert response.json() == { 1klm

47 "name": "Fooz", 

48 "description": "Item description", 

49 "price": 3, 

50 "tax": 10.5, 

51 "tags": ["tag1", "tag2"], 

52 } 

53 

54 

55def test_patch_name(client: TestClient): 1abdc

56 response = client.patch( 1nop

57 "/items/bar", 

58 json={"name": "Barz"}, 

59 ) 

60 assert response.json() == { 1nop

61 "name": "Barz", 

62 "description": "The bartenders", 

63 "price": 62, 

64 "tax": 20.2, 

65 "tags": [], 

66 } 

67 

68 

69def test_openapi_schema(client: TestClient): 1abdc

70 response = client.get("/openapi.json") 1hij

71 assert response.status_code == 200, response.text 1hij

72 assert response.json() == snapshot( 1hij

73 { 

74 "openapi": "3.1.0", 

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

76 "paths": { 

77 "/items/{item_id}": { 

78 "get": { 

79 "responses": { 

80 "200": { 

81 "description": "Successful Response", 

82 "content": { 

83 "application/json": { 

84 "schema": {"$ref": "#/components/schemas/Item"} 

85 } 

86 }, 

87 }, 

88 "422": { 

89 "description": "Validation Error", 

90 "content": { 

91 "application/json": { 

92 "schema": { 

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

94 } 

95 } 

96 }, 

97 }, 

98 }, 

99 "summary": "Read Item", 

100 "operationId": "read_item_items__item_id__get", 

101 "parameters": [ 

102 { 

103 "required": True, 

104 "schema": {"title": "Item Id", "type": "string"}, 

105 "name": "item_id", 

106 "in": "path", 

107 } 

108 ], 

109 }, 

110 "patch": { 

111 "responses": { 

112 "200": { 

113 "description": "Successful Response", 

114 "content": { 

115 "application/json": { 

116 "schema": {"$ref": "#/components/schemas/Item"} 

117 } 

118 }, 

119 }, 

120 "422": { 

121 "description": "Validation Error", 

122 "content": { 

123 "application/json": { 

124 "schema": { 

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

126 } 

127 } 

128 }, 

129 }, 

130 }, 

131 "summary": "Update Item", 

132 "operationId": "update_item_items__item_id__patch", 

133 "parameters": [ 

134 { 

135 "required": True, 

136 "schema": {"title": "Item Id", "type": "string"}, 

137 "name": "item_id", 

138 "in": "path", 

139 } 

140 ], 

141 "requestBody": { 

142 "content": { 

143 "application/json": { 

144 "schema": {"$ref": "#/components/schemas/Item"} 

145 } 

146 }, 

147 "required": True, 

148 }, 

149 }, 

150 } 

151 }, 

152 "components": { 

153 "schemas": { 

154 "Item": { 

155 "type": "object", 

156 "title": "Item", 

157 "properties": { 

158 "name": { 

159 "anyOf": [{"type": "string"}, {"type": "null"}], 

160 "title": "Name", 

161 }, 

162 "description": { 

163 "anyOf": [{"type": "string"}, {"type": "null"}], 

164 "title": "Description", 

165 }, 

166 "price": { 

167 "anyOf": [{"type": "number"}, {"type": "null"}], 

168 "title": "Price", 

169 }, 

170 "tax": {"title": "Tax", "type": "number", "default": 10.5}, 

171 "tags": { 

172 "title": "Tags", 

173 "type": "array", 

174 "items": {"type": "string"}, 

175 "default": [], 

176 }, 

177 }, 

178 }, 

179 "ValidationError": { 

180 "title": "ValidationError", 

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

182 "type": "object", 

183 "properties": { 

184 "loc": { 

185 "title": "Location", 

186 "type": "array", 

187 "items": { 

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

189 }, 

190 }, 

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

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

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

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

195 }, 

196 }, 

197 "HTTPValidationError": { 

198 "title": "HTTPValidationError", 

199 "type": "object", 

200 "properties": { 

201 "detail": { 

202 "title": "Detail", 

203 "type": "array", 

204 "items": { 

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

206 }, 

207 } 

208 }, 

209 }, 

210 } 

211 }, 

212 } 

213 )